f163c0b6d512c8be64cbd7617058540a3805db97
[platform/upstream/gst-plugins-good.git] / gst / cutter / gstcutter.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <gst/gst.h>
24 #include <gst/audio/audio.h>
25 #include "gstcutter.h"
26 #include "math.h"
27
28 /* elementfactory information */
29 static GstElementDetails cutter_details = {
30   "Cutter",
31   "Filter/Editor/Audio",
32   "Audio Cutter to split audio into non-silent bits",
33   "Thomas <thomas@apestaart.org>",
34 };
35
36
37 /* Filter signals and args */
38 enum {
39   /* FILL ME */
40   CUT_START,
41   CUT_STOP,
42   LAST_SIGNAL
43 };
44
45 enum {
46   ARG_0,
47   ARG_THRESHOLD,
48   ARG_THRESHOLD_DB,
49   ARG_RUN_LENGTH,
50   ARG_PRE_LENGTH,
51   ARG_LEAKY
52 };
53
54 static GstStaticPadTemplate cutter_src_factory =
55 GST_STATIC_PAD_TEMPLATE (
56   "src",
57   GST_PAD_SRC,
58   GST_PAD_ALWAYS,
59   GST_STATIC_CAPS (
60     GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
61     GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS
62   )
63 );
64
65 static GstStaticPadTemplate cutter_sink_factory =
66 GST_STATIC_PAD_TEMPLATE (
67   "sink",
68   GST_PAD_SINK,
69   GST_PAD_ALWAYS,
70   GST_STATIC_CAPS (
71     GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
72     GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS
73   )
74 );
75
76 static void     gst_cutter_base_init    (gpointer g_class);
77 static void     gst_cutter_class_init   (GstCutterClass *klass);
78 static void     gst_cutter_init         (GstCutter *filter);
79
80 static void     gst_cutter_set_property (GObject *object, guint prop_id,
81                                          const GValue *value,
82                                          GParamSpec *pspec);
83 static void     gst_cutter_get_property (GObject *object, guint prop_id,
84                                          GValue *value, GParamSpec *pspec);
85
86 static void     gst_cutter_chain        (GstPad *pad, GstData *_data);
87 static double
88 inline          gst_cutter_16bit_ms     (gint16* data, guint numsamples);
89 static double
90 inline          gst_cutter_8bit_ms      (gint8* data, guint numsamples);
91
92 void            gst_cutter_get_caps     (GstPad *pad, GstCutter* filter);
93
94 static GstElementClass *parent_class = NULL;
95 static guint gst_cutter_signals[LAST_SIGNAL] = { 0 };
96
97
98 GType
99 gst_cutter_get_type (void) {
100   static GType cutter_type = 0;
101
102   if (!cutter_type) {
103     static const GTypeInfo cutter_info = {
104       sizeof (GstCutterClass),
105       gst_cutter_base_init,
106       NULL,
107       (GClassInitFunc) gst_cutter_class_init, NULL, NULL,
108       sizeof (GstCutter), 0,
109       (GInstanceInitFunc) gst_cutter_init,
110     };
111     cutter_type = g_type_register_static (GST_TYPE_ELEMENT, "GstCutter",
112                                           &cutter_info, 0);
113   }
114   return cutter_type;
115 }
116
117 static void
118 gst_cutter_base_init (gpointer g_class)
119 {
120   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
121
122   gst_element_class_add_pad_template (element_class, gst_static_pad_template_get (&cutter_src_factory));
123   gst_element_class_add_pad_template (element_class, gst_static_pad_template_get (&cutter_sink_factory));
124   gst_element_class_set_details (element_class, &cutter_details);
125 }
126
127 static GstPadLinkReturn
128 gst_cutter_link (GstPad *pad, const GstCaps*caps)
129 {
130   GstCutter *filter;
131   GstPad *otherpad;
132
133   filter = GST_CUTTER (gst_pad_get_parent (pad));
134   g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
135   g_return_val_if_fail (GST_IS_CUTTER (filter), GST_PAD_LINK_REFUSED);
136   otherpad = (pad == filter->srcpad ? filter->sinkpad : filter->srcpad);
137
138   return gst_pad_try_set_caps (otherpad, caps);
139 }
140
141 static void
142 gst_cutter_class_init (GstCutterClass *klass)
143 {
144   GObjectClass *gobject_class;
145   GstElementClass *gstelement_class;
146
147   gobject_class = (GObjectClass*) klass;
148   gstelement_class = (GstElementClass*) klass;
149
150   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
151
152   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_THRESHOLD,
153     g_param_spec_double ("threshold", "Threshold",
154                          "Volume threshold before trigger",
155                          -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
156   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_THRESHOLD_DB,
157     g_param_spec_double ("threshold_dB", "Threshold (dB)",
158                          "Volume threshold before trigger (in dB)",
159                          -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
160   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RUN_LENGTH,
161     g_param_spec_double ("runlength", "Runlength",
162                          "Length of drop below threshold before cut_stop (seconds)",
163                         0.0, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
164   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PRE_LENGTH,
165     g_param_spec_double ("prelength", "prelength",
166                          "Length of pre-recording buffer (seconds)",
167                         0.0, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
168   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEAKY,
169     g_param_spec_boolean ("leaky", "Leaky",
170                           "do we leak buffers when below threshold ?",
171                           FALSE, G_PARAM_READWRITE));
172   gst_cutter_signals[CUT_START] =
173         g_signal_new ("cut-start", G_TYPE_FROM_CLASS (klass),
174                       G_SIGNAL_RUN_FIRST,
175                       G_STRUCT_OFFSET (GstCutterClass, cut_start), NULL, NULL,
176                       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
177   gst_cutter_signals[CUT_STOP] =
178         g_signal_new ("cut-stop", G_TYPE_FROM_CLASS (klass),
179                       G_SIGNAL_RUN_FIRST,
180                       G_STRUCT_OFFSET (GstCutterClass, cut_stop), NULL, NULL,
181                       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
182
183
184   gobject_class->set_property = gst_cutter_set_property;
185   gobject_class->get_property = gst_cutter_get_property;
186 }
187
188 static void
189 gst_cutter_init (GstCutter *filter)
190 {
191   filter->sinkpad = gst_pad_new_from_template (
192       gst_static_pad_template_get(&cutter_sink_factory),"sink");
193   filter->srcpad = gst_pad_new_from_template (
194       gst_static_pad_template_get(&cutter_src_factory),"src");
195
196   filter->threshold_level = 0.1;
197   filter->threshold_length = 0.5;
198   filter->silent_run_length = 0.0;
199   filter->silent = TRUE;
200
201   filter->pre_length = 0.2;
202   filter->pre_run_length = 0.0;
203   filter->pre_buffer = NULL;
204   filter->leaky = FALSE;
205
206   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
207   gst_pad_set_chain_function (filter->sinkpad, gst_cutter_chain);
208   gst_pad_set_link_function (filter->sinkpad, gst_cutter_link);
209
210   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
211   /*gst_pad_set_link_function (filter->srcpad, gst_cutter_link);*/
212 }
213
214 static void
215 gst_cutter_chain (GstPad *pad, GstData *_data)
216 {
217   GstBuffer *buf = GST_BUFFER (_data);
218   GstCutter *filter;
219   gint16 *in_data;
220   double RMS = 0.0;                     /* RMS of signal in buffer */
221   double ms = 0.0;                      /* mean square value of buffer */
222   static gboolean silent_prev = FALSE;  /* previous value of silent */
223   GstBuffer *prebuf;                    /* pointer to a prebuffer element */
224
225   g_return_if_fail (pad != NULL);
226   g_return_if_fail (GST_IS_PAD (pad));
227   g_return_if_fail (buf != NULL);
228
229   filter = GST_CUTTER (GST_OBJECT_PARENT (pad));
230   g_return_if_fail (filter != NULL);
231   g_return_if_fail (GST_IS_CUTTER (filter));
232
233   if (gst_audio_is_buffer_framed (pad, buf) == FALSE)
234     g_warning ("audio buffer is not framed !\n");
235
236   if (!filter->have_caps) gst_cutter_get_caps (pad, filter);
237
238   in_data = (gint16 *) GST_BUFFER_DATA (buf);
239   GST_DEBUG (
240              "length of prerec buffer: %.3f sec",
241              filter->pre_run_length);
242
243   /* calculate mean square value on buffer */
244   switch (filter->width)
245   {
246     case 16:
247       ms = gst_cutter_16bit_ms (in_data, GST_BUFFER_SIZE (buf) / 2);
248       break;
249     case 8:
250       ms = gst_cutter_8bit_ms ((gint8 *) in_data, GST_BUFFER_SIZE (buf));
251       break;
252     default:
253       /* this shouldn't happen */
254       g_print ("WARNING: no mean square function for width %d\n",
255                 filter->width);
256       break;
257   }
258
259   silent_prev = filter->silent;
260
261   RMS = sqrt (ms) / (double) filter->max_sample;
262   /* if RMS below threshold, add buffer length to silent run length count
263    * if not, reset
264    */
265   GST_DEBUG (
266              "buffer stats: ms %f, RMS %f, audio length %f",
267              ms, RMS, gst_audio_length (filter->srcpad, buf));
268   if (RMS < filter->threshold_level)
269     filter->silent_run_length += gst_audio_length (filter->srcpad, buf);
270   else
271   {
272     filter->silent_run_length = 0.0;
273     filter->silent = FALSE;
274   }
275
276   if (filter->silent_run_length > filter->threshold_length)
277     /* it has been silent long enough, flag it */
278     filter->silent = TRUE;
279
280   /* has the silent status changed ? if so, send right signal
281    * and, if from silent -> not silent, flush pre_record buffer
282    */
283   if (filter->silent != silent_prev)
284   {
285     if (filter->silent)
286     {
287 /*      g_print ("DEBUG: cutter: cut to here, turning off out\n"); */
288       g_signal_emit (G_OBJECT (filter), gst_cutter_signals[CUT_STOP], 0);
289     }
290     else
291     {
292       gint count = 0;
293 /*      g_print ("DEBUG: cutter: start from here, turning on out\n"); */
294       /* first of all, flush current buffer */
295       g_signal_emit (G_OBJECT (filter), gst_cutter_signals[CUT_START], 0);
296       GST_DEBUG (
297                  "flushing buffer of length %.3f",
298                  filter->pre_run_length);
299       while (filter->pre_buffer)
300       {
301         prebuf = (g_list_first (filter->pre_buffer))->data;
302         filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
303         gst_pad_push (filter->srcpad, GST_DATA (prebuf));
304         ++count;
305       }
306       GST_DEBUG ("flushed %d buffers", count);
307       filter->pre_run_length = 0.0;
308     }
309   }
310   /* now check if we have to send the new buffer to the internal buffer cache
311    * or to the srcpad */
312   if (filter->silent)
313   {
314       /* we ref it before putting it in the pre_buffer */
315           /* FIXME: we shouldn't probably do this, because the buffer
316            * arrives reffed already; the plugin should just push it
317            * or unref it to make it disappear */
318           /*
319       gst_buffer_ref (buf);
320       */
321       filter->pre_buffer = g_list_append (filter->pre_buffer, buf);
322       filter->pre_run_length += gst_audio_length (filter->srcpad, buf);
323       while (filter->pre_run_length > filter->pre_length)
324       {
325         prebuf = (g_list_first (filter->pre_buffer))->data;
326         g_assert (GST_IS_BUFFER (prebuf));
327         filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
328         filter->pre_run_length -= gst_audio_length (filter->srcpad, prebuf);
329         /* only pass buffers if we don't leak */
330         if (!filter->leaky)
331           gst_pad_push (filter->srcpad, GST_DATA (prebuf));
332         /* we unref it after getting it out of the pre_buffer */
333         gst_buffer_unref (prebuf);
334       }
335   }
336   else
337     gst_pad_push (filter->srcpad, GST_DATA (buf));
338 }
339
340 static double inline
341 gst_cutter_16bit_ms (gint16* data, guint num_samples)
342 #include "filter.func"
343
344 static double inline
345 gst_cutter_8bit_ms (gint8* data, guint num_samples)
346 #include "filter.func"
347
348 static void
349 gst_cutter_set_property (GObject *object, guint prop_id,
350                          const GValue *value, GParamSpec *pspec)
351 {
352   GstCutter *filter;
353
354   g_return_if_fail (GST_IS_CUTTER (object));
355   filter = GST_CUTTER (object);
356
357   switch (prop_id)
358   {
359     case ARG_THRESHOLD:
360         /* set the level */
361       filter->threshold_level = g_value_get_double (value);
362       GST_DEBUG (
363                  "DEBUG: set threshold level to %f",
364                  filter->threshold_level);
365       break;
366     case ARG_THRESHOLD_DB:
367       /* set the level given in dB
368        * value in dB = 20 * log (value)
369        * values in dB < 0 result in values between 0 and 1
370        */
371       filter->threshold_level = pow (10, g_value_get_double (value) / 20);
372       GST_DEBUG (
373                  "DEBUG: set threshold level to %f",
374                  filter->threshold_level);
375       break;
376     case ARG_RUN_LENGTH:
377       /* set the minimum length of the silent run required */
378       filter->threshold_length = g_value_get_double (value);
379       break;
380     case ARG_PRE_LENGTH:
381       /* set the length of the pre-record block */
382       filter->pre_length = g_value_get_double (value);
383       break;
384     case ARG_LEAKY:
385       /* set if the pre-record buffer is leaky or not */
386       filter->leaky = g_value_get_boolean (value);
387       break;
388     default:
389       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
390       break;
391   }
392 }
393
394 static void
395 gst_cutter_get_property (GObject *object, guint prop_id,
396                          GValue *value, GParamSpec *pspec)
397 {
398   GstCutter *filter;
399
400   g_return_if_fail (GST_IS_CUTTER (object));
401   filter = GST_CUTTER (object);
402
403   switch (prop_id)
404   {
405     case ARG_RUN_LENGTH:
406      g_value_set_double (value, filter->threshold_length);
407      break;
408     case ARG_THRESHOLD:
409      g_value_set_double (value, filter->threshold_level);
410      break;
411     case ARG_THRESHOLD_DB:
412      g_value_set_double (value, 20 * log (filter->threshold_level));
413      break;
414     case ARG_PRE_LENGTH:
415       g_value_set_double (value, filter->pre_length);
416       break;
417     case ARG_LEAKY:
418       g_value_set_boolean (value, filter->leaky);
419       break;
420     default:
421       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
422       break;
423   }
424 }
425
426 static gboolean
427 plugin_init (GstPlugin *plugin)
428 {
429     /* load audio support library */
430   if (!gst_library_load ("gstaudio"))
431     return FALSE;
432
433   if (!gst_element_register (plugin, "cutter", GST_RANK_NONE, GST_TYPE_CUTTER))
434     return FALSE;
435
436   return TRUE;
437 }
438
439 GST_PLUGIN_DEFINE (
440   GST_VERSION_MAJOR,
441   GST_VERSION_MINOR,
442   "cutter",
443   "Audio Cutter to split audio into non-silent bits",
444   plugin_init,
445   VERSION,
446   "LGPL",
447   GST_PACKAGE,
448   GST_ORIGIN)
449
450 void
451 gst_cutter_get_caps (GstPad *pad, GstCutter* filter)
452 {
453   const GstCaps *caps = NULL;
454   GstStructure *structure;
455
456   caps = GST_PAD_CAPS (pad);
457     /* FIXME : Please change this to a better warning method ! */
458   g_assert (caps != NULL);
459   if (caps == NULL)
460     printf ("WARNING: get_caps: Could not get caps of pad !\n");
461   structure = gst_caps_get_structure (caps, 0);
462   gst_structure_get_int (structure, "width", &filter->width);
463   filter->max_sample = gst_audio_highest_sample_value (pad);
464   filter->have_caps = TRUE;
465 }
466