Debian Codegraph @

plugins/actor/pseudotoad_flower/actor_flower.c

raw
1 /* Libvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 2004, 2005, 2006 Antti Silvast <[email protected]>
4 *
5 * Authors: Antti Silvast <[email protected]>
6 * Dennis Smit <[email protected]>
7 *
8 * $Id: actor_flower.c,v 1.11 2006/02/25 18:45:16 synap Exp $
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <math.h>
33 #include <gettext.h>
34
35 #ifdef HAVE_GL_GL_H
36 # include <GL/gl.h>
37 #elif defined(HAVE_OPENGL_GL_H)
38 # include <OpenGL/gl.h>
39 #else
40 # error neither GL/gl.h nor OpenGL/gl.h available
41 #endif
42
43 #if defined(HAVE_GL_GLU_H)
44 # include <GL/glu.h>
45 #elif defined(HAVE_OPENGL_GLU_H)
46 # include <OpenGL/glu.h>
47 #else
48 # error neither GL/glu.h nor OpenGL/glu.h available
49 #endif
50
51 #include <libvisual/libvisual.h>
52
53 #include "main.h"
54 #include "notch.h"
55
56 #define NOTCH_BANDS 32
57
58 typedef struct {
59 VisTimer t;
60 FlowerInternal flower;
61 int nof_bands;
62 NOTCH_FILTER *notch[NOTCH_BANDS];
63 VisRandomContext *rcxt;
64 } FlowerPrivate;
65
66 int lv_flower_init (VisPluginData *plugin);
67 int lv_flower_cleanup (VisPluginData *plugin);
68 int lv_flower_requisition (VisPluginData *plugin, int *width, int *height);
69 int lv_flower_dimension (VisPluginData *plugin, VisVideo *video, int width, int height);
70 int lv_flower_events (VisPluginData *plugin, VisEventQueue *events);
71 VisPalette *lv_flower_palette (VisPluginData *plugin);
72 int lv_flower_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio);
73
74 VISUAL_PLUGIN_API_VERSION_VALIDATOR
75
76 /* Main plugin stuff */
77 const VisPluginInfo *get_plugin_info (int *count)
78 {
79 static VisActorPlugin actor[] = {{
80 .requisition = lv_flower_requisition,
81 .palette = lv_flower_palette,
82 .render = lv_flower_render,
83 .vidoptions.depth = VISUAL_VIDEO_DEPTH_GL
84 }};
85
86 static VisPluginInfo info[] = {{
87 .type = VISUAL_PLUGIN_TYPE_ACTOR,
88
89 .plugname = "lv_flower",
90 .name = "libvisual Pseudotoad flower, yellow rose of texas",
91 .author = N_("Original by: Antti Silvast <[email protected]>, Port by: Dennis Smit <[email protected]>"),
92 .version = "0.1",
93 .about = N_("Libvisual yellow rose of texas port"),
94 .help = N_("This renders an awesome responsive flower"),
95 .license = VISUAL_PLUGIN_LICENSE_GPL,
96
97 .init = lv_flower_init,
98 .cleanup = lv_flower_cleanup,
99 .events = lv_flower_events,
100
101 .plugin = VISUAL_OBJECT (&actor[0])
102 }};
103
104 *count = sizeof (info) / sizeof (*info);
105
106 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_RED_SIZE, 5);
107 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_GREEN_SIZE, 5);
108 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_BLUE_SIZE, 5);
109 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_DEPTH_SIZE, 16);
110 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_DOUBLEBUFFER, 1);
111 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_RGBA, 1);
112
113 return info;
114 }
115
116 int lv_flower_init (VisPluginData *plugin)
117 {
118 FlowerPrivate *priv;
119 float b;
120 int i;
121
122 #if ENABLE_NLS
123 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
124 #endif
125
126 priv = visual_mem_new0 (FlowerPrivate, 1);
127 visual_object_set_private (VISUAL_OBJECT (plugin), priv);
128
129 priv->rcxt = visual_plugin_get_random_context (plugin);
130
131 visual_random_context_float (priv->rcxt);
132 init_flower (&priv->flower);
133
134 priv->flower.rotx = visual_random_context_float (priv->rcxt) * 360.0;
135 priv->flower.roty = visual_random_context_float (priv->rcxt) * 360.0;
136
137 priv->flower.tension = (visual_random_context_float (priv->rcxt) - 0.5) * 8.0;
138 priv->flower.continuity = (visual_random_context_float (priv->rcxt) - 0.5) * 16.0;
139
140
141 priv->nof_bands = NOTCH_BANDS;
142
143 for (i = 0; i < priv->nof_bands; i++) {
144 b = 80.0 + i * (22000.0 - 80.0) / priv->nof_bands;
145
146 priv->notch[i] = init_notch (b);
147 }
148
149
150 return 0;
151 }
152
153 int lv_flower_cleanup (VisPluginData *plugin)
154 {
155 FlowerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
156
157 visual_mem_free (priv);
158
159 return 0;
160 }
161
162 int lv_flower_requisition (VisPluginData *plugin, int *width, int *height)
163 {
164 int reqw, reqh;
165
166 reqw = *width;
167 reqh = *height;
168
169 if (reqw < 1)
170 reqw = 1;
171
172 if (reqh < 1)
173 reqh = 1;
174
175 *width = reqw;
176 *height = reqh;
177
178 return 0;
179 }
180
181 int lv_flower_dimension (VisPluginData *plugin, VisVideo *video, int width, int height)
182 {
183 FlowerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
184 GLfloat ratio;
185
186 visual_video_set_dimension (video, width, height);
187
188 ratio = (GLfloat) width / (GLfloat) height;
189
190 glViewport (0, 0, (GLsizei) width, (GLsizei) height);
191 glMatrixMode (GL_PROJECTION);
192 glLoadIdentity ();
193
194 gluPerspective (45.0, ratio, 0.1, 100.0);
195
196 glMatrixMode (GL_MODELVIEW);
197 glLoadIdentity ();
198
199 priv->flower.width = width;
200 priv->flower.height = height;
201
202 return 0;
203 }
204
205 int lv_flower_events (VisPluginData *plugin, VisEventQueue *events)
206 {
207 FlowerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
208 VisEvent ev;
209 VisParamEntry *param;
210
211 while (visual_event_queue_poll (events, &ev)) {
212 switch (ev.type) {
213 case VISUAL_EVENT_RESIZE:
214 lv_flower_dimension (plugin, ev.event.resize.video,
215 ev.event.resize.width, ev.event.resize.height);
216 break;
217
218 default: /* to avoid warnings */
219 break;
220 }
221 }
222
223 return 0;
224 }
225
226 VisPalette *lv_flower_palette (VisPluginData *plugin)
227 {
228 return NULL;
229 }
230
231 int lv_flower_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio)
232 {
233 FlowerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
234 VisBuffer pcmbuf;
235 VisBuffer freqbuf;
236 float pcm[512];
237 float freqnorm[256];
238 float temp_bars[NOTCH_BANDS];
239 float f;
240 int b;
241 int i;
242
243 visual_buffer_set_data_pair (&pcmbuf, pcm, sizeof (pcm));
244 visual_buffer_set_data_pair (&freqbuf, freqnorm, sizeof (freqnorm));
245
246 visual_audio_get_sample_mixed_simple (audio, &pcmbuf, 2,
247 VISUAL_AUDIO_CHANNEL_LEFT,
248 VISUAL_AUDIO_CHANNEL_RIGHT);
249
250 visual_audio_get_spectrum_for_sample (&freqbuf, &pcmbuf, TRUE);
251
252 /* Activate the effect change timer */
253 if (visual_timer_is_active (&priv->t) == FALSE)
254 visual_timer_start (&priv->t);
255
256 /* At 15 secs, do with new settings, reset timer */
257 if (visual_timer_has_passed_by_values (&priv->t, 15, 0)) {
258 priv->flower.tension_new = (-visual_random_context_float (priv->rcxt)) * 12.0;
259 priv->flower.continuity_new = (visual_random_context_float (priv->rcxt) - 0.5) * 32.0;
260
261 visual_timer_start (&priv->t);
262 }
263
264 /* Activate global timer */
265 if (visual_timer_is_active (&priv->flower.timer) == FALSE)
266 visual_timer_start (&priv->flower.timer);
267
268
269 for (b=0; b<priv->nof_bands; b++)
270 temp_bars[b]=0.0;
271
272 for (i=0; i<256; i++) {
273 for (b=0; b<priv->nof_bands; b++) {
274 f=process_notch (priv->notch[b], freqnorm[i] * 15);
275 if (fabs(f)>temp_bars[b])
276 temp_bars[b]=fabs(f);
277 }
278 }
279
280
281 /* Not part of the if !!! */
282 {
283 #define HEIGHT 1.0
284 #define D 0.45
285 #define TAU 0.25
286 #define DIF 5.0
287 float lk=2.0;
288 float l0=2.0;
289
290 float scale = HEIGHT / ( log((1 - D) / D) * 2 );
291 float x00 = D*D*1.0/(2 * D - 1);
292 float y00 = -log(-x00) * scale;
293 float y;
294 int i;
295 for (i=0; i<priv->nof_bands; i++) {
296 y=temp_bars[i];
297 y=y*(i*lk+l0);
298
299 y = ( log(y - x00) * scale + y00 ); /* Logarithmic amplitude */
300
301 y = ( (DIF-2.0)*y +
302 (i==0 ? 0 : temp_bars[i - 1]) +
303 (i==(NOTCH_BANDS - 1) ? 0 : temp_bars[i + 1])) / DIF;
304
305 y=((1.0-TAU)*priv->flower.audio_bars[i]+TAU*y) * 1.00;
306 priv->flower.audio_bars[i]=y;
307 }
308 }
309
310
311 priv->flower.roty += (priv->flower.audio_bars[15]) * 0.6;
312 priv->flower.rotx += (priv->flower.audio_bars[1]) * 0.7;
313 priv->flower.posz = +1;
314
315 render_flower_effect (&priv->flower);
316
317 return 0;
318 }
319