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);
}
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
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
@ -174,8 +174,8 @@ void SOUNDBUFFER::Mix(float (*buffer)[2], size_t samples)
const float sampleConvert = (sampleA - 128.0f) / 128.0f;
//Mix
buffer[sample][0] += (float)(sampleConvert * volume * volume_l);
buffer[sample][1] += (float)(sampleConvert * volume * volume_r);
*buffer++ += (float)(sampleConvert * volume * volume_l);
*buffer++ += (float)(sampleConvert * volume * volume_r);
//Increment position
samplePosition += freqPosition;
@ -199,23 +199,20 @@ void SOUNDBUFFER::Mix(float (*buffer)[2], size_t samples)
}
//Sound mixer
void AudioCallback(void *userdata, uint8_t *stream, int len)
void AudioCallback(void *userdata, Uint8 *stream, int len)
{
(void)userdata;
float (*buffer)[2] = (float(*)[2])stream;
const size_t samples = len / (sizeof(float) * 2);
float *buffer = (float*)stream;
const size_t frames = len / (sizeof(float) * 2);
//Clear stream
for (size_t sample = 0; sample < samples; ++sample)
{
buffer[sample][0] = 0.0f;
buffer[sample][1] = 0.0f;
}
for (size_t i = 0; i < frames * 2; ++i)
buffer[i] = 0.0f;
//Mix sounds to primary buffer
for (SOUNDBUFFER *sound = soundBuffers; sound != NULL; sound = sound->next)
sound->Mix(buffer, samples);
sound->Mix(buffer, frames);
}
//Sound things
@ -270,22 +267,21 @@ void PlaySoundObject(int no, int mode)
{
if (lpSECONDARYBUFFER[no])
{
if (mode == -1)
switch (mode)
{
lpSECONDARYBUFFER[no]->Play(true);
}
else if ( mode )
{
if ( mode == 1 )
{
case 0:
lpSECONDARYBUFFER[no]->Stop();
break;
case 1:
lpSECONDARYBUFFER[no]->Stop();
lpSECONDARYBUFFER[no]->SetCurrentPosition(0);
lpSECONDARYBUFFER[no]->Play(false);
}
}
else
{
lpSECONDARYBUFFER[no]->Stop();
break;
case -1:
lpSECONDARYBUFFER[no]->Play(true);
break;
}
}
}

View file

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