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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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;