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().
This commit is contained in:
Max Kellermann 2008-09-10 11:43:56 +02:00
parent cdbb9627a7
commit 8d1801c59d

View File

@ -289,6 +289,27 @@ static int flushAudioBuffer(void)
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 ret = -1;
@ -302,10 +323,7 @@ int openAudioDevice(const struct audio_format *audioFormat)
flushAudioBuffer();
if (audioFormat != NULL)
audio_buffer.format = *audioFormat;
audio_buffer.size = (audio_buffer.format.bits >> 3) *
audio_buffer.format.channels;
audio_buffer.size *= audio_buffer.format.sampleRate >> 5;
audio_buffer.buffer = xrealloc(audio_buffer.buffer, audio_buffer.size);
audio_buffer_resize(audio_buffer_size(&audio_buffer.format));
}
syncAudioDeviceStates();