[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Reopen libao device handles when audio parameters change.
From: |
Christopher Brannon |
Subject: |
[PATCH] Reopen libao device handles when audio parameters change. |
Date: |
Tue, 31 Aug 2010 06:00:56 -0500 |
Audio parameters can change from one track to another. E.G., a
fragment of speech might not have the same sample rate as a sound icon.
We need to reopen libao's ao_device * handle when such changes occur.
(This patch was made against the master branch.)
---
src/audio/libao.c | 51 +++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 41 insertions(+), 10 deletions(-)
diff --git a/src/audio/libao.c b/src/audio/libao.c
index bae941e..1772461 100644
--- a/src/audio/libao.c
+++ b/src/audio/libao.c
@@ -80,6 +80,37 @@ static int libao_log_level;
ao_device *device = NULL;
+/* Parameters associated with the current ao_device. */
+/* xxx These should probably be moved into a struct, along with the
+ ao_device * itself. */
+int current_bits = -1;
+int current_channels = -1;
+int current_rate = -1;
+
+static void libao_open_handle(int rate, int channels, int bits)
+{
+ ao_sample_format format = AO_FORMAT_INITIALIZER;
+
+ format.channels = channels;
+ format.rate = rate;
+ format.bits = bits;
+ format.byte_format = AO_FMT_NATIVE;
+ device = ao_open_live (default_driver, &format, NULL);
+
+ if (device != NULL)
+ {
+ current_rate = rate;
+ current_channels = channels;
+ current_bits = bits;
+ }
+}
+
+static void libao_close_handle(void)
+{
+ ao_close(device);
+ device = NULL;
+}
+
static AudioID * libao_open (void **pars)
{
AudioID * id;
@@ -103,15 +134,12 @@ static int libao_play (AudioID * id, AudioTrack track)
int i;
- ao_sample_format format = AO_FORMAT_INITIALIZER;
-
if (id == NULL)
return -1;
if (track.samples == NULL || track.num_samples <= 0)
return 0;
/* Choose the correct format */
- format.bits = track.bits;
if (track.bits == 16)
bytes_per_sample = 2;
else if (track.bits == 8)
@@ -121,14 +149,18 @@ static int libao_play (AudioID * id, AudioTrack track)
ERR ("Audio: Unrecognized sound data format.\n");
return -10;
}
- format.channels = track.num_channels;
- format.rate = track.sample_rate;
- format.byte_format = AO_FMT_NATIVE;
MSG (3, "Starting playback");
output_samples = track.samples;
num_bytes = track.num_samples * bytes_per_sample;
- if (device == NULL)
- device = ao_open_live (default_driver, &format, NULL);
+
+ if ((device == NULL) || (track.num_channels != current_channels)
+ || (track.sample_rate != current_rate) || (track.bits != current_bits))
+ {
+ if (device != NULL)
+ libao_close_handle();
+ libao_open_handle(track.sample_rate, track.num_channels, track.bits);
+ }
+
if (device == NULL)
{
ERR ("error opening libao dev");
@@ -150,8 +182,7 @@ static int libao_play (AudioID * id, AudioTrack track)
if (!ao_play (device, (char *) output_samples + outcnt, i))
{
- ao_close (device);
- device = NULL;
+ libao_close_handle();
ERR ("Audio: ao_play() - closing device - re-open it in next run\n");
return -1;
}
--
1.7.2.2