/*****************************************************************************
* mixer.c : audio output mixing operations
*****************************************************************************
- * Copyright (C) 2002 VideoLAN
- * $Id: mixer.c,v 1.4 2002/08/14 00:23:59 massiot Exp $
+ * Copyright (C) 2002-2004 the VideoLAN team
+ * $Id$
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <stdlib.h> /* calloc(), malloc(), free() */
-#include <string.h>
-
-#include <vlc/vlc.h>
-
-#ifdef HAVE_ALLOCA_H
-# include <alloca.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
#endif
-#include "audio_output.h"
+#include <stddef.h>
+#include <math.h>
+
+#include <vlc_common.h>
+#include <libvlc.h>
+#include <vlc_modules.h>
+#include <vlc_aout.h>
+#include <vlc_aout_mixer.h>
#include "aout_internal.h"
-/*****************************************************************************
- * aout_MixerNew: prepare a mixer plug-in
- *****************************************************************************/
-int aout_MixerNew( aout_instance_t * p_aout )
+#undef aout_MixerNew
+/**
+ * Creates a software amplifier.
+ */
+audio_mixer_t *aout_MixerNew(vlc_object_t *obj, vlc_fourcc_t format)
{
- p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL );
- if ( p_aout->mixer.p_module == NULL )
+ audio_mixer_t *mixer = vlc_custom_create(obj, sizeof (*mixer), "mixer");
+ if (unlikely(mixer == NULL))
+ return NULL;
+
+ mixer->format = format;
+ mixer->mix = NULL;
+ mixer->module = module_need(mixer, "audio mixer", NULL, false);
+ if (mixer->module == NULL)
{
- msg_Err( p_aout, "no suitable aout mixer" );
- return -1;
+ vlc_object_release(mixer);
+ mixer = NULL;
}
- return 0;
+ return mixer;
}
-/*****************************************************************************
- * aout_MixerDelete: delete the mixer
- *****************************************************************************/
-void aout_MixerDelete( aout_instance_t * p_aout )
+/**
+ * Destroys a software amplifier.
+ */
+void aout_MixerDelete(audio_mixer_t *mixer)
{
- module_Unneed( p_aout, p_aout->mixer.p_module );
+ if (mixer == NULL)
+ return;
+
+ module_unneed(mixer, mixer->module);
+ vlc_object_release(mixer);
}
-/*****************************************************************************
- * aout_MixerRun: entry point for the mixer & post-filters processing
- *****************************************************************************/
-void aout_MixerRun( aout_instance_t * p_aout )
+/**
+ * Applies replay gain and software volume to an audio buffer.
+ */
+void aout_MixerRun(audio_mixer_t *mixer, block_t *block, float amp)
{
- int i;
- aout_buffer_t * p_output_buffer;
+ mixer->mix(mixer, block, amp);
+}
- /* See if we have enough data to prepare a new buffer for the audio
- * output. */
- mtime_t wanted_date = 0, first_date = 0;
+/*** Replay gain ***/
+float (aout_ReplayGainSelect)(vlc_object_t *obj, const char *str,
+ const audio_replay_gain_t *replay_gain)
+{
+ float gain = 0.;
+ unsigned mode = AUDIO_REPLAY_GAIN_MAX;
- for ( i = 0; i < p_aout->i_nb_inputs; i++ )
- {
- aout_fifo_t * p_fifo = &p_aout->pp_inputs[i]->fifo;
- aout_buffer_t * p_buffer;
- vlc_mutex_lock( &p_fifo->lock );
- for ( p_buffer = p_fifo->p_first; p_buffer != NULL;
- p_buffer = p_buffer->p_next )
+ if (likely(str != NULL))
+ { /* Find selectrf mode */
+ if (!strcmp (str, "track"))
+ mode = AUDIO_REPLAY_GAIN_TRACK;
+ else
+ if (!strcmp (str, "album"))
+ mode = AUDIO_REPLAY_GAIN_ALBUM;
+
+ /* If the selectrf mode is not available, prefer the other one */
+ if (mode != AUDIO_REPLAY_GAIN_MAX && !replay_gain->pb_gain[mode])
{
- if ( !wanted_date )
- {
- if ( !p_aout->output.last_date )
- {
- first_date = p_buffer->start_date;
- wanted_date = p_buffer->start_date
- + (mtime_t)p_aout->output.i_nb_samples * 1000000
- / p_aout->output.output.i_rate;
- }
- else
- {
- first_date = p_aout->output.last_date;
- wanted_date = p_aout->output.last_date
- + (mtime_t)p_aout->output.i_nb_samples * 1000000
- / p_aout->output.output.i_rate;
- }
- }
-
- if ( p_buffer->end_date >= wanted_date ) break;
+ if (replay_gain->pb_gain[!mode])
+ mode = !mode;
}
- vlc_mutex_unlock( &p_fifo->lock );
- if ( p_buffer == NULL ) break;
}
- if ( i < p_aout->i_nb_inputs )
- {
- /* Interrupted before the end... We can't run. */
- return;
- }
+ /* */
+ if (mode == AUDIO_REPLAY_GAIN_MAX)
+ return 1.;
- /* Run the mixer. */
- aout_BufferAlloc( &p_aout->mixer.output_alloc,
- ((u64)p_aout->output.i_nb_samples * 1000000)
- / p_aout->output.output.i_rate,
- /* This is a bit kludgy, but is actually only used
- * for the S/PDIF dummy mixer : */
- p_aout->pp_inputs[0]->fifo.p_first,
- p_output_buffer );
- if ( p_output_buffer == NULL )
- {
- msg_Err( p_aout, "out of memory" );
- return;
- }
- p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
- p_output_buffer->i_nb_bytes = (wanted_date - first_date)
- * aout_FormatToByterate( &p_aout->mixer.output )
- / 1000000;
- p_output_buffer->start_date = first_date;
- p_output_buffer->end_date = wanted_date;
- p_aout->output.last_date = wanted_date;
+ if (replay_gain->pb_gain[mode])
+ gain = replay_gain->pf_gain[mode]
+ + var_InheritFloat (obj, "audio-replay-gain-preamp");
+ else
+ gain = var_InheritFloat (obj, "audio-replay-gain-default");
- p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
+ float multiplier = pow (10., gain / 20.);
- aout_OutputPlay( p_aout, p_output_buffer );
-}
+ if (replay_gain->pb_peak[mode]
+ && var_InheritBool (obj, "audio-replay-gain-peak-protection")
+ && replay_gain->pf_peak[mode] * multiplier > 1.0)
+ multiplier = 1.0f / replay_gain->pf_peak[mode];
+ return multiplier;
+}