Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Merged DirectSound dropout fix from SDL 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jun 23, 2006
1 parent caa675f commit e317642
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
40 changes: 25 additions & 15 deletions src/audio/windx5/SDL_dx5audio.c
Expand Up @@ -263,7 +263,7 @@ DX5_WaitAudio_BusyWait(_THIS)
/* Semi-busy wait, since we have no way of getting play notification
on a primary mixing buffer located in hardware (DirectX 5.0)
*/
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &cursor, &junk);
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &junk, &cursor);
if (result != DS_OK) {
if (result == DSERR_BUFFERLOST) {
IDirectSoundBuffer_Restore(mixbuf);
Expand All @@ -273,11 +273,10 @@ DX5_WaitAudio_BusyWait(_THIS)
#endif
return;
}
cursor /= mixlen;

while (cursor == playing) {
while ((cursor / mixlen) == lastchunk) {
/* FIXME: find out how much time is left and sleep that long */
SDL_Delay(10);
SDL_Delay(1);

/* Try to restore a lost sound buffer */
IDirectSoundBuffer_GetStatus(mixbuf, &status);
Expand All @@ -301,12 +300,11 @@ DX5_WaitAudio_BusyWait(_THIS)

/* Find out where we are playing */
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
&cursor, &junk);
&junk, &cursor);
if (result != DS_OK) {
SetDSerror("DirectSound GetCurrentPosition", result);
return;
}
cursor /= mixlen;
}
}

Expand Down Expand Up @@ -358,18 +356,31 @@ DX5_GetAudioBuf(_THIS)

/* Figure out which blocks to fill next */
locked_buf = NULL;
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &cursor, &junk);
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &junk, &cursor);
if (result == DSERR_BUFFERLOST) {
IDirectSoundBuffer_Restore(mixbuf);
result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
&cursor, &junk);
&junk, &cursor);
}
if (result != DS_OK) {
SetDSerror("DirectSound GetCurrentPosition", result);
return (NULL);
}
cursor /= mixlen;
playing = cursor;
#ifdef DEBUG_SOUND
/* Detect audio dropouts */
{
DWORD spot = cursor;
if (spot < lastchunk) {
spot += NUM_BUFFERS;
}
if (spot > lastchunk + 1) {
fprintf(stderr, "Audio dropout, missed %d fragments\n",
(spot - (lastchunk + 1)));
}
}
#endif
lastchunk = cursor;
cursor = (cursor + 1) % NUM_BUFFERS;
cursor *= mixlen;

Expand Down Expand Up @@ -511,7 +522,7 @@ CreateSecondary(LPDIRECTSOUND sndObj, HWND focus,
LPDIRECTSOUNDBUFFER * sndbuf, WAVEFORMATEX * wavefmt,
Uint32 chunksize)
{
const int numchunks = 2;
const int numchunks = 8;
HRESULT result;
DSBUFFERDESC format;
LPVOID pvAudioPtr1, pvAudioPtr2;
Expand Down Expand Up @@ -563,10 +574,9 @@ CreateSecondary(LPDIRECTSOUND sndObj, HWND focus,

/* Silence the initial audio buffer */
result = IDirectSoundBuffer_Lock(*sndbuf, 0, format.dwBufferBytes,
(LPVOID *) & pvAudioPtr1,
&dwAudioBytes1,
(LPVOID *) & pvAudioPtr2,
&dwAudioBytes2, DSBLOCK_ENTIREBUFFER);
(LPVOID *) & pvAudioPtr1, &dwAudioBytes1,
(LPVOID *) & pvAudioPtr2, &dwAudioBytes2,
DSBLOCK_ENTIREBUFFER);
if (result == DS_OK) {
if (wavefmt->wBitsPerSample == 8) {
SDL_memset(pvAudioPtr1, 0x80, dwAudioBytes1);
Expand Down Expand Up @@ -706,7 +716,7 @@ DX5_OpenAudio(_THIS, SDL_AudioSpec * spec)
#endif

/* The buffer will auto-start playing in DX5_WaitAudio() */
playing = 0;
lastchunk = 0;
mixlen = spec->size;

#ifdef USE_POSITION_NOTIFY
Expand Down
5 changes: 3 additions & 2 deletions src/audio/windx5/SDL_dx5audio.h
Expand Up @@ -38,7 +38,7 @@ struct SDL_PrivateAudioData
LPDIRECTSOUNDBUFFER mixbuf;
int NUM_BUFFERS;
int mixlen, silence;
DWORD playing;
DWORD lastchunk;
Uint8 *locked_buf;
HANDLE audio_event;
};
Expand All @@ -49,9 +49,10 @@ struct SDL_PrivateAudioData
#define NUM_BUFFERS (this->hidden->NUM_BUFFERS)
#define mixlen (this->hidden->mixlen)
#define silence (this->hidden->silence)
#define playing (this->hidden->playing)
#define lastchunk (this->hidden->lastchunk)
#define locked_buf (this->hidden->locked_buf)
#define audio_event (this->hidden->audio_event)

#endif /* _SDL_lowaudio_h */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit e317642

Please sign in to comment.