2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
25 #include "gstspectrum.h"
27 /* elementfactory information */
28 static GstElementDetails gst_spectrum_details = GST_ELEMENT_DETAILS (
30 "Filter/Analyzer/Audio",
31 "Run an FFT on the audio signal, output spectrum data",
32 "Erik Walthinsen <omega@cse.ogi.edu>"
35 /* Spectrum signals and args */
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);
51 static void gst_spectrum_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
53 static void gst_spectrum_chain (GstPad *pad, GstData *_data);
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);
61 static GstElementClass *parent_class = NULL;
62 /*static guint gst_spectrum_signals[LAST_SIGNAL] = { 0 }; */
65 gst_spectrum_get_type (void)
67 static GType spectrum_type = 0;
70 static const GTypeInfo spectrum_info = {
71 sizeof(GstSpectrumClass),
72 gst_spectrum_base_init,
74 (GClassInitFunc)gst_spectrum_class_init,
79 (GInstanceInitFunc)gst_spectrum_init,
81 spectrum_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSpectrum", &spectrum_info, 0);
87 gst_spectrum_base_init (gpointer g_class)
89 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
91 gst_element_class_set_details (element_class, &gst_spectrum_details);
94 gst_spectrum_class_init (GstSpectrumClass *klass)
96 GObjectClass *gobject_class;
98 gobject_class = (GObjectClass*)klass;
100 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
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 */
106 gobject_class->set_property = gst_spectrum_set_property;
110 gst_spectrum_init (GstSpectrum *spectrum)
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);
118 spectrum->width = 75;
122 gst_spectrum_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
124 GstSpectrum *spectrum;
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);
132 spectrum->width = g_value_get_int (value);
140 gst_spectrum_chain (GstPad *pad, GstData *_data)
142 GstBuffer *buf = GST_BUFFER (_data);
143 GstSpectrum *spectrum;
144 gint spec_base, spec_len;
145 gint16 *re, *im, *loud;
151 g_return_if_fail(pad != NULL);
152 g_return_if_fail(GST_IS_PAD(pad));
153 g_return_if_fail(buf != NULL);
155 spectrum = GST_SPECTRUM (GST_OBJECT_PARENT (pad));
157 samples = (gint16 *)GST_BUFFER_DATA(buf);
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);
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;
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);
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) {
183 spect[i] = (loud[pos] + 60) / 2;
186 /* if (spect[i] > 15); */
190 gst_buffer_unref(buf);
191 /* g_free(samples); */
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;
198 gst_pad_push(spectrum->srcpad,GST_DATA (newbuf));
202 plugin_init (GstPlugin *plugin)
204 return gst_element_register (plugin, "spectrum", GST_RANK_NONE, GST_TYPE_SPECTRUM);
211 "Run an FFT on the audio signal, output spectrum data",