Sound cleanup and optimisation

PlaySoundObject is also more like it was in the original source code
This commit is contained in:
Clownacy 2019-04-19 01:24:08 +01:00
parent 5a3e641492
commit 295673b813
2 changed files with 22 additions and 26 deletions

View file

@ -153,12 +153,12 @@ void SOUNDBUFFER::Stop()
SDL_UnlockAudioDevice(audioDevice); SDL_UnlockAudioDevice(audioDevice);
} }
void SOUNDBUFFER::Mix(float (*buffer)[2], size_t samples) void SOUNDBUFFER::Mix(float *buffer, size_t frames)
{ {
if (!playing) //This sound buffer isn't playing if (!playing) //This sound buffer isn't playing
return; return;
for (size_t sample = 0; sample < samples; sample++) for (size_t i = 0; i < frames; ++i)
{ {
const double freqPosition = frequency / FREQUENCY; //This is added to position at the end const double freqPosition = frequency / FREQUENCY; //This is added to position at the end
@ -174,8 +174,8 @@ void SOUNDBUFFER::Mix(float (*buffer)[2], size_t samples)
const float sampleConvert = (sampleA - 128.0f) / 128.0f; const float sampleConvert = (sampleA - 128.0f) / 128.0f;
//Mix //Mix
buffer[sample][0] += (float)(sampleConvert * volume * volume_l); *buffer++ += (float)(sampleConvert * volume * volume_l);
buffer[sample][1] += (float)(sampleConvert * volume * volume_r); *buffer++ += (float)(sampleConvert * volume * volume_r);
//Increment position //Increment position
samplePosition += freqPosition; samplePosition += freqPosition;
@ -199,23 +199,20 @@ void SOUNDBUFFER::Mix(float (*buffer)[2], size_t samples)
} }
//Sound mixer //Sound mixer
void AudioCallback(void *userdata, uint8_t *stream, int len) void AudioCallback(void *userdata, Uint8 *stream, int len)
{ {
(void)userdata; (void)userdata;
float (*buffer)[2] = (float(*)[2])stream; float *buffer = (float*)stream;
const size_t samples = len / (sizeof(float) * 2); const size_t frames = len / (sizeof(float) * 2);
//Clear stream //Clear stream
for (size_t sample = 0; sample < samples; ++sample) for (size_t i = 0; i < frames * 2; ++i)
{ buffer[i] = 0.0f;
buffer[sample][0] = 0.0f;
buffer[sample][1] = 0.0f;
}
//Mix sounds to primary buffer //Mix sounds to primary buffer
for (SOUNDBUFFER *sound = soundBuffers; sound != NULL; sound = sound->next) for (SOUNDBUFFER *sound = soundBuffers; sound != NULL; sound = sound->next)
sound->Mix(buffer, samples); sound->Mix(buffer, frames);
} }
//Sound things //Sound things
@ -270,22 +267,21 @@ void PlaySoundObject(int no, int mode)
{ {
if (lpSECONDARYBUFFER[no]) if (lpSECONDARYBUFFER[no])
{ {
if (mode == -1) switch (mode)
{ {
lpSECONDARYBUFFER[no]->Play(true); case 0:
} lpSECONDARYBUFFER[no]->Stop();
else if ( mode ) break;
{
if ( mode == 1 ) case 1:
{
lpSECONDARYBUFFER[no]->Stop(); lpSECONDARYBUFFER[no]->Stop();
lpSECONDARYBUFFER[no]->SetCurrentPosition(0); lpSECONDARYBUFFER[no]->SetCurrentPosition(0);
lpSECONDARYBUFFER[no]->Play(false); lpSECONDARYBUFFER[no]->Play(false);
} break;
}
else case -1:
{ lpSECONDARYBUFFER[no]->Play(true);
lpSECONDARYBUFFER[no]->Stop(); break;
} }
} }
} }

View file

@ -23,7 +23,7 @@ class SOUNDBUFFER
void Play(bool bLooping); void Play(bool bLooping);
void Stop(); void Stop();
void Mix(float (*buffer)[2], size_t samples); void Mix(float *buffer, size_t frames);
SOUNDBUFFER *next; SOUNDBUFFER *next;