Reword audio backend logic
This will make it easier to integrate into the enhanced branch, and also improved audio quality slightly (samples are mixed as 16-bit instead of 8-bit).
This commit is contained in:
parent
70e431d35d
commit
3fa4a91dc1
3 changed files with 37 additions and 22 deletions
|
@ -20,15 +20,8 @@ static unsigned long output_frequency;
|
|||
static void (*organya_callback)(void);
|
||||
static unsigned int organya_callback_milliseconds;
|
||||
|
||||
static void Callback(void *user_data, Uint8 *stream_uint8, int len)
|
||||
static void MixSoundsAndUpdateOrganya(long *stream, size_t frames_total)
|
||||
{
|
||||
(void)user_data;
|
||||
|
||||
short *stream = (short*)stream_uint8;
|
||||
unsigned int frames_total = len / sizeof(short) / 2;
|
||||
|
||||
memset(stream, 0, len);
|
||||
|
||||
if (organya_callback_milliseconds == 0)
|
||||
{
|
||||
Mixer_MixSounds(stream, frames_total);
|
||||
|
@ -60,16 +53,38 @@ static void Callback(void *user_data, Uint8 *stream_uint8, int len)
|
|||
organya_countdown -= frames_to_do;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clamp output, and convert from 8-bit to 16-bit
|
||||
for (unsigned int i = 0; i < frames_total * 2; ++i)
|
||||
static void Callback(void *user_data, Uint8 *stream_uint8, int len)
|
||||
{
|
||||
if (stream[i] > 0x7F)
|
||||
stream[i] = 0x7F00;
|
||||
else if (stream[i] < -0x7F)
|
||||
stream[i] = -0x7F00;
|
||||
(void)user_data;
|
||||
|
||||
short *stream = (short*)stream_uint8;
|
||||
const size_t frames_total = len / sizeof(short) / 2;
|
||||
|
||||
size_t frames_done = 0;
|
||||
|
||||
while (frames_done != frames_total)
|
||||
{
|
||||
long mix_buffer[0x800 * 2]; // 2 because stereo
|
||||
|
||||
size_t subframes = MIN(0x800, frames_total - frames_done);
|
||||
|
||||
memset(mix_buffer, 0, subframes * sizeof(long) * 2);
|
||||
|
||||
MixSoundsAndUpdateOrganya(mix_buffer, subframes);
|
||||
|
||||
for (size_t i = 0; i < subframes * 2; ++i)
|
||||
{
|
||||
if (mix_buffer[i] > 0x7FFF)
|
||||
*stream++ = 0x7FFF;
|
||||
else if (mix_buffer[i] < -0x7FFF)
|
||||
*stream++ = -0x7FFF;
|
||||
else
|
||||
stream[i] <<= 8;
|
||||
*stream++ = mix_buffer[i];
|
||||
}
|
||||
|
||||
frames_done += subframes;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -135,21 +135,21 @@ void Mixer_SetSoundPan(Mixer_Sound *sound, long pan)
|
|||
}
|
||||
|
||||
// Most CPU-intensive function in the game (2/3rd CPU time consumption in my experience), so marked with ATTRIBUTE_HOT so the compiler considers it a hot spot (as it is) when optimizing
|
||||
ATTRIBUTE_HOT void Mixer_MixSounds(short *stream, unsigned int frames_total)
|
||||
ATTRIBUTE_HOT void Mixer_MixSounds(long *stream, size_t frames_total)
|
||||
{
|
||||
for (Mixer_Sound *sound = sound_list_head; sound != NULL; sound = sound->next)
|
||||
{
|
||||
if (sound->playing)
|
||||
{
|
||||
short *stream_pointer = stream;
|
||||
long *stream_pointer = stream;
|
||||
|
||||
for (unsigned int frames_done = 0; frames_done < frames_total; ++frames_done)
|
||||
for (size_t frames_done = 0; frames_done < frames_total; ++frames_done)
|
||||
{
|
||||
// Perform linear interpolation
|
||||
const unsigned char subsample = sound->sample_offset_remainder >> 8;
|
||||
|
||||
const signed char interpolated_sample = ((sound->samples[sound->position] * (0x100 - subsample)) >> 8)
|
||||
+ ((sound->samples[sound->position + 1] * subsample) >> 8);
|
||||
const short interpolated_sample = sound->samples[sound->position] * (0x100 - subsample)
|
||||
+ sound->samples[sound->position + 1] * subsample;
|
||||
|
||||
// Mix, and apply volume
|
||||
*stream_pointer++ += (interpolated_sample * sound->volume_l) >> 8;
|
||||
|
|
|
@ -13,4 +13,4 @@ void Mixer_RewindSound(Mixer_Sound *sound);
|
|||
void Mixer_SetSoundFrequency(Mixer_Sound *sound, unsigned int frequency);
|
||||
void Mixer_SetSoundVolume(Mixer_Sound *sound, long volume);
|
||||
void Mixer_SetSoundPan(Mixer_Sound *sound, long pan);
|
||||
void Mixer_MixSounds(short *stream, unsigned int frames_total);
|
||||
void Mixer_MixSounds(long *stream, size_t frames_total);
|
||||
|
|
Loading…
Add table
Reference in a new issue