~ubuntu-branches/ubuntu/wily/alsaplayer/wily

« back to all changes in this revision

Viewing changes to interface/gtk2/scopes.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Hubert Chathi
  • Date: 2007-10-10 15:33:10 UTC
  • mto: (9.2.5 sid)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20071010153310-h3holq75eu2cigb0
Tags: upstream-0.99.80~rc4
ImportĀ upstreamĀ versionĀ 0.99.80~rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  scopes.cpp
2
 
 *  Copyright (C) 2003 Andy Lo A Foe <andy@alsaplayer.org>
3
 
 *
4
 
 *  This program is free software; you can redistribute it and/or modify
5
 
 *  it under the terms of the GNU General Public License as published by
6
 
 *  the Free Software Foundation; either version 2 of the License, or
7
 
 *  (at your option) any later version.
8
 
 *
9
 
 *  This program 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
12
 
 *  GNU General Public License for more details.
13
 
 *
14
 
 *  You should have received a copy of the GNU General Public License
15
 
 *  along with this program; if not, write to the Free Software
16
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
 
 *
18
 
 *  $Id: scopes.cpp 1032 2004-02-15 23:02:12Z adnans $
19
 
 *
20
 
*/ 
21
 
#include "scopes.h"
22
 
#include "support.h"
23
 
#include "AlsaPlayer.h"
24
 
#include "AlsaNode.h"
25
 
#include "AlsaSubscriber.h"
26
 
#include "alsaplayer_error.h"
27
 
#include "alsaplayer_fft.h"
28
 
#include "prefs.h"
29
 
#include <pthread.h>
30
 
#include <dlfcn.h>
31
 
#include <math.h>
32
 
#include <sys/types.h>
33
 
#include <sys/stat.h>
34
 
#include <dirent.h>
35
 
#include <unistd.h>
36
 
 
37
 
static scope_entry *root_scope = NULL;
38
 
static pthread_mutex_t sl_mutex;
39
 
static AlsaSubscriber *scopes = NULL;
40
 
 
41
 
void unregister_scopes(void);
42
 
bool register_scope(scope_plugin *plugin, bool, void *);
43
 
 
44
 
void unload_scope_addons()
45
 
{
46
 
        unregister_scopes();
47
 
        if (scopes)
48
 
                delete scopes;
49
 
}
50
 
 
51
 
 
52
 
void load_scope_addons()
53
 
{
54
 
        char path[1024];
55
 
        struct stat buf;
56
 
        scope_plugin *tmp;
57
 
        scope_plugin_info_type scope_plugin_info;
58
 
 
59
 
        snprintf(path, sizeof(path)-1, "%s/scopes2", global_pluginroot);
60
 
 
61
 
        DIR *dir = opendir(path);
62
 
        dirent *entry;
63
 
 
64
 
        if (dir) {
65
 
                while ((entry = readdir(dir)) != NULL) { // For each file in scopes
66
 
                        if (strcmp(entry->d_name, ".") == 0 ||
67
 
                                strcmp(entry->d_name, "..") == 0) {
68
 
                                continue;
69
 
                        }
70
 
                        sprintf(path, "%s/scopes2/%s", global_pluginroot, entry->d_name);
71
 
                        if (stat(path, &buf)) continue;
72
 
                        if (S_ISREG(buf.st_mode)) {
73
 
                                void *handle;
74
 
 
75
 
                                char *ext = strrchr(path, '.');
76
 
                                if (!ext)
77
 
                                        continue;
78
 
                                ext++;
79
 
                                if (strcasecmp(ext, "so"))
80
 
                                        continue;
81
 
                                if ((handle = dlopen(path, RTLD_NOW |RTLD_GLOBAL))) {
82
 
                                        scope_plugin_info = (scope_plugin_info_type) dlsym(handle, "scope_plugin_info");
83
 
                                        if (scope_plugin_info) { 
84
 
                                                alsaplayer_error("Loading scope addon: %s", path);
85
 
                                                tmp = scope_plugin_info();
86
 
                                                if (tmp) {
87
 
                                                                tmp->handle = handle;
88
 
                                                                if (!register_scope(tmp, false, NULL)) {
89
 
                                                                        alsaplayer_error("%s is deprecated", path);
90
 
                                                                }
91
 
                                                }               
92
 
                                        } else {
93
 
                                                dlclose(handle);
94
 
                                        }
95
 
                                } else {
96
 
                                        printf("%s\n", dlerror());
97
 
                                }
98
 
                        }
99
 
                }
100
 
                closedir(dir);
101
 
        }       
102
 
}
103
 
 
104
 
 
105
 
void dl_close_scopes()
106
 
{
107
 
        scope_entry *current = root_scope;
108
 
 
109
 
        while (current) {
110
 
                if (current->sp) {
111
 
                        dlclose(current->sp->handle);
112
 
                }       
113
 
                current = current->next;
114
 
        }       
115
 
}
116
 
 
117
 
void scope_entry_destroy_notify(gpointer)
118
 
{
119
 
}
120
 
 
121
 
#define SCOPE_BUFFER 2048
122
 
 
123
 
bool  scope_feeder_func(void *arg, void *data, int size) 
124
 
{
125
 
        static char buf[32768];
126
 
        static int latency = -1;
127
 
        static int fft_buf[512];
128
 
        static int fill = 0;
129
 
        static int left = 0;
130
 
        static int init = 0;
131
 
        static int buf_size = 0;
132
 
        static AlsaNode *the_node = NULL;
133
 
        int i;
134
 
        short *sound;
135
 
        int *left_pos;
136
 
        int *right_pos;
137
 
 
138
 
        static double fftmult[FFT_BUFFER_SIZE / 2 + 1];
139
 
 
140
 
        static sound_sample left_actEq[SCOPE_BUFFER];
141
 
        static double left_fftout[FFT_BUFFER_SIZE / 2 + 1];
142
 
        static fft_state *left_fftstate;
143
 
 
144
 
        static sound_sample right_actEq[SCOPE_BUFFER];
145
 
        static double right_fftout[FFT_BUFFER_SIZE / 2 + 1];
146
 
        static fft_state *right_fftstate;
147
 
 
148
 
        sound_sample *left_newset;
149
 
        sound_sample *right_newset;
150
 
 
151
 
        //alsaplayer_error("In scope_feeder_func()");
152
 
        
153
 
        if (size > 32768) 
154
 
                return true;
155
 
        if (!init) {
156
 
                for(i = 0; i <= FFT_BUFFER_SIZE / 2 + 1; i++) {
157
 
                        double mult = (double)128 / ((FFT_BUFFER_SIZE * 16384) ^ 2);
158
 
                        mult *= log(i + 1) / log(2);
159
 
                        mult *= 3;
160
 
                        fftmult[i] = mult;
161
 
                }
162
 
                right_fftstate = fft_init();
163
 
                left_fftstate = fft_init();
164
 
                if (!left_fftstate || !right_fftstate)
165
 
                        alsaplayer_error("WARNING: could not do fft_init()");
166
 
                buf_size = SCOPE_BUFFER <= (FFT_BUFFER_SIZE * 2) ? SCOPE_BUFFER : FFT_BUFFER_SIZE;
167
 
                
168
 
                the_node = (AlsaNode *)arg;
169
 
                if (the_node) {
170
 
                        latency = the_node->GetLatency();
171
 
                }
172
 
                if (latency < SCOPE_BUFFER)
173
 
                        latency = SCOPE_BUFFER;
174
 
                //init_effects();
175
 
                
176
 
                init = 1;       
177
 
        }       
178
 
 
179
 
        scope_entry *se = root_scope;
180
 
 
181
 
        //buffer_effect(data, size);
182
 
 
183
 
        if (fill + size >= SCOPE_BUFFER) {
184
 
                left = SCOPE_BUFFER - fill;
185
 
                memcpy(buf + fill, data, left);
186
 
 
187
 
                // Do global FFT
188
 
                left_newset = left_actEq;
189
 
                right_newset = right_actEq;
190
 
                
191
 
                sound = (short *)buf;
192
 
                //sound = (short *)delay_feed(latency, SCOPE_BUFFER);
193
 
                
194
 
                for (i = 0; i < buf_size; i++) {
195
 
                        *left_newset++ = (sound_sample)((int)(*sound));
196
 
                        *right_newset++ = (sound_sample)((int)(*(sound+1)));
197
 
                        sound +=2;
198
 
                }
199
 
                
200
 
                fft_perform(right_actEq, right_fftout, right_fftstate);
201
 
                fft_perform(left_actEq, left_fftout, left_fftstate);
202
 
                        
203
 
                for (i = 0, left_pos = fft_buf, right_pos = fft_buf + 256; i < 256; i++) {
204
 
                        left_pos[i] = (int)(sqrt(left_fftout[i + 1])) >> 8; //* fftmult[i]);
205
 
                        right_pos[i] = (int)(sqrt(right_fftout[i + 1])) >> 8; //* fftmult[i]);
206
 
                }       
207
 
                while (se && se->sp && se->active) {
208
 
                        if (se->sp->running()) {
209
 
                                if (se->sp->set_data)
210
 
                                        se->sp->set_data((short *)buf, SCOPE_BUFFER >> 1);
211
 
                                if (se->sp->set_fft)
212
 
                                        se->sp->set_fft((int *)fft_buf, 256, 2);
213
 
                        }       
214
 
                        if (se->next) 
215
 
                                se = se->next;
216
 
                        else 
217
 
                                break;
218
 
                }
219
 
                // Copy the remainder
220
 
                fill = 0;
221
 
                memcpy(buf + fill, ((char *)data) + left, size - left);
222
 
        } else {
223
 
                memcpy(buf + fill, data, size);
224
 
                fill += size;
225
 
        }       
226
 
        return true;
227
 
}
228
 
 
229
 
 
230
 
void unregister_scopes(void)
231
 
{
232
 
        scope_entry *current = root_scope;
233
 
        
234
 
        pthread_mutex_lock(&sl_mutex);
235
 
        while (current && current->sp) {
236
 
                //printf("closing and unloading scope plugin %s\n", current->sp->name);
237
 
                current->active = 0;
238
 
                current->sp->shutdown();
239
 
                current = current->next;
240
 
        }
241
 
        pthread_mutex_unlock(&sl_mutex);
242
 
}       
243
 
 
244
 
 
245
 
bool register_scope(scope_plugin *plugin, bool run, void *arg)
246
 
{
247
 
        scope_entry *se;
248
 
        
249
 
        se = new scope_entry;
250
 
        se->next = (scope_entry *)NULL;
251
 
        se->sp = plugin;
252
 
        if (se->sp->version != SCOPE_PLUGIN_VERSION) {
253
 
                alsaplayer_error("Wrong version number on scope plugin (v%d, wanted v%d)",
254
 
                        se->sp->version - 0x1000,
255
 
                        SCOPE_PLUGIN_VERSION - 0x1000);
256
 
                delete se;
257
 
                return false;
258
 
        }               
259
 
        se->active = 0;
260
 
 
261
 
        // Init scope
262
 
        if (!se->sp->init(arg)) {
263
 
                delete se;
264
 
                return false;
265
 
        }       
266
 
        // Add scope to scope list
267
 
        // NOTE: WE CURRENTLY NEVER UNLOAD SCOPES
268
 
        pthread_mutex_lock(&sl_mutex);  
269
 
        if (root_scope == NULL) {
270
 
                //alsaplayer_error("registering first scope...");
271
 
                root_scope = se;
272
 
                root_scope->next = (scope_entry *)NULL;
273
 
                root_scope->active = 1;
274
 
        } else { // Not root scope, so insert it at the start
275
 
                se->next = root_scope->next;
276
 
                se->active = 1;
277
 
                root_scope->next = se;
278
 
        }
279
 
        pthread_mutex_unlock(&sl_mutex);
280
 
        if (run)
281
 
                se->sp->start();
282
 
        return true;
283
 
}
284
 
 
285
 
 
286
 
void scopes_list_click(GtkWidget *widget, gint row, gint /* column */,
287
 
        GdkEvent *bevent, gpointer /* data */)
288
 
{
289
 
        if (bevent && bevent->type == GDK_2BUTTON_PRESS) {
290
 
                scope_entry *se = (scope_entry *)
291
 
                        gtk_clist_get_row_data(GTK_CLIST(widget), row);
292
 
                if (se && se->sp) {
293
 
#ifdef STUPID_FLUFF             
294
 
                        if (se->active)
295
 
                                se->sp->stop();
296
 
                        else
297
 
                                se->sp->start(NULL);
298
 
                        se->active = 1 - se->active;
299
 
                        if (se->active) {
300
 
                                gtk_clist_set_pixmap(GTK_CLIST(widget),
301
 
                                        row, 0, active_pix, active_mask);
302
 
                        } else {
303
 
                                gtk_clist_set_text(GTK_CLIST(widget),
304
 
                                        row, 0, "");    
305
 
                        }
306
 
#else
307
 
                        se->sp->start();
308
 
#endif                  
309
 
                }
310
 
        }
311
 
}
312
 
 
313
 
 
314
 
bool init_scopes(AlsaNode *node)
315
 
{
316
 
        // Init scope list
317
 
        pthread_mutex_init(&sl_mutex, (pthread_mutexattr_t *)NULL);
318
 
        // Load the scopes
319
 
        load_scope_addons();
320
 
        // Subscriber
321
 
        if (!node) {
322
 
                alsaplayer_error("Huh, no node?");
323
 
                return false;
324
 
        }       
325
 
        scopes = new AlsaSubscriber();
326
 
        scopes->Subscribe(node, POS_END);
327
 
        scopes->EnterStream(scope_feeder_func, node);
328
 
        return true;
329
 
}