comparison src/tta/aud-tta.c @ 290:fbd06b4aa776 trunk

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