4b45ad7b0cf4097486bcbc4e2106ae9ca1008446
[platform/upstream/gst-plugins-good.git] / gst / spectrum / gstspectrum.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 <string.h>
24
25 #include "gstspectrum.h"
26
27 /* elementfactory information */
28 static GstElementDetails gst_spectrum_details = GST_ELEMENT_DETAILS (
29   "Spectrum analyzer",
30   "Filter/Analyzer/Audio",
31   "Run an FFT on the audio signal, output spectrum data",
32   "Erik Walthinsen <omega@cse.ogi.edu>"
33 );
34
35 /* Spectrum signals and args */
36 enum {
37   /* FILL ME */
38   LAST_SIGNAL
39 };
40
41 enum {
42   ARG_0,
43   ARG_WIDTH,
44 };
45
46
47 static void     gst_spectrum_base_init  (gpointer g_class);
48 static void     gst_spectrum_class_init (GstSpectrumClass *klass);
49 static void     gst_spectrum_init       (GstSpectrum *spectrum);
50
51 static void     gst_spectrum_set_property       (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
52
53 static void     gst_spectrum_chain      (GstPad *pad, GstData *_data);
54
55 #define fixed short
56 int gst_spectrum_fix_fft(fixed fr[], fixed fi[], int m, int inverse);
57 void gst_spectrum_fix_loud(fixed loud[], fixed fr[], fixed fi[], int n, int scale_shift);
58 void gst_spectrum_window(fixed fr[], int n);
59
60
61 static GstElementClass *parent_class = NULL;
62 /*static guint gst_spectrum_signals[LAST_SIGNAL] = { 0 }; */
63
64 GType
65 gst_spectrum_get_type (void)
66 {
67   static GType spectrum_type = 0;
68
69   if (!spectrum_type) {
70     static const GTypeInfo spectrum_info = {
71       sizeof(GstSpectrumClass),
72       gst_spectrum_base_init,
73       NULL,
74       (GClassInitFunc)gst_spectrum_class_init,
75       NULL,
76       NULL,
77       sizeof(GstSpectrum),
78       0,
79       (GInstanceInitFunc)gst_spectrum_init,
80     };
81     spectrum_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSpectrum", &spectrum_info, 0);
82   }
83   return spectrum_type;
84 }
85
86 static void
87 gst_spectrum_base_init (gpointer g_class)
88 {
89   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
90
91   gst_element_class_set_details (element_class, &gst_spectrum_details);
92 }
93 static void
94 gst_spectrum_class_init (GstSpectrumClass *klass)
95 {
96   GObjectClass *gobject_class;
97
98   gobject_class = (GObjectClass*)klass;
99
100   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
101
102   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_WIDTH,
103     g_param_spec_int("width","width","width",
104                      G_MININT,G_MAXINT,0,G_PARAM_WRITABLE)); /* CHECKME */
105
106   gobject_class->set_property = gst_spectrum_set_property;
107 }
108
109 static void
110 gst_spectrum_init (GstSpectrum *spectrum)
111 {
112   spectrum->sinkpad = gst_pad_new("sink",GST_PAD_SINK);
113   gst_element_add_pad(GST_ELEMENT(spectrum),spectrum->sinkpad);
114   gst_pad_set_chain_function(spectrum->sinkpad,gst_spectrum_chain);
115   spectrum->srcpad = gst_pad_new("src",GST_PAD_SRC);
116   gst_element_add_pad(GST_ELEMENT(spectrum),spectrum->srcpad);
117
118   spectrum->width = 75;
119 }
120
121 static void
122 gst_spectrum_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
123 {
124   GstSpectrum *spectrum;
125
126   /* it's not null if we got it, but it might not be ours */
127   g_return_if_fail(GST_IS_SPECTRUM(object));
128   spectrum = GST_SPECTRUM(object);
129
130   switch (prop_id) {
131     case ARG_WIDTH:
132       spectrum->width = g_value_get_int (value);
133       break;
134     default:
135       break;
136   }
137 }
138
139 static void
140 gst_spectrum_chain (GstPad *pad, GstData *_data)
141 {
142   GstBuffer *buf = GST_BUFFER (_data);
143   GstSpectrum *spectrum;
144   gint spec_base, spec_len;
145   gint16 *re, *im, *loud;
146   gint16 *samples;
147   gint step,pos,i;
148   guchar *spect;
149   GstBuffer *newbuf;
150
151   g_return_if_fail(pad != NULL);
152   g_return_if_fail(GST_IS_PAD(pad));
153   g_return_if_fail(buf != NULL);
154
155   spectrum = GST_SPECTRUM (GST_OBJECT_PARENT (pad));
156
157   samples = (gint16 *)GST_BUFFER_DATA(buf);
158
159   spec_base = 8;
160   spec_len = 1024;
161
162   im = g_malloc(spec_len * sizeof(gint16));
163   g_return_if_fail(im != NULL);
164   loud = g_malloc(spec_len * sizeof(gint16));
165   g_return_if_fail(loud != NULL);
166
167   memset(im,0,spec_len * sizeof(gint16));
168   /*if (spectrum->meta->channels == 2) { */
169     re = g_malloc(spec_len * sizeof(gint16));
170     for (i=0;i<spec_len;i++)
171       re[i] = (samples[(i*2)] + samples[(i*2)+1]) >> 1;
172   /*} else */
173   /*  re = samples; */
174   gst_spectrum_window(re,spec_len);
175   gst_spectrum_fix_fft(re,im,spec_base,FALSE);
176   gst_spectrum_fix_loud(loud,re,im,spec_len,0);
177   if (re != samples) g_free(re);
178   g_free(im);
179   step = spec_len / (spectrum->width*2);
180   spect = (guchar *)g_malloc(spectrum->width);
181   for (i=0,pos=0;i<spectrum->width;i++,pos += step) {
182     if (loud[pos] > -60)
183       spect[i] = (loud[pos] + 60) / 2;
184     else
185       spect[i] = 0;
186 /*    if (spect[i] > 15); */
187 /*      spect[i] = 15; */
188   }
189   g_free(loud);
190   gst_buffer_unref(buf);
191 /*  g_free(samples); */
192
193   newbuf = gst_buffer_new();
194   g_return_if_fail(newbuf != NULL);
195   GST_BUFFER_DATA(newbuf) = spect;
196   GST_BUFFER_SIZE(newbuf) = spectrum->width;
197
198   gst_pad_push(spectrum->srcpad,GST_DATA (newbuf));
199 }
200
201 static gboolean
202 plugin_init (GstPlugin *plugin)
203 {
204   return gst_element_register (plugin, "spectrum", GST_RANK_NONE, GST_TYPE_SPECTRUM);
205 }
206
207 GST_PLUGIN_DEFINE (
208   GST_VERSION_MAJOR,
209   GST_VERSION_MINOR,
210   "spectrum",
211   "Run an FFT on the audio signal, output spectrum data",
212   plugin_init,
213   VERSION,
214   GST_LICENSE,
215   GST_COPYRIGHT,
216   GST_PACKAGE,
217   GST_ORIGIN
218 )