From d88c3c8462f12cec592d24ef8a3ebd570ede9ca6 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 2 May 2010 15:57:59 +0200 Subject: output_thread: call replay gain filter manually Don't add it to the filter chain, because we need to apply replay gain before cross-fading with the next song. Add a second replay_gain filter which is used for the song being faded in (chunk->other). --- src/output_control.c | 6 +++++ src/output_init.c | 11 ++++++-- src/output_internal.h | 13 ++++++++++ src/output_thread.c | 72 ++++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 85 insertions(+), 17 deletions(-) diff --git a/src/output_control.c b/src/output_control.c index bcedae247..5b9b2b902 100644 --- a/src/output_control.c +++ b/src/output_control.c @@ -300,6 +300,12 @@ void audio_output_finish(struct audio_output *ao) g_cond_free(ao->cond); g_mutex_free(ao->mutex); + if (ao->replay_gain_filter != NULL) + filter_free(ao->replay_gain_filter); + + if (ao->other_replay_gain_filter != NULL) + filter_free(ao->other_replay_gain_filter); + filter_free(ao->filter); pcm_buffer_deinit(&ao->cross_fade_buffer); diff --git a/src/output_init.c b/src/output_init.c index a091e749a..f4700dfb2 100644 --- a/src/output_init.c +++ b/src/output_init.c @@ -209,10 +209,17 @@ audio_output_init(struct audio_output *ao, const struct config_param *param, param, NULL); assert(ao->replay_gain_filter != NULL); - filter_chain_append(ao->filter, ao->replay_gain_filter); ao->replay_gain_serial = 0; - } else + + ao->other_replay_gain_filter = filter_new(&replay_gain_filter_plugin, + param, NULL); + assert(ao->other_replay_gain_filter != NULL); + + ao->other_replay_gain_serial = 0; + } else { ao->replay_gain_filter = NULL; + ao->other_replay_gain_filter = NULL; + } /* create the normalization filter (if configured) */ diff --git a/src/output_internal.h b/src/output_internal.h index 06e209374..9e4d1f25d 100644 --- a/src/output_internal.h +++ b/src/output_internal.h @@ -158,6 +158,19 @@ struct audio_output { */ unsigned replay_gain_serial; + /** + * The replay_gain_filter_plugin instance of this audio + * output, to be applied to the second chunk during + * cross-fading. + */ + struct filter *other_replay_gain_filter; + + /** + * The serial number of the last replay gain info by the + * "other" chunk during cross-fading. + */ + unsigned other_replay_gain_serial; + /** * The convert_filter_plugin instance of this audio output. * It is the last item in the filter chain, and is responsible diff --git a/src/output_thread.c b/src/output_thread.c index 7f1e4935e..df9d7801b 100644 --- a/src/output_thread.c +++ b/src/output_thread.c @@ -94,12 +94,33 @@ ao_filter_open(struct audio_output *ao, struct audio_format *audio_format, GError **error_r) { - return filter_open(ao->filter, audio_format, error_r); + /* the replay_gain filter cannot fail here */ + if (ao->replay_gain_filter != NULL) + filter_open(ao->replay_gain_filter, audio_format, error_r); + if (ao->other_replay_gain_filter != NULL) + filter_open(ao->other_replay_gain_filter, audio_format, + error_r); + + const struct audio_format *af + = filter_open(ao->filter, audio_format, error_r); + if (af == NULL) { + if (ao->replay_gain_filter != NULL) + filter_close(ao->replay_gain_filter); + if (ao->other_replay_gain_filter != NULL) + filter_close(ao->other_replay_gain_filter); + } + + return af; } static void ao_filter_close(struct audio_output *ao) { + if (ao->replay_gain_filter != NULL) + filter_close(ao->replay_gain_filter); + if (ao->other_replay_gain_filter != NULL) + filter_close(ao->other_replay_gain_filter); + filter_close(ao->filter); } @@ -258,6 +279,8 @@ ao_reopen(struct audio_output *ao) static const char * ao_chunk_data(struct audio_output *ao, const struct music_chunk *chunk, + struct filter *replay_gain_filter, + unsigned *replay_gain_serial_p, size_t *length_r) { assert(chunk != NULL); @@ -271,6 +294,26 @@ ao_chunk_data(struct audio_output *ao, const struct music_chunk *chunk, assert(length % audio_format_frame_size(&ao->in_audio_format) == 0); + if (length > 0 && replay_gain_filter != NULL) { + if (chunk->replay_gain_serial != *replay_gain_serial_p) { + replay_gain_filter_set_info(replay_gain_filter, + chunk->replay_gain_serial != 0 + ? &chunk->replay_gain_info + : NULL); + *replay_gain_serial_p = chunk->replay_gain_serial; + } + + GError *error = NULL; + data = filter_filter(replay_gain_filter, data, length, + &length, &error); + if (data == NULL) { + g_warning("\"%s\" [%s] failed to filter: %s", + ao->name, ao->plugin->name, error->message); + g_error_free(error); + return NULL; + } + } + *length_r = length; return data; } @@ -282,30 +325,29 @@ ao_filter_chunk(struct audio_output *ao, const struct music_chunk *chunk, GError *error = NULL; size_t length; - const char *data = ao_chunk_data(ao, chunk, &length); + const char *data = ao_chunk_data(ao, chunk, ao->replay_gain_filter, + &ao->replay_gain_serial, &length); + if (data == NULL) + return NULL; + if (length == 0) { /* empty chunk, nothing to do */ *length_r = 0; return data; } - /* update replay gain */ - - if (ao->replay_gain_filter != NULL && - chunk->replay_gain_serial != ao->replay_gain_serial) { - replay_gain_filter_set_info(ao->replay_gain_filter, - chunk->replay_gain_serial != 0 - ? &chunk->replay_gain_info - : NULL); - ao->replay_gain_serial = chunk->replay_gain_serial; - } - /* cross-fade */ if (chunk->other != NULL) { size_t other_length; - const char *other_data = ao_chunk_data(ao, chunk->other, - &other_length); + const char *other_data = + ao_chunk_data(ao, chunk->other, + ao->other_replay_gain_filter, + &ao->other_replay_gain_serial, + &other_length); + if (other_data == NULL) + return NULL; + if (other_length == 0) { *length_r = 0; return data; -- cgit v1.2.3