5 * Copyright (C) 2006 M. Derezynski
6 * Copyright (C) 2008 Eric Buehl
7 * Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
35 "rate = (int) [ 1, MAX ], " \
36 "channels = (int) [ 1, 2 ], " \
37 "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
38 "width = (int) { 16 }, " \
39 "depth = (int) { 16 }, " \
40 "signed = (boolean) true"
42 GST_DEBUG_CATEGORY_STATIC (gst_ofa_debug);
43 #define GST_CAT_DEFAULT gst_ofa_debug
52 GST_BOILERPLATE (GstOFA, gst_ofa, GstAudioFilter, GST_TYPE_AUDIO_FILTER);
54 static void gst_ofa_finalize (GObject * object);
55 static void gst_ofa_get_property (GObject * object, guint prop_id,
56 GValue * value, GParamSpec * pspec);
57 static GstFlowReturn gst_ofa_transform_ip (GstBaseTransform * trans,
59 static gboolean gst_ofa_event (GstBaseTransform * trans, GstEvent * event);
62 gst_ofa_base_init (gpointer g_class)
64 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
65 GstAudioFilterClass *audio_filter_class = (GstAudioFilterClass *) g_class;
68 gst_element_class_set_details_simple (gstelement_class, "OFA",
69 "MusicIP Fingerprinting element",
70 "Find a music fingerprint using MusicIP's libofa",
71 "Milosz Derezynski <internalerror@gmail.com>, Eric Buehl <eric.buehl@gmail.com>");
73 caps = gst_caps_from_string (PAD_CAPS);
74 gst_audio_filter_class_add_pad_templates (audio_filter_class, caps);
75 gst_caps_unref (caps);
79 gst_ofa_finalize (GObject * object)
81 GstOFA *ofa = GST_OFA (object);
84 g_object_unref (ofa->adapter);
88 g_free (ofa->fingerprint);
89 ofa->fingerprint = NULL;
91 G_OBJECT_CLASS (parent_class)->finalize (object);
95 gst_ofa_class_init (GstOFAClass * klass)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98 GstBaseTransformClass *gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
100 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_ofa_get_property);
102 g_object_class_install_property (gobject_class, PROP_FINGERPRINT,
103 g_param_spec_string ("fingerprint", "Resulting fingerprint",
104 "Resulting fingerprint", NULL,
105 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
107 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ofa_finalize);
109 gstbasetrans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_ofa_transform_ip);
110 gstbasetrans_class->event = GST_DEBUG_FUNCPTR (gst_ofa_event);
111 gstbasetrans_class->passthrough_on_same_caps = TRUE;
115 create_fingerprint (GstOFA * ofa)
118 GstAudioFilter *ofa_filter = GST_AUDIO_FILTER (ofa);
119 gint rate = ofa_filter->format.rate;
120 gint channels = ofa_filter->format.channels;
122 ofa_filter->format.bigend ? OFA_BIG_ENDIAN : OFA_LITTLE_ENDIAN;
126 available = gst_adapter_available (ofa->adapter);
128 if (available == 0) {
129 GST_WARNING_OBJECT (ofa, "No data to take fingerprint from");
134 if (GST_AUDIO_FILTER (ofa)->format.bigend)
135 endianness = OFA_BIG_ENDIAN;
137 endianness = OFA_LITTLE_ENDIAN;
140 GST_DEBUG_OBJECT (ofa, "Generating fingerprint for %u samples",
143 buf = gst_adapter_take_buffer (ofa->adapter, available);
145 ofa->fingerprint = g_strdup (ofa_create_print (GST_BUFFER_DATA (buf),
146 endianness, GST_BUFFER_SIZE (buf) / 2, rate,
147 (channels == 2) ? 1 : 0));
149 if (ofa->fingerprint) {
150 GST_INFO_OBJECT (ofa, "Generated fingerprint: %s", ofa->fingerprint);
152 GST_WARNING_OBJECT (ofa, "Failed to generate fingerprint");
155 gst_buffer_unref (buf);
157 if (ofa->fingerprint) {
158 tags = gst_tag_list_new ();
159 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
160 GST_TAG_OFA_FINGERPRINT, ofa->fingerprint, NULL);
161 gst_element_found_tags (GST_ELEMENT (ofa), tags);
163 g_object_notify (G_OBJECT (ofa), "fingerprint");
170 gst_ofa_event (GstBaseTransform * trans, GstEvent * event)
172 GstOFA *ofa = GST_OFA (trans);
174 switch (GST_EVENT_TYPE (event)) {
175 case GST_EVENT_FLUSH_STOP:
176 case GST_EVENT_NEWSEGMENT:
177 GST_DEBUG_OBJECT (ofa, "Got %s event, clearing buffer",
178 GST_EVENT_TYPE_NAME (event));
179 gst_adapter_clear (ofa->adapter);
181 g_free (ofa->fingerprint);
182 ofa->fingerprint = NULL;
185 /* we got to the end of the stream but never generated a fingerprint
186 * (probably under 135 seconds)
188 if (!ofa->fingerprint && ofa->record)
189 create_fingerprint (ofa);
199 gst_ofa_init (GstOFA * ofa, GstOFAClass * g_class)
201 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (ofa), TRUE);
203 ofa->fingerprint = NULL;
206 ofa->adapter = gst_adapter_new ();
210 gst_ofa_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
212 GstOFA *ofa = GST_OFA (trans);
213 GstAudioFilter *ofa_filter = GST_AUDIO_FILTER (ofa);
215 GstClockTime duration;
216 gint rate = ofa_filter->format.rate;
217 gint channels = ofa_filter->format.channels;
219 g_return_val_if_fail (rate > 0 && channels > 0, GST_FLOW_NOT_NEGOTIATED);
224 gst_adapter_push (ofa->adapter, gst_buffer_copy (buf));
226 nframes = gst_adapter_available (ofa->adapter) / (channels * 2);
227 duration = GST_FRAMES_TO_CLOCK_TIME (nframes, rate);
229 if (duration >= 135 * GST_SECOND && ofa->fingerprint == NULL)
230 create_fingerprint (ofa);
236 gst_ofa_get_property (GObject * object, guint prop_id, GValue * value,
239 GstOFA *ofa = GST_OFA (object);
242 case PROP_FINGERPRINT:
243 g_value_set_string (value, ofa->fingerprint);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253 plugin_init (GstPlugin * plugin)
257 int major, minor, rev;
259 GST_DEBUG_CATEGORY_INIT (gst_ofa_debug, "ofa", 0, "ofa element");
261 ofa_get_version (&major, &minor, &rev);
263 GST_DEBUG ("libofa %d.%d.%d", major, minor, rev);
265 ret = gst_element_register (plugin, "ofa", GST_RANK_NONE, GST_TYPE_OFA);
268 /* TODO: get this into core */
269 gst_tag_register (GST_TAG_OFA_FINGERPRINT, GST_TAG_FLAG_META,
270 G_TYPE_STRING, "ofa fingerprint", "OFA fingerprint", NULL);
276 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
279 "Calculate MusicIP fingerprint from audio files",
280 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)