Commit d88c3c84 authored by Max Kellermann's avatar Max Kellermann

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).
parent 5399a72e
...@@ -300,6 +300,12 @@ void audio_output_finish(struct audio_output *ao) ...@@ -300,6 +300,12 @@ void audio_output_finish(struct audio_output *ao)
g_cond_free(ao->cond); g_cond_free(ao->cond);
g_mutex_free(ao->mutex); 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); filter_free(ao->filter);
pcm_buffer_deinit(&ao->cross_fade_buffer); pcm_buffer_deinit(&ao->cross_fade_buffer);
......
...@@ -209,10 +209,17 @@ audio_output_init(struct audio_output *ao, const struct config_param *param, ...@@ -209,10 +209,17 @@ audio_output_init(struct audio_output *ao, const struct config_param *param,
param, NULL); param, NULL);
assert(ao->replay_gain_filter != NULL); assert(ao->replay_gain_filter != NULL);
filter_chain_append(ao->filter, ao->replay_gain_filter);
ao->replay_gain_serial = 0; 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->replay_gain_filter = NULL;
ao->other_replay_gain_filter = NULL;
}
/* create the normalization filter (if configured) */ /* create the normalization filter (if configured) */
......
...@@ -159,6 +159,19 @@ struct audio_output { ...@@ -159,6 +159,19 @@ struct audio_output {
unsigned replay_gain_serial; 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. * The convert_filter_plugin instance of this audio output.
* It is the last item in the filter chain, and is responsible * It is the last item in the filter chain, and is responsible
* for converting the input data into the appropriate format * for converting the input data into the appropriate format
......
...@@ -94,12 +94,33 @@ ao_filter_open(struct audio_output *ao, ...@@ -94,12 +94,33 @@ ao_filter_open(struct audio_output *ao,
struct audio_format *audio_format, struct audio_format *audio_format,
GError **error_r) 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 static void
ao_filter_close(struct audio_output *ao) 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); filter_close(ao->filter);
} }
...@@ -258,6 +279,8 @@ ao_reopen(struct audio_output *ao) ...@@ -258,6 +279,8 @@ ao_reopen(struct audio_output *ao)
static const char * static const char *
ao_chunk_data(struct audio_output *ao, const struct music_chunk *chunk, 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) size_t *length_r)
{ {
assert(chunk != NULL); assert(chunk != NULL);
...@@ -271,6 +294,26 @@ ao_chunk_data(struct audio_output *ao, const struct music_chunk *chunk, ...@@ -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); 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; *length_r = length;
return data; return data;
} }
...@@ -282,30 +325,29 @@ ao_filter_chunk(struct audio_output *ao, const struct music_chunk *chunk, ...@@ -282,30 +325,29 @@ ao_filter_chunk(struct audio_output *ao, const struct music_chunk *chunk,
GError *error = NULL; GError *error = NULL;
size_t length; 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) { if (length == 0) {
/* empty chunk, nothing to do */ /* empty chunk, nothing to do */
*length_r = 0; *length_r = 0;
return data; 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 */ /* cross-fade */
if (chunk->other != NULL) { if (chunk->other != NULL) {
size_t other_length; size_t other_length;
const char *other_data = ao_chunk_data(ao, chunk->other, const char *other_data =
&other_length); 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) { if (other_length == 0) {
*length_r = 0; *length_r = 0;
return data; return data;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment