1 /* GStreamer chromaprint audio fingerprinting element
2 * Copyright (C) 2006 M. Derezynski
3 * Copyright (C) 2008 Eric Buehl
4 * Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
5 * Copyright (C) 2011 Lukáš Lalinský <lalinsky@gmail.com>
6 * Copyright (C) 2012 Collabora Ltd. <tim.muller@collabora.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:element-chromaprint
28 * The chromaprint element calculates an acoustic fingerprint for an
29 * audio stream which can be used to identify a song and look up
30 * further metadata from the [Acoustid](http://acoustid.org/) and Musicbrainz
33 * ## Example launch line
35 * gst-launch-1.0 -m uridecodebin uri=file:///path/to/song.ogg ! audioconvert ! chromaprint ! fakesink
44 #include "gstchromaprint.h"
46 #define DEFAULT_MAX_DURATION 120
50 "format = (string) " GST_AUDIO_NE(S16) ", "\
51 "rate = (int) [ 1, MAX ], " \
52 "channels = (int) [ 1, 2 ]"
54 GST_DEBUG_CATEGORY_STATIC (gst_chromaprint_debug);
55 #define GST_CAT_DEFAULT gst_chromaprint_debug
64 static gboolean chromaprint_element_init (GstPlugin * plugin);
66 #define parent_class gst_chromaprint_parent_class
67 G_DEFINE_TYPE (GstChromaprint, gst_chromaprint, GST_TYPE_AUDIO_FILTER);
68 GST_ELEMENT_REGISTER_DEFINE_CUSTOM (chromaprint, chromaprint_element_init);
70 static void gst_chromaprint_finalize (GObject * object);
71 static void gst_chromaprint_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
73 static void gst_chromaprint_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec);
75 static GstFlowReturn gst_chromaprint_transform_ip (GstBaseTransform * trans,
77 static gboolean gst_chromaprint_sink_event (GstBaseTransform * trans,
81 gst_chromaprint_class_init (GstChromaprintClass * klass)
83 GObjectClass *gobject_class;
84 GstBaseTransformClass *gstbasetrans_class;
87 gobject_class = G_OBJECT_CLASS (klass);
88 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
90 gobject_class->set_property = gst_chromaprint_set_property;
91 gobject_class->get_property = gst_chromaprint_get_property;
93 g_object_class_install_property (gobject_class, PROP_FINGERPRINT,
94 g_param_spec_string ("fingerprint", "Resulting fingerprint",
95 "Resulting fingerprint", NULL, G_PARAM_READABLE));
97 g_object_class_install_property (gobject_class, PROP_MAX_DURATION,
98 g_param_spec_uint ("duration", "Duration limit",
99 "Number of seconds of audio to use for fingerprinting",
100 0, G_MAXUINT, DEFAULT_MAX_DURATION,
101 G_PARAM_READABLE | G_PARAM_WRITABLE));
103 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_chromaprint_finalize);
105 gstbasetrans_class->transform_ip =
106 GST_DEBUG_FUNCPTR (gst_chromaprint_transform_ip);
107 gstbasetrans_class->sink_event =
108 GST_DEBUG_FUNCPTR (gst_chromaprint_sink_event);
109 gstbasetrans_class->passthrough_on_same_caps = TRUE;
111 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
112 "Chromaprint fingerprinting element",
113 "Filter/Analyzer/Audio",
114 "Find an audio fingerprint using the Chromaprint library",
115 "Lukáš Lalinský <lalinsky@gmail.com>");
117 caps = gst_caps_from_string (PAD_CAPS);
118 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
120 gst_caps_unref (caps);
124 gst_chromaprint_reset (GstChromaprint * chromaprint)
126 if (chromaprint->fingerprint) {
127 chromaprint_dealloc (chromaprint->fingerprint);
128 chromaprint->fingerprint = NULL;
131 chromaprint->nsamples = 0;
132 chromaprint->duration = 0;
133 chromaprint->record = TRUE;
137 gst_chromaprint_create_fingerprint (GstChromaprint * chromaprint)
141 if (chromaprint->duration <= 3)
144 GST_DEBUG_OBJECT (chromaprint,
145 "Generating fingerprint based on %d seconds of audio",
146 chromaprint->duration);
148 chromaprint_finish (chromaprint->context);
149 chromaprint_get_fingerprint (chromaprint->context, &chromaprint->fingerprint);
150 chromaprint->record = FALSE;
152 g_object_notify ((GObject *) chromaprint, "fingerprint");
154 tags = gst_tag_list_new (GST_TAG_CHROMAPRINT_FINGERPRINT,
155 chromaprint->fingerprint, NULL);
157 gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (chromaprint),
158 gst_event_new_tag (tags));
162 gst_chromaprint_init (GstChromaprint * chromaprint)
164 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (chromaprint), TRUE);
166 chromaprint->context = chromaprint_new (CHROMAPRINT_ALGORITHM_DEFAULT);
167 chromaprint->fingerprint = NULL;
168 chromaprint->max_duration = DEFAULT_MAX_DURATION;
169 gst_chromaprint_reset (chromaprint);
173 gst_chromaprint_finalize (GObject * object)
175 GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
177 chromaprint->record = FALSE;
179 if (chromaprint->context) {
180 chromaprint_free (chromaprint->context);
181 chromaprint->context = NULL;
184 if (chromaprint->fingerprint) {
185 chromaprint_dealloc (chromaprint->fingerprint);
186 chromaprint->fingerprint = NULL;
189 G_OBJECT_CLASS (parent_class)->finalize (object);
193 gst_chromaprint_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
195 GstChromaprint *chromaprint = GST_CHROMAPRINT (trans);
196 GstAudioFilter *filter = GST_AUDIO_FILTER (trans);
201 rate = GST_AUDIO_INFO_RATE (&filter->info);
202 channels = GST_AUDIO_INFO_CHANNELS (&filter->info);
204 if (G_UNLIKELY (rate <= 0 || channels <= 0))
205 return GST_FLOW_NOT_NEGOTIATED;
207 if (!chromaprint->record)
210 if (!gst_buffer_map (buf, &map_info, GST_MAP_READ))
211 return GST_FLOW_ERROR;
213 nsamples = map_info.size / (channels * 2);
218 if (chromaprint->nsamples == 0) {
219 chromaprint_start (chromaprint->context, rate, channels);
221 chromaprint->nsamples += nsamples;
222 chromaprint->duration = chromaprint->nsamples / rate;
224 chromaprint_feed (chromaprint->context, (gint16 *) map_info.data,
225 map_info.size / sizeof (guint16));
227 if (chromaprint->duration >= chromaprint->max_duration
228 && !chromaprint->fingerprint) {
229 gst_chromaprint_create_fingerprint (chromaprint);
233 gst_buffer_unmap (buf, &map_info);
239 gst_chromaprint_sink_event (GstBaseTransform * trans, GstEvent * event)
241 GstChromaprint *chromaprint = GST_CHROMAPRINT (trans);
243 switch (GST_EVENT_TYPE (event)) {
244 case GST_EVENT_FLUSH_STOP:
245 case GST_EVENT_SEGMENT:
246 GST_DEBUG_OBJECT (trans, "Got %s event, clearing buffer",
247 GST_EVENT_TYPE_NAME (event));
248 gst_chromaprint_reset (chromaprint);
251 if (!chromaprint->fingerprint) {
252 gst_chromaprint_create_fingerprint (chromaprint);
259 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
263 gst_chromaprint_set_property (GObject * object, guint prop_id,
264 const GValue * value, GParamSpec * pspec)
266 GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
269 case PROP_MAX_DURATION:
270 chromaprint->max_duration = g_value_get_uint (value);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 gst_chromaprint_get_property (GObject * object, guint prop_id,
280 GValue * value, GParamSpec * pspec)
282 GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
285 case PROP_FINGERPRINT:
286 g_value_set_string (value, chromaprint->fingerprint);
288 case PROP_MAX_DURATION:
289 g_value_set_uint (value, chromaprint->max_duration);
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298 chromaprint_element_init (GstPlugin * plugin)
302 GST_DEBUG_CATEGORY_INIT (gst_chromaprint_debug, "chromaprint",
303 0, "chromaprint element");
305 GST_INFO ("libchromaprint %s", chromaprint_get_version ());
307 ret = gst_element_register (plugin, "chromaprint", GST_RANK_NONE,
308 GST_TYPE_CHROMAPRINT);
311 gst_tag_register (GST_TAG_CHROMAPRINT_FINGERPRINT, GST_TAG_FLAG_META,
312 G_TYPE_STRING, "chromaprint fingerprint", "Chromaprint fingerprint",
320 plugin_init (GstPlugin * plugin)
322 return GST_ELEMENT_REGISTER (chromaprint, plugin);
325 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
328 "Calculate Chromaprint fingerprint from audio files",
329 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)