Debian Codegraph @

plugins/actor/nastyfft/actor_nastyfft.c

raw
1 /***************************************************************************
2 * Copyright (C) 2005 by yodor *
3 * [email protected] *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <math.h>
29
30 #ifdef HAVE_GL_GL_H
31 # include <GL/gl.h>
32 #elif defined(HAVE_OPENGL_GL_H)
33 # include <OpenGL/gl.h>
34 #else
35 # error neither GL/gl.h nor OpenGL/gl.h available
36 #endif
37
38 #if defined(HAVE_GL_GLU_H)
39 # include <GL/glu.h>
40 #elif defined(HAVE_OPENGL_GLU_H)
41 # include <OpenGL/glu.h>
42 #else
43 # error neither GL/glu.h nor OpenGL/glu.h available
44 #endif
45
46 #include <gettext.h>
47
48 #include <libvisual/libvisual.h>
49
50
51 #define NUM_BANDS 16
52 #define SCOPE_DEPTH 16
53
54 typedef struct {
55 VisTime rendertime;
56 float cam_y;
57 float rot_x;
58 float step_z;
59 float fdist_z;
60 int nw; //=640;
61 int nh; //=480;
62 float heights[SCOPE_DEPTH][NUM_BANDS];
63 float scale; //1.0
64 GLuint CYLINDER;
65 int dx;
66 int catch;
67 int dy;
68 //short pcm_data[256];
69 } NastyfftPrivate;
70
71 int lv_nastyfft_init (VisPluginData *plugin);
72 int lv_nastyfft_cleanup (VisPluginData *plugin);
73 int lv_nastyfft_requisition (VisPluginData *plugin, int *width, int *height);
74 int lv_nastyfft_events (VisPluginData *plugin, VisEventQueue *events);
75 int lv_nastyfft_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio);
76 VisPalette *lv_nastyfft_palette (VisPluginData *plugin);
77 int lv_nastyfft_dimension (VisPluginData *plugin, VisVideo *video, int width, int height);
78
79 static int nastyfft_sound (NastyfftPrivate *priv, VisAudio *audio);
80 static int nastyfft_draw (NastyfftPrivate *priv, VisVideo *video);
81
82 static void init_gl(NastyfftPrivate *priv);
83 static void draw_scene(NastyfftPrivate *priv);
84 static void make_all(NastyfftPrivate *priv);
85
86 VISUAL_PLUGIN_API_VERSION_VALIDATOR
87
88 /* Main plugin stuff */
89 const VisPluginInfo *get_plugin_info (int *count)
90 {
91 static VisActorPlugin actor[] = {{
92 .requisition = lv_nastyfft_requisition,
93 .palette = lv_nastyfft_palette,
94 .render = lv_nastyfft_render,
95 .vidoptions.depth = VISUAL_VIDEO_DEPTH_GL
96 }};
97
98 static VisPluginInfo info[] = {{
99 .type = VISUAL_PLUGIN_TYPE_ACTOR,
100
101 .plugname = N_("nastyfft"),
102 .name = N_("Libvisual NastyFFT plugin"),
103 .author = N_("yodor <[email protected]>"),
104 .version = N_("0.5"),
105 .about = N_("The Libvisual NastyFFT plugin"),
106 .help = N_("This plugin shows nasty fft visualization effect using OpenGL"),
107 .license = VISUAL_PLUGIN_LICENSE_LGPL,
108
109 .init = lv_nastyfft_init,
110 .cleanup = lv_nastyfft_cleanup,
111 .events = lv_nastyfft_events,
112
113 .plugin = VISUAL_OBJECT (&actor[0])
114 }};
115
116 *count = sizeof (info) / sizeof (*info);
117
118 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_RED_SIZE, 5);
119 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_GREEN_SIZE, 5);
120 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_BLUE_SIZE, 5);
121 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_DEPTH_SIZE, 16);
122 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_DOUBLEBUFFER, 1);
123 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_RGBA, 1);
124
125 return info;
126 }
127
128 int lv_nastyfft_init (VisPluginData *plugin)
129 {
130 NastyfftPrivate *priv;
131
132 visual_log_return_val_if_fail (plugin != NULL, -1);
133
134 priv = visual_mem_new0 (NastyfftPrivate, 1);
135
136 visual_object_set_private (VISUAL_OBJECT (plugin), priv);
137
138 #if ENABLE_NLS
139 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
140 #endif
141
142 priv->cam_y=-1.0f;
143 priv->rot_x=5.0f;
144 priv->step_z=1.0f;
145 priv->fdist_z=-63.0f;
146 priv->scale=1.0f;
147 priv->dx=0;
148 priv->dy=0;
149 priv->catch=0;
150 priv->CYLINDER=1;
151
152 int x=0;
153 int z=0;
154 for (z=SCOPE_DEPTH-1;z>=0;z--)
155 {
156 for (x=0;x<NUM_BANDS;x++)
157 {
158 priv->heights[z][x]=0.0f;
159 }
160 }
161 //init_gl(priv);
162 make_all(priv);
163
164 return 0;
165 }
166
167
168
169 int lv_nastyfft_cleanup (VisPluginData *plugin)
170 {
171 NastyfftPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
172
173 visual_log_return_val_if_fail (plugin != NULL, -1);
174
175 visual_mem_free (priv);
176
177 return 0;
178 }
179
180 int lv_nastyfft_requisition (VisPluginData *plugin, int *width, int *height)
181 {
182 int reqw, reqh;
183
184 reqw = *width;
185 reqh = *height;
186
187 if (reqw < 32)
188 reqw = 32;
189
190 if (reqh < 32)
191 reqh = 32;
192
193 *width = reqw;
194 *height = reqh;
195
196 return 0;
197 }
198
199 VisPalette *lv_nastyfft_palette (VisPluginData *plugin)
200 {
201 return NULL;
202 }
203
204 int lv_nastyfft_dimension (VisPluginData *plugin, VisVideo *video, int width, int height)
205 {
206 NastyfftPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
207
208 visual_log_return_val_if_fail (plugin != NULL, -1);
209
210 visual_log_return_val_if_fail (video != NULL, -1);
211
212 visual_video_set_dimension (video, width, height);
213
214 priv->nw = width;
215 priv->nh = height;
216
217 init_gl(priv);
218
219 return 0;
220 }
221
222 int lv_nastyfft_events (VisPluginData *plugin, VisEventQueue *events)
223 {
224 VisEvent ev;
225
226 NastyfftPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
227
228 visual_log_return_val_if_fail (plugin != NULL, -1);
229
230 while (visual_event_queue_poll (events, &ev)) {
231 switch (ev.type) {
232 case VISUAL_EVENT_RESIZE:
233 lv_nastyfft_dimension (plugin,
234 ev.event.resize.video,
235 ev.event.resize.width, ev.event.resize.height);
236 break;
237 case VISUAL_EVENT_MOUSEBUTTONUP:
238 priv->catch=0;
239 break;
240 case VISUAL_EVENT_MOUSEBUTTONDOWN:
241
242 switch (ev.event.mousebutton.button)
243 {
244 case 2:
245 priv->catch=-2;
246 break;
247 case 1:
248 priv->catch=-1;
249 break;
250 case 4:
251 priv->step_z+=(GLfloat)0.1;
252 break;
253 case 5:
254 priv->step_z-=(GLfloat)0.1;
255 break;
256 }
257 if (priv->step_z<0)
258 priv->step_z=(GLfloat)0;
259 break;
260
261 case VISUAL_EVENT_MOUSEMOTION :
262
263 if (priv->catch==-1)
264 {
265 priv->dx+=ev.event.mousemotion.yrel;
266 priv->dy+=ev.event.mousemotion.xrel;
267 if (priv->dx>360)priv->dx=0;
268 if (priv->dy>360)priv->dy=0;
269 }
270 else if (priv->catch==-2)
271 {
272 priv->fdist_z+=(GLfloat)ev.event.mousemotion.yrel;
273 }
274 break;
275 default:
276 break;
277 }
278 }
279
280 return 0;
281 }
282
283 int lv_nastyfft_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio)
284 {
285 NastyfftPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
286
287 visual_log_return_val_if_fail (plugin != NULL, -1);
288 visual_log_return_val_if_fail (video != NULL, -1);
289 visual_log_return_val_if_fail (audio != NULL, -1);
290
291 nastyfft_sound (priv, audio);
292 nastyfft_draw (priv, video);
293
294 return 0;
295 }
296
297 static int nastyfft_sound (NastyfftPrivate *priv, VisAudio *audio)
298 {
299 int i, c, y;
300 GLfloat val;
301 int xscale[] = { 0, 1, 2, 3, 5, 7, 10, 14, 20, 28, 40, 54, 74, 101, 137, 187, 255 };
302 VisBuffer buffer;
303 VisBuffer pcmb;
304
305 float freq[256];
306 unsigned short rfreq[256];
307 float pcm[256];
308
309 visual_buffer_set_data_pair (&buffer, freq, sizeof (freq));
310 visual_buffer_set_data_pair (&pcmb, pcm, sizeof (pcm));
311
312 visual_audio_get_sample_mixed_simple (audio, &pcmb, 2,
313 VISUAL_AUDIO_CHANNEL_LEFT,
314 VISUAL_AUDIO_CHANNEL_RIGHT);
315
316 visual_audio_get_spectrum_for_sample (&buffer, &pcmb, TRUE);
317
318 for (i = 0; i < 256; i++)
319 rfreq[i] = freq[i] * 320000.0;
320
321 for(y = SCOPE_DEPTH-1; y > 0; y--) {
322 for(i = 0; i < NUM_BANDS; i++)
323 priv->heights[y][i] = priv->heights[y - 1][i];
324 }
325
326 for(i = 0; i < NUM_BANDS; i++) {
327 for(c = xscale[i], y = 0; c < xscale[i + 1]; c++) {
328 if(rfreq[c] > y)
329 y = rfreq[c];
330 }
331 y >>= 7;
332 if(y > 0)
333 val = (log(y) * priv->scale);
334 else
335 val = 0;
336 priv->heights[0][i] = val;
337 }
338
339 return 0;
340 }
341
342 static int nastyfft_draw (NastyfftPrivate *priv, VisVideo *video)
343 {
344 draw_scene (priv);
345 glFinish ();
346 //
347 return 0;
348 }
349
350 static void init_gl(NastyfftPrivate *priv)
351 {
352 glViewport(0, 0, priv->nw, priv->nh);
353
354 const double defFOVY = 30.0; // field-of-view in degrees in up direction
355 double aspect = priv->nw/priv->nh;
356 // field-of-view
357 double fov = (priv->nw < priv->nh) ? defFOVY : defFOVY/aspect;
358 float nearDist = 0.1f;
359 float farDist = 500.0f;
360
361 glMatrixMode(GL_PROJECTION);
362 glLoadIdentity();
363
364 gluPerspective(fov, aspect, nearDist, farDist);
365
366 glMatrixMode(GL_MODELVIEW);
367 glLoadIdentity();
368
369
370 GLfloat ambientMaterial[] = { 1.0, 1.0, 1.0, 1.0 };
371 GLfloat difuseMaterial[] = { .5, .5, .5, 1.0 };
372 GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 1.0 };
373
374
375 glDepthFunc(GL_LEQUAL);
376 glEnable(GL_DEPTH_TEST);
377
378
379 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
380 glEnable (GL_BLEND);
381
382
383 //glShadeModel (GL_SMOOTH);
384 //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
385
386 //glMaterialfv(GL_FRONT, GL_DIFFUSE, ambientMaterial);
387 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
388
389 //glMaterialf(GL_FRONT, GL_SHININESS, 0.5);
390
391 //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
392 glEnable(GL_CULL_FACE);
393 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
394
395 glEnable(GL_COLOR_MATERIAL);
396 GLfloat light_position1[] = { 0, 10, 0, 1.0 };
397 glLightfv(GL_LIGHT0, GL_POSITION, light_position1);
398 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
399 glEnable(GL_LIGHTING);
400 glEnable(GL_LIGHT0);
401
402
403 }
404
405
406 static void make_all(NastyfftPrivate *priv)
407 {
408 //cylinder
409 GLUquadric *obj = gluNewQuadric();
410 priv->CYLINDER = glGenLists(1);
411 glNewList(priv->CYLINDER, GL_COMPILE);
412
413 glRotatef(-90.0f,1.0f,0.0f,0.0f);
414 glRotatef(-90.0f,0.0f,0.0f,1.0f);
415 gluCylinder(obj, 0.5f, 0.5f, 0.1f , 6,6);
416 glRotatef(180,1,0,0);
417 gluDisk(obj, 0, 0.5f ,6,6);
418 glRotatef(-180,1,0,0);
419 glTranslatef(0,0,0.1f);
420 gluDisk(obj, 0, 0.5f ,6,6);
421
422 glEndList();
423 gluDeleteQuadric(obj);
424 }
425
426 static void draw_scene(NastyfftPrivate *priv)
427 {
428 glClearColor( 0.13, 0.17, 0.32, 0.0);
429 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
430
431
432 glLoadIdentity();
433
434 glTranslatef(-(NUM_BANDS/2.0f)+0.5f,priv->cam_y,priv->fdist_z);
435
436 glRotatef(priv->rot_x+priv->dx,1,0,0);
437 glRotatef(priv->dy,0,1,0);
438
439 double alpha=1.0;
440 double s = 0.0;
441 double xx=0.0;
442
443 int a,z;
444
445 for (z=SCOPE_DEPTH-1;z>=0;z--)
446 {
447
448 xx=0.0;
449 alpha=1-sqrt((double)z/(double)SCOPE_DEPTH);
450
451
452 for (a=0;a<NUM_BANDS;a++)
453 {
454 s=priv->heights[z][a];
455
456 if (s>10.0f)s=10.0f;
457 if (s<0.1f)s=0.1f;
458
459 double d = (double)a/(double)NUM_BANDS;
460
461 glPushMatrix();
462 glColor4d(d,0,1-d,alpha);
463
464 glScaled(1,s*10,1);
465 glCallList(priv->CYLINDER);
466
467 glPopMatrix();
468
469 glTranslated(1,0,0);
470 xx+=1.0;
471 }
472 glTranslated(-xx,0,0);
473 glTranslated(0,0,priv->step_z);
474 }
475 }
476