We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 69

Summary: Sound under Windows
Product: SDL Reporter: Sam Lantinga <slouken>
Component: audioAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: don't know   
Hardware: x86   
OS: Windows (All)   

Description Sam Lantinga 2006-01-24 00:20:44 UTC
Date: Thu, 13 Mar 2003 11:56:55 -0500
From: Mark Aikens <marka@desert.cx>
Subject: [SDL] 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;
+       const int numchunks = 8;
        HRESULT result;
        DSBUFFERDESC format;
        LPVOID pvAudioPtr1, pvAudioPtr2;
Comment 1 Ryan C. Gordon 2006-01-27 11:23:21 UTC
Setting Sam as "QA Contact" on all bugs (even resolved ones) so he'll definitely be in the loop to any further discussion here about SDL.

--ryan.

Comment 2 Sam Lantinga 2006-06-23 05:05:57 UTC
A variation on this patch is in subversion, and is ready for testing.