Skip to content

Commit

Permalink
Fix the truncation at the end of each song ranging from 1ms to 500ms.
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuanx committed Mar 22, 2024
1 parent cde06d5 commit 99dc26b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 3 additions & 1 deletion ogg-winmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ DWORD WINAPI player_main(void *unused)
break;
}
}

plr_reset(command == MCI_PLAY);
}

/* Sending notify successful message:*/
Expand All @@ -109,7 +111,6 @@ DWORD WINAPI player_main(void *unused)
}

mode = MCI_MODE_STOP;
plr_reset();
if (command == MCI_DELETE) break;
}

Expand Down Expand Up @@ -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);

Expand Down
15 changes: 10 additions & 5 deletions player.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include <windows.h>
#include <vorbis/vorbisfile.h>

#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;
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion player.h
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

0 comments on commit 99dc26b

Please sign in to comment.