diff --git a/README.md b/README.md index 376c7d7..487c2b8 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,9 @@ NOTE: It actually can also work on Win95/98 if you follow the extra procedure be # Revisions: +v.2024.03.21 +- Fix the truncation at the end of each song ranging from 1ms to 500ms. + v.2023.07.24 - Correctly report stereo CD separate channel volume control capability. diff --git a/ogg-winmm.c b/ogg-winmm.c index bc23471..04516dc 100644 --- a/ogg-winmm.c +++ b/ogg-winmm.c @@ -99,6 +99,8 @@ DWORD WINAPI player_main(void *unused) break; } } + + plr_reset(command == MCI_PLAY); } /* Sending notify successful message:*/ @@ -109,7 +111,6 @@ DWORD WINAPI player_main(void *unused) } mode = MCI_MODE_STOP; - plr_reset(); if (command == MCI_DELETE) break; } @@ -188,6 +189,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) } #endif command = MCI_DELETE; + plr_stop(); if (event) SetEvent(event); if (player) WaitForSingleObject(player, INFINITE); diff --git a/player.c b/player.c index a3cde66..dad4986 100644 --- a/player.c +++ b/player.c @@ -4,8 +4,9 @@ #include #include -#define WAV_BUF_CNT (2) // Dual buffer -#define WAV_BUF_LEN (44100*2) // 44100Hz, 16-bit, 2-channel, 1/2 second buffer +#define WAV_BUF_CNT (2) // Dual buffer +#define WAV_BUF_TME (1000) // The expected playtime of the buffer in milliseconds: 1000ms +#define WAV_BUF_LEN (44100*2*2*(WAV_BUF_TME/1000)) // 44100Hz, 16-bit, 2-channel, 1 second buffer bool plr_run = false; bool plr_bsy = false; @@ -39,13 +40,19 @@ unsigned int plr_length(const char *path) // in millisecond return ret; } -void plr_reset() +// plr_reset() should only be called by the thread itself! +void plr_reset(BOOL wait) // Wait for remaining buffer to drain or not. { if (plr_vf.datasource) { ov_clear(&plr_vf); } if (plr_hw) { + if (wait) { + for (int n = 0; n < WAV_BUF_CNT; n++, plr_que = (plr_que+1) % WAV_BUF_CNT) { + if (!(plr_hdr[plr_que].dwFlags & WHDR_DONE)) WaitForSingleObject(plr_ev, WAV_BUF_TME); + } + } waveOutReset(plr_hw); for (int i = 0; i < WAV_BUF_CNT; i++) { waveOutUnprepareHeader(plr_hw, &plr_hdr[i], sizeof(WAVEHDR)); @@ -62,8 +69,6 @@ void plr_reset() int plr_play(const char *path, unsigned int from, unsigned int to) { - plr_reset(); - if (ov_fopen(path, &plr_vf) != 0) return 0; vorbis_info *vi = ov_info(&plr_vf, -1); diff --git a/player.h b/player.h index e8afd18..6a96adb 100644 --- a/player.h +++ b/player.h @@ -1,5 +1,5 @@ void plr_volume(int vol_l, int vol_r); -void plr_reset(); +void plr_reset(BOOL wait); void plr_stop(); void plr_pause(); void plr_resume();