1 /* vim: set sts=2 sw=2 et: */
4 * Copyright (C) 2006 Josep Torra <j.torra@telefonica.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include <gst/audio/audio.h>
30 GST_DEBUG_CATEGORY_STATIC (gst_pitch_debug);
31 #define GST_CAT_DEFAULT gst_pitch_debug
34 #define RATESTR "32000"
35 #define WANTED RATE * 2
36 #define ZERO_PADDING_FACTOR 2
37 #define FFT_LEN (RATE * ZERO_PADDING_FACTOR)
39 /* Filter signals and args */
50 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_STATIC_CAPS ("audio/x-raw-int, "
54 "rate = (int) " RATESTR ", "
55 "channels = (int) 1, "
56 "endianness = (int) BYTE_ORDER, "
57 "width = (int) 16, " "depth = (int) 16, " "signed = (boolean) true")
60 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
63 GST_STATIC_CAPS ("audio/x-raw-int, "
64 "rate = (int) [ 1, MAX ], "
65 "channels = (int) [1, MAX], "
66 "endianness = (int) BYTE_ORDER, "
67 "width = (int) 16, " "depth = (int) 16, " "signed = (boolean) true")
70 #define DEBUG_INIT(bla) \
71 GST_DEBUG_CATEGORY_INIT (gst_pitch_debug, "Pitch", 0, "fundamental frequency plugin");
73 GST_BOILERPLATE_FULL (GstPitch, gst_pitch, GstBaseTransform,
74 GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
76 static void gst_pitch_set_property (GObject * object, guint prop_id,
77 const GValue * value, GParamSpec * pspec);
78 static void gst_pitch_get_property (GObject * object, guint prop_id,
79 GValue * value, GParamSpec * pspec);
80 static void gst_pitch_dispose (GObject * object);
82 static gboolean gst_pitch_set_caps (GstBaseTransform * trans, GstCaps * in,
84 static gboolean gst_pitch_start (GstBaseTransform * trans);
86 static GstFlowReturn gst_pitch_transform_ip (GstBaseTransform * trans,
89 #define DEFAULT_PROP_ALGORITHM GST_PITCH_ALGORITHM_FFT
91 #define GST_TYPE_PITCH_ALGORITHM (gst_pitch_algorithm_get_type())
93 gst_pitch_algorithm_get_type (void)
95 static GType pitch_algorithm_type = 0;
96 static const GEnumValue pitch_algorithm[] = {
97 {GST_PITCH_ALGORITHM_FFT, "fft", "fft"},
98 {GST_PITCH_ALGORITHM_HPS, "hps", "hps"},
102 if (!pitch_algorithm_type) {
103 pitch_algorithm_type =
104 g_enum_register_static ("GstPitchAlgorithm",
107 return pitch_algorithm_type;
110 /* GObject vmethod implementations */
113 gst_pitch_base_init (gpointer klass)
115 static GstElementDetails element_details = {
117 "Filter/Analyzer/Audio",
118 "Run an FFT on the audio signal, output fundamental frequency",
119 "Josep Torra <j.torra@telefonica.net>"
121 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
123 gst_element_class_add_pad_template (element_class,
124 gst_static_pad_template_get (&src_template));
125 gst_element_class_add_pad_template (element_class,
126 gst_static_pad_template_get (&sink_template));
127 gst_element_class_set_details (element_class, &element_details);
131 gst_pitch_class_init (GstPitchClass * klass)
133 GObjectClass *gobject_class;
134 GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
136 gobject_class = (GObjectClass *) klass;
137 gobject_class->set_property = gst_pitch_set_property;
138 gobject_class->get_property = gst_pitch_get_property;
139 gobject_class->dispose = gst_pitch_dispose;
141 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_pitch_set_caps);
142 trans_class->start = GST_DEBUG_FUNCPTR (gst_pitch_start);
143 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_pitch_transform_ip);
144 trans_class->passthrough_on_same_caps = TRUE;
146 g_object_class_install_property (gobject_class, PROP_SIGNAL_FFREQ,
147 g_param_spec_boolean ("message", "Message",
148 "Post a fundamental frequency message for each passed interval",
149 TRUE, G_PARAM_READWRITE));
151 g_object_class_install_property (gobject_class, PROP_MINFREQ,
152 g_param_spec_int ("minfreq", "MinFreq",
153 "Initial scan frequency, default 30 Hz",
154 1, G_MAXINT, 30, G_PARAM_READWRITE));
156 g_object_class_install_property (gobject_class, PROP_MAXFREQ,
157 g_param_spec_int ("maxfreq", "MaxFreq",
158 "Final scan frequency, default 1500 Hz",
159 1, G_MAXINT, 1500, G_PARAM_READWRITE));
161 g_object_class_install_property (gobject_class, PROP_ALGORITHM,
162 g_param_spec_enum ("algorithm", "Algorithm",
163 "Pitch detection algorithm to use",
164 GST_TYPE_PITCH_ALGORITHM, DEFAULT_PROP_ALGORITHM,
168 GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
169 GST_DEBUG_FUNCPTR (gst_pitch_transform_ip);
173 gst_pitch_setup_algorithm (GstPitch * filter, GstPitchAlgorithm algorithm)
175 g_mutex_lock (filter->mutex);
176 if (algorithm == GST_PITCH_ALGORITHM_HPS) {
177 if (NULL == filter->module)
178 filter->module = (gint *) g_malloc (FFT_LEN * sizeof (gint));
182 g_free (filter->module);
184 filter->module = NULL;
186 filter->algorithm = algorithm;
187 g_mutex_unlock (filter->mutex);
191 gst_pitch_init (GstPitch * filter, GstPitchClass * klass)
193 filter->adapter = gst_adapter_new ();
195 filter->minfreq = 30;
196 filter->maxfreq = 1500;
197 filter->message = TRUE;
198 filter->mutex = g_mutex_new ();
200 gst_pitch_setup_algorithm (filter, DEFAULT_PROP_ALGORITHM);
204 gst_pitch_dispose (GObject * object)
206 GstPitch *filter = GST_PITCH (object);
208 if (filter->adapter) {
209 g_object_unref (filter->adapter);
210 filter->adapter = NULL;
213 g_free (filter->fft_cfg);
214 g_free (filter->signal);
215 g_free (filter->spectrum);
217 g_free (filter->module);
219 g_mutex_free (filter->mutex);
223 G_OBJECT_CLASS (parent_class)->dispose (object);
227 gst_pitch_set_property (GObject * object, guint prop_id,
228 const GValue * value, GParamSpec * pspec)
230 GstPitch *filter = GST_PITCH (object);
233 case PROP_SIGNAL_FFREQ:
234 filter->message = g_value_get_boolean (value);
237 filter->minfreq = g_value_get_int (value);
240 filter->maxfreq = g_value_get_int (value);
243 gst_pitch_setup_algorithm (filter, g_value_get_enum (value));
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 gst_pitch_get_property (GObject * object, guint prop_id,
253 GValue * value, GParamSpec * pspec)
255 GstPitch *filter = GST_PITCH (object);
258 case PROP_SIGNAL_FFREQ:
259 g_value_set_boolean (value, filter->message);
262 g_value_set_int (value, filter->minfreq);
265 g_value_set_int (value, filter->maxfreq);
268 g_value_set_enum (value, filter->algorithm);
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277 gst_pitch_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
279 GstPitch *filter = GST_PITCH (trans);
281 filter->fft_cfg = kiss_fft_alloc (FFT_LEN, 0, NULL, NULL);
283 (kiss_fft_cpx *) g_malloc0 (FFT_LEN * sizeof (kiss_fft_cpx));
285 (kiss_fft_cpx *) g_malloc (FFT_LEN * sizeof (kiss_fft_cpx));
291 gst_pitch_start (GstBaseTransform * trans)
293 GstPitch *filter = GST_PITCH (trans);
295 gst_adapter_clear (filter->adapter);
301 gst_pitch_message_new (GstPitch * filter)
304 gint i, min_i, max_i;
305 gint freq_index, frequency_module;
308 /* Extract fundamental frequency */
310 frequency_module = 0;
312 min_i = filter->minfreq * ZERO_PADDING_FACTOR;
313 max_i = filter->maxfreq * ZERO_PADDING_FACTOR;
315 GST_DEBUG_OBJECT (filter, "min_freq = %d, max_freq = %d", filter->minfreq,
317 /*GST_DEBUG_OBJECT (filter, "min_i = %d, max_i = %d", min_i, max_i); */
319 g_mutex_lock (filter->mutex);
320 switch (filter->algorithm) {
322 case GST_PITCH_ALGORITHM_FFT:
326 for (i = min_i; i < max_i; i++) {
327 module = (filter->spectrum[i].r * filter->spectrum[i].r);
328 module += (filter->spectrum[i].i * filter->spectrum[i].i);
331 GST_LOG_OBJECT (filter, "module[%d] = %d", i, module);
333 /* find strongest peak */
334 if (module > frequency_module) {
335 frequency_module = module;
342 case GST_PITCH_ALGORITHM_HPS:
344 gint prev_frequency = 0;
347 for (i = min_i; i < FFT_LEN; i++) {
348 filter->module[i] = (filter->spectrum[i].r * filter->spectrum[i].r);
349 filter->module[i] += (filter->spectrum[i].i * filter->spectrum[i].i);
351 if (filter->module[i] > 0)
352 GST_LOG_OBJECT (filter, "module[%d] = %d", i, filter->module[i]);
355 /* Harmonic Product Spectrum algorithm */
356 #define MAX_DS_FACTOR (6)
357 for (i = min_i; (i <= max_i) && (i < FFT_LEN); i++) {
358 for (j = 2; j <= MAX_DS_FACTOR; j++) {
359 t = i * j * ZERO_PADDING_FACTOR;
363 /* this is not part of the HPS but it seems
364 * there are lots of zeroes in the spectrum ...
366 if (filter->module[t] != 0)
367 filter->module[i] *= filter->module[t];
370 /* find strongest peak */
371 if (filter->module[i] > frequency_module) {
372 prev_frequency = freq_index;
373 frequency_module = filter->module[i];
378 /* try to correct octave error */
380 if (freq_index != 0 && prev_frequency != 0) {
381 float ratio = (float) frequency / (float) prev_frequency;
382 if (ratio >= 1.9 && ratio < 2.1 && (float) filter->module[prev_frequency] >= 0.2 * (float) frequency_module ) {
383 g_debug("Chose freq %d[%d] over %d[%d]\n", prev_frequency, filter->module[prev_frequency], frequency, filter->module[frequency]);
384 frequency = prev_frequency;
385 frequency_module = filter->module[prev_frequency];
394 g_mutex_unlock (filter->mutex);
396 frequency = (gfloat) freq_index / ZERO_PADDING_FACTOR;
398 g_debug("freq %d[%d]\n", frequency, frequency_module);
400 GST_DEBUG_OBJECT (filter, "preparing message, frequency = %d ", frequency);
402 s = gst_structure_new ("pitch", "frequency", G_TYPE_FLOAT, frequency, NULL);
404 return gst_message_new_element (GST_OBJECT (filter), s);
407 /* GstBaseTransform vmethod implementations */
409 /* this function does the actual processing
412 gst_pitch_transform_ip (GstBaseTransform * trans, GstBuffer * in)
414 GstPitch *filter = GST_PITCH (trans);
419 GST_DEBUG_OBJECT (filter, "transform : %ld bytes", GST_BUFFER_SIZE (in));
420 gst_adapter_push (filter->adapter, gst_buffer_ref (in));
422 /* required number of bytes */
423 avail = gst_adapter_available (filter->adapter);
424 GST_DEBUG_OBJECT (filter, "avail: %d wanted: %d", avail, WANTED);
426 if (avail > WANTED) {
428 /* copy sample data in the complex vector */
429 samples = (gint16 *) gst_adapter_peek (filter->adapter, WANTED);
431 for (i = 0; i < RATE; i++) {
432 filter->signal[i].r = (kiss_fft_scalar) (samples[i]);
435 /* flush half second of data to implement sliding window */
436 gst_adapter_flush (filter->adapter, WANTED >> 1);
438 GST_DEBUG ("perform fft");
439 kiss_fft (filter->fft_cfg, filter->signal, filter->spectrum);
441 if (filter->message) {
442 GstMessage *m = gst_pitch_message_new (filter);
443 gst_element_post_message (GST_ELEMENT (filter), m);
451 /* entry point to initialize the plug-in
452 * initialize the plug-in itself
453 * register the element factories and pad templates
454 * register the features
456 * exchange the string 'plugin' with your elemnt name
458 /* static */ gboolean
459 plugin_pitch_init (GstPlugin * plugin)
461 return gst_element_register (plugin, "pitch", GST_RANK_NONE, GST_TYPE_PITCH);
464 /* this is the structure that gstreamer looks for to register plugins
466 * exchange the strings 'plugin' and 'Template plugin' with you plugin name and
470 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
473 "Run an FFT on the audio signal, output fundamental frequency",
474 plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")