[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: |
Wed, 1 Sep 2010 10:25:19 -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 | 52 ++++++++++++++++++++++++++++++++++++----------------
1 files changed, 36 insertions(+), 16 deletions(-)
diff --git a/src/audio/libao.c b/src/audio/libao.c
index bae941e..0b4734c 100644
--- a/src/audio/libao.c
+++ b/src/audio/libao.c
@@ -72,6 +72,7 @@
This is the most portable way to initialize a stack-allocated struct to
zero. */
static ao_sample_format AO_FORMAT_INITIALIZER;
+static ao_sample_format current_ao_parameters;
static volatile int ao_stop_playback = 0;
@@ -80,6 +81,29 @@ static int libao_log_level;
ao_device *device = NULL;
+static inline 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_ao_parameters = format;
+}
+
+static inline void libao_close_handle(void)
+{
+ if (device != NULL)
+ {
+ ao_close(device);
+ device = NULL;
+ }
+}
+
static AudioID * libao_open (void **pars)
{
AudioID * id;
@@ -103,15 +127,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 +142,19 @@ 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_ao_parameters.channels)
+ || (track.sample_rate != current_ao_parameters.rate)
+ || (track.bits != current_ao_parameters.bits))
+ {
+ libao_close_handle();
+ libao_open_handle(track.sample_rate, track.num_channels, track.bits);
+ }
+
if (device == NULL)
{
ERR ("error opening libao dev");
@@ -150,8 +176,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;
}
@@ -172,16 +197,11 @@ static int libao_stop (AudioID * id)
static int libao_close (AudioID * id)
{
-
- if (device != NULL)
- ao_close (device);
- device = NULL;
-
+ libao_close_handle();
ao_shutdown ();
free (id);
id = NULL;
-
return 0;
}
--
1.7.2.2
- [PATCH] Reopen libao device handles when audio parameters change., (continued)
[PATCH] Reopen libao device handles when audio parameters change., Christopher Brannon, 2010/09/01
[PATCH] Reopen libao device handles when audio parameters change., Christopher Brannon, 2010/09/01
- [PATCH] Reopen libao device handles when audio parameters change.,
Christopher Brannon <=