comparison Plugins/Visualization/paranormal/pn/pnaudiodata.c @ 1507:0c5fdcf3f947 trunk

[svn] - incomplete stuff
author nenolod
date Sun, 06 Aug 2006 01:53:29 -0700
parents
children
comparison
equal deleted inserted replaced
1506:2a8e193c07a6 1507:0c5fdcf3f947
1 /* Paranormal - A highly customizable audio visualization library
2 * Copyright (C) 2001 Jamie Gennis <jgennis@mindspring.com>
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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <math.h>
20
21 #include <config.h>
22 #include "pnaudiodata.h"
23
24 /* Initialization */
25 static void pn_audio_data_class_init (PnAudioDataClass *class);
26 static void pn_audio_data_init (PnAudioData *audio_data,
27 PnAudioDataClass *class);
28
29 static GObjectClass *parent_class = NULL;
30
31 GType
32 pn_audio_data_get_type (void)
33 {
34 static GType audio_data_type = 0;
35
36 if (! audio_data_type)
37 {
38 static const GTypeInfo audio_data_info =
39 {
40 sizeof (PnAudioDataClass),
41 NULL, /* base_init */
42 NULL, /* base_finalize */
43 (GClassInitFunc) pn_audio_data_class_init,
44 NULL, /* class_finalize */
45 NULL, /* class_data */
46 sizeof (PnAudioData),
47 0, /* n_preallocs */
48 (GInstanceInitFunc) pn_audio_data_init
49 };
50
51 /* FIXME: should this be dynamic? */
52 audio_data_type = g_type_register_static (PN_TYPE_OBJECT,
53 "PnAudioData",
54 &audio_data_info,
55 0);
56 }
57 return audio_data_type;
58 }
59
60 static void
61 pn_audio_data_class_init (PnAudioDataClass *class)
62 {
63 GObjectClass *gobject_class;
64 PnObjectClass *object_class;
65
66 gobject_class = (GObjectClass *) class;
67 object_class = (PnObjectClass *) class;
68
69 parent_class = g_type_class_peek_parent (class);
70 }
71
72 static void
73 pn_audio_data_init (PnAudioData *audio_data, PnAudioDataClass *class)
74 {
75 audio_data->stereo = TRUE;
76
77 /* Initialize with a 1-sample and 1-band pcm_ and freq_data, respecitvely */
78 audio_data->pcm_samples = 1;
79 audio_data->pcm_data[0] = g_new0 (gfloat, 1);
80 audio_data->pcm_data[1] = g_new0 (gfloat, 1);
81 audio_data->pcm_data[2] = g_new0 (gfloat, 1);
82
83 audio_data->freq_bands = 1;
84 audio_data->freq_data[0] = g_new0 (gfloat, 1);
85 audio_data->freq_data[1] = g_new0 (gfloat, 1);
86 audio_data->freq_data[2] = g_new0 (gfloat, 1);
87 }
88
89 /**
90 * pn_audio_data_new
91 *
92 * Creates a new #PnAudioData object.
93 *
94 * Returns: The new #PnAudioData object
95 */
96 PnAudioData*
97 pn_audio_data_new (void)
98 {
99 return (PnAudioData *) g_object_new (PN_TYPE_AUDIO_DATA, NULL);
100 }
101
102 /**
103 * pn_audio_data_set_stereo
104 * @audio_data: a #PnAudioData
105 * @stereo: TRUE or FALSE
106 *
107 * Sets whether @audio_data will use stereo audio data. If
108 * stereo is %FALSE, each channel buffer points to the same memory
109 * location; otherwise they are separate buffers.
110 */
111 void
112 pn_audio_data_set_stereo (PnAudioData *audio_data, gboolean stereo)
113 {
114 gboolean changed;
115
116 g_return_if_fail (audio_data != NULL);
117 g_return_if_fail (PN_IS_AUDIO_DATA (audio_data));
118
119 changed = audio_data->stereo == stereo ? TRUE : FALSE;
120 audio_data->stereo = stereo;
121
122 if (changed)
123 {
124 pn_audio_data_set_pcm_samples (audio_data, audio_data->pcm_samples);
125 pn_audio_data_set_freq_bands (audio_data, audio_data->freq_bands);
126 }
127 }
128
129 /**
130 * pn_audio_data_get_stereo
131 * @audio_data: a #PnAudioData
132 *
133 * Retrieves the whether or not @audio_data contains stereo audio data
134 *
135 * Returns: %TRUE if it contains stereo data, %FALSE otherwise
136 */
137 gboolean
138 pn_audio_data_get_stereo (PnAudioData *audio_data)
139 {
140 g_return_val_if_fail (audio_data != NULL, FALSE);
141 g_return_val_if_fail (PN_IS_AUDIO_DATA (audio_data), FALSE);
142
143 return audio_data->stereo;
144 }
145
146 /**
147 * pn_audio_data_set_pcm_samples
148 * @audio_data: a #PnAudioData
149 * @samples: the number of samples
150 *
151 * Sets the number of samples that @audio_data's pcm data
152 * contains.
153 */
154 void
155 pn_audio_data_set_pcm_samples (PnAudioData *audio_data, guint samples)
156 {
157 g_return_if_fail (audio_data != NULL);
158 g_return_if_fail (PN_IS_AUDIO_DATA (audio_data));
159 g_return_if_fail (samples > 0);
160
161 if (audio_data->pcm_data[0])
162 {
163 g_free (audio_data->pcm_data[0]);
164 if (audio_data->stereo)
165 {
166 g_free (audio_data->pcm_data[1]);
167 g_free (audio_data->pcm_data[2]);
168 }
169 }
170
171 audio_data->pcm_samples = samples;
172 audio_data->pcm_data[0] = g_new0 (gfloat, samples);
173 if (audio_data->stereo)
174 {
175 audio_data->pcm_data[1] = g_new0 (gfloat, samples);
176 audio_data->pcm_data[2] = g_new0 (gfloat, samples);
177 }
178 else
179 audio_data->pcm_data[1] = audio_data->pcm_data[2] = audio_data->pcm_data[0];
180 }
181
182 /**
183 * pn_audio_data_get_pcm_samples
184 * @audio_data: a #PnAudioData
185 *
186 * Retrieves the number of samples that @audio_data's pcm data
187 * contains
188 *
189 * Returns: The number of samples
190 */
191 guint
192 pn_audio_data_get_pcm_samples (PnAudioData *audio_data)
193 {
194 g_return_val_if_fail (audio_data != NULL, 0);
195 g_return_val_if_fail (PN_IS_AUDIO_DATA (audio_data), 0);
196
197 return audio_data->pcm_samples;
198 }
199
200 /**
201 * pn_audio_data_set_freq_bands
202 * @audio_data: a #PnAudioData
203 * @bands: the number of bands
204 *
205 * Sets the number of bands that @audio_data's frequency data
206 * contains.
207 */
208 void
209 pn_audio_data_set_freq_bands (PnAudioData *audio_data, guint bands)
210 {
211 g_return_if_fail (audio_data != NULL);
212 g_return_if_fail (PN_IS_AUDIO_DATA (audio_data));
213 g_return_if_fail (bands > 0);
214
215
216 if (audio_data->freq_data[0])
217 {
218 g_free (audio_data->freq_data[0]);
219 if (audio_data->stereo)
220 {
221 g_free (audio_data->freq_data[1]);
222 g_free (audio_data->freq_data[2]);
223 }
224 }
225
226 audio_data->freq_bands = bands;
227 audio_data->freq_data[0] = g_new0 (gfloat, bands);
228 if (audio_data->stereo)
229 {
230 audio_data->freq_data[1] = g_new0 (gfloat, bands);
231 audio_data->freq_data[2] = g_new0 (gfloat, bands);
232 }
233 else
234 audio_data->freq_data[1] = audio_data->freq_data[2] = audio_data->freq_data[0];
235 }
236
237 /**
238 * pn_audio_data_get_freq_bands
239 * @audio_data: a #PnAudioData
240 *
241 * Retrieves the number of bands that @audio_data's frequency data
242 * contains
243 *
244 * Returns: The number of bands
245 */
246 guint
247 pn_audio_data_get_freq_bands (PnAudioData *audio_data)
248 {
249 g_return_val_if_fail (audio_data != NULL, 0);
250 g_return_val_if_fail (PN_IS_AUDIO_DATA (audio_data), 0);
251
252 return audio_data->freq_bands;
253 }
254
255 /**
256 * pn_audio_data_get_volume
257 * @audio_data: a #PnAudioData
258 *
259 * Retrieves the volume level (from 0.0 to 1.0) of the audio frame
260 * contained within a #PnAudioData object.
261 *
262 * Returns: The volume level
263 */
264 gfloat
265 pn_audio_data_get_volume (PnAudioData *audio_data)
266 {
267 g_return_val_if_fail (audio_data != NULL, 0.0);
268 g_return_val_if_fail (PN_IS_AUDIO_DATA (audio_data), 0.0);
269
270 return audio_data->volume;
271 }
272
273 /**
274 * pn_audio_data_update
275 * @audio_data: a #PnAudioData
276 *
277 * Updates the information about the audio data frame in a #PnAudioData.
278 * This function should be called after all updates to pcm_data or freq_data.
279 */
280 void
281 pn_audio_data_update (PnAudioData *audio_data)
282 {
283 guint i;
284
285 g_return_if_fail (audio_data != NULL);
286 g_return_if_fail (PN_IS_AUDIO_DATA (audio_data));
287
288 /* Get the volume */
289 audio_data->volume = 0.0;
290 for (i=0; i<audio_data->pcm_samples; i++)
291 audio_data->volume = MAX (audio_data->volume, fabs (audio_data->pcm_data[PN_CHANNEL_LEFT][i]));
292 }