Commit 8d1801c5 authored by Max Kellermann's avatar Max Kellermann

audio: added function audio_buffer_resize()

To make openAudioDevice() smaller and more readable, move code to a static function. Also don't use realloc(), since the old value of the buffer isn't needed anymore, saving a memcpy().
parent cdbb9627
...@@ -289,6 +289,27 @@ static int flushAudioBuffer(void) ...@@ -289,6 +289,27 @@ static int flushAudioBuffer(void)
return ret; return ret;
} }
static size_t audio_buffer_size(const struct audio_format *af)
{
return (af->bits >> 3) * af->channels * (af->sampleRate >> 5);
}
static void audio_buffer_resize(size_t size)
{
if (audio_buffer.size == size)
return;
if (audio_buffer.buffer != NULL)
free(audio_buffer.buffer);
if (size > 0)
audio_buffer.buffer = xmalloc(size);
else
audio_buffer.buffer = NULL;
audio_buffer.size = size;
}
int openAudioDevice(const struct audio_format *audioFormat) int openAudioDevice(const struct audio_format *audioFormat)
{ {
int ret = -1; int ret = -1;
...@@ -302,10 +323,7 @@ int openAudioDevice(const struct audio_format *audioFormat) ...@@ -302,10 +323,7 @@ int openAudioDevice(const struct audio_format *audioFormat)
flushAudioBuffer(); flushAudioBuffer();
if (audioFormat != NULL) if (audioFormat != NULL)
audio_buffer.format = *audioFormat; audio_buffer.format = *audioFormat;
audio_buffer.size = (audio_buffer.format.bits >> 3) * audio_buffer_resize(audio_buffer_size(&audio_buffer.format));
audio_buffer.format.channels;
audio_buffer.size *= audio_buffer.format.sampleRate >> 5;
audio_buffer.buffer = xrealloc(audio_buffer.buffer, audio_buffer.size);
} }
syncAudioDeviceStates(); syncAudioDeviceStates();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment