aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-09-10 11:43:56 +0200
committerMax Kellermann <max@duempel.org>2008-09-10 11:43:56 +0200
commit8d1801c59d67617541447a47b7fdd2cbcb4bc7cb (patch)
tree238755d6a3941dc85183ffe79d35f50323863082
parentcdbb9627a7fa32d2bc90d4bc1a93bf426bb7e8be (diff)
downloadmpd-8d1801c59d67617541447a47b7fdd2cbcb4bc7cb.tar.gz
mpd-8d1801c59d67617541447a47b7fdd2cbcb4bc7cb.tar.xz
mpd-8d1801c59d67617541447a47b7fdd2cbcb4bc7cb.zip
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().
-rw-r--r--src/audio.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/audio.c b/src/audio.c
index aa5f03e3f..b868edb30 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -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();