diff options
| author | René Stadler <mail@renestadler.de> | 2006-10-06 15:56:01 +0000 | 
|---|---|---|
| committer | Tim-Philipp Müller <tim@centricular.net> | 2006-10-06 15:56:01 +0000 | 
| commit | edc2785eeb8304b58cd4122d9a228d093f6c1c79 (patch) | |
| tree | 5d64aaa7f752b91def302ba03fe78b85a5c6265e | |
| parent | 1e2737eb83dff0d03e76f709da26792a9e083df3 (diff) | |
Add ReplayGain analysis element (#357069).
Original commit message from CVS:
Patch by: René Stadler  <mail at renestadler de>
* configure.ac:
* docs/plugins/Makefile.am:
* docs/plugins/gst-plugins-bad-plugins-docs.sgml:
* docs/plugins/gst-plugins-bad-plugins-sections.txt:
* gst/replaygain/Makefile.am:
* gst/replaygain/gstrganalysis.c: (gst_rg_analysis_base_init),
(gst_rg_analysis_class_init), (gst_rg_analysis_init),
(gst_rg_analysis_set_property), (gst_rg_analysis_get_property),
(gst_rg_analysis_start), (gst_rg_analysis_set_caps),
(gst_rg_analysis_transform_ip), (gst_rg_analysis_event),
(gst_rg_analysis_stop), (gst_rg_analysis_handle_tags),
(gst_rg_analysis_handle_eos), (gst_rg_analysis_track_result),
(gst_rg_analysis_album_result), (plugin_init):
* gst/replaygain/gstrganalysis.h:
* gst/replaygain/rganalysis.c: (yule_filter), (butter_filter),
(apply_filters), (reset_filters), (accumulator_add),
(accumulator_clear), (accumulator_result), (rg_analysis_new),
(rg_analysis_set_sample_rate), (rg_analysis_destroy),
(rg_analysis_analyze_mono_float),
(rg_analysis_analyze_stereo_float),
(rg_analysis_analyze_mono_int16),
(rg_analysis_analyze_stereo_int16), (rg_analysis_analyze),
(rg_analysis_track_result), (rg_analysis_album_result),
(rg_analysis_reset_album), (rg_analysis_reset):
* gst/replaygain/rganalysis.h:
Add ReplayGain analysis element (#357069).
* tests/check/Makefile.am:
* tests/check/elements/.cvsignore:
* tests/check/elements/rganalysis.c: (get_expected_gain),
(setup_rganalysis), (cleanup_rganalysis), (set_playing_state),
(send_eos_event), (send_tag_event), (poll_eos), (poll_tags),
(fail_unless_track_gain), (fail_unless_track_peak),
(fail_unless_album_gain), (fail_unless_album_peak),
(fail_if_track_tags), (fail_if_album_tags),
(fail_unless_num_tracks), (test_buffer_const_float_mono),
(test_buffer_const_float_stereo), (test_buffer_const_int16_mono),
(test_buffer_const_int16_stereo), (test_buffer_square_float_mono),
(test_buffer_square_float_stereo), (test_buffer_square_int16_mono),
(test_buffer_square_int16_stereo), (push_buffer), (GST_START_TEST),
(rganalysis_suite), (main):
Unit tests for the new replaygain element.
| -rw-r--r-- | gst/replaygain/Makefile.am | 13 | ||||
| -rw-r--r-- | gst/replaygain/gstrganalysis.c | 686 | ||||
| -rw-r--r-- | gst/replaygain/gstrganalysis.h | 83 | ||||
| -rw-r--r-- | gst/replaygain/rganalysis.c | 772 | ||||
| -rw-r--r-- | gst/replaygain/rganalysis.h | 58 | ||||
| -rw-r--r-- | tests/check/elements/rganalysis.c | 1871 | 
6 files changed, 3483 insertions, 0 deletions
| diff --git a/gst/replaygain/Makefile.am b/gst/replaygain/Makefile.am new file mode 100644 index 00000000..eb44591e --- /dev/null +++ b/gst/replaygain/Makefile.am @@ -0,0 +1,13 @@ +plugin_LTLIBRARIES = libgstreplaygain.la + +libgstreplaygain_la_SOURCES = \ +	gstrganalysis.c       \ +	rganalysis.c +libgstreplaygain_la_CFLAGS  = $(GST_CFLAGS) $(GST_BASE_CFLAGS) +libgstreplaygain_la_LIBADD  = $(GST_LIBS) $(GST_BASE_LIBS) +libgstreplaygain_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) + +# headers we need but don't want installed +noinst_HEADERS =         \ +	gstrganalysis.h  \ +	rganalysis.h diff --git a/gst/replaygain/gstrganalysis.c b/gst/replaygain/gstrganalysis.c new file mode 100644 index 00000000..adf85555 --- /dev/null +++ b/gst/replaygain/gstrganalysis.c @@ -0,0 +1,686 @@ +/* GStreamer ReplayGain analysis + * + * Copyright (C) 2006 Rene Stadler <mail@renestadler.de> + *  + * gstrganalysis.c: Element that performs the ReplayGain analysis + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + *  + * This library 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 GNU + * Lesser General Public License for more details. + *  + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +/** + * SECTION:element-rganalysis + * + * <refsect2> + * <para> + * GstRgAnalysis analyzes raw audio sample data in accordance with the + * proposed <ulink url="http://replaygain.org">ReplayGain + * standard</ulink> for calculating the ideal replay gain for music + * tracks and albums.  The element is designed as a pass-through + * filter that never modifies any data.  As it receives an EOS event, + * it finalizes the ongoing analysis and generates a tag list + * containing the results.  It is sent downstream with a TAG event and + * posted on the message bus with a TAG message.  The EOS event is + * forwarded as normal afterwards.  Result tag lists at least contain + * the tags #GST_TAG_TRACK_GAIN and #GST_TAG_TRACK_PEAK. + * </para> + * <title>Album processing</title> + * <para> + * Analyzing several streams sequentially and assigning them a common + * result gain is known as "album processing".  If this gain is used + * during playback (by switching to "album mode"), all tracks receive + * the same amplification.  This keeps the relative volume levels + * between the tracks intact.  To enable this, set the <link + * linkend="GstRgAnalysis--num-tracks">num-tracks</link> property to + * the number of streams that will be processed as album tracks. + * Every time an EOS event is received, the value of this property + * will be decremented by one.  As it reaches zero, it is assumed that + * the last track of the album finished.  The tag list for the final + * stream will contain the additional tags #GST_TAG_ALBUM_GAIN and + * #GST_TAG_ALBUM_PEAK.  All other streams just get the two track tags + * posted because the values for the album tags are not known before + * all tracks are analyzed.  Applications need to make sure that the + * album gain and peak values are also associated with the other + * tracks when storing the results.  It is thus a bit more complex to + * implement, but should not be avoided since the album gain is + * generally more valuable for use during playback than the track + * gain. + * </para> + * <title>Skipping processing</title>  + * <para> + * For assisting transcoder/converter applications, the element can + * silently skip the processing of streams that already contain the + * necessary meta data tags.  Data will flow as usual but the element + * will not consume CPU time and will not generate result tags.  To + * enable possible skipping, set the <link + * linkend="GstRgAnalysis--forced">forced</link> property to #FALSE. + * If used in conjunction with album processing, the element will skip + * the number of remaining album tracks if a full set of tags is found + * for the first track.  If a subsequent track of the album is missing + * tags, processing cannot start again.  If this is undesired, your + * application has to scan all files beforehand and enable forcing of + * processing if needed. + * </para> + * <title>Tips</title> + * <itemizedlist> + * <listitem><para> + * Because the generated metadata tags become available at the end of + * streams, downstream muxer and encoder elements are normally unable + * to save them in their output since they generally save metadata in + * the file header.  Therefore, it is often necessary that + * applications read the results in a bus event handler for the tag + * message.  Obtaining the values this way is always needed for album + * processing since the album gain and peak values need to be + * associated with all tracks of an album, not just the last one. + * </para></listitem> + * <listitem><para> + * To perform album processing, the element has to preserve data + * between streams.  This cannot survive a state change to the NULL or + * READY state.  If you change your pipeline's state to NULL or READY + * between tracks, lock the rganalysis element's state using + * gst_element_set_locked_state() when it is in PAUSED or PLAYING.  As + * with any other element, don't forget to unlock it again and set it + * to the NULL state before dropping the last reference. + * </para></listitem> + * <listitem><para> + * If the total number of album tracks is unknown beforehand, set the + * num-tracks property to some large value like #G_MAXINT (or set it + * to >= 2 before each track starts).  Before the last track ends, set + * the property value to 1. + * </para></listitem> + * </itemizedlist> + * <title>Compliance</title> + * <para> + * Analyzing the ReplayGain pink noise reference waveform will compute + * a result of +6.00dB instead of the expected 0.00dB because the + * default reference level is 89dB.  To obtain values as lined out in + * the original proposal of ReplayGain, set the <link + * linkend="GstRgAnalysis--reference-level">reference-level</link> + * property to 83.  Almost all software uses 89dB as a reference + * however, which works against the tendency of the algorithm to + * advise to drastically lower the volume of music with a highly + * compressed dynamic range and high average output levels.  This + * tendency is normally to be fought during playback (if wanted), by + * using a default pre-amp value of at least +6.00dB.  At one point, + * the majority of analyzer implementations switched to 89dB which + * moved this adjustment to the analyzing/metadata writing process. + * This change has been acknowledged by the author of the ReplayGain + * proposal, however at the time of this writing, the webpage is still + * not updated. + * </para> + * <title>Example launch lines</title> + * <para>Analyze a simple test waveform:</para> + * <programlisting> + * gst-launch -t audiotestsrc wave=sine num-buffers=512 ! rganalysis ! fakesink + * </programlisting> + * <para>Analyze a given file:</para> + * <programlisting> + * gst-launch -t filesrc location="Some file.ogg" ! decodebin ! audioconvert ! audioresample ! rganalysis ! fakesink + * </programlisting> + * <para>Analyze the pink noise reference file:</para> + * <programlisting> + * gst-launch -t gnomevfssrc location=http://replaygain.hydrogenaudio.org/ref_pink.wav ! wavparse ! rganalysis ! fakesink + * </programlisting> + * <title>Acknowledgements</title> + * <para> + * This element is based on code used in the <ulink + * url="http://sjeng.org/vorbisgain.html">vorbisgain</ulink> program + * and many others.  The relevant parts are copyrighted by David + * Robinson, Glen Sawyer and Frank Klemm. + * </para> + * </refsect2> + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <string.h> +#include <gst/gst.h> +#include <gst/base/gstbasetransform.h> + +#include "gstrganalysis.h" + +GST_DEBUG_CATEGORY_STATIC (gst_rg_analysis_debug); +#define GST_CAT_DEFAULT gst_rg_analysis_debug + +static const GstElementDetails rganalysis_details = { +  "ReplayGain analysis", +  "Filter/Analyzer/Audio", +  "Perform the ReplayGain analysis", +  "Ren\xc3\xa9 Stadler <mail@renestadler.de>" +}; + +/* Default property value. */ +#define FORCED_DEFAULT TRUE + +enum +{ +  PROP_0, +  PROP_NUM_TRACKS, +  PROP_FORCED, +  PROP_REFERENCE_LEVEL +}; + +/* The ReplayGain algorithm is intended for use with mono and stereo + * audio.  The used implementation has filter coefficients for the + * "usual" sample rates in the 8000 to 48000 Hz range. */ +#define REPLAY_GAIN_CAPS                                                \ +  "channels = (int) { 1, 2 }, "                                         \ +  "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, "     \ +  "44100, 48000 }" + +static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", +    GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, " +        "width = (int) 32, " "endianness = (int) BYTE_ORDER, " +        REPLAY_GAIN_CAPS "; " +        "audio/x-raw-int, " +        "width = (int) 16, " "depth = (int) [ 1, 16 ], " +        "signed = (boolean) true, " "endianness = (int) BYTE_ORDER, " +        REPLAY_GAIN_CAPS)); + +static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", +    GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, " +        "width = (int) 32, " "endianness = (int) BYTE_ORDER, " +        REPLAY_GAIN_CAPS "; " +        "audio/x-raw-int, " +        "width = (int) 16, " "depth = (int) [ 1, 16 ], " +        "signed = (boolean) true, " "endianness = (int) BYTE_ORDER, " +        REPLAY_GAIN_CAPS)); + +GST_BOILERPLATE (GstRgAnalysis, gst_rg_analysis, GstBaseTransform, +    GST_TYPE_BASE_TRANSFORM); + +static void gst_rg_analysis_class_init (GstRgAnalysisClass * klass); +static void gst_rg_analysis_init (GstRgAnalysis * filter, +    GstRgAnalysisClass * gclass); + +static void gst_rg_analysis_set_property (GObject * object, guint prop_id, +    const GValue * value, GParamSpec * pspec); +static void gst_rg_analysis_get_property (GObject * object, guint prop_id, +    GValue * value, GParamSpec * pspec); + +static gboolean gst_rg_analysis_start (GstBaseTransform * base); +static gboolean gst_rg_analysis_set_caps (GstBaseTransform * base, +    GstCaps * incaps, GstCaps * outcaps); +static GstFlowReturn gst_rg_analysis_transform_ip (GstBaseTransform * base, +    GstBuffer * buf); +static gboolean gst_rg_analysis_event (GstBaseTransform * base, +    GstEvent * event); +static gboolean gst_rg_analysis_stop (GstBaseTransform * base); + +static void gst_rg_analysis_handle_tags (GstRgAnalysis * filter, +    const GstTagList * tag_list); +static void gst_rg_analysis_handle_eos (GstRgAnalysis * filter); +static gboolean gst_rg_analysis_track_result (GstRgAnalysis * filter, +    GstTagList ** tag_list); +static gboolean gst_rg_analysis_album_result (GstRgAnalysis * filter, +    GstTagList ** tag_list); + +static void +gst_rg_analysis_base_init (gpointer g_class) +{ +  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + +  gst_element_class_add_pad_template (element_class, +      gst_static_pad_template_get (&src_factory)); +  gst_element_class_add_pad_template (element_class, +      gst_static_pad_template_get (&sink_factory)); +  gst_element_class_set_details (element_class, &rganalysis_details); + +  GST_DEBUG_CATEGORY_INIT (gst_rg_analysis_debug, "rganalysis", 0, +      "ReplayGain analysis element"); +} + +static void +gst_rg_analysis_class_init (GstRgAnalysisClass * klass) +{ +  GObjectClass *gobject_class; +  GstBaseTransformClass *trans_class; + +  gobject_class = (GObjectClass *) klass; +  gobject_class->set_property = gst_rg_analysis_set_property; +  gobject_class->get_property = gst_rg_analysis_get_property; + +  g_object_class_install_property (gobject_class, PROP_NUM_TRACKS, +      g_param_spec_int ("num-tracks", "Number of album tracks", +          "Number of remaining tracks in the album", +          0, G_MAXINT, 0, G_PARAM_READWRITE)); +  g_object_class_install_property (gobject_class, PROP_FORCED, +      g_param_spec_boolean ("forced", "Force processing", +          "Analyze streams even when ReplayGain tags exist", +          FORCED_DEFAULT, G_PARAM_READWRITE)); +  g_object_class_install_property (gobject_class, PROP_REFERENCE_LEVEL, +      g_param_spec_double ("reference-level", "Reference level", +          "Reference level in dB (83.0 for original proposal)", +          0.0, G_MAXDOUBLE, RG_REFERENCE_LEVEL, G_PARAM_READWRITE)); + +  trans_class = (GstBaseTransformClass *) klass; +  trans_class->start = GST_DEBUG_FUNCPTR (gst_rg_analysis_start); +  trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_rg_analysis_set_caps); +  trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_rg_analysis_transform_ip); +  trans_class->event = GST_DEBUG_FUNCPTR (gst_rg_analysis_event); +  trans_class->stop = GST_DEBUG_FUNCPTR (gst_rg_analysis_stop); +  trans_class->passthrough_on_same_caps = TRUE; +} + +static void +gst_rg_analysis_init (GstRgAnalysis * filter, GstRgAnalysisClass * gclass) +{ +  filter->num_tracks = 0; +  filter->forced = FORCED_DEFAULT; +  filter->reference_level = RG_REFERENCE_LEVEL; + +  filter->ctx = NULL; +  filter->analyze = NULL; +} + +static void +gst_rg_analysis_set_property (GObject * object, guint prop_id, +    const GValue * value, GParamSpec * pspec) +{ +  GstRgAnalysis *filter = GST_RG_ANALYSIS (object); + +  switch (prop_id) { +    case PROP_NUM_TRACKS: +      filter->num_tracks = g_value_get_int (value); +      break; +    case PROP_FORCED: +      filter->forced = g_value_get_boolean (value); +      break; +    case PROP_REFERENCE_LEVEL: +      filter->reference_level = g_value_get_double (value); +      break; +    default: +      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); +      break; +  } +} + +static void +gst_rg_analysis_get_property (GObject * object, guint prop_id, +    GValue * value, GParamSpec * pspec) +{ +  GstRgAnalysis *filter = GST_RG_ANALYSIS (object); + +  switch (prop_id) { +    case PROP_NUM_TRACKS: +      g_value_set_int (value, filter->num_tracks); +      break; +    case PROP_FORCED: +      g_value_set_boolean (value, filter->forced); +      break; +    case PROP_REFERENCE_LEVEL: +      g_value_set_double (value, filter->reference_level); +      break; +    default: +      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); +      break; +  } +} + +static gboolean +gst_rg_analysis_start (GstBaseTransform * base) +{ +  GstRgAnalysis *filter = GST_RG_ANALYSIS (base); + +  filter->ignore_tags = FALSE; +  filter->skip = FALSE; +  filter->has_track_gain = FALSE; +  filter->has_track_peak = FALSE; +  filter->has_album_gain = FALSE; +  filter->has_album_peak = FALSE; + +  filter->ctx = rg_analysis_new (); +  filter->analyze = NULL; + +  GST_DEBUG_OBJECT (filter, "Started"); + +  return TRUE; +} + +static gboolean +gst_rg_analysis_set_caps (GstBaseTransform * base, GstCaps * in_caps, +    GstCaps * out_caps) +{ +  GstRgAnalysis *filter = GST_RG_ANALYSIS (base); +  GstStructure *structure; +  const gchar *mime_type; +  gint n_channels, sample_rate, sample_bit_size, sample_size; + +  g_return_val_if_fail (filter->ctx != NULL, FALSE); + +  GST_DEBUG_OBJECT (filter, +      "set_caps in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, +      in_caps, out_caps); + +  structure = gst_caps_get_structure (in_caps, 0); +  mime_type = gst_structure_get_name (structure); + +  if (!gst_structure_get_int (structure, "width", &sample_bit_size) +      || !gst_structure_get_int (structure, "channels", &n_channels) +      || !gst_structure_get_int (structure, "rate", &sample_rate)) +    goto invalid_format; + +  if (!rg_analysis_set_sample_rate (filter->ctx, sample_rate)) +    goto invalid_format; + +  if (sample_bit_size % 8 != 0) +    goto invalid_format; +  sample_size = sample_bit_size / 8; + +  if (strcmp (mime_type, "audio/x-raw-float") == 0) { + +    if (sample_size != sizeof (gfloat)) +      goto invalid_format; + +    /* The depth is not variable for float formats of course.  It just +     * makes the transform function nice and simple if the +     * rg_analysis_analyze_* functions have a common signature. */ +    filter->depth = sizeof (gfloat) * 8; + +    if (n_channels == 1) +      filter->analyze = rg_analysis_analyze_mono_float; +    else if (n_channels == 2) +      filter->analyze = rg_analysis_analyze_stereo_float; +    else +      goto invalid_format; + +  } else if (strcmp (mime_type, "audio/x-raw-int") == 0) { + +    if (sample_size != sizeof (gint16)) +      goto invalid_format; + +    if (!gst_structure_get_int (structure, "depth", &filter->depth)) +      goto invalid_format; +    if (filter->depth < 1 || filter->depth > 16) +      goto invalid_format; + +    if (n_channels == 1) +      filter->analyze = rg_analysis_analyze_mono_int16; +    else if (n_channels == 2) +      filter->analyze = rg_analysis_analyze_stereo_int16; +    else +      goto invalid_format; + +  } else { + +    goto invalid_format; +  } + +  return TRUE; + +  /* Errors. */ +invalid_format: +  { +    filter->analyze = NULL; +    GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, +        ("Invalid incoming caps: %" GST_PTR_FORMAT, in_caps), (NULL)); +    return FALSE; +  } +} + +static GstFlowReturn +gst_rg_analysis_transform_ip (GstBaseTransform * base, GstBuffer * buf) +{ +  GstRgAnalysis *filter = GST_RG_ANALYSIS (base); + +  g_return_val_if_fail (filter->ctx != NULL, GST_FLOW_ERROR); +  g_return_val_if_fail (filter->analyze != NULL, GST_FLOW_ERROR); + +  if (filter->skip) +    return GST_FLOW_OK; + +  GST_DEBUG_OBJECT (filter, "Processing buffer of size %u", +      GST_BUFFER_SIZE (buf)); + +  filter->analyze (filter->ctx, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf), +      filter->depth); + +  return GST_FLOW_OK; +} + +static gboolean +gst_rg_analysis_event (GstBaseTransform * base, GstEvent * event) +{ +  GstRgAnalysis *filter = GST_RG_ANALYSIS (base); + +  g_return_val_if_fail (filter->ctx != NULL, TRUE); + +  switch (GST_EVENT_TYPE (event)) { + +    case GST_EVENT_EOS: +    { +      GST_DEBUG_OBJECT (filter, "Received EOS event"); + +      gst_rg_analysis_handle_eos (filter); + +      GST_DEBUG_OBJECT (filter, "Passing on EOS event"); + +      break; +    } +    case GST_EVENT_TAG: +    { +      GstTagList *tag_list; + +      /* The reference to the tag list is borrowed. */ +      gst_event_parse_tag (event, &tag_list); +      gst_rg_analysis_handle_tags (filter, tag_list); + +      break; +    } +    default: +      break; +  } + +  return TRUE; +} + +static gboolean +gst_rg_analysis_stop (GstBaseTransform * base) +{ +  GstRgAnalysis *filter = GST_RG_ANALYSIS (base); + +  g_return_val_if_fail (filter->ctx != NULL, FALSE); + +  rg_analysis_destroy (filter->ctx); +  filter->ctx = NULL; + +  GST_DEBUG_OBJECT (filter, "Stopped"); + +  return TRUE; +} + +static void +gst_rg_analysis_handle_tags (GstRgAnalysis * filter, +    const GstTagList * tag_list) +{ +  gboolean album_processing = (filter->num_tracks > 0); +  gdouble dummy; + +  if (!album_processing) +    filter->ignore_tags = FALSE; + +  if (filter->skip && album_processing) { +    GST_INFO_OBJECT (filter, "Ignoring TAG event: Skipping album"); +    return; +  } else if (filter->skip) { +    GST_INFO_OBJECT (filter, "Ignoring TAG event: Skipping track"); +    return; +  } else if (filter->ignore_tags) { +    GST_INFO_OBJECT (filter, "Ignoring TAG event: Cannot skip anyways"); +    return; +  } + +  filter->has_track_gain |= gst_tag_list_get_double (tag_list, +      GST_TAG_TRACK_GAIN, &dummy); +  filter->has_track_peak |= gst_tag_list_get_double (tag_list, +      GST_TAG_TRACK_PEAK, &dummy); +  filter->has_album_gain |= gst_tag_list_get_double (tag_list, +      GST_TAG_ALBUM_GAIN, &dummy); +  filter->has_album_peak |= gst_tag_list_get_double (tag_list, +      GST_TAG_ALBUM_PEAK, &dummy); + +  if (!(filter->has_track_gain && filter->has_track_peak)) { +    GST_INFO_OBJECT (filter, "Track tags not complete yet"); +    return; +  } + +  if (album_processing && !(filter->has_album_gain && filter->has_album_peak)) { +    GST_INFO_OBJECT (filter, "Album tags not complete yet"); +    return; +  } + +  if (filter->forced) { +    GST_INFO_OBJECT (filter, +        "Existing tags are sufficient, but processing anyway (forced)"); +    return; +  } + +  filter->skip = TRUE; +  rg_analysis_reset (filter->ctx); + +  if (!album_processing) +    GST_INFO_OBJECT (filter, +        "Existing tags are sufficient, will not process this track"); +  else +    GST_INFO_OBJECT (filter, +        "Existing tags are sufficient, will not process this album"); +} + +static void +gst_rg_analysis_handle_eos (GstRgAnalysis * filter) +{ +  gboolean album_processing = (filter->num_tracks > 0); +  gboolean album_finished = (filter->num_tracks == 1); +  gboolean album_skipping = album_processing && filter->skip; + +  filter->has_track_gain = FALSE; +  filter->has_track_peak = FALSE; + +  if (album_finished) { +    filter->ignore_tags = FALSE; +    filter->skip = FALSE; +    filter->has_album_gain = FALSE; +    filter->has_album_peak = FALSE; +  } else if (!album_skipping) { +    filter->skip = FALSE; +  } + +  /* We might have just fully processed a track because it has +   * incomplete tags.  If we do album processing and allow skipping +   * (not forced), prevent switching to skipping if a later track with +   * full tags comes along: */ +  if (!filter->forced && album_processing && !album_finished) +    filter->ignore_tags = TRUE; + +  if (!filter->skip) { +    GstTagList *tag_list = NULL; +    gboolean track_success; +    gboolean album_success = FALSE; + +    track_success = gst_rg_analysis_track_result (filter, &tag_list); + +    if (album_finished) +      album_success = gst_rg_analysis_album_result (filter, &tag_list); +    else if (!album_processing) +      rg_analysis_reset_album (filter->ctx); + +    if (track_success || album_success) { +      GST_DEBUG_OBJECT (filter, "Posting tag list with results"); +      /* This steals our reference to the list: */ +      gst_element_found_tags_for_pad (GST_ELEMENT (filter), +          GST_BASE_TRANSFORM_SRC_PAD (GST_BASE_TRANSFORM (filter)), tag_list); +    } +  } + +  if (album_processing) { +    filter->num_tracks--; + +    if (!album_finished) +      GST_INFO_OBJECT (filter, "Album not finished yet (num-tracks is now %u)", +          filter->num_tracks); +    else +      GST_INFO_OBJECT (filter, "Album finished (num-tracks is now 0)"); +  } + +  if (album_processing) +    g_object_notify (G_OBJECT (filter), "num-tracks"); +} + +static gboolean +gst_rg_analysis_track_result (GstRgAnalysis * filter, GstTagList ** tag_list) +{ +  gboolean track_success; +  gdouble track_gain, track_peak; + +  track_success = rg_analysis_track_result (filter->ctx, &track_gain, +      &track_peak); + +  if (track_success) { +    track_gain += filter->reference_level - RG_REFERENCE_LEVEL; +    GST_INFO_OBJECT (filter, "Track gain is %+.2f dB, peak %.6f", track_gain, +        track_peak); +  } else { +    GST_INFO_OBJECT (filter, "Track was too short to analyze"); +  } + +  if (track_success) { +    if (*tag_list == NULL) +      *tag_list = gst_tag_list_new (); +    gst_tag_list_add (*tag_list, GST_TAG_MERGE_APPEND, +        GST_TAG_TRACK_PEAK, track_peak, GST_TAG_TRACK_GAIN, track_gain, NULL); +  } + +  return track_success; +} + +static gboolean +gst_rg_analysis_album_result (GstRgAnalysis * filter, GstTagList ** tag_list) +{ +  gboolean album_success; +  gdouble album_gain, album_peak; + +  album_success = rg_analysis_album_result (filter->ctx, &album_gain, +      &album_peak); + +  if (album_success) { +    album_gain += filter->reference_level - RG_REFERENCE_LEVEL; +    GST_INFO_OBJECT (filter, "Album gain is %+.2f dB, peak %.6f", album_gain, +        album_peak); +  } else { +    GST_INFO_OBJECT (filter, "Album was too short to analyze"); +  } + +  if (album_success) { +    if (*tag_list == NULL) +      *tag_list = gst_tag_list_new (); +    gst_tag_list_add (*tag_list, GST_TAG_MERGE_APPEND, +        GST_TAG_ALBUM_PEAK, album_peak, GST_TAG_ALBUM_GAIN, album_gain, NULL); +  } + +  return album_success; +} + +static gboolean +plugin_init (GstPlugin * plugin) +{ +  return gst_element_register (plugin, "rganalysis", GST_RANK_NONE, +      GST_TYPE_RG_ANALYSIS); +} + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, "replaygain", +    "ReplayGain analysis", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, +    GST_PACKAGE_ORIGIN); diff --git a/gst/replaygain/gstrganalysis.h b/gst/replaygain/gstrganalysis.h new file mode 100644 index 00000000..121ce4af --- /dev/null +++ b/gst/replaygain/gstrganalysis.h @@ -0,0 +1,83 @@ +/* GStreamer ReplayGain analysis + * + * Copyright (C) 2006 Rene Stadler <mail@renestadler.de> + * + * gstrganalysis.h: Element that performs the ReplayGain analysis + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + *  + * This library 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 GNU + * Lesser General Public License for more details. + *  + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef __GST_RG_ANALYSIS_H__ +#define __GST_RG_ANALYSIS_H__ + +#include <gst/gst.h> +#include <gst/base/gstbasetransform.h> + +#include "rganalysis.h" + +G_BEGIN_DECLS + +#define GST_TYPE_RG_ANALYSIS \ +  (gst_rg_analysis_get_type()) +#define GST_RG_ANALYSIS(obj) \ +  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RG_ANALYSIS,GstRgAnalysis)) +#define GST_RG_ANALYSIS_CLASS(klass) \ +  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RG_ANALYSIS,GstRgAnalysisClass)) +#define GST_IS_RG_ANALYSIS(obj) \ +  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RG_ANALYSIS)) +#define GST_IS_RG_ANALYSIS_CLASS(klass) \ +  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RG_ANALYSIS)) +typedef struct _GstRgAnalysis GstRgAnalysis; +typedef struct _GstRgAnalysisClass GstRgAnalysisClass; + +/** + * GstRgAnalysis: + * + * Opaque data structure. + */ +struct _GstRgAnalysis +{ +  GstBaseTransform element; + +  /*< private >*/ + +  RgAnalysisCtx *ctx; +  void (*analyze) (RgAnalysisCtx * ctx, gconstpointer data, gsize size, +      guint depth); +  gint depth; + +  /* Property values. */ +  guint num_tracks; +  gdouble reference_level; +  gboolean forced; + +  /* State machinery for skipping. */ +  gboolean ignore_tags; +  gboolean skip; +  gboolean has_track_gain; +  gboolean has_track_peak; +  gboolean has_album_gain; +  gboolean has_album_peak; +}; + +struct _GstRgAnalysisClass +{ +  GstBaseTransformClass parent_class; +}; + +G_END_DECLS + +#endif /* __GST_RG_ANALYSIS_H__ */ diff --git a/gst/replaygain/rganalysis.c b/gst/replaygain/rganalysis.c new file mode 100644 index 00000000..b20a08f5 --- /dev/null +++ b/gst/replaygain/rganalysis.c @@ -0,0 +1,772 @@ +/* GStreamer ReplayGain analysis + * + * Copyright (C) 2006 Rene Stadler <mail@renestadler.de> + * Copyright (C) 2001 David Robinson <David@Robinson.org> + *                    Glen Sawyer <glensawyer@hotmail.com> + * + * rganalysis.c: Analyze raw audio data in accordance with ReplayGain + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + *  + * This library 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 GNU + * Lesser General Public License for more details. + *  + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +/* Based on code with Copyright (C) 2001 David Robinson + * <David@Robinson.org> and Glen Sawyer <glensawyer@hotmail.com>, + * which is distributed under the LGPL as part of the vorbisgain + * program.  The original code also mentions Frank Klemm + * (http://www.uni-jena.de/~pfk/mpp/) for having contributed lots of + * good code.  Specifically, this is based on the file + * "gain_analysis.c" from vorbisgain version 0.34. + */ + +/* Room for future improvement: Mono data is currently in fact copied + * to two channels which get processed normally.  This means that mono + * input data is processed twice. + */ + +/* Helpful information for understanding this code: The two IIR + * filters depend on previous input _and_ previous output samples (up + * to the filter's order number of samples).  This explains the whole + * lot of memcpy'ing done in rg_analysis_analyze and why the context + * holds so many buffers. + */ + +#include <math.h> +#include <string.h> +#include <glib.h> + +#include "rganalysis.h" + +#define YULE_ORDER         10 +#define BUTTER_ORDER        2 +/* Percentile which is louder than the proposed level: */ +#define RMS_PERCENTILE     95 +/* Duration of RMS window in milliseconds: */ +#define RMS_WINDOW_MSECS   50 +/* Histogram array elements per dB: */ +#define STEPS_PER_DB      100 +/* Histogram upper bound in dB (normal max. values in the wild are + * assumed to be around 70, 80 dB): */ +#define MAX_DB            120 +/* Calibration value: */ +#define PINK_REF           64.82        /* 298640883795 */ + +#define MAX_ORDER         MAX (BUTTER_ORDER, YULE_ORDER) +#define MAX_SAMPLE_RATE   48000 +/* The + 999 has the effect of ceil()ing: */ +#define MAX_SAMPLE_WINDOW (guint) \ +  ((MAX_SAMPLE_RATE * RMS_WINDOW_MSECS + 999) / 1000) + +/* Analysis result accumulator. */ + +struct _RgAnalysisAcc +{ +  guint32 histogram[STEPS_PER_DB * MAX_DB]; +  gdouble peak; +}; + +typedef struct _RgAnalysisAcc RgAnalysisAcc; + +/* Analysis context. */ + +struct _RgAnalysisCtx +{ +  /* Filter buffers for left channel. */ +  gfloat inprebuf_l[MAX_ORDER * 2]; +  gfloat *inpre_l; +  gfloat stepbuf_l[MAX_SAMPLE_WINDOW + MAX_ORDER]; +  gfloat *step_l; +  gfloat outbuf_l[MAX_SAMPLE_WINDOW + MAX_ORDER]; +  gfloat *out_l; +  /* Filter buffers for right channel. */ +  gfloat inprebuf_r[MAX_ORDER * 2]; +  gfloat *inpre_r; +  gfloat stepbuf_r[MAX_SAMPLE_WINDOW + MAX_ORDER]; +  gfloat *step_r; +  gfloat outbuf_r[MAX_SAMPLE_WINDOW + MAX_ORDER]; +  gfloat *out_r; + +  /* Number of samples to reach duration of the RMS window: */ +  guint window_n_samples; +  /* Progress of the running window: */ +  guint window_n_samples_done; +  gdouble window_square_sum; + +  gint sample_rate; +  gint sample_rate_index; + +  RgAnalysisAcc track; +  RgAnalysisAcc album; +}; + +/* Filter coefficients for the IIR filters that form the equal + * loudness filter.  XFilter[ctx->sample_rate_index] gives the array + * of the X coefficients (A or B) for the configured sample rate. */ + +#ifdef G_OS_WIN32 +/* Disable double-to-float warning: */ +#pragma warning ( disable : 4305 ) +#endif + +static const gfloat AYule[9][11] = { +  {1., -3.84664617118067, 7.81501653005538, -11.34170355132042, +        13.05504219327545, -12.28759895145294, 9.48293806319790, +        -5.87257861775999, 2.75465861874613, -0.86984376593551, +      0.13919314567432}, +  {1., -3.47845948550071, 6.36317777566148, -8.54751527471874, 9.47693607801280, +        -8.81498681370155, 6.85401540936998, -4.39470996079559, +      2.19611684890774, -0.75104302451432, 0.13149317958808}, +  {1., -2.37898834973084, 2.84868151156327, -2.64577170229825, 2.23697657451713, +        -1.67148153367602, 1.00595954808547, -0.45953458054983, +      0.16378164858596, -0.05032077717131, 0.02347897407020}, +  {1., -1.61273165137247, 1.07977492259970, -0.25656257754070, +        -0.16276719120440, -0.22638893773906, 0.39120800788284, +        -0.22138138954925, 0.04500235387352, 0.02005851806501, +      0.00302439095741}, +  {1., -1.49858979367799, 0.87350271418188, 0.12205022308084, -0.80774944671438, +        0.47854794562326, -0.12453458140019, -0.04067510197014, +      0.08333755284107, -0.04237348025746, 0.02977207319925}, +  {1., -0.62820619233671, 0.29661783706366, -0.37256372942400, 0.00213767857124, +        -0.42029820170918, 0.22199650564824, 0.00613424350682, 0.06747620744683, +      0.05784820375801, 0.03222754072173}, +  {1., -1.04800335126349, 0.29156311971249, -0.26806001042947, 0.00819999645858, +        0.45054734505008, -0.33032403314006, 0.06739368333110, +      -0.04784254229033, 0.01639907836189, 0.01807364323573}, +  {1., -0.51035327095184, -0.31863563325245, -0.20256413484477, +        0.14728154134330, 0.38952639978999, -0.23313271880868, +        -0.05246019024463, -0.02505961724053, 0.02442357316099, +      0.01818801111503}, +  {1., -0.25049871956020, -0.43193942311114, -0.03424681017675, +        -0.04678328784242, 0.26408300200955, 0.15113130533216, +        -0.17556493366449, -0.18823009262115, 0.05477720428674, +      0.04704409688120} +}; + +static const gfloat BYule[9][11] = { +  {0.03857599435200, -0.02160367184185, -0.00123395316851, -0.00009291677959, +        -0.01655260341619, 0.02161526843274, -0.02074045215285, +      0.00594298065125, 0.00306428023191, 0.00012025322027, 0.00288463683916}, +  {0.05418656406430, -0.02911007808948, -0.00848709379851, -0.00851165645469, +        -0.00834990904936, 0.02245293253339, -0.02596338512915, +        0.01624864962975, -0.00240879051584, 0.00674613682247, +      -0.00187763777362}, +  {0.15457299681924, -0.09331049056315, -0.06247880153653, 0.02163541888798, +        -0.05588393329856, 0.04781476674921, 0.00222312597743, 0.03174092540049, +      -0.01390589421898, 0.00651420667831, -0.00881362733839}, +  {0.30296907319327, -0.22613988682123, -0.08587323730772, 0.03282930172664, +        -0.00915702933434, -0.02364141202522, -0.00584456039913, +        0.06276101321749, -0.00000828086748, 0.00205861885564, +      -0.02950134983287}, +  {0.33642304856132, -0.25572241425570, -0.11828570177555, 0.11921148675203, +        -0.07834489609479, -0.00469977914380, -0.00589500224440, +        0.05724228140351, 0.00832043980773, -0.01635381384540, +      -0.01760176568150}, +  {0.44915256608450, -0.14351757464547, -0.22784394429749, -0.01419140100551, +        0.04078262797139, -0.12398163381748, 0.04097565135648, 0.10478503600251, +      -0.01863887810927, -0.03193428438915, 0.00541907748707}, +  {0.56619470757641, -0.75464456939302, 0.16242137742230, 0.16744243493672, +        -0.18901604199609, 0.30931782841830, -0.27562961986224, +        0.00647310677246, 0.08647503780351, -0.03788984554840, +      -0.00588215443421}, +  {0.58100494960553, -0.53174909058578, -0.14289799034253, 0.17520704835522, +        0.02377945217615, 0.15558449135573, -0.25344790059353, 0.01628462406333, +      0.06920467763959, -0.03721611395801, -0.00749618797172}, +  {0.53648789255105, -0.42163034350696, -0.00275953611929, 0.04267842219415, +        -0.10214864179676, 0.14590772289388, -0.02459864859345, +        -0.11202315195388, -0.04060034127000, 0.04788665548180, +      -0.02217936801134} +}; + +static const gfloat AButter[9][3] = { +  {1., -1.97223372919527, 0.97261396931306}, +  {1., -1.96977855582618, 0.97022847566350}, +  {1., -1.95835380975398, 0.95920349965459}, +  {1., -1.95002759149878, 0.95124613669835}, +  {1., -1.94561023566527, 0.94705070426118}, +  {1., -1.92783286977036, 0.93034775234268}, +  {1., -1.91858953033784, 0.92177618768381}, +  {1., -1.91542108074780, 0.91885558323625}, +  {1., -1.88903307939452, 0.89487434461664} +}; + +static const gfloat BButter[9][3] = { +  {0.98621192462708, -1.97242384925416, 0.98621192462708}, +  {0.98500175787242, -1.97000351574484, 0.98500175787242}, +  {0.97938932735214, -1.95877865470428, 0.97938932735214}, +  {0.97531843204928, -1.95063686409857, 0.97531843204928}, +  {0.97316523498161, -1.94633046996323, 0.97316523498161}, +  {0.96454515552826, -1.92909031105652, 0.96454515552826}, +  {0.96009142950541, -1.92018285901082, 0.96009142950541}, +  {0.95856916599601, -1.91713833199203, 0.95856916599601}, +  {0.94597685600279, -1.89195371200558, 0.94597685600279} +}; + +#ifdef G_OS_WIN32 +#pragma warning ( default : 4305 ) +#endif + +/* Filter functions.  These access elements with negative indices of + * the input and output arrays (up to the filter's order). */ + +/* For much better performance, the function below has been + * implemented by unrolling the inner loop for our two use cases. */ + +/* + * static inline void + * apply_filter (const gfloat * input, gfloat * output, guint n_samples, + *     const gfloat * a, const gfloat * b, guint order) + * { + *   gfloat y; + *   gint i, k; + *  + *   for (i = 0; i < n_samples; i++) { + *     y = input[i] * b[0]; + *     for (k = 1; k <= order; k++) + *       y += input[i - k] * b[k] - output[i - k] * a[k]; + *     output[i] = y; + *   } + * } + */ + +static inline void +yule_filter (const gfloat * input, gfloat * output, +    const gfloat * a, const gfloat * b) +{ +  output[0] = input[0] * b[0] +      + input[-1] * b[1] - output[-1] * a[1] +      + input[-2] * b[2] - output[-2] * a[2] +      + input[-3] * b[3] - output[-3] * a[3] +      + input[-4] * b[4] - output[-4] * a[4] +      + input[-5] * b[5] - output[-5] * a[5] +      + input[-6] * b[6] - output[-6] * a[6] +      + input[-7] * b[7] - output[-7] * a[7] +      + input[-8] * b[8] - output[-8] * a[8] +      + input[-9] * b[9] - output[-9] * a[9] +      + input[-10] * b[10] - output[-10] * a[10]; +} + +static inline void +butter_filter (const gfloat * input, gfloat * output, +    const gfloat * a, const gfloat * b) +{ +  output[0] = input[0] * b[0] +      + input[-1] * b[1] - output[-1] * a[1] +      + input[-2] * b[2] - output[-2] * a[2]; +} + +/* Because butter_filter and yule_filter are inlined, this function is + * a bit blown-up (code-size wise), but not inlining gives a ca. 40% + * performance penalty. */ + +static inline void +apply_filters (const RgAnalysisCtx * ctx, const gfloat * input_l, +    const gfloat * input_r, guint n_samples) +{ +  const gfloat *ayule = AYule[ctx->sample_rate_index]; +  const gfloat *byule = BYule[ctx->sample_rate_index]; +  const gfloat *abutter = AButter[ctx->sample_rate_index]; +  const gfloat *bbutter = BButter[ctx->sample_rate_index]; +  gint pos = ctx->window_n_samples_done; +  gint i; + +  for (i = 0; i < n_samples; i++, pos++) { +    yule_filter (input_l + i, ctx->step_l + pos, ayule, byule); +    butter_filter (ctx->step_l + pos, ctx->out_l + pos, abutter, bbutter); + +    yule_filter (input_r + i, ctx->step_r + pos, ayule, byule); +    butter_filter (ctx->step_r + pos, ctx->out_r + pos, abutter, bbutter); +  } +} + +/* Clear filter buffer state and current RMS window. */ + +static void +reset_filters (RgAnalysisCtx * ctx) +{ +  gint i; + +  for (i = 0; i < MAX_ORDER; i++) { + +    ctx->inprebuf_l[i] = 0.; +    ctx->stepbuf_l[i] = 0.; +    ctx->outbuf_l[i] = 0.; + +    ctx->inprebuf_r[i] = 0.; +    ctx->stepbuf_r[i] = 0.; +    ctx->outbuf_r[i] = 0.; +  } + +  ctx->window_square_sum = 0.; +  ctx->window_n_samples_done = 0; +} + +/* Accumulator functions. */ + +/* Add two accumulators in-place.  The sum is defined as the result of + * the vector sum of the histogram array and the maximum value of the + * peak field.  Thus "adding" the accumulators for all tracks yields + * the correct result for obtaining the album gain and peak. */ + +static void +accumulator_add (RgAnalysisAcc * acc, const RgAnalysisAcc * acc_other) +{ +  gint i; + +  for (i = 0; i < G_N_ELEMENTS (acc->histogram); i++) +    acc->histogram[i] += acc_other->histogram[i]; + +  acc->peak = MAX (acc->peak, acc_other->peak); +} + +/* Reset an accumulator to zero. */ + +static void +accumulator_clear (RgAnalysisAcc * acc) +{ +  memset (acc->histogram, 0, sizeof (acc->histogram)); +  acc->peak = 0.; +} + +/* Obtain final analysis result from an accumulator.  Returns TRUE on + * success, FALSE on error (if accumulator is still zero). */ + +static gboolean +accumulator_result (const RgAnalysisAcc * acc, gdouble * result_gain, +    gdouble * result_peak) +{ +  guint32 sum = 0; +  guint32 upper; +  guint i; + +  for (i = 0; i < G_N_ELEMENTS (acc->histogram); i++) +    sum += acc->histogram[i]; + +  if (sum == 0) +    /* All entries are 0: We got less than 50ms of data. */ +    return FALSE; + +  upper = (guint32) ceil (sum * (1. - (gdouble) (RMS_PERCENTILE / 100.))); + +  for (i = G_N_ELEMENTS (acc->histogram); i--;) { +    if (upper <= acc->histogram[i]) +      break; +    upper -= acc->histogram[i]; +  } + +  if (result_peak != NULL) +    *result_peak = acc->peak; +  if (result_gain != NULL) +    *result_gain = PINK_REF - (gdouble) i / STEPS_PER_DB; + +  return TRUE; +} + +/* Functions that operate on contexts, for external usage. */ + +/* Create a new context.  Before it can be used, a sample rate must be + * configured using rg_analysis_set_sample_rate. */ + +RgAnalysisCtx * +rg_analysis_new (void) +{ +  RgAnalysisCtx *ctx; + +  ctx = g_new (RgAnalysisCtx, 1); + +  ctx->inpre_l = ctx->inprebuf_l + MAX_ORDER; +  ctx->step_l = ctx->stepbuf_l + MAX_ORDER; +  ctx->out_l = ctx->outbuf_l + MAX_ORDER; + +  ctx->inpre_r = ctx->inprebuf_r + MAX_ORDER; +  ctx->step_r = ctx->stepbuf_r + MAX_ORDER; +  ctx->out_r = ctx->outbuf_r + MAX_ORDER; + +  ctx->sample_rate = 0; + +  accumulator_clear (&ctx->track); +  accumulator_clear (&ctx->album); + +  return ctx; +} + +/* Adapt to given sample rate.  Does nothing if already the current + * rate (returns TRUE then).  Returns FALSE only if given sample rate + * is not supported.  If the configured rate changes, the last + * unprocessed incomplete 50ms chunk of data is dropped because the + * filters are reset. */ + +gboolean +rg_analysis_set_sample_rate (RgAnalysisCtx * ctx, gint sample_rate) +{ +  g_return_val_if_fail (ctx != NULL, FALSE); + +  if (ctx->sample_rate == sample_rate) +    return TRUE; + +  switch (sample_rate) { +    case 48000: +      ctx->sample_rate_index = 0; +      break; +    case 44100: +      ctx->sample_rate_index = 1; +      break; +    case 32000: +      ctx->sample_rate_index = 2; +      break; +    case 24000: +      ctx->sample_rate_index = 3; +      break; +    case 22050: +      ctx->sample_rate_index = 4; +      break; +    case 16000: +      ctx->sample_rate_index = 5; +      break; +    case 12000: +      ctx->sample_rate_index = 6; +      break; +    case 11025: +      ctx->sample_rate_index = 7; +      break; +    case 8000: +      ctx->sample_rate_index = 8; +      break; +    default: +      return FALSE; +  } + +  ctx->sample_rate = sample_rate; +  /* The + 999 has the effect of ceil()ing: */ +  ctx->window_n_samples = (guint) ((sample_rate * RMS_WINDOW_MSECS + 999) +      / 1000); + +  reset_filters (ctx); + +  return TRUE; +} + +void +rg_analysis_destroy (RgAnalysisCtx * ctx) +{ +  g_free (ctx); +} + +/* Entry points for analyzing sample data in common raw data formats. + * The stereo format functions expect interleaved frames.  It is + * possible to pass data in different formats for the same context, + * there are no restrictions.  All functions have the same signature; + * the depth argument for the float functions is not variable and must + * be given the value 32. */ + +void +rg_analysis_analyze_mono_float (RgAnalysisCtx * ctx, gconstpointer data, +    gsize size, guint depth) +{ +  gfloat conv_samples[512]; +  const gfloat *samples = (gfloat *) data; +  guint n_samples = size / sizeof (gfloat); +  gint i; + +  g_return_if_fail (depth == 32); +  g_return_if_fail (size % sizeof (gfloat) == 0); + +  while (n_samples) { +    gint n = MIN (n_samples, G_N_ELEMENTS (conv_samples)); + +    n_samples -= n; +    memcpy (conv_samples, samples, n * sizeof (gfloat)); +    for (i = 0; i < n; i++) { +      ctx->track.peak = MAX (ctx->track.peak, fabs (conv_samples[i])); +      conv_samples[i] *= 32768.; +    } +    samples += n; +    rg_analysis_analyze (ctx, conv_samples, NULL, n); +  } +} + +void +rg_analysis_analyze_stereo_float (RgAnalysisCtx * ctx, gconstpointer data, +    gsize size, guint depth) +{ +  gfloat conv_samples_l[256]; +  gfloat conv_samples_r[256]; +  const gfloat *samples = (gfloat *) data; +  guint n_frames = size / (sizeof (gfloat) * 2); +  gint i; + +  g_return_if_fail (depth == 32); +  g_return_if_fail (size % (sizeof (gfloat) * 2) == 0); + +  while (n_frames) { +    gint n = MIN (n_frames, G_N_ELEMENTS (conv_samples_l)); + +    n_frames -= n; +    for (i = 0; i < n; i++) { +      gfloat old_sample; + +      old_sample = samples[2 * i]; +      ctx->track.peak = MAX (ctx->track.peak, fabs (old_sample)); +      conv_samples_l[i] = old_sample * 32768.; + +      old_sample = samples[2 * i + 1]; +      ctx->track.peak = MAX (ctx->track.peak, fabs (old_sample)); +      conv_samples_r[i] = old_sample * 32768.; +    } +    samples += 2 * n; +    rg_analysis_analyze (ctx, conv_samples_l, conv_samples_r, n); +  } +} + +void +rg_analysis_analyze_mono_int16 (RgAnalysisCtx * ctx, gconstpointer data, +    gsize size, guint depth) +{ +  gfloat conv_samples[512]; +  gint32 peak_sample = 0; +  const gint16 *samples = (gint16 *) data; +  guint n_samples = size / sizeof (gint16); +  gint shift = sizeof (gint16) * 8 - depth; +  gint i; + +  g_return_if_fail (depth <= (sizeof (gint16) * 8)); +  g_return_if_fail (size % sizeof (gint16) == 0); + +  while (n_samples) { +    gint n = MIN (n_samples, G_N_ELEMENTS (conv_samples)); + +    n_samples -= n; +    for (i = 0; i < n; i++) { +      gint16 old_sample = samples[i] << shift; + +      peak_sample = MAX (peak_sample, ABS ((gint32) old_sample)); +      conv_samples[i] = (gfloat) old_sample; +    } +    samples += n; +    rg_analysis_analyze (ctx, conv_samples, NULL, n); +  } +  ctx->track.peak = MAX (ctx->track.peak, +      (gdouble) peak_sample / ((gdouble) (1u << 15))); +} + +void +rg_analysis_analyze_stereo_int16 (RgAnalysisCtx * ctx, gconstpointer data, +    gsize size, guint depth) +{ +  gfloat conv_samples_l[256]; +  gfloat conv_samples_r[256]; +  gint32 peak_sample = 0; +  const gint16 *samples = (gint16 *) data; +  guint n_frames = size / (sizeof (gint16) * 2); +  gint shift = sizeof (gint16) * 8 - depth; +  gint i; + +  g_return_if_fail (depth <= (sizeof (gint16) * 8)); +  g_return_if_fail (size % (sizeof (gint16) * 2) == 0); + +  while (n_frames) { +    gint n = MIN (n_frames, G_N_ELEMENTS (conv_samples_l)); + +    n_frames -= n; +    for (i = 0; i < n; i++) { +      gint16 old_sample; + +      old_sample = samples[2 * i] << shift; +      peak_sample = MAX (peak_sample, ABS ((gint32) old_sample)); +      conv_samples_l[i] = (gfloat) old_sample; + +      old_sample = samples[2 * i + 1] << shift; +      peak_sample = MAX (peak_sample, ABS ((gint32) old_sample)); +      conv_samples_r[i] = (gfloat) old_sample; +    } +    samples += 2 * n; +    rg_analysis_analyze (ctx, conv_samples_l, conv_samples_r, n); +  } +  ctx->track.peak = MAX (ctx->track.peak, +      (gdouble) peak_sample / ((gdouble) (1u << 15))); +} + +/* Analyze the given chunk of samples.  The sample data is given in + * floating point format but should be scaled such that the values + * +/-32768.0 correspond to the -0dBFS reference amplitude. + * + * samples_l: Buffer with sample data for the left channel or of the + * mono channel. + * + * samples_r: Buffer with sample data for the right channel or NULL + * for mono. + * + * n_samples: Number of samples passed in each buffer. + */ + +void +rg_analysis_analyze (RgAnalysisCtx * ctx, const gfloat * samples_l, +    const gfloat * samples_r, guint n_samples) +{ +  const gfloat *input_l, *input_r; +  guint n_samples_done; +  gint i; + +  g_return_if_fail (ctx != NULL); +  g_return_if_fail (samples_l != NULL); +  g_return_if_fail (ctx->sample_rate != 0); + +  if (n_samples == 0) +    return; + +  if (samples_r == NULL) +    /* Mono. */ +    samples_r = samples_l; + +  memcpy (ctx->inpre_l, samples_l, +      MIN (n_samples, MAX_ORDER) * sizeof (gfloat)); +  memcpy (ctx->inpre_r, samples_r, +      MIN (n_samples, MAX_ORDER) * sizeof (gfloat)); + +  n_samples_done = 0; +  while (n_samples_done < n_samples) { +    /* Limit number of samples to be processed in this iteration to +     * the number needed to complete the next window: */ +    guint n_samples_current = MIN (n_samples - n_samples_done, +        ctx->window_n_samples - ctx->window_n_samples_done); + +    if (n_samples_done < MAX_ORDER) { +      input_l = ctx->inpre_l + n_samples_done; +      input_r = ctx->inpre_r + n_samples_done; +      n_samples_current = MIN (n_samples_current, MAX_ORDER - n_samples_done); +    } else { +      input_l = samples_l + n_samples_done; +      input_r = samples_r + n_samples_done; +    } + +    apply_filters (ctx, input_l, input_r, n_samples_current); + +    /* Update the square sum. */ +    for (i = 0; i < n_samples_current; i++) +      ctx->window_square_sum += ctx->out_l[ctx->window_n_samples_done + i] +          * ctx->out_l[ctx->window_n_samples_done + i] +          + ctx->out_r[ctx->window_n_samples_done + i] +          * ctx->out_r[ctx->window_n_samples_done + i]; + +    ctx->window_n_samples_done += n_samples_current; + +    g_return_if_fail (ctx->window_n_samples_done <= ctx->window_n_samples); + +    if (ctx->window_n_samples_done == ctx->window_n_samples) { +      /* Get the Root Mean Square (RMS) for this set of samples. */ +      gdouble val = STEPS_PER_DB * 10. * log10 (ctx->window_square_sum / +          ctx->window_n_samples * 0.5 + 1.e-37); +      gint ival = CLAMP ((gint) val, 0, +          (gint) G_N_ELEMENTS (ctx->track.histogram) - 1); + +      ctx->track.histogram[ival]++; +      ctx->window_square_sum = 0.; +      ctx->window_n_samples_done = 0; + +      /* No need for memmove here, the areas never overlap: Even for +       * the smallest sample rate, the number of samples needed for +       * the window is greater than MAX_ORDER. */ + +      memcpy (ctx->stepbuf_l, ctx->stepbuf_l + ctx->window_n_samples, +          MAX_ORDER * sizeof (gfloat)); +      memcpy (ctx->outbuf_l, ctx->outbuf_l + ctx->window_n_samples, +          MAX_ORDER * sizeof (gfloat)); + +      memcpy (ctx->stepbuf_r, ctx->stepbuf_r + ctx->window_n_samples, +          MAX_ORDER * sizeof (gfloat)); +      memcpy (ctx->outbuf_r, ctx->outbuf_r + ctx->window_n_samples, +          MAX_ORDER * sizeof (gfloat)); +    } + +    n_samples_done += n_samples_current; +  } + +  if (n_samples >= MAX_ORDER) { + +    memcpy (ctx->inprebuf_l, samples_l + n_samples - MAX_ORDER, +        MAX_ORDER * sizeof (gfloat)); + +    memcpy (ctx->inprebuf_r, samples_r + n_samples - MAX_ORDER, +        MAX_ORDER * sizeof (gfloat)); + +  } else { + +    memmove (ctx->inprebuf_l, ctx->inprebuf_l + n_samples, +        (MAX_ORDER - n_samples) * sizeof (gfloat)); +    memcpy (ctx->inprebuf_l + MAX_ORDER - n_samples, samples_l, +        n_samples * sizeof (gfloat)); + +    memmove (ctx->inprebuf_r, ctx->inprebuf_r + n_samples, +        (MAX_ORDER - n_samples) * sizeof (gfloat)); +    memcpy (ctx->inprebuf_r + MAX_ORDER - n_samples, samples_r, +        n_samples * sizeof (gfloat)); + +  } +} + +/* Obtain track gain and peak.  Returns TRUE on success.  Can fail if + * not enough samples have been processed.  Updates album accumulator. + * Resets track accumulator. */ + +gboolean +rg_analysis_track_result (RgAnalysisCtx * ctx, gdouble * gain, gdouble * peak) +{ +  gboolean result; + +  g_return_val_if_fail (ctx != NULL, FALSE); + +  accumulator_add (&ctx->album, &ctx->track); +  result = accumulator_result (&ctx->track, gain, peak); +  accumulator_clear (&ctx->track); + +  reset_filters (ctx); + +  return result; +} + +/* Obtain album gain and peak.  Returns TRUE on success.  Can fail if + * not enough samples have been processed.  Resets album + * accumulator. */ + +gboolean +rg_analysis_album_result (RgAnalysisCtx * ctx, gdouble * gain, gdouble * peak) +{ +  gboolean result; + +  g_return_val_if_fail (ctx != NULL, FALSE); + +  result = accumulator_result (&ctx->album, gain, peak); +  accumulator_clear (&ctx->album); + +  return result; +} + +void +rg_analysis_reset_album (RgAnalysisCtx * ctx) +{ +  accumulator_clear (&ctx->album); +} + +/* Reset internal buffers as well as track and album accumulators. + * Configured sample rate is kept intact. */ + +void +rg_analysis_reset (RgAnalysisCtx * ctx) +{ +  g_return_if_fail (ctx != NULL); + +  reset_filters (ctx); +  accumulator_clear (&ctx->track); +  accumulator_clear (&ctx->album); +} diff --git a/gst/replaygain/rganalysis.h b/gst/replaygain/rganalysis.h new file mode 100644 index 00000000..39bf9b41 --- /dev/null +++ b/gst/replaygain/rganalysis.h @@ -0,0 +1,58 @@ +/* GStreamer ReplayGain analysis + * + * Copyright (C) 2006 Rene Stadler <mail@renestadler.de> + * Copyright (C) 2001 David Robinson <David@Robinson.org> + *                    Glen Sawyer <glensawyer@hotmail.com> + * + * rganalysis.h: Analyze raw audio data in accordance with ReplayGain + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + *  + * This library 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 GNU + * Lesser General Public License for more details. + *  + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef __RG_ANALYSIS_H__ +#define __RG_ANALYSIS_H__ + +#include <glib.h> + +G_BEGIN_DECLS + +#define RG_REFERENCE_LEVEL 89. + +typedef struct _RgAnalysisCtx RgAnalysisCtx; + +RgAnalysisCtx *rg_analysis_new (void); +gboolean rg_analysis_set_sample_rate (RgAnalysisCtx * ctx, gint sample_rate); +void rg_analysis_analyze_mono_float (RgAnalysisCtx * ctx, gconstpointer data, +    gsize size, guint depth); +void rg_analysis_analyze_stereo_float (RgAnalysisCtx * ctx, gconstpointer data, +    gsize size, guint depth); +void rg_analysis_analyze_mono_int16 (RgAnalysisCtx * ctx, gconstpointer data, +    gsize size, guint depth); +void rg_analysis_analyze_stereo_int16 (RgAnalysisCtx * ctx, gconstpointer data, +    gsize size, guint depth); +void rg_analysis_analyze (RgAnalysisCtx * ctx, const gfloat * samples_l, +    const gfloat * samples_r, guint n_samples); +gboolean rg_analysis_track_result (RgAnalysisCtx * ctx, gdouble * gain, +    gdouble * peak); +gboolean rg_analysis_album_result (RgAnalysisCtx * ctx, gdouble * gain, +    gdouble * peak); +void rg_analysis_reset_album (RgAnalysisCtx * ctx); +void rg_analysis_reset (RgAnalysisCtx * ctx); +void rg_analysis_destroy (RgAnalysisCtx * ctx); + +G_END_DECLS + +#endif /* __RG_ANALYSIS_H__ */ diff --git a/tests/check/elements/rganalysis.c b/tests/check/elements/rganalysis.c new file mode 100644 index 00000000..17b4d62f --- /dev/null +++ b/tests/check/elements/rganalysis.c @@ -0,0 +1,1871 @@ +/* GStreamer ReplayGain analysis + * + * Copyright (C) 2006 Rene Stadler <mail@renestadler.de> + * + * rganalysis.c: Unit test for the rganalysis element + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + *  + * This library 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 GNU + * Lesser General Public License for more details. + *  + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +/* Some things to note about the RMS window length of the analysis + * algorithm and thus the implementation used in the element: + * Processing divides input data into 50ms windows at some point. + * Some details about this that normally do not matter: + * + *  1. At the end of a stream, the remainder of data that did not fill + *     up the last 50ms window is simply discarded. + * + *  2. If the sample rate changes during a stream, the currently + *     running window is discarded and the equal loudness filter gets + *     reset as if a new stream started. + * + *  3. For the album gain, it is not entirely correct to think of + *     obtaining it like "as if all the tracks are analyzed as one + *     track".  There isn't a separate window being tracked for album + *     processing, so at stream (track) end, the remaining unfilled + *     window does not contribute to the album gain either. + * + *  4. If a waveform with a result gain G is concatenated to itself + *     and the result processed as a track, the gain can be different + *     from G if and only if the duration of the original waveform is + *     not an integer multiple of 50ms.  If the original waveform gets + *     processed as a single track and then the same data again as a + *     subsequent track, the album result gain will always match G + *     (this is implied by 3.). + * + *  5. A stream shorter than 50ms cannot be analyzed.  At 8000 and + *     48000 Hz, this corresponds to 400 resp. 2400 frames.  If a + *     stream is shorter than 50ms, the element will not generate tags + *     at EOS (only if an album finished, but only album tags are + *     generated then).  This is not an erroneous condition, the + *     element should behave normally. + * + * The limitations outlined in 1.-4. do not apply to the peak values. + * Every single sample is accounted for when looking for the peak. + * Thus the album peak is guaranteed to be the maximum value of all + * track peaks. + * + * In normal day-to-day use, these little facts are unlikely to be + * relevant, but they have to be kept in mind for writing the tests + * here. + */ + +#include <gst/check/gstcheck.h> + +GList *buffers = NULL; + +/* For ease of programming we use globals to keep refs for our floating + * src and sink pads we create; otherwise we always have to do get_pad, + * get_peer, and then remove references in every test function */ +static GstPad *mysrcpad, *mysinkpad; + +/* Mapping from supported sample rates to the correct result gain for + * the following test waveform: 20 * 512 samples with a quarter-full + * amplitude of toggling sign, changing every 48 samples and starting + * with the positive value. + * + * Even if we would generate a wave describing a signal with the same + * frequency at each sampling rate, the results would vary (slightly). + * Hence the simple generation method, since we cannot use a constant + * value as expected result anyways.  For all sample rates, changing + * the sign every 48 frames gives a sane frequency.  Buffers + * containing data that forms such a waveform is created using the + * test_buffer_square_{float,int16}_{mono,stereo} functions below. + * + * The results have been checked against what the metaflac and + * wavegain programs generate for such a stream.  If you want to + * verify these, be sure that the metaflac program does not produce + * incorrect results in your environment: I found a strange bug in the + * (defacto) reference code for the analysis that sometimes leads to + * incorrect RMS window lengths. */ + +struct rate_test +{ +  guint sample_rate; +  gdouble gain; +}; + +static const struct rate_test supported_rates[] = { +  8000, -0.91, +  11025, -2.80, +  12000, -3.13, +  16000, -4.26, +  22050, -5.64, +  24000, -5.87, +  32000, -6.03, +  44100, -6.20, +  48000, -6.14 +}; + +/* Lookup the correct gain adjustment result in above array. */ + +static gdouble +get_expected_gain (guint sample_rate) +{ +  gint i; + +  for (i = G_N_ELEMENTS (supported_rates); i--;) +    if (supported_rates[i].sample_rate == sample_rate) +      return supported_rates[i].gain; +  g_return_val_if_reached (0.0); +} + +#define SILENCE_GAIN 64.82 + +#define REPLAY_GAIN_CAPS                                \ +  "channels = (int) { 1, 2 }, "                         \ +  "rate = (int) { 8000, 11025, 12000, 16000, 22050, "   \ +  "24000, 32000, 44100, 48000 }" + +#define RG_ANALYSIS_CAPS_TEMPLATE_STRING  \ +  "audio/x-raw-float, "                   \ +  "width = (int) 32, "                    \ +  "endianness = (int) BYTE_ORDER, "       \ +  REPLAY_GAIN_CAPS                        \ +  "; "                                    \ +  "audio/x-raw-int, "                     \ +  "width = (int) 16, "                    \ +  "depth = (int) [ 1, 16 ], "             \ +  "signed = (boolean) true, "             \ +  "endianness = (int) BYTE_ORDER, "       \ +  REPLAY_GAIN_CAPS + +static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", +    GST_PAD_SINK, +    GST_PAD_ALWAYS, +    GST_STATIC_CAPS (RG_ANALYSIS_CAPS_TEMPLATE_STRING) +    ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", +    GST_PAD_SRC, +    GST_PAD_ALWAYS, +    GST_STATIC_CAPS (RG_ANALYSIS_CAPS_TEMPLATE_STRING) +    ); + +GstElement * +setup_rganalysis () +{ +  GstElement *analysis; +  GstBus *bus; + +  GST_DEBUG ("setup_rganalysis"); +  analysis = gst_check_setup_element ("rganalysis"); +  mysrcpad = gst_check_setup_src_pad (analysis, &srctemplate, NULL); +  mysinkpad = gst_check_setup_sink_pad (analysis, &sinktemplate, NULL); +  gst_pad_set_active (mysrcpad, TRUE); +  gst_pad_set_active (mysinkpad, TRUE); + +  bus = gst_bus_new (); +  gst_element_set_bus (analysis, bus); +  /* gst_element_set_bus does not steal a reference. */ +  gst_object_unref (bus); + +  return analysis; +} + +void +cleanup_rganalysis (GstElement * element) +{ +  GST_DEBUG ("cleanup_rganalysis"); + +  g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL); +  g_list_free (buffers); +  buffers = NULL; + +  /* The bus owns references to the element: */ +  gst_element_set_bus (element, NULL); + +  gst_check_teardown_src_pad (element); +  gst_check_teardown_sink_pad (element); +  gst_check_teardown_element (element); +} + +static void +set_playing_state (GstElement * element) +{ +  fail_unless (gst_element_set_state (element, +          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, +      "Could not set state to PLAYING"); +} + +static void +send_eos_event (GstElement * element) +{ +  GstBus *bus = gst_element_get_bus (element); +  GstPad *pad = gst_element_get_pad (element, "sink"); +  GstEvent *event = gst_event_new_eos (); + +  fail_unless (gst_pad_send_event (pad, event), +      "Cannot send EOS event: Not handled."); + +  /* There is no sink element, so _we_ post the EOS message on the bus +   * here.  Of course we generate any EOS ourselves, but this allows +   * us to poll for the EOS message in poll_eos if we expect the +   * element to _not_ generate a TAG message.  That's better than +   * waiting for a timeout to lapse. */ +  fail_unless (gst_bus_post (bus, gst_message_new_eos (NULL))); + +  gst_object_unref (bus); +  gst_object_unref (pad); +} + +static void +send_tag_event (GstElement * element, GstTagList * tag_list) +{ +  GstPad *pad = gst_element_get_pad (element, "sink"); +  GstEvent *event = gst_event_new_tag (tag_list); + +  fail_unless (gst_pad_send_event (pad, event), +      "Cannot send TAG event: Not handled."); + +  gst_object_unref (pad); +} + +static void +poll_eos (GstElement * element) +{ +  GstBus *bus = gst_element_get_bus (element); +  GstMessage *message; + +  message = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_TAG, GST_SECOND); +  fail_unless (message != NULL, "Could not poll for EOS message: Timed out"); +  fail_unless (message->type == GST_MESSAGE_EOS, +      "Could not poll for eos message: got message of type %s instead", +      gst_message_type_get_name (message->type)); + +  gst_message_unref (message); +  gst_object_unref (bus); +} + +/* This also polls for EOS since the TAG message comes right before + * the end of streams. */ + +static GstTagList * +poll_tags (GstElement * element) +{ +  GstBus *bus = gst_element_get_bus (element); +  GstTagList *tag_list; +  GstMessage *message; + +  message = gst_bus_poll (bus, GST_MESSAGE_TAG, GST_SECOND); +  fail_unless (message != NULL, "Could not poll for TAG message: Timed out"); + +  fail_unless (GST_MESSAGE_SRC (message) == GST_OBJECT (element)); + +  gst_message_parse_tag (message, &tag_list); +  gst_message_unref (message); +  gst_object_unref (bus); + +  poll_eos (element); + +  return tag_list; +} + +#define MATCH_PEAK(p1, p2) ((p1 < p2 + 1e-6) && (p2 < p1 + 1e-6)) +#define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-13) && (g2 < g1 + 1e-13)) + +static void +fail_unless_track_gain (const GstTagList * tag_list, gdouble gain) +{ +  gdouble result; + +  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &result), +      "Tag list contains no track gain value"); +  fail_unless (MATCH_GAIN (gain, result), +      "Track gain %+.2f does not match, expected %+.2f", result, gain); +} + +static void +fail_unless_track_peak (const GstTagList * tag_list, gdouble peak) +{ +  gdouble result; + +  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &result), +      "Tag list contains no track peak value"); +  fail_unless (MATCH_PEAK (peak, result), +      "Track peak %f does not match, expected %f", result, peak); +} + +static void +fail_unless_album_gain (const GstTagList * tag_list, gdouble gain) +{ +  gdouble result; + +  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &result), +      "Tag list contains no album gain value"); +  fail_unless (MATCH_GAIN (result, gain), +      "Album gain %+.2f does not match, expected %+.2f", result, gain); +} + +static void +fail_unless_album_peak (const GstTagList * tag_list, gdouble peak) +{ +  gdouble result; + +  fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &result), +      "Tag list contains no album peak value"); +  fail_unless (MATCH_PEAK (peak, result), +      "Album peak %f does not match, expected %f", result, peak); +} + +static void +fail_if_track_tags (const GstTagList * tag_list) +{ +  gdouble result; + +  fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &result), +      "Tag list contains track gain value (but should not)"); +  fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &result), +      "Tag list contains track peak value (but should not)"); +} + +static void +fail_if_album_tags (const GstTagList * tag_list) +{ +  gdouble result; + +  fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &result), +      "Tag list contains album gain value (but should not)"); +  fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &result), +      "Tag list contains album peak value (but should not)"); +} + +static void +fail_unless_num_tracks (GstElement * element, guint num_tracks) +{ +  guint current; + +  g_object_get (element, "num-tracks", ¤t, NULL); +  fail_unless (current == num_tracks, +      "num-tracks property has incorrect value %u, expected %u", +      current, num_tracks); +} + +/* Functions that create buffers with constant sample values, for peak + * tests. */ + +static GstBuffer * +test_buffer_const_float_mono (gint sample_rate, gsize n_frames, gfloat value) +{ +  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat)); +  gfloat *data = (gfloat *) GST_BUFFER_DATA (buf); +  GstCaps *caps; +  gint i; + +  for (i = n_frames; i--;) +    *data++ = value; + +  caps = gst_caps_new_simple ("audio/x-raw-float", +      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1, +      "endianess", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL); +  gst_buffer_set_caps (buf, caps); +  gst_caps_unref (caps); + +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + +  return buf; +} + +static GstBuffer * +test_buffer_const_float_stereo (gint sample_rate, gsize n_frames, +    gfloat value_l, gfloat value_r) +{ +  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat) * 2); +  gfloat *data = (gfloat *) GST_BUFFER_DATA (buf); +  GstCaps *caps; +  gint i; + +  for (i = n_frames; i--;) { +    *data++ = value_l; +    *data++ = value_r; +  } + +  caps = gst_caps_new_simple ("audio/x-raw-float", +      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2, +      "endianess", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL); +  gst_buffer_set_caps (buf, caps); +  gst_caps_unref (caps); + +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + +  return buf; +} + +static GstBuffer * +test_buffer_const_int16_mono (gint sample_rate, gint depth, gsize n_frames, +    gint16 value) +{ +  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16)); +  gint16 *data = (gint16 *) GST_BUFFER_DATA (buf); +  GstCaps *caps; +  gint i; + +  for (i = n_frames; i--;) +    *data++ = value; + +  caps = gst_caps_new_simple ("audio/x-raw-int", +      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1, +      "endianess", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE, +      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL); +  gst_buffer_set_caps (buf, caps); +  gst_caps_unref (caps); + +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + +  return buf; +} + +static GstBuffer * +test_buffer_const_int16_stereo (gint sample_rate, gint depth, gsize n_frames, +    gint16 value_l, gint16 value_r) +{ +  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16) * 2); +  gint16 *data = (gint16 *) GST_BUFFER_DATA (buf); +  GstCaps *caps; +  gint i; + +  for (i = n_frames; i--;) { +    *data++ = value_l; +    *data++ = value_r; +  } + +  caps = gst_caps_new_simple ("audio/x-raw-int", +      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2, +      "endianess", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE, +      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL); +  gst_buffer_set_caps (buf, caps); +  gst_caps_unref (caps); + +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + +  return buf; +} + +/* Functions that create data buffers containing square signal + * waveforms. */ + +static GstBuffer * +test_buffer_square_float_mono (gint * accumulator, gint sample_rate, +    gsize n_frames, gfloat value) +{ +  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat)); +  gfloat *data = (gfloat *) GST_BUFFER_DATA (buf); +  GstCaps *caps; +  gint i; + +  for (i = n_frames; i--;) { +    *accumulator += 1; +    *accumulator %= 96; + +    if (*accumulator < 48) +      *data++ = value; +    else +      *data++ = -value; +  } + +  caps = gst_caps_new_simple ("audio/x-raw-float", +      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1, +      "endianess", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL); +  gst_buffer_set_caps (buf, caps); +  gst_caps_unref (caps); + +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + +  return buf; +} + +static GstBuffer * +test_buffer_square_float_stereo (gint * accumulator, gint sample_rate, +    gsize n_frames, gfloat value_l, gfloat value_r) +{ +  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gfloat) * 2); +  gfloat *data = (gfloat *) GST_BUFFER_DATA (buf); +  GstCaps *caps; +  gint i; + +  for (i = n_frames; i--;) { +    *accumulator += 1; +    *accumulator %= 96; + +    if (*accumulator < 48) { +      *data++ = value_l; +      *data++ = value_r; +    } else { +      *data++ = -value_l; +      *data++ = -value_r; +    } +  } + +  caps = gst_caps_new_simple ("audio/x-raw-float", +      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2, +      "endianess", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL); +  gst_buffer_set_caps (buf, caps); +  gst_caps_unref (caps); + +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + +  return buf; +} + +static GstBuffer * +test_buffer_square_int16_mono (gint * accumulator, gint sample_rate, +    gint depth, gsize n_frames, gint16 value) +{ +  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16)); +  gint16 *data = (gint16 *) GST_BUFFER_DATA (buf); +  GstCaps *caps; +  gint i; + +  for (i = n_frames; i--;) { +    *accumulator += 1; +    *accumulator %= 96; + +    if (*accumulator < 48) +      *data++ = value; +    else +      *data++ = -MAX (value, -32767); +  } + +  caps = gst_caps_new_simple ("audio/x-raw-int", +      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 1, +      "endianess", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE, +      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL); +  gst_buffer_set_caps (buf, caps); +  gst_caps_unref (caps); + +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + +  return buf; +} + +static GstBuffer * +test_buffer_square_int16_stereo (gint * accumulator, gint sample_rate, +    gint depth, gsize n_frames, gint16 value_l, gint16 value_r) +{ +  GstBuffer *buf = gst_buffer_new_and_alloc (n_frames * sizeof (gint16) * 2); +  gint16 *data = (gint16 *) GST_BUFFER_DATA (buf); +  GstCaps *caps; +  gint i; + +  for (i = n_frames; i--;) { +    *accumulator += 1; +    *accumulator %= 96; + +    if (*accumulator < 48) { +      *data++ = value_l; +      *data++ = value_r; +    } else { +      *data++ = -MAX (value_l, -32767); +      *data++ = -MAX (value_r, -32767); +    } +  } + +  caps = gst_caps_new_simple ("audio/x-raw-int", +      "rate", G_TYPE_INT, sample_rate, "channels", G_TYPE_INT, 2, +      "endianess", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE, +      "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, depth, NULL); +  gst_buffer_set_caps (buf, caps); +  gst_caps_unref (caps); + +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + +  return buf; +} + +static void +push_buffer (GstBuffer * buf) +{ +  /* gst_pad_push steals a reference. */ +  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK); +  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); +} + +/*** Start of the tests. ***/ + +/* This test looks redundant, but early versions of the element + * crashed when doing, well, nothing: */ + +GST_START_TEST (test_no_buffer) +{ +  GstElement *element = setup_rganalysis (); + +  set_playing_state (element); +  send_eos_event (element); +  poll_eos (element); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_no_buffer_album_1) +{ +  GstElement *element = setup_rganalysis (); + +  set_playing_state (element); + +  /* Single track: */ +  send_eos_event (element); +  poll_eos (element); + +  /* First album: */ +  g_object_set (element, "num-tracks", 3, NULL); + +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 2); + +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 1); + +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 0); + +  /* Second album: */ +  g_object_set (element, "num-tracks", 2, NULL); + +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 1); + +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 0); + +  /* Single track: */ +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 0); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_no_buffer_album_2) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i; + +  g_object_set (element, "num-tracks", 3, NULL); +  set_playing_state (element); + +  /* No buffer for the first track. */ + +  send_eos_event (element); +  /* No tags should be posted, there was nothing to analyze: */ +  poll_eos (element); +  fail_unless_num_tracks (element, 2); + +  /* A test waveform with known gain result as second track: */ + +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_mono (&accumulator, 44100, 512, +            0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, -6.20); +  /* Album is not finished yet: */ +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 1); + +  /* No buffer for the last track. */ + +  send_eos_event (element); + +  tag_list = poll_tags (element); +  fail_unless_album_peak (tag_list, 0.25); +  fail_unless_album_gain (tag_list, -6.20); +  /* No track tags should be posted, as there was no data for it: */ +  fail_if_track_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 0); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_empty_buffers) +{ +  GstElement *element = setup_rganalysis (); + +  set_playing_state (element); + +  /* Single track: */ +  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0)); +  send_eos_event (element); +  poll_eos (element); + +  /* First album: */ +  g_object_set (element, "num-tracks", 2, NULL); + +  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0)); +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 1); + +  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0)); +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 0); + +  /* Second album, with a single track: */ +  g_object_set (element, "num-tracks", 1, NULL); +  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0)); +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 0); + +  /* Single track: */ +  push_buffer (test_buffer_const_float_stereo (44100, 0, 0.0, 0.0)); +  send_eos_event (element); +  poll_eos (element); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* Tests for correctness of the peak values. */ + +/* Float peak test.  For stereo, one channel has the constant value of + * -1.369, the other one 0.0.  This tests many things: The result peak + * value should occur on any channel.  The peak is of course the + * absolute amplitude, so 1.369 should be the result.  This will also + * detect if the code uses the absolute value during the comparison. + * If it is buggy it will return 0.0 since 0.0 > -1.369.  Furthermore, + * this makes sure that there is no problem with headroom (exceeding + * 0dBFS).  In the wild you get float samples > 1.0 from stuff like + * vorbis. */ + +GST_START_TEST (test_peak_float) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; + +  set_playing_state (element); +  push_buffer (test_buffer_const_float_stereo (8000, 512, -1.369, 0.0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.369); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, -1.369)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.369); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_float_mono (8000, 512, -1.369)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.369); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_peak_int16_16) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; + +  set_playing_state (element); + +  /* Half amplitude. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 1 << 14, 0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, 1 << 14)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_int16_mono (8000, 16, 512, 1 << 14)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Half amplitude, negative variant. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, -1 << 14, 0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, -1 << 14)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_int16_mono (8000, 16, 512, -1 << 14)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + + +  /* Now check for correct normalization of the peak value: Sample +   * values of this format range from -32768 to 32767.  So for the +   * highest positive amplitude we do not reach 1.0, only for +   * -32768! */ + +  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 32767, 0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 32767. / 32768.); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, 32767)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 32767. / 32768.); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_int16_mono (8000, 16, 512, 32767)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 32767. / 32768.); +  gst_tag_list_free (tag_list); + + +  /* Negative variant, reaching 1.0. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, -32768, 0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 16, 512, 0, -32768)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_int16_mono (8000, 16, 512, -32768)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* Same as the test before, but with 8 bits (packed into 16 bits). */ + +GST_START_TEST (test_peak_int16_8) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; + +  set_playing_state (element); + +  /* Half amplitude. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 1 << 6, 0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, 1 << 6)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_int16_mono (8000, 8, 512, 1 << 6)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + + +  /* Half amplitude, negative variant. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, -1 << 6, 0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, -1 << 6)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_int16_mono (8000, 8, 512, -1 << 6)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); + + +  /* Almost full amplitude (maximum positive value). */ +  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, (1 << 7) - 1, 0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.9921875); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, (1 << 7) - 1)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.9921875); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_int16_mono (8000, 8, 512, (1 << 7) - 1)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.9921875); +  gst_tag_list_free (tag_list); + + +  /* Full amplitude (maximum negative value). */ +  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, -1 << 7, 0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  gst_tag_list_free (tag_list); + +  /* Swapped channels. */ +  push_buffer (test_buffer_const_int16_stereo (8000, 8, 512, 0, -1 << 7)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  gst_tag_list_free (tag_list); + +  /* Mono. */ +  push_buffer (test_buffer_const_int16_mono (8000, 8, 512, -1 << 7)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_peak_album) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; + +  g_object_set (element, "num-tracks", 2, NULL); +  set_playing_state (element); + +  push_buffer (test_buffer_const_float_stereo (8000, 1024, 1.0, 0.0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 1); + +  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.0, 0.5)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  fail_unless_album_peak (tag_list, 1.0); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 0); + +  /* Try a second album: */ +  g_object_set (element, "num-tracks", 3, NULL); + +  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.4, 0.4)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.4); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 2); + +  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.45, 0.45)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.45); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 1); + +  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.2, 0.2)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.2); +  fail_unless_album_peak (tag_list, 0.45); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 0); + +  /* And now a single track, not in album mode (num-tracks is 0 +   * now): */ +  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.1, 0.1)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.1); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* Switching from track to album mode. */ + +GST_START_TEST (test_peak_track_album) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  guint num; + +  set_playing_state (element); + +  push_buffer (test_buffer_const_float_mono (8000, 1024, 1.0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); + +  g_object_set (element, "num-tracks", 1, NULL); +  push_buffer (test_buffer_const_float_mono (8000, 1024, 0.5)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  fail_unless_album_peak (tag_list, 0.5); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 0); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* Disabling album processing before the end of the album.  Probably a + * rare edge case and applications should not rely on this to work. + * They need to send the element to the READY state to clear up after + * an aborted album anyway since they might need to process another + * album afterwards. */ + +GST_START_TEST (test_peak_album_abort_to_track) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  guint num; + +  g_object_set (element, "num-tracks", 2, NULL); +  set_playing_state (element); + +  push_buffer (test_buffer_const_float_stereo (8000, 1024, 1.0, 0.0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 1.0); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 1); + +  g_object_set (element, "num-tracks", 0, NULL); + +  push_buffer (test_buffer_const_float_stereo (8000, 1024, 0.0, 0.5)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_gain_album) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator; +  gint i; + +  g_object_set (element, "num-tracks", 3, NULL); +  set_playing_state (element); + +  /* The three tracks are constructed such that if any of these is in +   * fact ignored for the album gain, the album gain will differ. */ + +  accumulator = 0; +  for (i = 8; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.75, 0.75)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.75); +  fail_unless_track_gain (tag_list, -15.70); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); + +  accumulator = 0; +  for (i = 12; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.5, 0.5)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.5); +  fail_unless_track_gain (tag_list, -12.22); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); + +  accumulator = 0; +  for (i = 180; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.25, 0.25)); +  send_eos_event (element); + +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, -6.20); +  fail_unless_album_peak (tag_list, 0.75); +  /* Strangely, wavegain reports -12.17 for the album, but the fixed +   * metaflac agrees to us.  Could be a 32767 vs. 32768 issue. */ +  fail_unless_album_gain (tag_list, -12.18); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* Checks ensuring that the "forced" property works as advertised. */ + +GST_START_TEST (test_forced) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i; + +  g_object_set (element, "forced", FALSE, NULL); +  set_playing_state (element); + +  tag_list = gst_tag_list_new (); +  /* Provided values are totally arbitrary. */ +  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, +      GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL); +  send_tag_event (element, tag_list); + +  for (i = 20; i--;) +    push_buffer (test_buffer_const_float_stereo (44100, 512, 0.5, 0.5)); +  send_eos_event (element); +  /* This fails if a tag message is generated: */ +  poll_eos (element); + +  /* Now back to a track without tags. */ + +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.25, 0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (44100)); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* Sending track gain and peak in separate tag lists. */ + +GST_START_TEST (test_forced_separate) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i; + +  g_object_set (element, "forced", FALSE, NULL); +  set_playing_state (element); + +  tag_list = gst_tag_list_new (); +  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_GAIN, 2.21, +      NULL); +  send_tag_event (element, tag_list); + +  tag_list = gst_tag_list_new (); +  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_PEAK, 1.0, +      NULL); +  send_tag_event (element, tag_list); + +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.5, 0.5)); +  send_eos_event (element); +  /* This fails if a tag message is generated: */ +  poll_eos (element); + +  /* Now a track without tags. */ + +  accumulator = 0; +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.25, 0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (44100)); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* A TAG event is sent _after_ data has already been processed.  In + * real pipelines, this could happen if there is more than one + * rganalysis element (by accident).  While it would have analyzed all + * the data prior to receiving the event, I expect it to not post its + * results if not forced.  This test is almost equivalent to + * test_forced. */ + +GST_START_TEST (test_forced_after_data) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i; + +  g_object_set (element, "forced", FALSE, NULL); +  set_playing_state (element); + +  for (i = 20; i--;) +    push_buffer (test_buffer_const_float_stereo (8000, 512, 0.5, 0.5)); + +  tag_list = gst_tag_list_new (); +  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, +      GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL); +  send_tag_event (element, tag_list); + +  send_eos_event (element); +  poll_eos (element); + +  /* Now back to a normal track, this one has no tags: */ +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25, +            0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (8000)); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* Like test_forced, but *analyze* an album afterwards.  The two tests + * following this one check the *skipping* of albums. */ + +GST_START_TEST (test_forced_album) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator; +  gint i; + +  g_object_set (element, "forced", FALSE, NULL); +  set_playing_state (element); + +  tag_list = gst_tag_list_new (); +  /* Provided values are totally arbitrary. */ +  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, +      GST_TAG_TRACK_PEAK, 1.0, GST_TAG_TRACK_GAIN, 2.21, NULL); +  send_tag_event (element, tag_list); + +  accumulator = 0; +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.5, 0.5)); +  send_eos_event (element); +  /* This fails if a tag message is generated: */ +  poll_eos (element); + +  /* Now an album without tags. */ +  g_object_set (element, "num-tracks", 2, NULL); + +  accumulator = 0; +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.25, 0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (44100)); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 1); + +  accumulator = 0; +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.25, 0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (44100)); +  fail_unless_album_peak (tag_list, 0.25); +  fail_unless_album_gain (tag_list, get_expected_gain (44100)); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 0); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_forced_album_skip) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i; + +  g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL); +  set_playing_state (element); + +  tag_list = gst_tag_list_new (); +  /* Provided values are totally arbitrary. */ +  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, +      GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21, +      GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL); +  send_tag_event (element, tag_list); + +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25, +            0.25)); +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 1); + +  /* This track has no tags, but needs to be skipped anyways since we +   * are in album processing mode. */ +  for (i = 20; i--;) +    push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0)); +  send_eos_event (element); +  poll_eos (element); +  fail_unless_num_tracks (element, 0); + +  /* Normal track after the album.  Of course not to be skipped. */ +  accumulator = 0; +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25, +            0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (8000)); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_forced_album_no_skip) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i; + +  g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL); +  set_playing_state (element); + +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25, +            0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (8000)); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 1); + +  /* The second track has indeed full tags, but although being not +   * forced, this one has to be processed because album processing is +   * on. */ +  tag_list = gst_tag_list_new (); +  /* Provided values are totally arbitrary. */ +  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, +      GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21, +      GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL); +  send_tag_event (element, tag_list); +  for (i = 20; i--;) +    push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.0); +  fail_unless_track_gain (tag_list, SILENCE_GAIN); +  /* Second track was just silence so the album peak equals the first +   * track's peak. */ +  fail_unless_album_peak (tag_list, 0.25); +  /* Statistical processing leads to the second track being +   * ignored for the gain (because it is so short): */ +  fail_unless_album_gain (tag_list, get_expected_gain (8000)); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 0); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_forced_abort_album_no_skip) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i; + +  g_object_set (element, "forced", FALSE, "num-tracks", 2, NULL); +  set_playing_state (element); + +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 8000, 512, 0.25, +            0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (8000)); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); +  fail_unless_num_tracks (element, 1); + +  /* Disabling album processing before end of album: */ +  g_object_set (element, "num-tracks", 0, NULL); + +  /* Processing a track that has to be skipped. */ +  tag_list = gst_tag_list_new (); +  /* Provided values are totally arbitrary. */ +  gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, +      GST_TAG_TRACK_PEAK, 0.75, GST_TAG_TRACK_GAIN, 2.21, +      GST_TAG_ALBUM_PEAK, 0.80, GST_TAG_ALBUM_GAIN, -0.11, NULL); +  send_tag_event (element, tag_list); +  for (i = 20; i--;) +    push_buffer (test_buffer_const_float_stereo (8000, 512, 0.0, 0.0)); +  send_eos_event (element); +  poll_eos (element); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_reference_level) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i; + +  g_object_set (element, "reference-level", 83., "num-tracks", 2, NULL); +  set_playing_state (element); + +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.25, 0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (44100) - 6.); +  fail_if_album_tags (tag_list); +  gst_tag_list_free (tag_list); + +  accumulator = 0; +  for (i = 20; i--;) +    push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, +            0.25, 0.25)); +  send_eos_event (element); +  tag_list = poll_tags (element); +  fail_unless_track_peak (tag_list, 0.25); +  fail_unless_track_gain (tag_list, get_expected_gain (44100) - 6.); +  fail_unless_album_peak (tag_list, 0.25); +  /* We provided the same waveform twice, with a reset separating +   * them.  Therefore, the album gain matches the track gain. */ +  fail_unless_album_gain (tag_list, get_expected_gain (44100) - 6.); +  gst_tag_list_free (tag_list); + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +GST_START_TEST (test_all_formats) +{ +  GstElement *element = setup_rganalysis (); +  GstTagList *tag_list; +  gint accumulator = 0; +  gint i, j; + +  set_playing_state (element); +  for (i = G_N_ELEMENTS (supported_rates); i--;) { +    accumulator = 0; +    for (j = 0; j < 4; j++) +      push_buffer (test_buffer_square_float_stereo (&accumulator, +              supported_rates[i].sample_rate, 512, 0.25, 0.25)); +    for (j = 0; j < 3; j++) +      push_buffer (test_buffer_square_float_mono (&accumulator, +              supported_rates[i].sample_rate, 512, 0.25)); +    for (j = 0; j < 4; j++) +      push_buffer (test_buffer_square_int16_stereo (&accumulator, +              supported_rates[i].sample_rate, 16, 512, 1 << 13, 1 << 13)); +    for (j = 0; j < 3; j++) +      push_buffer (test_buffer_square_int16_mono (&accumulator, +              supported_rates[i].sample_rate, 16, 512, 1 << 13)); +    for (j = 0; j < 3; j++) +      push_buffer (test_buffer_square_int16_stereo (&accumulator, +              supported_rates[i].sample_rate, 8, 512, 1 << 5, 1 << 5)); +    for (j = 0; j < 3; j++) +      push_buffer (test_buffer_square_int16_mono (&accumulator, +              supported_rates[i].sample_rate, 8, 512, 1 << 5)); +    send_eos_event (element); +    tag_list = poll_tags (element); +    fail_unless_track_peak (tag_list, 0.25); +    fail_unless_track_gain (tag_list, supported_rates[i].gain); +    gst_tag_list_free (tag_list); +  } + +  cleanup_rganalysis (element); +} + +GST_END_TEST; + +/* Checks ensuring all advertised supported sample rates are really + * accepted, for integer and float, mono and stereo.  This also + * verifies that the correct gain is computed for all formats (except + * odd bit depths). */ + +#define MAKE_GAIN_TEST_FLOAT_MONO(sample_rate)                        \ +  GST_START_TEST (test_gain_float_mono_##sample_rate)                 \ +{                                                                     \ +  GstElement *element = setup_rganalysis ();                          \ +  GstTagList *tag_list;                                               \ +  gint accumulator = 0;                                               \ +  gint i;                                                             \ +                                                                      \ +  set_playing_state (element);                                        \ +                                                                      \ +  for (i = 0; i < 20; i++)                                            \ +    push_buffer (test_buffer_square_float_mono (&accumulator,         \ +            sample_rate, 512, 0.25));                                 \ +  send_eos_event (element);                                           \ +  tag_list = poll_tags (element);                                     \ +  fail_unless_track_peak (tag_list, 0.25);                            \ +  fail_unless_track_gain (tag_list,                                   \ +      get_expected_gain (sample_rate));                               \ +  gst_tag_list_free (tag_list);                                       \ +                                                                      \ +  cleanup_rganalysis (element);                                       \ +}                                                                     \ +                                                                      \ +GST_END_TEST; + +#define MAKE_GAIN_TEST_FLOAT_STEREO(sample_rate)                      \ +  GST_START_TEST (test_gain_float_stereo_##sample_rate)               \ +{                                                                     \ +  GstElement *element = setup_rganalysis ();                          \ +  GstTagList *tag_list;                                               \ +  gint accumulator = 0;                                               \ +  gint i;                                                             \ +                                                                      \ +  set_playing_state (element);                                        \ +                                                                      \ +  for (i = 0; i < 20; i++)                                            \ +    push_buffer (test_buffer_square_float_stereo (&accumulator,       \ +            sample_rate, 512, 0.25, 0.25));                           \ +  send_eos_event (element);                                           \ +  tag_list = poll_tags (element);                                     \ +  fail_unless_track_peak (tag_list, 0.25);                            \ +  fail_unless_track_gain (tag_list,                                   \ +      get_expected_gain (sample_rate));                               \ +  gst_tag_list_free (tag_list);                                       \ +                                                                      \ +  cleanup_rganalysis (element);                                       \ +}                                                                     \ +                                                                      \ +GST_END_TEST; + +#define MAKE_GAIN_TEST_INT16_MONO(sample_rate, depth)                 \ +  GST_START_TEST (test_gain_int16_##depth##_mono_##sample_rate)       \ +{                                                                     \ +  GstElement *element = setup_rganalysis ();                          \ +  GstTagList *tag_list;                                               \ +  gint accumulator = 0;                                               \ +  gint i;                                                             \ +                                                                      \ +  set_playing_state (element);                                        \ +                                                                      \ +  for (i = 0; i < 20; i++)                                            \ +    push_buffer (test_buffer_square_int16_mono (&accumulator,         \ +            sample_rate, depth, 512, 1 << (13 + depth - 16)));        \ +                                                                      \ +  send_eos_event (element);                                           \ +  tag_list = poll_tags (element);                                     \ +  fail_unless_track_peak (tag_list, 0.25);                            \ +  fail_unless_track_gain (tag_list,                                   \ +      get_expected_gain (sample_rate));                               \ +  gst_tag_list_free (tag_list);                                       \ +                                                                      \ +  cleanup_rganalysis (element);                                       \ +}                                                                     \ +                                                                      \ +GST_END_TEST; + +#define MAKE_GAIN_TEST_INT16_STEREO(sample_rate, depth)               \ +  GST_START_TEST (test_gain_int16_##depth##_stereo_##sample_rate)     \ +{                                                                     \ +  GstElement *element = setup_rganalysis ();                          \ +  GstTagList *tag_list;                                               \ +  gint accumulator = 0;                                               \ +  gint i;                                                             \ +                                                                      \ +  set_playing_state (element);                                        \ +                                                                      \ +  for (i = 0; i < 20; i++)                                            \ +    push_buffer (test_buffer_square_int16_stereo (&accumulator,       \ +            sample_rate, depth, 512, 1 << (13 + depth - 16),          \ +            1 << (13 + depth - 16)));                                 \ +  send_eos_event (element);                                           \ +  tag_list = poll_tags (element);                                     \ +  fail_unless_track_peak (tag_list, 0.25);                            \ +  fail_unless_track_gain (tag_list,                                   \ +      get_expected_gain (sample_rate));                               \ +  gst_tag_list_free (tag_list);                                       \ +                                                                      \ +  cleanup_rganalysis (element);                                       \ +}                                                                     \ +                                                                      \ +GST_END_TEST; + +MAKE_GAIN_TEST_FLOAT_MONO (8000); +MAKE_GAIN_TEST_FLOAT_MONO (11025); +MAKE_GAIN_TEST_FLOAT_MONO (12000); +MAKE_GAIN_TEST_FLOAT_MONO (16000); +MAKE_GAIN_TEST_FLOAT_MONO (22050); +MAKE_GAIN_TEST_FLOAT_MONO (24000); +MAKE_GAIN_TEST_FLOAT_MONO (32000); +MAKE_GAIN_TEST_FLOAT_MONO (44100); +MAKE_GAIN_TEST_FLOAT_MONO (48000); + +MAKE_GAIN_TEST_FLOAT_STEREO (8000); +MAKE_GAIN_TEST_FLOAT_STEREO (11025); +MAKE_GAIN_TEST_FLOAT_STEREO (12000); +MAKE_GAIN_TEST_FLOAT_STEREO (16000); +MAKE_GAIN_TEST_FLOAT_STEREO (22050); +MAKE_GAIN_TEST_FLOAT_STEREO (24000); +MAKE_GAIN_TEST_FLOAT_STEREO (32000); +MAKE_GAIN_TEST_FLOAT_STEREO (44100); +MAKE_GAIN_TEST_FLOAT_STEREO (48000); + +MAKE_GAIN_TEST_INT16_MONO (8000, 16); +MAKE_GAIN_TEST_INT16_MONO (11025, 16); +MAKE_GAIN_TEST_INT16_MONO (12000, 16); +MAKE_GAIN_TEST_INT16_MONO (16000, 16); +MAKE_GAIN_TEST_INT16_MONO (22050, 16); +MAKE_GAIN_TEST_INT16_MONO (24000, 16); +MAKE_GAIN_TEST_INT16_MONO (32000, 16); +MAKE_GAIN_TEST_INT16_MONO (44100, 16); +MAKE_GAIN_TEST_INT16_MONO (48000, 16); + +MAKE_GAIN_TEST_INT16_STEREO (8000, 16); +MAKE_GAIN_TEST_INT16_STEREO (11025, 16); +MAKE_GAIN_TEST_INT16_STEREO (12000, 16); +MAKE_GAIN_TEST_INT16_STEREO (16000, 16); +MAKE_GAIN_TEST_INT16_STEREO (22050, 16); +MAKE_GAIN_TEST_INT16_STEREO (24000, 16); +MAKE_GAIN_TEST_INT16_STEREO (32000, 16); +MAKE_GAIN_TEST_INT16_STEREO (44100, 16); +MAKE_GAIN_TEST_INT16_STEREO (48000, 16); + +MAKE_GAIN_TEST_INT16_MONO (8000, 8); +MAKE_GAIN_TEST_INT16_MONO (11025, 8); +MAKE_GAIN_TEST_INT16_MONO (12000, 8); +MAKE_GAIN_TEST_INT16_MONO (16000, 8); +MAKE_GAIN_TEST_INT16_MONO (22050, 8); +MAKE_GAIN_TEST_INT16_MONO (24000, 8); +MAKE_GAIN_TEST_INT16_MONO (32000, 8); +MAKE_GAIN_TEST_INT16_MONO (44100, 8); +MAKE_GAIN_TEST_INT16_MONO (48000, 8); + +MAKE_GAIN_TEST_INT16_STEREO (8000, 8); +MAKE_GAIN_TEST_INT16_STEREO (11025, 8); +MAKE_GAIN_TEST_INT16_STEREO (12000, 8); +MAKE_GAIN_TEST_INT16_STEREO (16000, 8); +MAKE_GAIN_TEST_INT16_STEREO (22050, 8); +MAKE_GAIN_TEST_INT16_STEREO (24000, 8); +MAKE_GAIN_TEST_INT16_STEREO (32000, 8); +MAKE_GAIN_TEST_INT16_STEREO (44100, 8); +MAKE_GAIN_TEST_INT16_STEREO (48000, 8); + +Suite * +rganalysis_suite (void) +{ +  Suite *s = suite_create ("rganalysis"); +  TCase *tc_chain = tcase_create ("general"); + +  suite_add_tcase (s, tc_chain); + +  tcase_add_test (tc_chain, test_no_buffer); +  tcase_add_test (tc_chain, test_no_buffer_album_1); +  tcase_add_test (tc_chain, test_no_buffer_album_2); +  tcase_add_test (tc_chain, test_empty_buffers); + +  tcase_add_test (tc_chain, test_peak_float); +  tcase_add_test (tc_chain, test_peak_int16_16); +  tcase_add_test (tc_chain, test_peak_int16_8); + +  tcase_add_test (tc_chain, test_peak_album); +  tcase_add_test (tc_chain, test_peak_track_album); +  tcase_add_test (tc_chain, test_peak_album_abort_to_track); + +  tcase_add_test (tc_chain, test_gain_album); + +  tcase_add_test (tc_chain, test_forced); +  tcase_add_test (tc_chain, test_forced_separate); +  tcase_add_test (tc_chain, test_forced_after_data); +  tcase_add_test (tc_chain, test_forced_album); +  tcase_add_test (tc_chain, test_forced_album_skip); +  tcase_add_test (tc_chain, test_forced_album_no_skip); +  tcase_add_test (tc_chain, test_forced_abort_album_no_skip); + +  tcase_add_test (tc_chain, test_reference_level); + +  tcase_add_test (tc_chain, test_all_formats); + +  tcase_add_test (tc_chain, test_gain_float_mono_8000); +  tcase_add_test (tc_chain, test_gain_float_mono_11025); +  tcase_add_test (tc_chain, test_gain_float_mono_12000); +  tcase_add_test (tc_chain, test_gain_float_mono_16000); +  tcase_add_test (tc_chain, test_gain_float_mono_22050); +  tcase_add_test (tc_chain, test_gain_float_mono_24000); +  tcase_add_test (tc_chain, test_gain_float_mono_32000); +  tcase_add_test (tc_chain, test_gain_float_mono_44100); +  tcase_add_test (tc_chain, test_gain_float_mono_48000); + +  tcase_add_test (tc_chain, test_gain_float_stereo_8000); +  tcase_add_test (tc_chain, test_gain_float_stereo_11025); +  tcase_add_test (tc_chain, test_gain_float_stereo_12000); +  tcase_add_test (tc_chain, test_gain_float_stereo_16000); +  tcase_add_test (tc_chain, test_gain_float_stereo_22050); +  tcase_add_test (tc_chain, test_gain_float_stereo_24000); +  tcase_add_test (tc_chain, test_gain_float_stereo_32000); +  tcase_add_test (tc_chain, test_gain_float_stereo_44100); +  tcase_add_test (tc_chain, test_gain_float_stereo_48000); + +  tcase_add_test (tc_chain, test_gain_int16_16_mono_8000); +  tcase_add_test (tc_chain, test_gain_int16_16_mono_11025); +  tcase_add_test (tc_chain, test_gain_int16_16_mono_12000); +  tcase_add_test (tc_chain, test_gain_int16_16_mono_16000); +  tcase_add_test (tc_chain, test_gain_int16_16_mono_22050); +  tcase_add_test (tc_chain, test_gain_int16_16_mono_24000); +  tcase_add_test (tc_chain, test_gain_int16_16_mono_32000); +  tcase_add_test (tc_chain, test_gain_int16_16_mono_44100); +  tcase_add_test (tc_chain, test_gain_int16_16_mono_48000); + +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_8000); +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_11025); +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_12000); +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_16000); +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_22050); +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_24000); +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_32000); +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_44100); +  tcase_add_test (tc_chain, test_gain_int16_16_stereo_48000); + +  tcase_add_test (tc_chain, test_gain_int16_8_mono_8000); +  tcase_add_test (tc_chain, test_gain_int16_8_mono_11025); +  tcase_add_test (tc_chain, test_gain_int16_8_mono_12000); +  tcase_add_test (tc_chain, test_gain_int16_8_mono_16000); +  tcase_add_test (tc_chain, test_gain_int16_8_mono_22050); +  tcase_add_test (tc_chain, test_gain_int16_8_mono_24000); +  tcase_add_test (tc_chain, test_gain_int16_8_mono_32000); +  tcase_add_test (tc_chain, test_gain_int16_8_mono_44100); +  tcase_add_test (tc_chain, test_gain_int16_8_mono_48000); + +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_8000); +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_11025); +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_12000); +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_16000); +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_22050); +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_24000); +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_32000); +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_44100); +  tcase_add_test (tc_chain, test_gain_int16_8_stereo_48000); + +  return s; +} + +int +main (int argc, char **argv) +{ +  gint nf; + +  Suite *s = rganalysis_suite (); +  SRunner *sr = srunner_create (s); + +  gst_check_init (&argc, &argv); + +  srunner_run_all (sr, CK_ENV); +  nf = srunner_ntests_failed (sr); +  srunner_free (sr); + +  return nf; +} | 
