1048
|
1
|
|
2 /*
|
|
3 todo:
|
|
4 - if any cdio_* returns an error, stop playing immediately
|
|
5 */
|
|
6
|
|
7 #include <string.h>
|
|
8 #include <stdlib.h>
|
|
9 #include <unistd.h>
|
|
10 #include <errno.h>
|
|
11 #include <libgen.h>
|
|
12
|
|
13 #include <cdio/cdio.h>
|
|
14 #include <cdio/cdtext.h>
|
|
15 #include <cdio/track.h>
|
|
16 #include <cdio/cdda.h>
|
|
17 #include <cdio/audio.h>
|
|
18 #include <cdio/sector.h>
|
|
19 #include <cdio/cd_types.h>
|
|
20
|
|
21 #include <glib.h>
|
|
22
|
|
23 #include <audacious/i18n.h>
|
|
24 #include <audacious/configdb.h>
|
|
25 #include <audacious/util.h>
|
|
26 #include <audacious/titlestring.h>
|
|
27 #include <audacious/output.h>
|
|
28
|
|
29 #define DEF_STRING_LEN 256
|
|
30
|
|
31
|
|
32 typedef struct {
|
|
33
|
|
34 char performer[DEF_STRING_LEN];
|
|
35 char name[DEF_STRING_LEN];
|
|
36 char genre[DEF_STRING_LEN];
|
|
37 int startlsn;
|
|
38 int endlsn;
|
|
39
|
|
40 } trackinfo_t;
|
|
41
|
|
42
|
|
43 static int firsttrackno = -1;
|
|
44 static int lasttrackno = -1;
|
|
45 static cdrom_drive_t *pcdrom_drive = NULL;
|
|
46 static trackinfo_t *trackinfo = NULL;
|
|
47 static char album_name[DEF_STRING_LEN];
|
|
48 static gboolean use_dao = FALSE;
|
|
49 static gboolean is_paused = FALSE;
|
|
50 static int playing_track = -1;
|
|
51
|
|
52
|
|
53 static void cdaudio_init();
|
|
54 static void cdaudio_about();
|
|
55 static void cdaudio_configure();
|
|
56 static gint cdaudio_is_our_file(gchar *filename);
|
|
57 static GList *cdaudio_scan_dir(gchar *dirname);
|
|
58 static void cdaudio_play_file(InputPlayback *pinputplayback);
|
|
59 static void cdaudio_stop(InputPlayback *pinputplayback);
|
|
60 static void cdaudio_pause(InputPlayback *pinputplayback, gshort paused);
|
|
61 static void cdaudio_seek(InputPlayback *pinputplayback, gint time);
|
|
62 static gint cdaudio_get_time(InputPlayback *pinputplayback);
|
|
63 static gint cdaudio_get_volume(gint *l, gint *r);
|
|
64 static gint cdaudio_set_volume(gint l, gint r);
|
|
65 static void cdaudio_cleanup();
|
|
66 static void cdaudio_get_song_info(gchar *filename, gchar **title, gint *length);
|
|
67 static void cdaudio_file_info_box(gchar *filename);
|
|
68 static TitleInput *cdaudio_get_song_tuple(gchar *filename);
|
|
69
|
|
70 static int calculate_track_length(int startlsn, int endlsn);
|
|
71 static int find_trackno_from_filename(char *filename);
|
|
72
|
|
73 /*
|
|
74 static int calculate_digit_sum(int n);
|
|
75 static unsigned long calculate_cddb_discid();
|
|
76 */
|
|
77
|
|
78
|
|
79 static InputPlugin inputplugin = {
|
|
80 NULL,
|
|
81 NULL,
|
|
82 NULL,
|
|
83 cdaudio_init,
|
|
84 cdaudio_about,
|
|
85 cdaudio_configure,
|
|
86 cdaudio_is_our_file,
|
|
87 cdaudio_scan_dir,
|
|
88 cdaudio_play_file,
|
|
89 cdaudio_stop,
|
|
90 cdaudio_pause,
|
|
91 cdaudio_seek,
|
|
92 NULL,
|
|
93 cdaudio_get_time,
|
|
94 cdaudio_get_volume,
|
|
95 cdaudio_set_volume,
|
|
96 cdaudio_cleanup,
|
|
97 NULL,
|
|
98 NULL,
|
|
99 NULL,
|
|
100 NULL,
|
|
101 cdaudio_get_song_info,
|
|
102 cdaudio_file_info_box,
|
|
103 NULL,
|
|
104 cdaudio_get_song_tuple
|
|
105 };
|
|
106
|
|
107
|
|
108 InputPlugin *get_iplugin_info(void)
|
|
109 {
|
|
110 inputplugin.description = g_strdup_printf("Zither's CD Audio Plugin");
|
|
111 return &inputplugin;
|
|
112 }
|
|
113
|
|
114
|
|
115 void cdaudio_init()
|
|
116 {
|
|
117 cdio_init();
|
|
118 }
|
|
119
|
|
120 void cdaudio_about()
|
|
121 {
|
|
122 }
|
|
123
|
|
124 void cdaudio_configure()
|
|
125 {
|
|
126 }
|
|
127
|
|
128 gint cdaudio_is_our_file(gchar *filename)
|
|
129 {
|
|
130 printf("is_our_file(\"%s\")\n", filename);
|
|
131 if ((filename != NULL) && strlen(filename) > 4 && (!strcasecmp(filename + strlen(filename) - 4, ".cda"))) {
|
|
132 if (pcdrom_drive == NULL) { /* no CD information yet */
|
|
133 printf("No CD information, rescanning\n");
|
|
134 cdaudio_scan_dir("/mnt/cdrom"); // todo: :)
|
|
135 }
|
|
136
|
|
137 if (cdio_get_media_changed(pcdrom_drive->p_cdio)) {
|
|
138 printf("CD changed, rescanning\n");
|
|
139 cdaudio_scan_dir("/mnt/cdrom"); // todo: change the hardcoded path
|
|
140 }
|
|
141
|
|
142 return 1;
|
|
143 }
|
|
144 else
|
|
145 return 0;
|
|
146 }
|
|
147
|
|
148 GList *cdaudio_scan_dir(gchar *dirname)
|
|
149 {
|
|
150 printf("scan_dir(\"%s\")\n", dirname);
|
|
151
|
|
152 if (strstr(dirname, "/mnt/cdrom") == NULL) // todo: replace this with a more standardised string
|
|
153 return NULL;
|
|
154
|
|
155 /* find the first available, audio capable, cd drive */
|
|
156 char **ppcd_drives = cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false);
|
|
157 if (ppcd_drives != NULL) { /* we have at least one audio capable cd drive */
|
|
158 pcdrom_drive = cdio_cddap_identify(*ppcd_drives, 1, NULL);
|
|
159 }
|
|
160 else {
|
|
161 printf("Unable find or access a CD-ROM drive with an audio CD in it.\n");
|
|
162 return NULL;
|
|
163 }
|
|
164 cdio_free_device_list(ppcd_drives);
|
|
165
|
|
166 /* get track information */
|
|
167 firsttrackno = cdio_get_first_track_num(pcdrom_drive->p_cdio);
|
|
168 lasttrackno = cdio_get_last_track_num(pcdrom_drive->p_cdio);
|
|
169
|
|
170 /* add track "file" names to the list */
|
|
171 GList *list = NULL;
|
|
172 if (trackinfo != NULL)
|
|
173 free(trackinfo);
|
|
174 trackinfo = (trackinfo_t *) malloc(sizeof(trackinfo_t) * (lasttrackno + 1));
|
|
175 int trackno;
|
|
176 for (trackno = firsttrackno; trackno <= lasttrackno; trackno++) {
|
|
177 list = g_list_append(list, g_strdup_printf("track%02u.cda", trackno));
|
|
178 cdtext_t *pcdtext = cdio_get_cdtext(pcdrom_drive->p_cdio, trackno);
|
|
179
|
|
180 if (pcdtext != NULL) {
|
|
181 strcpy(trackinfo[trackno].performer, pcdtext->field[CDTEXT_PERFORMER] != NULL ? pcdtext->field[CDTEXT_PERFORMER] : "");
|
|
182 strcpy(trackinfo[trackno].name, pcdtext->field[CDTEXT_TITLE] != NULL ? pcdtext->field[CDTEXT_TITLE] : "");
|
|
183 strcpy(trackinfo[trackno].genre, pcdtext->field[CDTEXT_GENRE] != NULL ? pcdtext->field[CDTEXT_GENRE] : "");
|
|
184 }
|
|
185 else {
|
|
186 strcpy(trackinfo[trackno].performer, "");
|
|
187 strcpy(trackinfo[trackno].name, "");
|
|
188 strcpy(trackinfo[trackno].genre, "");
|
|
189 }
|
|
190
|
|
191 if (strlen(trackinfo[trackno].name) == 0)
|
|
192 sprintf(trackinfo[trackno].name, "CD Audio Track %02u", trackno);
|
|
193
|
|
194 trackinfo[trackno].startlsn = cdio_get_track_lsn(pcdrom_drive->p_cdio, trackno);
|
|
195 trackinfo[trackno].endlsn = cdio_get_track_last_lsn(pcdrom_drive->p_cdio, trackno);
|
|
196 }
|
|
197
|
|
198 return list;
|
|
199 }
|
|
200
|
|
201 void cdaudio_play_file(InputPlayback *pinputplayback)
|
|
202 {
|
|
203 printf("play_file(\"%s\")\n", pinputplayback->filename);
|
|
204
|
|
205 if (trackinfo == NULL) {
|
|
206 printf("No CD information, rescanning\n");
|
|
207 cdaudio_scan_dir("/mnt/cdrom"); // todo: change the hardcoded path
|
|
208 }
|
|
209
|
|
210 if (cdio_get_media_changed(pcdrom_drive->p_cdio)) {
|
|
211 printf("CD changed, rescanning\n");
|
|
212 cdaudio_scan_dir("/mnt/cdrom"); // todo: change the hardcoded path
|
|
213 }
|
|
214
|
|
215 int trackno = find_trackno_from_filename(pinputplayback->filename);
|
|
216 if (trackno < firsttrackno || trackno > lasttrackno)
|
|
217 return;
|
|
218
|
|
219 msf_t startmsf, endmsf;
|
|
220 cdio_lsn_to_msf(trackinfo[trackno].startlsn, &startmsf);
|
|
221 cdio_lsn_to_msf(trackinfo[trackno].endlsn, &endmsf);
|
|
222 cdio_audio_play_msf(pcdrom_drive->p_cdio, &startmsf, &endmsf);
|
|
223
|
|
224 pinputplayback->playing = TRUE;
|
|
225 playing_track = trackno;
|
|
226
|
|
227 char title[DEF_STRING_LEN];
|
|
228
|
|
229 if (strlen(trackinfo[trackno].performer) > 0) {
|
|
230 strcpy(title, trackinfo[trackno].performer);
|
|
231 strcat(title, " - ");
|
|
232 }
|
|
233 else
|
|
234 strcpy(title, "");
|
|
235 strcat(title, trackinfo[trackno].name);
|
|
236
|
|
237 inputplugin.set_info(title, calculate_track_length(trackinfo[trackno].startlsn, trackinfo[trackno].endlsn), 128000, 44100, 2);
|
|
238 }
|
|
239
|
|
240 void cdaudio_stop(InputPlayback *pinputplayback)
|
|
241 {
|
|
242 printf("stop(\"%s\")\n", pinputplayback->filename);
|
|
243
|
|
244 cdio_audio_stop(pcdrom_drive->p_cdio);
|
|
245 pinputplayback->playing = FALSE;
|
|
246 playing_track = -1;
|
|
247 }
|
|
248
|
|
249 void cdaudio_pause(InputPlayback *pinputplayback, gshort paused)
|
|
250 {
|
|
251 if (!is_paused) {
|
|
252 is_paused = TRUE;
|
|
253 cdio_audio_pause(pcdrom_drive->p_cdio);
|
|
254 }
|
|
255 else {
|
|
256 is_paused = FALSE;
|
|
257 cdio_audio_resume(pcdrom_drive->p_cdio);
|
|
258 }
|
|
259 }
|
|
260
|
|
261 void cdaudio_seek(InputPlayback *pinputplayback, gint time)
|
|
262 {
|
|
263 printf("seek(%d)\n", time);
|
|
264 if (playing_track == -1)
|
|
265 return;
|
|
266
|
|
267 int lsnoffs = (time * 75);
|
|
268 int startlsn = trackinfo[playing_track].startlsn + lsnoffs;
|
|
269
|
|
270 msf_t startmsf, endmsf;
|
|
271 cdio_lsn_to_msf(startlsn, &startmsf);
|
|
272 cdio_lsn_to_msf(trackinfo[playing_track].endlsn, &endmsf);
|
|
273 cdio_audio_play_msf(pcdrom_drive->p_cdio, &startmsf, &endmsf);
|
|
274 }
|
|
275
|
|
276 gint cdaudio_get_time(InputPlayback *pinputplayback)
|
|
277 {
|
|
278 if (playing_track == -1)
|
|
279 return -1;
|
|
280
|
|
281 cdio_subchannel_t subchannel;
|
|
282 cdio_audio_read_subchannel(pcdrom_drive->p_cdio, &subchannel);
|
|
283 int currentlsn = cdio_msf_to_lsn(&subchannel.abs_addr);
|
|
284
|
|
285 /* check to see if we have reached the end of the song */
|
|
286 if (currentlsn == trackinfo[playing_track].endlsn) {
|
|
287 cdaudio_stop(pinputplayback);
|
|
288 return -1;
|
|
289 }
|
|
290
|
|
291 int seconds = calculate_track_length(trackinfo[playing_track].startlsn, currentlsn);
|
|
292 // printf("%d\n", seconds);
|
|
293 return seconds;
|
|
294 }
|
|
295
|
|
296 gint cdaudio_get_volume(gint *l, gint *r)
|
|
297 {
|
|
298 // printf("get_volume()\n");
|
|
299
|
|
300 cdio_audio_volume_t volume;;
|
|
301 cdio_audio_set_volume(pcdrom_drive->p_cdio, &volume);
|
|
302 *l = volume.level[0];
|
|
303 *r = volume.level[1];
|
|
304
|
|
305 return 0;
|
|
306 }
|
|
307
|
|
308 gint cdaudio_set_volume(gint l, gint r)
|
|
309 {
|
|
310 printf("set_volume(%d, %d)\n", l, r);
|
|
311
|
|
312 cdio_audio_volume_t volume = {{l, r, 0, 0}};
|
|
313 cdio_audio_set_volume(pcdrom_drive->p_cdio, &volume);
|
|
314
|
|
315 return 0;
|
|
316 }
|
|
317
|
|
318 void cdaudio_cleanup()
|
|
319 {
|
|
320 cdio_destroy(pcdrom_drive->p_cdio);
|
|
321 }
|
|
322
|
|
323 void cdaudio_get_song_info(gchar *filename, gchar **title, gint *length)
|
|
324 {
|
|
325 printf("get_song_info(\"%s\")\n", filename);
|
|
326 }
|
|
327
|
|
328 void cdaudio_file_info_box(gchar *filename)
|
|
329 {
|
|
330
|
|
331 }
|
|
332
|
|
333 TitleInput *cdaudio_get_song_tuple(gchar *filename)
|
|
334 {
|
|
335 printf("get_song_tuple(\"%s\")\n", filename);
|
|
336
|
|
337 TitleInput *tuple = bmp_title_input_new();
|
|
338
|
|
339 /* return information about the requested track */
|
|
340 int trackno = find_trackno_from_filename(filename);
|
|
341 if (trackno < firsttrackno || trackno > lasttrackno)
|
|
342 return NULL;
|
|
343
|
|
344 tuple->performer = strlen(trackinfo[trackno].performer) > 0 ? g_strdup(trackinfo[trackno].performer) : NULL;
|
|
345 tuple->album_name = strlen(album_name) > 0 ? g_strdup(album_name) : NULL;
|
|
346 tuple->track_name = strlen(trackinfo[trackno].name) > 0 ? g_strdup(trackinfo[trackno].name) : NULL;
|
|
347 tuple->track_number = trackno;
|
|
348 tuple->file_name = g_strdup(basename(filename));
|
|
349 tuple->file_path = g_strdup(basename(filename));
|
|
350 tuple->file_ext = g_strdup("cda");
|
|
351 tuple->length = calculate_track_length(trackinfo[trackno].startlsn, trackinfo[trackno].endlsn);
|
|
352 tuple->genre = strlen(trackinfo[trackno].genre) > 0 ? g_strdup(trackinfo[trackno].genre) : NULL;
|
|
353 //tuple->year = 0; todo: set the year
|
|
354
|
|
355 return tuple;
|
|
356 }
|
|
357
|
|
358
|
|
359 /* auxiliar functions */
|
|
360
|
|
361 /*
|
|
362 static int calculate_digit_sum(int n)
|
|
363 {
|
|
364 int ret = 0;
|
|
365
|
|
366 while (1) {
|
|
367 ret += n % 10;
|
|
368 n = n / 10;
|
|
369 if (n == 0)
|
|
370 return ret;
|
|
371 }
|
|
372 }
|
|
373 */
|
|
374
|
|
375 /*
|
|
376 static unsigned long calculate_cddb_discid()
|
|
377 {
|
|
378 int trackno, t, n = 0;
|
|
379 msf_t startmsf;
|
|
380 msf_t msf;
|
|
381
|
|
382 for (trackno = firsttrackno; trackno <= lasttrackno; trackno++) {
|
|
383 cdio_get_track_msf(pcdrom_drive->p_cdio, trackno, &msf);
|
|
384 n += calculate_digit_sum(cdio_audio_get_msf_seconds(&msf));
|
|
385 }
|
|
386
|
|
387 cdio_get_track_msf(pcdrom_drive->p_cdio, 1, &startmsf);
|
|
388 cdio_get_track_msf(pcdrom_drive->p_cdio, CDIO_CDROM_LEADOUT_TRACK, &msf);
|
|
389
|
|
390 t = cdio_audio_get_msf_seconds(&msf) - cdio_audio_get_msf_seconds(&startmsf);
|
|
391
|
|
392 return ((n % 0xFF) << 24 | t << 8 | (lasttrackno - firsttrackno + 1));
|
|
393 }
|
|
394 */
|
|
395
|
|
396 int calculate_track_length(int startlsn, int endlsn)
|
|
397 {
|
|
398 return ((endlsn - startlsn + 1) * 1000) / 75;
|
|
399 }
|
|
400
|
|
401 int find_trackno_from_filename(char *filename)
|
|
402 {
|
|
403 if ((filename == NULL) || strlen(filename) <= 6)
|
|
404 return -1;
|
|
405
|
|
406 char tracknostr[3];
|
|
407 strncpy(tracknostr, filename + strlen(filename) - 6, 2);
|
|
408 tracknostr[2] = '\0';
|
|
409 return strtol(tracknostr, NULL, 10);
|
|
410 }
|