Mercurial > audlegacy-plugins
comparison src/madplug/plugin.c @ 610:862190d39e00 trunk
[svn] - add madplug. It is not yet hooked up, I'll do that later.
author | nenolod |
---|---|
date | Mon, 05 Feb 2007 12:28:01 -0800 |
parents | |
children | 3f7a52adfe0e |
comparison
equal
deleted
inserted
replaced
609:9b73eb35f4ff | 610:862190d39e00 |
---|---|
1 /* | |
2 * mad plugin for audacious | |
3 * Copyright (C) 2005-2007 William Pitcock, Yoshiki Yazawa | |
4 * | |
5 * Portions derived from xmms-mad: | |
6 * Copyright (C) 2001-2002 Sam Clegg - See COPYING | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; under version 2 of the License. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 */ | |
21 | |
22 #include "config.h" | |
23 #include "plugin.h" | |
24 #include "input.h" | |
25 | |
26 #include <math.h> | |
27 | |
28 #include <gtk/gtk.h> | |
29 #include <audacious/util.h> | |
30 #include <audacious/configdb.h> | |
31 #include <stdarg.h> | |
32 #include <fcntl.h> | |
33 #include <audacious/vfs.h> | |
34 #include <sys/stat.h> | |
35 | |
36 /* | |
37 * Global variables | |
38 */ | |
39 struct audmad_config_t audmad_config; /**< global configuration */ | |
40 static GStaticMutex mutex; | |
41 InputPlugin *mad_plugin = NULL; | |
42 | |
43 /* | |
44 * static variables | |
45 */ | |
46 static GThread *decode_thread; /**< the single decoder thread */ | |
47 static struct mad_info_t info; /**< info for current track */ | |
48 | |
49 #ifndef NOGUI | |
50 static GtkWidget *error_dialog = 0; | |
51 #endif | |
52 | |
53 extern gboolean scan_file(struct mad_info_t *info, gboolean fast); | |
54 | |
55 /* | |
56 * Function extname (filename) | |
57 * | |
58 * Return pointer within filename to its extenstion, or NULL if | |
59 * filename has no extension. | |
60 * | |
61 */ | |
62 static gchar *extname(const char *filename) | |
63 { | |
64 gchar *ext = strrchr(filename, '.'); | |
65 | |
66 if (ext != NULL) | |
67 ++ext; | |
68 | |
69 return ext; | |
70 } | |
71 | |
72 void audmad_config_compute(struct audmad_config_t *config) | |
73 { | |
74 /* set some config parameters by parsing text fields | |
75 (RG default gain, etc..) | |
76 */ | |
77 const gchar *text; | |
78 gdouble x; | |
79 | |
80 text = config->pregain_db; | |
81 x = g_strtod(text, NULL); | |
82 config->pregain_scale = (x != 0) ? pow(10.0, x / 20) : 1; | |
83 #ifdef DEBUG | |
84 g_message("pregain=[%s] -> %g -> %g", text, x, config->pregain_scale); | |
85 #endif | |
86 text = config->replaygain.default_db; | |
87 x = g_strtod(text, NULL); | |
88 config->replaygain.default_scale = (x != 0) ? pow(10.0, x / 20) : 1; | |
89 #ifdef DEBUG | |
90 g_message("RG.default=[%s] -> %g -> %g", text, x, | |
91 config->replaygain.default_scale); | |
92 #endif | |
93 } | |
94 | |
95 static void audmad_init() | |
96 { | |
97 ConfigDb *db = NULL; | |
98 | |
99 audmad_config.fast_play_time_calc = TRUE; | |
100 audmad_config.use_xing = TRUE; | |
101 audmad_config.dither = TRUE; | |
102 audmad_config.pregain_db = "+0.00"; | |
103 audmad_config.replaygain.enable = TRUE; | |
104 audmad_config.replaygain.track_mode = FALSE; | |
105 audmad_config.hard_limit = FALSE; | |
106 audmad_config.replaygain.default_db = "-9.00"; | |
107 | |
108 db = bmp_cfg_db_open(); | |
109 if (db) { | |
110 bmp_cfg_db_get_bool(db, "MAD", "fast_play_time_calc", | |
111 &audmad_config.fast_play_time_calc); | |
112 bmp_cfg_db_get_bool(db, "MAD", "use_xing", | |
113 &audmad_config.use_xing); | |
114 bmp_cfg_db_get_bool(db, "MAD", "dither", &audmad_config.dither); | |
115 bmp_cfg_db_get_bool(db, "MAD", "hard_limit", | |
116 &audmad_config.hard_limit); | |
117 bmp_cfg_db_get_string(db, "MAD", "pregain_db", | |
118 &audmad_config.pregain_db); | |
119 bmp_cfg_db_get_bool(db, "MAD", "RG.enable", | |
120 &audmad_config.replaygain.enable); | |
121 bmp_cfg_db_get_bool(db, "MAD", "RG.track_mode", | |
122 &audmad_config.replaygain.track_mode); | |
123 bmp_cfg_db_get_string(db, "MAD", "RG.default_db", | |
124 &audmad_config.replaygain.default_db); | |
125 bmp_cfg_db_get_bool(db, "MAD", "title_override", | |
126 &audmad_config.title_override); | |
127 bmp_cfg_db_get_string(db, "MAD", "id3_format", | |
128 &audmad_config.id3_format); | |
129 | |
130 bmp_cfg_db_close(db); | |
131 } | |
132 | |
133 g_static_mutex_init(&mutex); | |
134 audmad_config_compute(&audmad_config); | |
135 | |
136 } | |
137 | |
138 static void audmad_cleanup() | |
139 { | |
140 } | |
141 | |
142 static gboolean mp3_head_check(guint32 head) | |
143 { | |
144 /* | |
145 * First two bytes must be a sync header (11 bits all 1) | |
146 * http://www.mp3-tech.org/programmer/frame_header.html | |
147 */ | |
148 if ((head & 0xffe00000) != 0xffe00000) | |
149 return FALSE; | |
150 | |
151 /* check if bits 18 and 19 are set */ | |
152 if (!((head >> 17) & 3)) | |
153 return FALSE; | |
154 | |
155 /* check if bits 13 - 16 are all set */ | |
156 if (((head >> 12) & 0xf) == 0xf) | |
157 return FALSE; | |
158 | |
159 /* check if bits 13 - 16 are all not set */ | |
160 if (!((head >> 12) & 0xf)) | |
161 return FALSE; | |
162 | |
163 /* check if bit 11 and 12 are both set */ | |
164 if (((head >> 10) & 0x3) == 0x3) | |
165 return FALSE; | |
166 | |
167 /* check if bits 17 - 20 are all set */ | |
168 if (((head >> 19) & 1) == 1 && | |
169 ((head >> 17) & 3) == 3 && ((head >> 16) & 1) == 1) | |
170 return FALSE; | |
171 | |
172 /* not sure why we check this, but ok! */ | |
173 if ((head & 0xffff0000) == 0xfffe0000) | |
174 return FALSE; | |
175 | |
176 return TRUE; | |
177 } | |
178 | |
179 static int mp3_head_convert(const guchar * hbuf) | |
180 { | |
181 return ((unsigned long) hbuf[0] << 24) | | |
182 ((unsigned long) hbuf[1] << 16) | | |
183 ((unsigned long) hbuf[2] << 8) | (unsigned long) hbuf[3]; | |
184 } | |
185 | |
186 // audacious vfs fast version | |
187 static int audmad_is_our_fd(char *filename, VFSFile *fin) | |
188 { | |
189 guint32 check; | |
190 gchar *ext = extname(filename); | |
191 gint cyc = 0; | |
192 guchar buf[4]; | |
193 guchar tmp[4096]; | |
194 gint ret, i; | |
195 | |
196 /* I've seen some flac files beginning with id3 frames.. | |
197 so let's exclude known non-mp3 filename extensions */ | |
198 if (!strcasecmp(".flac", ext) || !strcasecmp(".mpc", ext) || | |
199 !strcasecmp(".tta", ext)) | |
200 return 0; | |
201 | |
202 if (fin == NULL) | |
203 return FALSE; | |
204 | |
205 vfs_fread(buf, 1, 4, fin); | |
206 | |
207 check = mp3_head_convert(buf); | |
208 | |
209 if (memcmp(buf, "ID3", 3) == 0) | |
210 return 1; | |
211 else if (memcmp(buf, "RIFF", 4) == 0) | |
212 { | |
213 vfs_fseek(fin, 4, SEEK_CUR); | |
214 vfs_fread(buf, 1, 4, fin); | |
215 | |
216 if (memcmp(buf, "RMP3", 4) == 0) | |
217 return 1; | |
218 } | |
219 | |
220 while (!mp3_head_check(check)) | |
221 { | |
222 ret = vfs_fread(tmp, 1, 4096, fin); | |
223 if (ret == 0) | |
224 return 0; | |
225 | |
226 for (i = 0; i < ret; i++) | |
227 { | |
228 check <<= 8; | |
229 check |= tmp[i]; | |
230 | |
231 if (mp3_head_check(check)) | |
232 return 1; | |
233 } | |
234 | |
235 if (++cyc > 1024) | |
236 return 0; | |
237 } | |
238 | |
239 return 1; | |
240 } | |
241 | |
242 // audacious vfs version | |
243 static int audmad_is_our_file(char *filename) | |
244 { | |
245 VFSFile *fin = NULL; | |
246 gint rtn; | |
247 | |
248 fin = vfs_fopen(filename, "rb"); | |
249 | |
250 if (fin == NULL) | |
251 return 0; | |
252 | |
253 rtn = audmad_is_our_fd(filename, fin); | |
254 vfs_fclose(fin); | |
255 | |
256 return rtn; | |
257 } | |
258 | |
259 static void audmad_stop(InputPlayback *playback) | |
260 { | |
261 #ifdef DEBUG | |
262 g_message("f: audmad_stop"); | |
263 #endif /* DEBUG */ | |
264 g_static_mutex_lock(&mutex); | |
265 if (decode_thread) { | |
266 info.playback->playing = 0; | |
267 #ifdef DEBUG | |
268 g_message("waiting for thread"); | |
269 #endif /* DEBUG */ | |
270 g_thread_join(decode_thread); | |
271 #ifdef DEBUG | |
272 g_message("thread done"); | |
273 #endif /* DEBUG */ | |
274 input_term(&info); | |
275 decode_thread = NULL; | |
276 } | |
277 g_static_mutex_unlock(&mutex); | |
278 } | |
279 | |
280 | |
281 static void audmad_play_file(InputPlayback *playback) | |
282 { | |
283 gboolean rtn; | |
284 gchar *url = playback->filename; | |
285 | |
286 #ifdef DEBUG | |
287 g_message("playing %s", url); | |
288 #endif /* DEBUG */ | |
289 | |
290 if (input_init(&info, url) == FALSE) { | |
291 g_message("error initialising input"); | |
292 return; | |
293 } | |
294 | |
295 rtn = input_get_info(&info, audmad_config.fast_play_time_calc); | |
296 | |
297 if (rtn == FALSE) { | |
298 g_message("error reading input info"); | |
299 return; | |
300 } | |
301 | |
302 info.playback = playback; | |
303 info.playback->playing = 1; | |
304 | |
305 decode_thread = g_thread_create(decode_loop, (void *) &info, TRUE, NULL); | |
306 } | |
307 | |
308 static void audmad_pause(InputPlayback *playback, short paused) | |
309 { | |
310 mad_plugin->output->pause(paused); | |
311 } | |
312 | |
313 static void audmad_seek(InputPlayback *playback, int time) | |
314 { | |
315 /* xmms gives us the desired seek time in seconds */ | |
316 info.seek = time; | |
317 } | |
318 | |
319 /** | |
320 * Scan the given file or URL. | |
321 * Fills in the title string and the track length in milliseconds. | |
322 */ | |
323 void audmad_get_song_info(char *url, char **title, int *length) | |
324 { | |
325 struct mad_info_t myinfo; | |
326 #ifdef DEBUG | |
327 g_message("f: audmad_get_song_info: %s", url); | |
328 #endif /* DEBUG */ | |
329 | |
330 input_init(&myinfo, url); | |
331 | |
332 if (input_get_info(&myinfo, audmad_config.fast_play_time_calc) == TRUE) | |
333 { | |
334 *title = strdup(myinfo.title); | |
335 *length = mad_timer_count(myinfo.duration, MAD_UNITS_MILLISECONDS); | |
336 } | |
337 else | |
338 { | |
339 *title = strdup(url); | |
340 *length = -1; | |
341 } | |
342 | |
343 input_term(&myinfo); | |
344 | |
345 #ifdef DEBUG | |
346 g_message("e: audmad_get_song_info"); | |
347 #endif /* DEBUG */ | |
348 } | |
349 | |
350 static void audmad_about() | |
351 { | |
352 static GtkWidget *aboutbox; | |
353 gchar *scratch; | |
354 | |
355 if (aboutbox != NULL) | |
356 return; | |
357 | |
358 scratch = g_strdup_printf( | |
359 "Audacious MPEG Audio Plugin\n" | |
360 "\n" | |
361 "Compiled against libMAD version: %d.%d.%d%s\n" | |
362 "\n" | |
363 "Written by:\n" | |
364 " William Pitcock <nenolod@sacredspiral.co.uk>\n" | |
365 " Yoshiki Yazawa <yaz@cc.rim.or.jp>\n" | |
366 "\n" | |
367 "Portions derived from XMMS-MAD by:\n" | |
368 " Sam Clegg\n" | |
369 "\n" | |
370 "ReplayGain support by:\n" | |
371 " Samuel Krempp", | |
372 MAD_VERSION_MAJOR, MAD_VERSION_MINOR, MAD_VERSION_PATCH, | |
373 MAD_VERSION_EXTRA); | |
374 | |
375 aboutbox = xmms_show_message("About MPEG Audio Plugin", | |
376 scratch, | |
377 "Ok", FALSE, NULL, NULL); | |
378 | |
379 g_free(scratch); | |
380 | |
381 g_signal_connect(G_OBJECT(aboutbox), "destroy", | |
382 G_CALLBACK(gtk_widget_destroyed), &aboutbox); | |
383 } | |
384 | |
385 /** | |
386 * Display a GTK box containing the given error message. | |
387 * Taken from mpg123 plugin. | |
388 */ | |
389 void audmad_error(char *error, ...) | |
390 { | |
391 #ifndef NOGUI | |
392 if (!error_dialog) { | |
393 va_list args; | |
394 char string[256]; | |
395 va_start(args, error); | |
396 vsnprintf(string, 256, error, args); | |
397 va_end(args); | |
398 GDK_THREADS_ENTER(); | |
399 error_dialog = | |
400 xmms_show_message("Error", string, "Ok", FALSE, 0, 0); | |
401 gtk_signal_connect(GTK_OBJECT(error_dialog), "destroy", | |
402 GTK_SIGNAL_FUNC(gtk_widget_destroyed), | |
403 &error_dialog); | |
404 GDK_THREADS_LEAVE(); | |
405 } | |
406 #endif /* !NOGUI */ | |
407 } | |
408 | |
409 extern void audmad_get_file_info(char *filename); | |
410 extern void audmad_configure(); | |
411 | |
412 // tuple stuff | |
413 static TitleInput *audmad_get_song_tuple(char *filename) | |
414 { | |
415 TitleInput *tuple = NULL; | |
416 gchar *string = NULL; | |
417 VFSFile *file; | |
418 | |
419 struct id3_file *id3file = NULL; | |
420 struct id3_tag *tag = NULL; | |
421 | |
422 #ifdef DEBUG | |
423 string = str_to_utf8(filename); | |
424 g_message("f: mad: audmad_get_song_tuple: %s", string); | |
425 if (string) { | |
426 g_free(string); | |
427 string = NULL; | |
428 } | |
429 #endif | |
430 | |
431 if ((file = vfs_fopen(filename, "rb")) != NULL) { | |
432 tuple = bmp_title_input_new(); | |
433 | |
434 id3file = id3_file_open(filename, ID3_FILE_MODE_READONLY); | |
435 | |
436 if (id3file) { | |
437 tag = id3_file_tag(id3file); | |
438 | |
439 if (tag) { | |
440 tuple->performer = | |
441 input_id3_get_string(tag, ID3_FRAME_ARTIST); | |
442 tuple->album_name = | |
443 input_id3_get_string(tag, ID3_FRAME_ALBUM); | |
444 tuple->track_name = | |
445 input_id3_get_string(tag, ID3_FRAME_TITLE); | |
446 | |
447 // year | |
448 string = NULL; | |
449 string = input_id3_get_string(tag, ID3_FRAME_YEAR); //TDRC | |
450 if (!string) | |
451 string = input_id3_get_string(tag, "TYER"); | |
452 | |
453 if (string) { | |
454 tuple->year = atoi(string); | |
455 g_free(string); | |
456 string = NULL; | |
457 } | |
458 | |
459 tuple->file_name = g_path_get_basename(filename); | |
460 tuple->file_path = g_path_get_dirname(filename); | |
461 tuple->file_ext = extname(filename); | |
462 | |
463 // length | |
464 { | |
465 char *dummy = NULL; | |
466 int length = 0; | |
467 audmad_get_song_info(filename, &dummy, &length); | |
468 tuple->length = length; | |
469 g_free(dummy); | |
470 } | |
471 | |
472 // track number | |
473 string = input_id3_get_string(tag, ID3_FRAME_TRACK); | |
474 if (string) { | |
475 tuple->track_number = atoi(string); | |
476 g_free(string); | |
477 string = NULL; | |
478 } | |
479 // genre | |
480 tuple->genre = input_id3_get_string(tag, ID3_FRAME_GENRE); | |
481 #ifdef DEBUG | |
482 g_message("genre = %s\n", tuple->genre); | |
483 #endif | |
484 // comment | |
485 tuple->comment = | |
486 input_id3_get_string(tag, ID3_FRAME_COMMENT); | |
487 | |
488 // mtime | |
489 // tuple->mtime = audmad_get_mtime(filename); | |
490 | |
491 } | |
492 id3_file_close(id3file); | |
493 } | |
494 vfs_fclose(file); | |
495 } | |
496 #ifdef DEBUG | |
497 g_message("e: mad: audmad_get_song_tuple"); | |
498 #endif | |
499 tuple->formatter = NULL; //ensure | |
500 return tuple; | |
501 | |
502 } | |
503 | |
504 /** | |
505 * Retrieve meta-information about URL. | |
506 * For local files this means ID3 tag etc. | |
507 */ | |
508 gboolean mad_get_info(struct mad_info_t * info, gboolean fast_scan) | |
509 { | |
510 TitleInput *tuple = NULL; | |
511 | |
512 g_message("f: mad_get_info: %s", info->filename); | |
513 | |
514 if (info->remote) | |
515 return TRUE; | |
516 | |
517 tuple = audmad_get_song_tuple(info->filename); | |
518 info->title = xmms_get_titlestring(audmad_config.title_override == TRUE ? | |
519 audmad_config.id3_format : xmms_get_gentitle_format(), tuple); | |
520 | |
521 /* scan mp3 file, decoding headers unless fast_scan is set */ | |
522 if (scan_file(info, fast_scan) == FALSE) | |
523 return FALSE; | |
524 | |
525 /* reset the input file to the start */ | |
526 vfs_rewind(info->infile); | |
527 info->offset = 0; | |
528 | |
529 /* use the filename for the title as a last resort */ | |
530 if (!info->title) | |
531 { | |
532 char *pos = strrchr(info->filename, '/'); | |
533 if (pos) | |
534 info->title = g_strdup(pos + 1); | |
535 else | |
536 info->title = g_strdup(info->filename); | |
537 } | |
538 | |
539 g_message("e: mad_get_info"); | |
540 return TRUE; | |
541 } | |
542 | |
543 static gchar *fmts[] = { "mp3", "mp2", "mpg", NULL }; | |
544 | |
545 InputPlugin *get_iplugin_info(void) | |
546 { | |
547 if (mad_plugin != NULL) | |
548 return mad_plugin; | |
549 | |
550 mad_plugin = g_new0(InputPlugin, 1); | |
551 mad_plugin->description = g_strdup(_("MPEG Audio Plugin")); | |
552 mad_plugin->init = audmad_init; | |
553 mad_plugin->about = audmad_about; | |
554 mad_plugin->configure = audmad_configure; | |
555 mad_plugin->is_our_file = audmad_is_our_file; | |
556 mad_plugin->play_file = audmad_play_file; | |
557 mad_plugin->stop = audmad_stop; | |
558 mad_plugin->pause = audmad_pause; | |
559 mad_plugin->seek = audmad_seek; | |
560 mad_plugin->cleanup = audmad_cleanup; | |
561 mad_plugin->get_song_info = audmad_get_song_info; | |
562 mad_plugin->file_info_box = audmad_get_file_info; | |
563 mad_plugin->get_song_tuple = audmad_get_song_tuple; | |
564 mad_plugin->is_our_file_from_vfs = audmad_is_our_fd; | |
565 mad_plugin->vfs_extensions = fmts; | |
566 | |
567 return mad_plugin; | |
568 } |