Commits

illume  committed 05f77b1

a work around for 8 bit samples being stereo reversed with SDL_mixer. Also check the return value of Mix_SetPanning, and raise an error on volume errors. Also changed default chunk size to avoid a common scratchy sound problem.

  • Participants
  • Parent commits eb26a37

Comments (0)

Files changed (2)

 #include "pygamedocs.h"
 #include "mixer.h"
 
-#define MIX_DEFAULT_CHUNKSIZE	1024
+
+#error MIX_DEFAULT_FORMAT
+
+#define MIX_DEFAULT_CHUNKSIZE 3072
 
 staticforward PyTypeObject PySound_Type;
 staticforward PyTypeObject PyChannel_Type;
 static PyObject*
 autoinit (PyObject* self, PyObject* arg)
 {
-    int freq, size, stereo, chunk;
+    int freq, stereo, chunk;
     int i;
+    Uint16 size;
+
     freq = request_frequency;
     size = request_size;
     stereo = request_stereo;
     else
         stereo = 1;
 
+    /* printf("size:%d:\n", size); */
+
     if (size == 8)
         size = AUDIO_U8;
     else if (size == -8)
     else if (size == -16)
         size = AUDIO_S16SYS;
 
+
+    /* printf("size:%d:\n", size); */
+
+
+
     /*make chunk a power of 2*/
-    for (i = 0; 1 << i < chunk; ++i); //yes, semicolon on for loop
-    chunk = MAX (1 << i, 256);
+    for (i = 0; 1 << i < chunk; ++i) {
+        chunk = MAX (1 << i, 256);
+    }
 
     if (!SDL_WasInit (SDL_INIT_AUDIO))
     {
         if (SDL_InitSubSystem (SDL_INIT_AUDIO) == -1)
             return PyInt_FromLong (0);
 
-        if (Mix_OpenAudio (freq, (Uint16)size, stereo, chunk) == -1)
+        if (Mix_OpenAudio (freq, size, stereo, chunk) == -1)
         {
             SDL_QuitSubSystem (SDL_INIT_AUDIO);
             return PyInt_FromLong (0);
         Mix_ChannelFinished (endsound_callback);
 #endif
 
+        /* A bug in sdl_mixer where the stereo is reversed for 8 bit.
+           So we use this CPU hogging effect to reverse it for us.
+           Hopefully this bug is fixed in SDL_mixer 1.2.9
+        printf("MIX_MAJOR_VERSION :%d: MIX_MINOR_VERSION :%d: MIX_PATCHLEVEL :%d: \n", 
+               MIX_MAJOR_VERSION, MIX_MINOR_VERSION, MIX_PATCHLEVEL);
+        */
+
+#if MIX_MAJOR_VERSION>=1 && MIX_MINOR_VERSION>=2 && MIX_PATCHLEVEL<=8
+        if(size == AUDIO_U8) {
+            if(!Mix_SetReverseStereo(MIX_CHANNEL_POST, 1)) {
+                /* We do nothing... because might as well just let it go ahead. */
+                /* return RAISE (PyExc_SDLError, Mix_GetError());
+                */
+            }
+        }
+#endif
+
+
         Mix_VolumeMusic (127);
     }
     return PyInt_FromLong (1);
     int channelnum = PyChannel_AsInt (self);
     float volume, stereovolume=-1.11f;
     int result;
+    Uint8 left, right;
 
     if (!PyArg_ParseTuple (args, "f|f", &volume, &stereovolume))
         return NULL;
          * something else.  NOTE: there is no way to GetPanning
          * variables.
          */
-        result = Mix_SetPanning (channelnum, (Uint8)255, (Uint8)255);
-            
+        left = 255;
+        right = 255;
+
+        if(!Mix_SetPanning(channelnum, left, right)) {
+            return RAISE (PyExc_SDLError, Mix_GetError());
+        } 
     }
     else
     {
         /* NOTE: here the volume will be set to 1.0 and the panning will
          * be used. */
-        result = Mix_SetPanning (channelnum, (Uint8)(volume * 255),
-                                 (Uint8)(stereovolume * 255));
+        left = (Uint8)(volume * 255);
+        right = (Uint8)(stereovolume * 255);
+        /*
+        printf("left:%d:  right:%d:\n", left, right);
+        */
+
+        if(!Mix_SetPanning(channelnum, left, right)) {
+            return RAISE (PyExc_SDLError, Mix_GetError());
+        }
+
         volume = 1.0f;
     }
 #else
     { "pause", (PyCFunction) mixer_pause, METH_NOARGS, DOC_PYGAMEMIXERPAUSE },
     { "unpause", (PyCFunction) mixer_unpause, METH_NOARGS,
       DOC_PYGAMEMIXERUNPAUSE },
-/*	{ "lookup_frequency", lookup_frequency, 1, doc_lookup_frequency },*/
+/*  { "lookup_frequency", lookup_frequency, 1, doc_lookup_frequency },*/
 
     { NULL, NULL, 0, NULL }
 };

File src/mixer.doc

 resampling, so the mixer should be initialized to match the values of
 your audio resources.
 
-NOTE: there is currently a bug on some windows machines which makes
-sound play back 'scratchy'.  There is not enough cpu in the sound 
-thread to feed the buffer to the sound api.
-To get around this you can increase the buffer size.  However this
-means that there is more of a delay between the time you ask to play
-the sound and when it gets played.  Try calling this before the pygame.init or 
-pygame.mixer.init calls.  pygame.mixer.pre_init(44100,-16,2, 1024 * 3)
+NOTE: Not to get less laggy sound, use a smaller buffer size.  The default
+is set to reduce the chance of scratchy sounds on some computers.  You can 
+change the default buffer by calling pygame.mixer.pre_init before 
+pygame.mixer.init or pygame.init is called.  For example:
+pygame.mixer.pre_init(44100,-16,2, 1024)
+The default size was changed from 1024 to 3072 in pygame 1.8.
 <SECTION>
 
 init
 initialize the mixer module
-pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=1024): return None
+pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=3072): return None
 
 Initialize the mixer module for Sound loading and playback. The default
 arguments can be overridden to provide specific audio mixing. The size