Mercurial > audlegacy-plugins
annotate src/tta/aud-tta.c @ 368:c812e846b84e trunk
[svn] - add support for extension probing to many plugins.
author | nenolod |
---|---|
date | Tue, 12 Dec 2006 18:54:01 -0800 |
parents | fbd06b4aa776 |
children | a157306caf03 |
rev | line source |
---|---|
290 | 1 /* |
2 * aud--tta.c | |
3 * | |
4 * Description: TTA input plug-in for Audacious | |
5 * Developed by: Alexander Djourik <sasha@iszf.irk.ru> | |
6 * Pavel Zhilin <pzh@iszf.irk.ru> | |
7 * Audacious port: Yoshiki Yazawa <yaz@cc.rim.or.jp> | |
8 * | |
9 * Copyright (c) 2004 Alexander Djourik. All rights reserved. | |
10 * | |
11 */ | |
12 | |
13 /* | |
14 * This library is free software; you can redistribute it and/or | |
15 * modify it under the terms of the GNU Lesser General Public | |
16 * License as published by the Free Software Foundation; either | |
17 * version 2.1 of the License, or (at your option) any later version. | |
18 * | |
19 * This library is distributed in the hope that it will be useful, | |
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 * Lesser General Public License for more details. | |
23 * | |
24 * You should have received a copy of the GNU Lesser General Public | |
25 * License along with this library; if not, write to the Free Software | |
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
27 * | |
28 * Please see the file COPYING in this directory for full copyright | |
29 * information. | |
30 */ | |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 #include <time.h> | |
35 #include <sys/types.h> | |
36 #include <sys/stat.h> | |
37 #include <glib.h> | |
38 #include <pthread.h> | |
39 #include <glib/gi18n.h> | |
40 #include <string.h> | |
41 | |
42 #include <audacious/util.h> | |
43 #include <audacious/plugin.h> | |
44 #include <audacious/titlestring.h> | |
45 #include <audacious/vfs.h> | |
46 #include "aud-support.h" | |
47 #include "audacious/output.h" | |
48 #include "audacious/util.h" | |
49 | |
50 #include <id3tag.h> | |
51 | |
52 | |
53 | |
54 #define PLUGIN_VERSION "1.2" | |
55 #define PROJECT_URL "<http://www.true-audio.com>" | |
56 | |
57 #pragma pack (1) | |
58 #include "ttalib.h" | |
59 | |
60 #define OUTPUT_ERROR (MEMORY_ERROR+1) | |
61 #define MAX_BSIZE (MAX_BPS>>3) | |
62 | |
63 | |
64 static void init (); | |
65 static void cleanup (); | |
66 static int is_our_file (char *filename); | |
67 static void play_file (char *filename); | |
68 static void tta_pause (short paused); | |
69 static void stop (void); | |
70 static void seek (int time); | |
71 static int get_time (void); | |
72 static void get_song_info (char *filename, char **title, int *length); | |
73 static void file_info (char *filename); | |
74 static void about (); | |
75 static TitleInput *get_song_tuple(char *filename); | |
76 static gchar *extname(const char *filename); | |
77 | |
78 InputPlugin tta_ip = | |
79 { | |
80 NULL, | |
81 NULL, | |
82 NULL, | |
83 init, | |
84 about, | |
85 NULL, | |
86 is_our_file, | |
87 NULL, | |
88 play_file, | |
89 stop, | |
90 tta_pause, | |
91 seek, | |
92 NULL, | |
93 get_time, | |
94 NULL, | |
95 NULL, | |
96 cleanup, | |
97 NULL, | |
98 NULL, | |
99 NULL, | |
100 NULL, | |
101 get_song_info, | |
102 file_info, | |
103 NULL, | |
104 get_song_tuple, // get_song_tuple | |
105 NULL, // set_song_tuple | |
368
c812e846b84e
[svn] - add support for extension probing to many plugins.
nenolod
parents:
290
diff
changeset
|
106 NULL, // buffer |
c812e846b84e
[svn] - add support for extension probing to many plugins.
nenolod
parents:
290
diff
changeset
|
107 { "tta", NULL }, |
290 | 108 }; |
109 | |
110 InputPlugin * | |
111 get_iplugin_info (void) | |
112 { | |
113 tta_ip.description = g_strdup_printf ("True Audio Plugin %s", PLUGIN_VERSION); | |
114 return &tta_ip; | |
115 } | |
116 | |
117 static pthread_t decode_thread; | |
118 static char sample_buffer[PCM_BUFFER_LENGTH * MAX_BSIZE * MAX_NCH]; | |
119 static tta_info info; // currently playing file info | |
120 static long seek_position = -1; | |
121 static int playing = FALSE; | |
122 static int read_samples = -1; | |
123 | |
124 static void | |
125 tta_error (int error) | |
126 { | |
127 char *message; | |
128 static GtkWidget *errorbox; | |
129 if (errorbox != NULL) return; | |
130 | |
131 switch (error) | |
132 { | |
133 case OPEN_ERROR: | |
134 message = "Can't open file\n"; | |
135 break; | |
136 case FORMAT_ERROR: | |
137 message = "Not supported file format\n"; | |
138 break; | |
139 case FILE_ERROR: | |
140 message = "File is corrupted\n"; | |
141 break; | |
142 case READ_ERROR: | |
143 message = "Can't read from file\n"; | |
144 break; | |
145 case MEMORY_ERROR: | |
146 message = "Insufficient memory available\n"; | |
147 break; | |
148 case OUTPUT_ERROR: | |
149 message = "Output plugin error\n"; | |
150 break; | |
151 default: | |
152 message = "Unknown error\n"; | |
153 break; | |
154 } | |
155 | |
156 xmms_show_message ("TTA Decoder Error", message, | |
157 "Ok", FALSE, NULL, NULL); | |
158 | |
159 gtk_signal_connect(GTK_OBJECT(errorbox), "destroy", | |
160 G_CALLBACK(gtk_widget_destroyed), &errorbox); | |
161 } | |
162 | |
163 static gchar * | |
164 get_title (char *filename, tta_info *ttainfo) | |
165 { | |
166 char *name, *p; | |
167 | |
168 if (ttainfo->id3v2.id3has && | |
169 (*ttainfo->id3v2.artist || *ttainfo->id3v2.title)) { | |
170 if (*ttainfo->id3v2.artist && *ttainfo->id3v2.title) | |
171 return g_strdup_printf("%s - %s", ttainfo->id3v2.artist, ttainfo->id3v2.title); | |
172 else if (*ttainfo->id3v2.artist && *ttainfo->id3v2.album) | |
173 return g_strdup_printf("%s - %s", ttainfo->id3v2.artist, ttainfo->id3v2.album); | |
174 else if (*ttainfo->id3v2.artist) return g_strdup(ttainfo->id3v2.artist); | |
175 else if (*ttainfo->id3v2.title) return g_strdup(ttainfo->id3v2.title); | |
176 } else if (ttainfo->id3v1.id3has && | |
177 (*ttainfo->id3v1.artist || *ttainfo->id3v1.title)) { | |
178 if (*ttainfo->id3v1.artist && *ttainfo->id3v1.title) | |
179 return g_strdup_printf("%s - %s", ttainfo->id3v1.artist, ttainfo->id3v1.title); | |
180 else if (*ttainfo->id3v1.artist && *ttainfo->id3v1.album) | |
181 return g_strdup_printf("%s - %s", ttainfo->id3v1.artist, ttainfo->id3v1.album); | |
182 else if (*ttainfo->id3v1.artist) return g_strdup(ttainfo->id3v1.artist); | |
183 else if (*ttainfo->id3v1.title) return g_strdup(ttainfo->id3v1.title); | |
184 } | |
185 name = g_strdup (g_basename(filename)); | |
186 p = name + strlen(name); | |
187 while (*p != '.' && p >= name) p--; | |
188 if (*p == '.') *p = '\0'; | |
189 p = g_strdup (name); | |
190 g_free (name); | |
191 return p; | |
192 } | |
193 | |
194 static void * | |
195 play_loop (void *arg) | |
196 { | |
197 int bufsize = PCM_BUFFER_LENGTH * info.BSIZE * info.NCH; | |
198 | |
199 //////////////////////////////////////// | |
200 // decode PCM_BUFFER_LENGTH samples | |
201 // into the current PCM buffer position | |
202 | |
203 while (playing) | |
204 { | |
205 while ((read_samples = get_samples (sample_buffer)) > 0) | |
206 { | |
207 | |
208 while ((tta_ip.output->buffer_free () < bufsize) | |
209 && seek_position == -1) | |
210 { | |
211 if (!playing) | |
212 goto DONE; | |
213 xmms_usleep (10000); | |
214 } | |
215 if (seek_position == -1) | |
216 { | |
217 produce_audio(tta_ip.output->written_time(), | |
218 ((info.BPS == 8) ? FMT_U8 : FMT_S16_LE), | |
219 info.NCH, | |
220 read_samples * info.NCH * info.BSIZE, | |
221 sample_buffer, | |
222 NULL); | |
223 } | |
224 else | |
225 { | |
226 set_position (seek_position); | |
227 tta_ip.output->flush (seek_position * SEEK_STEP); | |
228 seek_position = -1; | |
229 } | |
230 } | |
231 tta_ip.output->buffer_free (); | |
232 tta_ip.output->buffer_free (); | |
233 xmms_usleep(10000); | |
234 } | |
235 DONE: | |
236 | |
237 //////////////////////// | |
238 // destroy memory pools | |
239 player_stop (); | |
240 | |
241 /////////////////////////////// | |
242 // close currently playing file | |
243 close_tta_file (&info); | |
244 | |
245 pthread_exit (NULL); | |
246 } | |
247 | |
248 static void | |
249 init () | |
250 { | |
251 memset (&info, 0, sizeof (tta_info)); | |
252 } | |
253 | |
254 static void | |
255 cleanup () | |
256 { | |
257 } | |
258 | |
259 static void | |
260 about () | |
261 { | |
262 static GtkWidget *aboutbox; | |
263 if (aboutbox != NULL) return; | |
264 | |
265 aboutbox = xmms_show_message( | |
266 "About True Audio Plugin", | |
267 "TTA input plugin" PLUGIN_VERSION "for BMP\n" | |
268 "Copyright (c) 2004 True Audio Software\n" | |
269 PROJECT_URL, "Ok", FALSE, NULL, NULL); | |
270 | |
271 gtk_signal_connect(GTK_OBJECT(aboutbox), "destroy", | |
272 G_CALLBACK(gtk_widget_destroyed), &aboutbox); | |
273 } | |
274 | |
275 static GtkWidget *window = NULL; | |
276 static GtkWidget *filename_entry, *title_entry, | |
277 *artist_entry, *album_entry, | |
278 *year_entry, *tracknum_entry, | |
279 *comment_entry, *genre_entry, | |
280 *info_frame; | |
281 | |
282 extern char *genre[]; | |
283 | |
284 static void | |
285 file_info (char *filename) | |
286 { | |
287 tta_info ttainfo; | |
288 char *title; | |
289 gchar *utf_filename = NULL; | |
290 | |
291 if (!window) { | |
292 GtkWidget *vbox, *hbox, *left_vbox, *table; | |
293 GtkWidget *label, *filename_hbox, *button_ok; | |
294 | |
295 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
296 gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE); | |
297 gtk_signal_connect(GTK_OBJECT(window), "destroy", | |
298 G_CALLBACK(gtk_widget_destroyed), &window); | |
299 gtk_container_set_border_width(GTK_CONTAINER(window), 10); | |
300 | |
301 vbox = gtk_vbox_new(FALSE, 10); | |
302 gtk_container_add(GTK_CONTAINER(window), vbox); | |
303 | |
304 filename_hbox = gtk_hbox_new(FALSE, 5); | |
305 gtk_box_pack_start(GTK_BOX(vbox), filename_hbox, FALSE, TRUE, 0); | |
306 label = gtk_label_new("Filename:"); | |
307 gtk_box_pack_start(GTK_BOX(filename_hbox), label, FALSE, TRUE, 0); | |
308 | |
309 filename_entry = gtk_entry_new_with_max_length(1024); | |
310 gtk_editable_set_editable(GTK_EDITABLE(filename_entry), FALSE); | |
311 gtk_box_pack_start(GTK_BOX(filename_hbox), filename_entry, TRUE, TRUE, 0); | |
312 | |
313 hbox = gtk_hbox_new(FALSE, 10); | |
314 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0); | |
315 left_vbox = gtk_vbox_new(FALSE, 10); | |
316 gtk_box_pack_start(GTK_BOX(hbox), left_vbox, FALSE, FALSE, 0); | |
317 | |
318 info_frame = gtk_frame_new("ID3 Tag:"); | |
319 gtk_box_pack_start(GTK_BOX(left_vbox), info_frame, FALSE, FALSE, 0); | |
320 | |
321 table = gtk_table_new(5, 5, FALSE); | |
322 gtk_container_set_border_width(GTK_CONTAINER(table), 5); | |
323 gtk_container_add(GTK_CONTAINER(info_frame), table); | |
324 | |
325 label = gtk_label_new("Title:"); | |
326 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); | |
327 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, GTK_FILL, 5, 5); | |
328 | |
329 title_entry = gtk_entry_new_with_max_length(1024); | |
330 gtk_editable_set_editable(GTK_EDITABLE(title_entry), FALSE); | |
331 gtk_table_attach(GTK_TABLE(table), title_entry, 1, 4, 0, 1, | |
332 GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
333 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 5); | |
334 | |
335 label = gtk_label_new("Artist:"); | |
336 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); | |
337 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2, | |
338 GTK_FILL, GTK_FILL, 5, 5); | |
339 | |
340 artist_entry = gtk_entry_new_with_max_length(1024); | |
341 gtk_editable_set_editable(GTK_EDITABLE(artist_entry), FALSE); | |
342 gtk_table_attach(GTK_TABLE(table), artist_entry, 1, 4, 1, 2, | |
343 GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
344 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 5); | |
345 | |
346 label = gtk_label_new("Album:"); | |
347 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); | |
348 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3, | |
349 GTK_FILL, GTK_FILL, 5, 5); | |
350 | |
351 album_entry = gtk_entry_new_with_max_length(1024); | |
352 gtk_editable_set_editable(GTK_EDITABLE(album_entry), FALSE); | |
353 gtk_table_attach(GTK_TABLE(table), album_entry, 1, 4, 2, 3, | |
354 GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
355 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 5); | |
356 | |
357 label = gtk_label_new("Comment:"); | |
358 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); | |
359 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4, | |
360 GTK_FILL, GTK_FILL, 5, 5); | |
361 | |
362 comment_entry = gtk_entry_new_with_max_length(1024); | |
363 gtk_editable_set_editable(GTK_EDITABLE(comment_entry), FALSE); | |
364 gtk_table_attach(GTK_TABLE(table), comment_entry, 1, 4, 3, 4, | |
365 GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
366 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 5); | |
367 | |
368 label = gtk_label_new("Year:"); | |
369 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); | |
370 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 4, 5, | |
371 GTK_FILL, GTK_FILL, 5, 5); | |
372 | |
373 year_entry = gtk_entry_new_with_max_length(4); | |
374 gtk_editable_set_editable(GTK_EDITABLE(year_entry), FALSE); | |
375 gtk_widget_set_usize(year_entry, 40, -1); | |
376 gtk_table_attach(GTK_TABLE(table), year_entry, 1, 2, 4, 5, | |
377 GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
378 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 5); | |
379 | |
380 label = gtk_label_new("Track number:"); | |
381 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); | |
382 gtk_table_attach(GTK_TABLE(table), label, 2, 3, 4, 5, | |
383 GTK_FILL, GTK_FILL, 5, 5); | |
384 | |
385 tracknum_entry = gtk_entry_new_with_max_length(3); | |
386 gtk_editable_set_editable(GTK_EDITABLE(tracknum_entry), FALSE); | |
387 gtk_widget_set_usize(tracknum_entry, 40, -1); | |
388 gtk_table_attach(GTK_TABLE(table), tracknum_entry, 3, 4, 4, 5, | |
389 GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
390 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 5); | |
391 | |
392 label = gtk_label_new("Genre:"); | |
393 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); | |
394 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 5, 6, | |
395 GTK_FILL, GTK_FILL, 5, 5); | |
396 | |
397 genre_entry = gtk_entry_new_with_max_length(1024); | |
398 gtk_editable_set_editable(GTK_EDITABLE(genre_entry), FALSE); | |
399 gtk_widget_set_usize(genre_entry, 40, -1); | |
400 gtk_table_attach(GTK_TABLE(table), genre_entry, 1, 4, 5, 6, | |
401 GTK_FILL | GTK_EXPAND | GTK_SHRINK, | |
402 GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 5); | |
403 | |
404 button_ok = gtk_button_new_with_label("Ok"); | |
405 gtk_signal_connect_object(GTK_OBJECT(button_ok), "clicked", | |
406 G_CALLBACK(gtk_widget_destroy), G_OBJECT(window)); | |
407 GTK_WIDGET_SET_FLAGS(button_ok, GTK_CAN_DEFAULT); | |
408 gtk_box_pack_start(GTK_BOX(vbox), button_ok, TRUE, TRUE, 0); | |
409 | |
410 gtk_widget_show_all (window); | |
411 } | |
412 | |
413 utf_filename = str_to_utf8(filename); | |
414 title = g_strdup_printf(_("File Info - %s"), g_basename(utf_filename)); | |
415 gtk_window_set_title(GTK_WINDOW(window), title); | |
416 g_free(title); | |
417 | |
418 gtk_entry_set_text(GTK_ENTRY(filename_entry), utf_filename); | |
419 gtk_editable_set_position(GTK_EDITABLE(filename_entry), -1); | |
420 | |
421 #if 1 | |
422 title = g_strdup(g_basename(utf_filename)); | |
423 // if ((tmp = strrchr(title, '.')) != NULL) *tmp = '\0'; | |
424 gtk_entry_set_text(GTK_ENTRY(title_entry), title); | |
425 g_free(title); | |
426 #endif | |
427 g_free(utf_filename); | |
428 | |
429 if (open_tta_file (filename, &ttainfo, 0) >= 0) | |
430 { | |
431 gtk_entry_set_text(GTK_ENTRY(title_entry), ""); | |
432 gtk_entry_set_text(GTK_ENTRY(artist_entry), ""); | |
433 gtk_entry_set_text(GTK_ENTRY(album_entry), ""); | |
434 gtk_entry_set_text(GTK_ENTRY(year_entry), ""); | |
435 gtk_entry_set_text(GTK_ENTRY(tracknum_entry), ""); | |
436 gtk_entry_set_text(GTK_ENTRY(comment_entry), ""); | |
437 gtk_entry_set_text(GTK_ENTRY(genre_entry), ""); | |
438 | |
439 if (ttainfo.id3v2.id3has) | |
440 { | |
441 gtk_entry_set_text(GTK_ENTRY(title_entry), ttainfo.id3v2.title); | |
442 gtk_entry_set_text(GTK_ENTRY(artist_entry), ttainfo.id3v2.artist); | |
443 gtk_entry_set_text(GTK_ENTRY(album_entry), ttainfo.id3v2.album); | |
444 gtk_entry_set_text(GTK_ENTRY(year_entry), ttainfo.id3v2.year); | |
445 gtk_entry_set_text(GTK_ENTRY(tracknum_entry), ttainfo.id3v2.track); | |
446 gtk_entry_set_text(GTK_ENTRY(comment_entry), ttainfo.id3v2.comment); | |
447 gtk_entry_set_text(GTK_ENTRY(genre_entry), ttainfo.id3v2.genre); | |
448 } | |
449 else if (ttainfo.id3v1.id3has) | |
450 { | |
451 gchar *track = g_strdup_printf ("%2d", ttainfo.id3v1.track); | |
452 gtk_entry_set_text(GTK_ENTRY(title_entry), ttainfo.id3v1.title); | |
453 gtk_entry_set_text(GTK_ENTRY(artist_entry), ttainfo.id3v1.artist); | |
454 gtk_entry_set_text(GTK_ENTRY(album_entry), ttainfo.id3v1.album); | |
455 gtk_entry_set_text(GTK_ENTRY(year_entry), ttainfo.id3v1.year); | |
456 gtk_entry_set_text(GTK_ENTRY(tracknum_entry), track); | |
457 gtk_entry_set_text(GTK_ENTRY(comment_entry), ttainfo.id3v1.comment); | |
458 gtk_entry_set_text(GTK_ENTRY(genre_entry), | |
459 genre[ttainfo.id3v1.genre <= GENRES ? ttainfo.id3v1.genre : 12]); | |
460 g_free (track); | |
461 } | |
462 } | |
463 close_tta_file (&ttainfo); | |
464 | |
465 gtk_widget_set_sensitive(info_frame, TRUE); | |
466 } | |
467 | |
468 static int | |
469 is_our_file (char *filename) | |
470 { | |
471 if (!strcasecmp (filename + strlen (filename) - 4, ".tta")) | |
472 { | |
473 return TRUE; | |
474 } | |
475 return FALSE; | |
476 } | |
477 | |
478 static void | |
479 play_file (char *filename) | |
480 { | |
481 char *title; | |
482 long datasize, origsize, bitrate; | |
483 | |
484 playing = FALSE; | |
485 | |
486 //////////////////////////////////////// | |
487 // open TTA file | |
488 if (open_tta_file (filename, &info, 0) < 0) | |
489 { | |
490 tta_error (info.STATE); | |
491 close_tta_file (&info); | |
492 return; | |
493 } | |
494 | |
495 //////////////////////////////////////// | |
496 // initialize TTA player | |
497 if (player_init (&info) < 0) | |
498 { | |
499 tta_error (info.STATE); | |
500 close_tta_file (&info); | |
501 return; | |
502 } | |
503 | |
504 | |
505 if (tta_ip.output->open_audio ((info.BPS == 8) ? FMT_U8 : FMT_S16_LE, | |
506 info.SAMPLERATE, info.NCH) == 0) | |
507 { | |
508 tta_error (OUTPUT_ERROR); | |
509 close_tta_file (&info); | |
510 return; | |
511 } | |
512 title = get_title (filename, &info); | |
513 printf("title @1 = %s\n", title); | |
514 { | |
515 TitleInput *tuple; | |
516 | |
517 tuple = get_song_tuple(filename); | |
518 if(tuple->track_name) { | |
519 g_free(title); | |
520 title = g_strdup(tuple->track_name); | |
521 printf("title @2 = %s\n", title); | |
522 } | |
523 | |
524 bmp_title_input_free(tuple); | |
525 } | |
526 | |
527 datasize = file_size(filename) - info.DATAPOS; | |
528 origsize = info.DATALENGTH * info.BSIZE * info.NCH; | |
529 | |
530 bitrate = (long) ((float) datasize / origsize * | |
531 (info.SAMPLERATE * info.NCH * info.BPS)); | |
532 | |
533 tta_ip.set_info (title, 1000 * info.LENGTH, bitrate, info.SAMPLERATE, info.NCH); | |
534 | |
535 if (title) | |
536 g_free (title); | |
537 | |
538 playing = TRUE; | |
539 seek_position = -1; | |
540 read_samples = -1; | |
541 | |
542 pthread_create (&decode_thread, NULL, play_loop, NULL); | |
543 } | |
544 | |
545 static void | |
546 tta_pause (short paused) | |
547 { | |
548 tta_ip.output->pause (paused); | |
549 } | |
550 static void | |
551 stop (void) | |
552 { | |
553 if (playing) | |
554 { | |
555 playing = FALSE; | |
556 pthread_join (decode_thread, NULL); | |
557 tta_ip.output->close_audio (); | |
558 close_tta_file (&info); | |
559 read_samples = 0; | |
560 } | |
561 } | |
562 | |
563 static void | |
564 seek (int time) | |
565 { | |
566 if (playing) | |
567 { | |
568 seek_position = 1000 * time / SEEK_STEP; | |
569 | |
570 while (seek_position != -1) | |
571 xmms_usleep (10000); | |
572 } | |
573 } | |
574 | |
575 static int | |
576 get_time (void) | |
577 { | |
578 if (playing && (read_samples || tta_ip.output->buffer_playing())) | |
579 return tta_ip.output->output_time(); | |
580 | |
581 return -1; | |
582 } | |
583 | |
584 static void | |
585 get_song_info (char *filename, char **title, int *length) | |
586 { | |
587 tta_info ttainfo; | |
588 | |
589 if (open_tta_file (filename, &ttainfo, 0) >= 0) | |
590 { | |
591 *title = get_title (filename, &ttainfo); | |
592 *length = ttainfo.LENGTH * 1000; | |
593 } | |
594 close_tta_file (&ttainfo); | |
595 } | |
596 | |
597 | |
598 | |
599 static TitleInput * | |
600 get_song_tuple(char *filename) | |
601 { | |
602 TitleInput *tuple = NULL; | |
603 tta_info *ttainfo; | |
604 VFSFile *file; | |
605 | |
606 ttainfo = g_malloc0(sizeof(tta_info)); | |
607 | |
608 if((file = vfs_fopen(filename, "rb")) != NULL) { | |
609 | |
610 #ifdef DEBUG | |
611 printf("about to open_tta_file\n"); | |
612 #endif | |
613 if(open_tta_file(filename, ttainfo, 0) >= 0) { | |
614 tuple = bmp_title_input_new(); | |
615 #ifdef DEBUG | |
616 printf("open_tta_file succeed\n"); | |
617 #endif | |
618 tuple->file_name = g_path_get_basename(filename); | |
619 tuple->file_path = g_path_get_dirname(filename); | |
620 tuple->file_ext = extname(filename); | |
621 tuple->length = ttainfo->LENGTH * 1000; | |
622 | |
623 if (ttainfo->id3v2.id3has) { | |
624 if(ttainfo->id3v2.artist) | |
625 tuple->performer = g_strdup(ttainfo->id3v2.artist); | |
626 | |
627 if(ttainfo->id3v2.album) | |
628 tuple->album_name = g_strdup(ttainfo->id3v2.album); | |
629 | |
630 if(ttainfo->id3v2.title) | |
631 tuple->track_name = g_strdup(ttainfo->id3v2.title); | |
632 | |
633 tuple->year = atoi(ttainfo->id3v2.year); | |
634 | |
635 tuple->track_number = atoi(ttainfo->id3v2.track); | |
636 | |
637 if(ttainfo->id3v2.genre){ | |
638 // printf("genre = %s\n",ttainfo->id3v2.genre); | |
639 tuple->genre = g_strdup(ttainfo->id3v2.genre); | |
640 } | |
641 if(ttainfo->id3v2.comment) | |
642 tuple->comment = g_strdup(ttainfo->id3v2.comment); | |
643 } else if (ttainfo->id3v1.id3has) { | |
644 if(ttainfo->id3v1.artist) | |
645 tuple->performer = g_strdup(ttainfo->id3v1.artist); | |
646 | |
647 if(ttainfo->id3v1.album) | |
648 tuple->album_name = g_strdup(ttainfo->id3v1.album); | |
649 | |
650 if(ttainfo->id3v1.title) | |
651 tuple->track_name = g_strdup(ttainfo->id3v1.title); | |
652 | |
653 tuple->year = atoi(ttainfo->id3v1.year); | |
654 | |
655 tuple->track_number = (int)ttainfo->id3v1.track; | |
656 | |
657 if(ttainfo->id3v1.genre) | |
658 tuple->genre = g_strdup(genre[ttainfo->id3v1.genre <= GENRES ? ttainfo->id3v1.genre : 12]); | |
659 if(ttainfo->id3v1.comment) | |
660 tuple->comment = g_strdup(ttainfo->id3v1.comment); | |
661 } | |
662 | |
663 close_tta_file (ttainfo); | |
664 } | |
665 | |
666 vfs_fclose(file); | |
667 } | |
668 return tuple; | |
669 } | |
670 | |
671 static gchar * | |
672 extname(const char *filename) | |
673 { | |
674 gchar *ext = strrchr(filename, '.'); | |
675 | |
676 if (ext != NULL) | |
677 ++ext; | |
678 | |
679 return ext; | |
680 } | |
681 | |
682 /* return length in letters */ | |
683 size_t tta_ucs4len(id3_ucs4_t *ucs) | |
684 { | |
685 id3_ucs4_t *ptr = ucs; | |
686 size_t len = 0; | |
687 | |
688 while(*ptr++ != 0) | |
689 len++; | |
690 | |
691 return len; | |
692 } | |
693 | |
694 /* duplicate id3_ucs4_t string. new string will be terminated with 0. */ | |
695 id3_ucs4_t *tta_ucs4dup(id3_ucs4_t *org) | |
696 { | |
697 id3_ucs4_t *new = NULL; | |
698 size_t len = tta_ucs4len(org); | |
699 | |
700 new = g_malloc0((len + 1) * sizeof(id3_ucs4_t)); | |
701 memcpy(new, org, len * sizeof(id3_ucs4_t)); | |
702 *(new + len) = 0; //terminate | |
703 | |
704 return new; | |
705 } | |
706 | |
707 #define BYTES(x) ((x) * sizeof(id3_ucs4_t)) | |
708 | |
709 id3_ucs4_t *tta_parse_genre(const id3_ucs4_t *string) | |
710 { | |
711 id3_ucs4_t *ret = NULL; | |
712 id3_ucs4_t *tmp = NULL; | |
713 id3_ucs4_t *genre = NULL; | |
714 id3_ucs4_t *ptr, *end, *tail, *tp; | |
715 size_t ret_len = 0; //num of ucs4 char! | |
716 size_t tmp_len = 0; | |
717 gboolean is_num = TRUE; | |
718 | |
719 tail = (id3_ucs4_t *)string + tta_ucs4len((id3_ucs4_t *)string); | |
720 | |
721 ret = g_malloc0(1024); | |
722 | |
723 for(ptr = (id3_ucs4_t *)string; *ptr != 0 && ptr <= tail; ptr++) { | |
724 if(*ptr == '(') { | |
725 if(*(++ptr) == '(') { // escaped text like: ((something) | |
726 for(end = ptr; *end != ')' && *end != 0;) { // copy "(something)" | |
727 end++; | |
728 } | |
729 end++; //include trailing ')' | |
730 memcpy(ret, ptr, BYTES(end - ptr)); | |
731 ret_len += (end - ptr); | |
732 *(ret + ret_len) = 0; //terminate | |
733 ptr = end + 1; | |
734 } | |
735 else { | |
736 // reference to an id3v1 genre code | |
737 for(end = ptr; *end != ')' && *end != 0;) { | |
738 end++; | |
739 } | |
740 | |
741 tmp = g_malloc0(BYTES(end - ptr + 1)); | |
742 memcpy(tmp, ptr, BYTES(end - ptr)); | |
743 *(tmp + (end - ptr)) = 0; //terminate | |
744 ptr += end - ptr; | |
745 | |
746 genre = (id3_ucs4_t *)id3_genre_name((const id3_ucs4_t *)tmp); | |
747 | |
748 g_free(tmp); | |
749 tmp = NULL; | |
750 | |
751 tmp_len = tta_ucs4len(genre); | |
752 | |
753 memcpy(ret + BYTES(ret_len), genre, BYTES(tmp_len)); | |
754 | |
755 ret_len += tmp_len; | |
756 *(ret + ret_len) = 0; //terminate | |
757 } | |
758 } | |
759 else { | |
760 for(end = ptr; *end != '(' && *end != 0; ) { | |
761 end++; | |
762 } | |
763 // scan string to determine whether a genre code number or not | |
764 tp = ptr; | |
765 is_num = TRUE; | |
766 while(tp < end) { | |
767 if(*tp < '0' || *tp > '9') { // anything else than number appears. | |
768 is_num = FALSE; | |
769 break; | |
770 } | |
771 tp++; | |
772 } | |
773 if(is_num) { | |
774 #ifdef DEBUG | |
775 printf("is_num!\n"); | |
776 #endif | |
777 tmp = g_malloc0(BYTES(end - ptr + 1)); | |
778 memcpy(tmp, ptr, BYTES(end - ptr)); | |
779 *(tmp + (end - ptr)) = 0; //terminate | |
780 ptr += end - ptr; | |
781 | |
782 genre = (id3_ucs4_t *)id3_genre_name((const id3_ucs4_t *)tmp); | |
783 #ifdef DEBUG | |
784 printf("genre length = %d\n", tta_ucs4len(genre)); | |
785 #endif | |
786 g_free(tmp); | |
787 tmp = NULL; | |
788 | |
789 tmp_len = tta_ucs4len(genre); | |
790 | |
791 memcpy(ret + BYTES(ret_len), genre, BYTES(tmp_len)); | |
792 | |
793 ret_len += tmp_len; | |
794 *(ret + ret_len) = 0; //terminate | |
795 } | |
796 else { // plain text | |
797 #ifdef DEBUG | |
798 printf("plain!\n"); | |
799 printf("ret_len = %d\n", ret_len); | |
800 #endif | |
801 memcpy(ret + BYTES(ret_len), ptr, BYTES(end - ptr)); | |
802 ret_len = ret_len + (end - ptr); | |
803 *(ret + ret_len) = 0; //terminate | |
804 ptr += (end - ptr); | |
805 } | |
806 } | |
807 } | |
808 return ret; | |
809 } | |
810 | |
811 gchar *tta_input_id3_get_string(struct id3_tag * tag, char *frame_name) | |
812 { | |
813 gchar *rtn; | |
814 gchar *rtn2; | |
815 const id3_ucs4_t *string_const; | |
816 id3_ucs4_t *string; | |
817 id3_ucs4_t *ucsptr; | |
818 struct id3_frame *frame; | |
819 union id3_field *field; | |
820 gboolean flagutf = FALSE; | |
821 | |
822 frame = id3_tag_findframe(tag, frame_name, 0); | |
823 if (!frame) | |
824 return NULL; | |
825 | |
826 if (frame_name == ID3_FRAME_COMMENT) | |
827 field = id3_frame_field(frame, 3); | |
828 else | |
829 field = id3_frame_field(frame, 1); | |
830 | |
831 if (!field) | |
832 return NULL; | |
833 | |
834 if (frame_name == ID3_FRAME_COMMENT) | |
835 string_const = id3_field_getfullstring(field); | |
836 else | |
837 string_const = id3_field_getstrings(field, 0); | |
838 | |
839 if (!string_const) | |
840 return NULL; | |
841 | |
842 string = tta_ucs4dup((id3_ucs4_t *)string_const); | |
843 | |
844 if (frame_name == ID3_FRAME_GENRE) { | |
845 id3_ucs4_t *string2 = NULL; | |
846 string2 = tta_parse_genre(string); | |
847 g_free((void *)string); | |
848 string = string2; | |
849 } | |
850 | |
851 ucsptr = (id3_ucs4_t *)string; | |
852 while (*ucsptr) { | |
853 if (*ucsptr > 0x000000ffL) { | |
854 flagutf = TRUE; | |
855 break; | |
856 } | |
857 ucsptr++; | |
858 } | |
859 | |
860 if (flagutf) { | |
861 #ifdef DEBUG | |
862 g_message("aud-tta: flagutf!\n"); | |
863 #endif | |
864 rtn = id3_ucs4_utf8duplicate(string); | |
865 } | |
866 else { | |
867 rtn = id3_ucs4_latin1duplicate(string); | |
868 rtn2 = str_to_utf8(rtn); | |
869 free(rtn); | |
870 rtn = rtn2; | |
871 } | |
872 g_free(string); | |
873 string = NULL; | |
874 #ifdef DEBUG | |
875 g_print("string = %s\n", rtn); | |
876 #endif | |
877 return rtn; | |
878 } | |
879 | |
880 int get_id3_tags (const char *filename, tta_info *ttainfo) { | |
881 int id3v2_size; | |
882 gchar *str = NULL; | |
883 | |
884 struct id3_file *id3file = NULL; | |
885 struct id3_tag *tag = NULL; | |
886 | |
887 ttainfo->id3v2.id3has = 0; | |
888 ttainfo->id3v1.id3has = 0; | |
889 | |
890 id3file = id3_file_open (filename, ID3_FILE_MODE_READONLY); | |
891 | |
892 if (id3file) { | |
893 tag = id3_file_tag (id3file); | |
894 | |
895 if (tag) { | |
896 str = tta_input_id3_get_string (tag, ID3_FRAME_ARTIST); | |
897 if(str) { | |
898 strcpy(ttainfo->id3v2.artist, str); | |
899 strncpy(ttainfo->id3v1.artist, str, 30); | |
900 } | |
901 free(str); | |
902 str = NULL; | |
903 | |
904 str = tta_input_id3_get_string (tag, ID3_FRAME_ALBUM); | |
905 if(str){ | |
906 strcpy(ttainfo->id3v2.album, str); | |
907 strncpy(ttainfo->id3v1.album, str, 30); | |
908 } | |
909 free(str); | |
910 str = NULL; | |
911 | |
912 str = tta_input_id3_get_string (tag, ID3_FRAME_TITLE); | |
913 if(str) { | |
914 strcpy(ttainfo->id3v2.title, str); | |
915 strncpy(ttainfo->id3v1.title, str, 30); | |
916 } | |
917 free(str); | |
918 str = NULL; | |
919 | |
920 // year | |
921 str = tta_input_id3_get_string (tag, ID3_FRAME_YEAR); //TDRC | |
922 if(!str) { | |
923 str = tta_input_id3_get_string (tag, "TYER"); | |
924 } | |
925 | |
926 if(str){ | |
927 strncpy(ttainfo->id3v2.year, str, 5); | |
928 strncpy(ttainfo->id3v1.year, str, 5); | |
929 } | |
930 free(str); | |
931 str = NULL; | |
932 | |
933 // track number | |
934 str = tta_input_id3_get_string (tag, ID3_FRAME_TRACK); | |
935 if(str) | |
936 strcpy(ttainfo->id3v2.track, str); | |
937 free(str); | |
938 str = NULL; | |
939 | |
940 // genre | |
941 str = tta_input_id3_get_string (tag, ID3_FRAME_GENRE); | |
942 if(str) { | |
943 id3_ucs4_t *tmp = NULL; | |
944 strcpy(ttainfo->id3v2.genre, str); | |
945 tmp = id3_latin1_ucs4duplicate((id3_latin1_t *)str); | |
946 ttainfo->id3v1.genre = id3_genre_number(tmp); | |
947 g_free(tmp); | |
948 } | |
949 free(str); | |
950 str = NULL; | |
951 | |
952 // comment | |
953 str = tta_input_id3_get_string (tag, ID3_FRAME_COMMENT); | |
954 if(str) { | |
955 strcpy(ttainfo->id3v2.comment, str); | |
956 strncpy(ttainfo->id3v2.comment, str, 30); | |
957 } | |
958 free(str); | |
959 str = NULL; | |
960 | |
961 if(*(ttainfo->id3v2.title) && *(ttainfo->id3v2.artist)) { | |
962 ttainfo->id3v2.id3has = 1; | |
963 ttainfo->id3v2.id3has = 1; | |
964 } | |
965 } | |
966 id3_file_close(id3file); | |
967 } | |
968 return id3v2_size; // not used | |
969 } |