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>
7  *
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.
12  *
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.
17  *
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.
22  */
23 
24 /**
25  * SECTION:element-chromaprint
26  * @title: chromaprint
27  *
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 <ulink url="http://acoustid.org/">Acoustid</ulink>
31  * and Musicbrainz databases.
32  *
33  * ## Example launch line
34  * |[
35  * gst-launch-1.0 -m uridecodebin uri=file:///path/to/song.ogg ! audioconvert ! chromaprint ! fakesink
36  * ]|
37  *
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43 
44 #include "gstchromaprint.h"
45 
46 #define DEFAULT_MAX_DURATION 120
47 
48 #define PAD_CAPS \
49 	"audio/x-raw, " \
50         "format = (string) " GST_AUDIO_NE(S16) ", "\
51         "rate = (int) [ 1, MAX ], " \
52         "channels = (int) [ 1, 2 ]"
53 
54 GST_DEBUG_CATEGORY_STATIC (gst_chromaprint_debug);
55 #define GST_CAT_DEFAULT gst_chromaprint_debug
56 
57 enum
58 {
59   PROP_0,
60   PROP_FINGERPRINT,
61   PROP_MAX_DURATION
62 };
63 
64 #define parent_class gst_chromaprint_parent_class
65 G_DEFINE_TYPE (GstChromaprint, gst_chromaprint, GST_TYPE_AUDIO_FILTER);
66 
67 static void gst_chromaprint_finalize (GObject * object);
68 static void gst_chromaprint_set_property (GObject * object, guint prop_id,
69     const GValue * value, GParamSpec * pspec);
70 static void gst_chromaprint_get_property (GObject * object, guint prop_id,
71     GValue * value, GParamSpec * pspec);
72 static GstFlowReturn gst_chromaprint_transform_ip (GstBaseTransform * trans,
73     GstBuffer * buf);
74 static gboolean gst_chromaprint_sink_event (GstBaseTransform * trans,
75     GstEvent * event);
76 
77 static void
gst_chromaprint_class_init(GstChromaprintClass * klass)78 gst_chromaprint_class_init (GstChromaprintClass * klass)
79 {
80   GObjectClass *gobject_class;
81   GstBaseTransformClass *gstbasetrans_class;
82   GstCaps *caps;
83 
84   gobject_class = G_OBJECT_CLASS (klass);
85   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
86 
87   gobject_class->set_property = gst_chromaprint_set_property;
88   gobject_class->get_property = gst_chromaprint_get_property;
89 
90   g_object_class_install_property (gobject_class, PROP_FINGERPRINT,
91       g_param_spec_string ("fingerprint", "Resulting fingerprint",
92           "Resulting fingerprint", NULL, G_PARAM_READABLE));
93 
94   g_object_class_install_property (gobject_class, PROP_MAX_DURATION,
95       g_param_spec_uint ("duration", "Duration limit",
96           "Number of seconds of audio to use for fingerprinting",
97           0, G_MAXUINT, DEFAULT_MAX_DURATION,
98           G_PARAM_READABLE | G_PARAM_WRITABLE));
99 
100   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_chromaprint_finalize);
101 
102   gstbasetrans_class->transform_ip =
103       GST_DEBUG_FUNCPTR (gst_chromaprint_transform_ip);
104   gstbasetrans_class->sink_event =
105       GST_DEBUG_FUNCPTR (gst_chromaprint_sink_event);
106   gstbasetrans_class->passthrough_on_same_caps = TRUE;
107 
108   gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
109       "Chromaprint fingerprinting element",
110       "Filter/Analyzer/Audio",
111       "Find an audio fingerprint using the Chromaprint library",
112       "Lukáš Lalinský <lalinsky@gmail.com>");
113 
114   caps = gst_caps_from_string (PAD_CAPS);
115   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
116       caps);
117   gst_caps_unref (caps);
118 }
119 
120 static void
gst_chromaprint_reset(GstChromaprint * chromaprint)121 gst_chromaprint_reset (GstChromaprint * chromaprint)
122 {
123   if (chromaprint->fingerprint) {
124     chromaprint_dealloc (chromaprint->fingerprint);
125     chromaprint->fingerprint = NULL;
126   }
127 
128   chromaprint->nsamples = 0;
129   chromaprint->duration = 0;
130   chromaprint->record = TRUE;
131 }
132 
133 static void
gst_chromaprint_create_fingerprint(GstChromaprint * chromaprint)134 gst_chromaprint_create_fingerprint (GstChromaprint * chromaprint)
135 {
136   GstTagList *tags;
137 
138   if (chromaprint->duration <= 3)
139     return;
140 
141   GST_DEBUG_OBJECT (chromaprint,
142       "Generating fingerprint based on %d seconds of audio",
143       chromaprint->duration);
144 
145   chromaprint_finish (chromaprint->context);
146   chromaprint_get_fingerprint (chromaprint->context, &chromaprint->fingerprint);
147   chromaprint->record = FALSE;
148 
149   g_object_notify ((GObject *) chromaprint, "fingerprint");
150 
151   tags = gst_tag_list_new (GST_TAG_CHROMAPRINT_FINGERPRINT,
152       chromaprint->fingerprint, NULL);
153 
154   gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (chromaprint),
155       gst_event_new_tag (tags));
156 }
157 
158 static void
gst_chromaprint_init(GstChromaprint * chromaprint)159 gst_chromaprint_init (GstChromaprint * chromaprint)
160 {
161   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (chromaprint), TRUE);
162 
163   chromaprint->context = chromaprint_new (CHROMAPRINT_ALGORITHM_DEFAULT);
164   chromaprint->fingerprint = NULL;
165   chromaprint->max_duration = DEFAULT_MAX_DURATION;
166   gst_chromaprint_reset (chromaprint);
167 }
168 
169 static void
gst_chromaprint_finalize(GObject * object)170 gst_chromaprint_finalize (GObject * object)
171 {
172   GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
173 
174   chromaprint->record = FALSE;
175 
176   if (chromaprint->context) {
177     chromaprint_free (chromaprint->context);
178     chromaprint->context = NULL;
179   }
180 
181   if (chromaprint->fingerprint) {
182     chromaprint_dealloc (chromaprint->fingerprint);
183     chromaprint->fingerprint = NULL;
184   }
185 
186   G_OBJECT_CLASS (parent_class)->finalize (object);
187 }
188 
189 static GstFlowReturn
gst_chromaprint_transform_ip(GstBaseTransform * trans,GstBuffer * buf)190 gst_chromaprint_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
191 {
192   GstChromaprint *chromaprint = GST_CHROMAPRINT (trans);
193   GstAudioFilter *filter = GST_AUDIO_FILTER (trans);
194   GstMapInfo map_info;
195   guint nsamples;
196   gint rate, channels;
197 
198   rate = GST_AUDIO_INFO_RATE (&filter->info);
199   channels = GST_AUDIO_INFO_CHANNELS (&filter->info);
200 
201   if (G_UNLIKELY (rate <= 0 || channels <= 0))
202     return GST_FLOW_NOT_NEGOTIATED;
203 
204   if (!chromaprint->record)
205     return GST_FLOW_OK;
206 
207   if (!gst_buffer_map (buf, &map_info, GST_MAP_READ))
208     return GST_FLOW_ERROR;
209 
210   nsamples = map_info.size / (channels * 2);
211 
212   if (nsamples == 0)
213     goto end;
214 
215   if (chromaprint->nsamples == 0) {
216     chromaprint_start (chromaprint->context, rate, channels);
217   }
218   chromaprint->nsamples += nsamples;
219   chromaprint->duration = chromaprint->nsamples / rate;
220 
221   chromaprint_feed (chromaprint->context, (gint16 *) map_info.data,
222       map_info.size / sizeof (guint16));
223 
224   if (chromaprint->duration >= chromaprint->max_duration
225       && !chromaprint->fingerprint) {
226     gst_chromaprint_create_fingerprint (chromaprint);
227   }
228 
229 end:
230   gst_buffer_unmap (buf, &map_info);
231 
232   return GST_FLOW_OK;
233 }
234 
235 static gboolean
gst_chromaprint_sink_event(GstBaseTransform * trans,GstEvent * event)236 gst_chromaprint_sink_event (GstBaseTransform * trans, GstEvent * event)
237 {
238   GstChromaprint *chromaprint = GST_CHROMAPRINT (trans);
239 
240   switch (GST_EVENT_TYPE (event)) {
241     case GST_EVENT_FLUSH_STOP:
242     case GST_EVENT_SEGMENT:
243       GST_DEBUG_OBJECT (trans, "Got %s event, clearing buffer",
244           GST_EVENT_TYPE_NAME (event));
245       gst_chromaprint_reset (chromaprint);
246       break;
247     case GST_EVENT_EOS:
248       if (!chromaprint->fingerprint) {
249         gst_chromaprint_create_fingerprint (chromaprint);
250       }
251       break;
252     default:
253       break;
254   }
255 
256   return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
257 }
258 
259 static void
gst_chromaprint_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)260 gst_chromaprint_set_property (GObject * object, guint prop_id,
261     const GValue * value, GParamSpec * pspec)
262 {
263   GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
264 
265   switch (prop_id) {
266     case PROP_MAX_DURATION:
267       chromaprint->max_duration = g_value_get_uint (value);
268       break;
269     default:
270       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271       break;
272   }
273 }
274 
275 static void
gst_chromaprint_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)276 gst_chromaprint_get_property (GObject * object, guint prop_id,
277     GValue * value, GParamSpec * pspec)
278 {
279   GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
280 
281   switch (prop_id) {
282     case PROP_FINGERPRINT:
283       g_value_set_string (value, chromaprint->fingerprint);
284       break;
285     case PROP_MAX_DURATION:
286       g_value_set_uint (value, chromaprint->max_duration);
287       break;
288     default:
289       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290       break;
291   }
292 }
293 
294 static gboolean
plugin_init(GstPlugin * plugin)295 plugin_init (GstPlugin * plugin)
296 {
297   gboolean ret;
298 
299   GST_DEBUG_CATEGORY_INIT (gst_chromaprint_debug, "chromaprint",
300       0, "chromaprint element");
301 
302   GST_INFO ("libchromaprint %s", chromaprint_get_version ());
303 
304   ret = gst_element_register (plugin, "chromaprint", GST_RANK_NONE,
305       GST_TYPE_CHROMAPRINT);
306 
307   if (ret) {
308     gst_tag_register (GST_TAG_CHROMAPRINT_FINGERPRINT, GST_TAG_FLAG_META,
309         G_TYPE_STRING, "chromaprint fingerprint", "Chromaprint fingerprint",
310         NULL);
311   }
312 
313   return ret;
314 }
315 
316 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
317     GST_VERSION_MINOR,
318     chromaprint,
319     "Calculate Chromaprint fingerprint from audio files",
320     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
321