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