Update Wii U audio backend

This commit is contained in:
Clownacy 2020-06-24 17:34:25 +01:00
parent f64f924b54
commit dae87c6106

View file

@ -27,12 +27,12 @@ static OSMutex organya_mutex;
static AXVoice *voices[2]; static AXVoice *voices[2];
static short *stream_buffers[2]; static short *stream_buffers[2];
static float *stream_buffer_float; static long *stream_buffer_long;
static size_t buffer_length; static size_t buffer_length;
static unsigned long output_frequency; static unsigned long output_frequency;
static void FillMixerBuffer(float *stream, size_t frames_total) static void MixSoundsAndUpdateOrganya(long *stream, size_t frames_total)
{ {
OSLockMutex(&organya_mutex); OSLockMutex(&organya_mutex);
@ -91,39 +91,38 @@ static void FrameCallback(void)
if (current_buffer != last_buffer) if (current_buffer != last_buffer)
{ {
// Clear the mixer buffer // Clear the mixer buffer
for (unsigned int i = 0; i < buffer_length * 2; ++i) memset(stream_buffer_long, 0, buffer_length * sizeof(long) * 2);
stream_buffer_float[i] = 0.0f;
// Fill mixer buffer // Fill mixer buffer
FillMixerBuffer(stream_buffer_float, buffer_length); MixSoundsAndUpdateOrganya(stream_buffer_long, buffer_length);
// Deinterlate samples, convert them to S16, and write them to the double-buffers // Deinterlate samples, convert them to S16, and write them to the double-buffers
short *left_output_buffer = &stream_buffers[0][buffer_length * last_buffer]; short *left_output_buffer = &stream_buffers[0][buffer_length * last_buffer];
short *right_output_buffer = &stream_buffers[1][buffer_length * last_buffer]; short *right_output_buffer = &stream_buffers[1][buffer_length * last_buffer];
float *mixer_buffer_pointer = stream_buffer_float; long *mixer_buffer_pointer = stream_buffer_long;
short *left_output_buffer_pointer = left_output_buffer; short *left_output_buffer_pointer = left_output_buffer;
short *right_output_buffer_pointer = right_output_buffer; short *right_output_buffer_pointer = right_output_buffer;
for (unsigned int i = 0; i < buffer_length; ++i) for (unsigned int i = 0; i < buffer_length; ++i)
{ {
float left_sample = *mixer_buffer_pointer++; const long left_sample = *mixer_buffer_pointer++;
float right_sample = *mixer_buffer_pointer++; const long right_sample = *mixer_buffer_pointer++;
// Clamp samples to sane limits // Clamp samples to sane limits, convert to S16, and store in double-buffers
if (left_sample < -1.0f) if (left_sample > 0x7FFF)
left_sample = -1.0f; *left_output_buffer_pointer++ = 0x7FFF;
else if (left_sample > 1.0f) else if (left_sample < -0x7FFF)
left_sample = 1.0f; *left_output_buffer_pointer++ = -0x7FFF;
else
*left_output_buffer_pointer++ = (short)left_sample;
if (right_sample < -1.0f) if (right_sample > 0x7FFF)
right_sample = -1.0f; *right_output_buffer_pointer++ = 0x7FFF;
else if (right_sample > 1.0f) else if (right_sample < -0x7FFF)
right_sample = 1.0f; *right_output_buffer_pointer++ = -0x7FFF;
else
// Convert to S16 and store in double-buffers *right_output_buffer_pointer++ = (short)right_sample;
*left_output_buffer_pointer++ = (short)(left_sample * 32767.0f);
*right_output_buffer_pointer++ = (short)(right_sample * 32767.0f);
} }
// Make sure the sound hardware can see our data // Make sure the sound hardware can see our data
@ -158,11 +157,11 @@ bool AudioBackend_Init(void)
// Create and initialise two 'voices': each one will stream its own // Create and initialise two 'voices': each one will stream its own
// audio - one for the left speaker, and one for the right. // audio - one for the left speaker, and one for the right.
// The software-mixer outputs interlaced float samples, so create // The software-mixer outputs interlaced samples into a buffer of `long`s,
// a buffer for it here. // so create a buffer for it here.
stream_buffer_float = (float*)malloc(buffer_length * sizeof(float) * 2); // `* 2` because it's an interlaced stereo buffer stream_buffer_long = (long*)malloc(buffer_length * sizeof(long) * 2); // `* 2` because it's an interlaced stereo buffer
if (stream_buffer_float != NULL) if (stream_buffer_long != NULL)
{ {
stream_buffers[0] = (short*)malloc(buffer_length * sizeof(short) * 2); // `* 2` because it's a double-buffer stream_buffers[0] = (short*)malloc(buffer_length * sizeof(short) * 2); // `* 2` because it's a double-buffer
@ -232,7 +231,7 @@ bool AudioBackend_Init(void)
free(stream_buffers[0]); free(stream_buffers[0]);
} }
free(stream_buffer_float); free(stream_buffer_long);
} }
return false; return false;