Debian Codegraph @

plugins/actor/lv_analyzer/actor_lv_analyzer.c

raw
1 /* Libvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 2004, 2005, 2006 Dennis Smit <[email protected]>
4 *
5 * Authors: Dennis Smit <[email protected]>
6 *
7 * $Id: actor_lv_analyzer.c,v 1.28 2006/01/27 20:19:16 synap Exp $
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <gettext.h>
32
33 #include <libvisual/libvisual.h>
34
35 #define BARS 256
36
37 typedef struct {
38 VisPalette pal;
39 } AnalyzerPrivate;
40
41 static void draw_bar (VisVideo *video, int index, int nbars, float amplitude);
42 static inline void draw_vline (VisVideo *video, int x1, int x2, int y, uint8_t color);
43
44 int lv_analyzer_init (VisPluginData *plugin);
45 int lv_analyzer_cleanup (VisPluginData *plugin);
46 int lv_analyzer_requisition (VisPluginData *plugin, int *width, int *height);
47 int lv_analyzer_dimension (VisPluginData *plugin, VisVideo *video, int width, int height);
48 int lv_analyzer_events (VisPluginData *plugin, VisEventQueue *events);
49 VisPalette *lv_analyzer_palette (VisPluginData *plugin);
50 int lv_analyzer_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio);
51
52 VISUAL_PLUGIN_API_VERSION_VALIDATOR
53
54 const VisPluginInfo *get_plugin_info (int *count)
55 {
56 static VisActorPlugin actor[] = {{
57 .requisition = lv_analyzer_requisition,
58 .palette = lv_analyzer_palette,
59 .render = lv_analyzer_render,
60 .vidoptions.depth = VISUAL_VIDEO_DEPTH_8BIT
61 }};
62
63 static VisPluginInfo info[] = {{
64 .type = VISUAL_PLUGIN_TYPE_ACTOR,
65
66 .plugname = "lv_analyzer",
67 .name = "libvisual analyzer",
68 .author = N_("Dennis Smit <[email protected]>"),
69 .version = "1.0",
70 .about = N_("Libvisual analyzer plugin"),
71 .help = N_("A nice simple spectrum analyzer plugin."),
72 .license = VISUAL_PLUGIN_LICENSE_LGPL,
73
74 .init = lv_analyzer_init,
75 .cleanup = lv_analyzer_cleanup,
76 .events = lv_analyzer_events,
77
78 .plugin = VISUAL_OBJECT (&actor[0])
79 }};
80
81 *count = sizeof (info) / sizeof (*info);
82
83 return info;
84 }
85
86 int lv_analyzer_init (VisPluginData *plugin)
87 {
88 AnalyzerPrivate *priv;
89
90 #if ENABLE_NLS
91 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
92 #endif
93
94 priv = visual_mem_new0 (AnalyzerPrivate, 1);
95 visual_object_set_private (VISUAL_OBJECT (plugin), priv);
96
97 visual_palette_allocate_colors (&priv->pal, 256);
98
99 return 0;
100 }
101
102 int lv_analyzer_cleanup (VisPluginData *plugin)
103 {
104 AnalyzerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
105
106 visual_palette_free_colors (&priv->pal);
107
108 visual_mem_free (priv);
109
110 return 0;
111 }
112
113 int lv_analyzer_requisition (VisPluginData *plugin, int *width, int *height)
114 {
115 int reqw;
116
117 reqw = *width;
118
119 while (reqw % 2 || reqw % 4)
120 reqw--;
121
122 if (reqw < 32)
123 reqw = 32;
124
125 *width = reqw;
126
127 return 0;
128 }
129
130 int lv_analyzer_dimension (VisPluginData *plugin, VisVideo *video, int width, int height)
131 {
132 visual_video_set_dimension (video, width, height);
133
134 return 0;
135 }
136
137 int lv_analyzer_events (VisPluginData *plugin, VisEventQueue *events)
138 {
139 VisEvent ev;
140
141 while (visual_event_queue_poll (events, &ev)) {
142 switch (ev.type) {
143 case VISUAL_EVENT_RESIZE:
144 lv_analyzer_dimension (plugin, ev.event.resize.video,
145 ev.event.resize.width, ev.event.resize.height);
146 break;
147 default: /* to avoid warnings */
148 break;
149 }
150 }
151
152 return 0;
153 }
154
155 VisPalette *lv_analyzer_palette (VisPluginData *plugin)
156 {
157 AnalyzerPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
158 int i;
159
160 for (i = 0; i < 256; i++) {
161 priv->pal.colors[i].r = 0;
162 priv->pal.colors[i].g = 0;
163 priv->pal.colors[i].r = 0;
164 }
165
166 for (i = 1; i < 64; i++) {
167 priv->pal.colors[i].r = i * 4;
168 priv->pal.colors[i].g = 255;
169 priv->pal.colors[i].b = 0;
170
171 priv->pal.colors[i + 63].r = 255;
172 priv->pal.colors[i + 63].g = (63 - i) * 4;
173 priv->pal.colors[i + 63].b = 0;
174 }
175
176 return &priv->pal;
177 }
178
179 int lv_analyzer_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio)
180 {
181 VisBuffer buffer;
182 VisBuffer pcmb;
183 float freq[BARS];
184 float pcm[BARS * 2];
185 int i;
186
187 visual_video_fill_color (video, NULL);
188
189 visual_buffer_set_data_pair (&buffer, freq, sizeof (freq));
190 visual_buffer_set_data_pair (&pcmb, pcm, sizeof (pcm));
191
192 visual_audio_get_sample_mixed_simple (audio, &pcmb, 2,
193 VISUAL_AUDIO_CHANNEL_LEFT,
194 VISUAL_AUDIO_CHANNEL_RIGHT);
195
196 visual_audio_get_spectrum_for_sample (&buffer, &pcmb, TRUE);
197
198 for (i = 0; i < BARS; i++)
199 draw_bar (video, i, BARS, freq[i]);
200
201 return 0;
202 }
203
204 static void draw_bar (VisVideo *video, int index, int nbars, float amplitude)
205 {
206 int startx = (video->width / nbars) * index;
207 int endx = ((video->width / nbars) * (index + 1));
208 int height = video->height * amplitude;
209 int i;
210 float scale = 128.0 / video->height;
211
212 for (i = video->height - 1; i > (video->height - height); i--) {
213 draw_vline (video, index, index + 1, i, (video->height - i) * scale);
214 }
215 }
216
217 static inline void draw_vline (VisVideo *video, int x1, int x2, int y, uint8_t color)
218 {
219 uint8_t *pixels = visual_video_get_pixels (video);
220 int i;
221
222 if (video->depth != VISUAL_VIDEO_DEPTH_8BIT)
223 return;
224
225 pixels += (y * video->pitch) + x1;
226
227 visual_mem_set (pixels, color, x2 - x1);
228 }
229