Sound under Windows

Hello.

A couple friends and I wrote a game using pygame (which uses SDL) and it
runs great under Linux. However, when running it under Windows, the
music breaks up. Increasing the sound buffer size makes it better but
then the latency of sound effects is terrible.

I traced the problem to the SDL directsound audio driver and modified it
slightly to fix it on my machine. The patch also fixed the breakups on
one of my friend’s Windows machine. My other friend reports that the
sound quality got worse with the patch though.

I’m not a DirectSound or Windows expert so maybe someone can explain
what is going on. I’ve included the patch in this message.

Thanks.

-Mark

P.S. I’m not on the list so please CC me any replies.

Patch Description:
According to DirectX documentation, you’re not supposed to write
between the play cursor and the write cursor, only after the write
cursor. Also enabled position notifications by default. Notifications
and polling were both tested and improved sound quality on my machine.

Patch:
— …/SDL-1.2.5.orig/src/audio/windx5/SDL_dx5audio.c 2002-08-24 13:30:49.000000000 -0400
+++ src/audio/windx5/SDL_dx5audio.c 2003-03-07 18:48:31.000000000 -0500
@@ -37,7 +37,7 @@
#include “SDL_dx5audio.h”

/* Define this if you want to use DirectX 6 DirectSoundNotify interface */
-//#define USE_POSITION_NOTIFY
+#define USE_POSITION_NOTIFY

/* DirectX function pointers for audio */
HRESULT (WINAPI *DSoundCreate)(LPGUID, LPDIRECTSOUND , LPUNKNOWN);
@@ -261,7 +261,7 @@
/
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);
    @@ -271,9 +271,8 @@
    #endif
    return;
    }
  • cursor /= mixlen;

  • while ( cursor == playing ) {

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

@@ -299,12 +298,11 @@

	/* 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;
    
    }
    }

@@ -353,7 +351,7 @@

/* 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,
    @@ -498,7 +496,7 @@
    static int CreateSecondary(LPDIRECTSOUND sndObj, HWND focus,
    LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize)
    {
  • const int numchunks = 2;

A couple friends and I wrote a game using pygame (which uses SDL) and it
runs great under Linux. However, when running it under Windows, the
music breaks up. Increasing the sound buffer size makes it better but
then the latency of sound effects is terrible.

I traced the problem to the SDL directsound audio driver and modified it
slightly to fix it on my machine. The patch also fixed the breakups on
one of my friend’s Windows machine. My other friend reports that the
sound quality got worse with the patch though.

I’ve entered your bug into the SDL bug tracking database:
https://bugzilla.libsdl.org/show_bug.cgi?id=69

Many people have reported it, and we’re trying to find people who can investigate it.
Were you able to find any more clues on this?

Thanks!
-Sam Lantinga, Senior Software Engineer, Blizzard Entertainment