Commit a3d7913c072c727f4e383b7f90bed2a4dc8029f0

Ozkan Sezer 2020-10-14T23:01:05

SDL_os2audio.c (OS2_OpenDevice): change spec->samples assignment: Original code assigned MCIMixSetup.ulSamplesPerSec value to it, but it is just the freq... We now change spec->samples only either if it is 0 or we changed the frequency, by picking a default of ~46 ms at desired frequency (code taken from SDL_audio.c:prepare_audiospec()). With this, the crashes I have been experiencing are gone.

diff --git a/src/audio/os2/SDL_os2audio.c b/src/audio/os2/SDL_os2audio.c
index 7b83d24..f906e89 100644
--- a/src/audio/os2/SDL_os2audio.c
+++ b/src/audio/os2/SDL_os2audio.c
@@ -268,7 +268,9 @@ static int OS2_OpenDevice(_THIS, void *handle, const char *devname,
   MCI_BUFFER_PARMS      stMCIBuffer;
   ULONG                 ulRC;
   ULONG                 ulIdx;
+  BOOL                  new_freq;
 
+  new_freq = FALSE;
   SDL_zero(stMCIAmpOpen);
   SDL_zero(stMCIBuffer);
 
@@ -350,10 +352,12 @@ static int OS2_OpenDevice(_THIS, void *handle, const char *devname,
   if ( this->spec.freq < 8000 )
   {
     this->spec.freq = 8000;
+    new_freq = TRUE;
   }
   else if ( this->spec.freq > 48000 )
   {
     this->spec.freq = 48000;
+    new_freq = TRUE;
   }
 
   // Setup mixer.
@@ -377,6 +381,7 @@ static int OS2_OpenDevice(_THIS, void *handle, const char *devname,
                      MCI_WAIT | MCI_MIXSETUP_INIT, &pAData->stMCIMixSetup, 0 );
   if ( ( ulRC != MCIERR_SUCCESS ) && ( this->spec.freq > 44100 ) )
   {
+    new_freq = TRUE;
     pAData->stMCIMixSetup.ulSamplesPerSec = 44100;
     this->spec.freq = 44100;
     ulRC = mciSendCommand( pAData->usDeviceId, MCI_MIXSETUP,
@@ -395,13 +400,22 @@ static int OS2_OpenDevice(_THIS, void *handle, const char *devname,
     return _MCIError( "MCI_MIXSETUP", ulRC );
   }
 
-  this->spec.samples = pAData->stMCIMixSetup.ulSamplesPerSec;
+  if (this->spec.samples == 0 || new_freq == TRUE) {
+  /* also see SDL_audio.c:prepare_audiospec() */
+  /* Pick a default of ~46 ms at desired frequency */
+    Uint32 samples = (this->spec.freq / 1000) * 46;
+    Uint32 power2 = 1;
+    while (power2 < samples) {
+      power2 <<= 1;
+    }
+    this->spec.samples = power2;
+  }
   /* Update the fragment size as size in bytes */
   SDL_CalculateAudioSpec( &this->spec );
 
   // Allocate memory buffers
 
-  stMCIBuffer.ulBufferSize = (this->spec.freq / 1000) * 100;// this->spec.size;
+  stMCIBuffer.ulBufferSize = this->spec.size;// (this->spec.freq / 1000) * 100;
   stMCIBuffer.ulNumBuffers = NUM_BUFFERS;
   stMCIBuffer.pBufList     = &pAData->aMixBuffers;