1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Nicolas Pennequin, Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
45 #define CUE_DIR ROCKBOX_DIR "/cue"
47 struct cuesheet
*curr_cue
;
49 #if CONFIG_CODEC != SWCODEC
50 /* special trickery because the hwcodec playback engine is in firmware/ */
51 static bool cuesheet_handler(const char *filename
)
53 return cuesheet_is_enabled() && look_for_cuesheet_file(filename
, NULL
);
57 void cuesheet_init(void)
59 if (global_settings
.cuesheet
) {
60 curr_cue
= (struct cuesheet
*)buffer_alloc(sizeof(struct cuesheet
));
61 #if CONFIG_CODEC != SWCODEC
62 audio_set_cuesheet_callback(cuesheet_handler
);
69 bool cuesheet_is_enabled(void)
71 return (curr_cue
!= NULL
);
74 bool look_for_cuesheet_file(const char *trackpath
, char *found_cue_path
)
76 /* DEBUGF("look for cue file\n"); */
78 char cuepath
[MAX_PATH
];
81 slash
= strrchr(trackpath
, '/');
84 found_cue_path
= NULL
;
88 strncpy(cuepath
, trackpath
, MAX_PATH
);
89 dot
= strrchr(cuepath
, '.');
92 if (!file_exists(cuepath
))
94 strcpy(cuepath
, CUE_DIR
);
95 strcat(cuepath
, slash
);
96 char *dot
= strrchr(cuepath
, '.');
98 if (!file_exists(cuepath
))
101 found_cue_path
= NULL
;
107 strncpy(found_cue_path
, cuepath
, MAX_PATH
);
111 static char *get_string(const char *line
)
115 start
= strchr(line
, '"');
118 start
= strchr(line
, ' ');
124 end
= strchr(++start
, '"');
131 /* parse cuesheet "file" and store the information in "cue" */
132 bool parse_cuesheet(char *file
, struct cuesheet
*cue
)
138 DEBUGF("cue parse\n");
139 int fd
= open_utf8(file
,O_RDONLY
);
142 /* couln't open the file */
145 if(lseek(fd
, 0, SEEK_CUR
) > 0)
149 memset(cue
, 0, sizeof(struct cuesheet
));
150 strcpy(cue
->path
, file
);
151 cue
->curr_track
= cue
->tracks
;
153 while ( read_line(fd
,line
,MAX_PATH
) && cue
->track_count
< MAX_TRACKS
)
155 s
= skip_whitespace(line
);
157 if (!strncmp(s
, "TRACK", 5))
161 else if (!strncmp(s
, "INDEX 01", 8))
164 s
= skip_whitespace(s
);
166 s
= skip_whitespace(s
);
167 cue
->tracks
[cue
->track_count
-1].offset
= 60*1000 * atoi(s
);
168 s
= strchr(s
,':') + 1;
169 cue
->tracks
[cue
->track_count
-1].offset
+= 1000 * atoi(s
);
170 s
= strchr(s
,':') + 1;
171 cue
->tracks
[cue
->track_count
-1].offset
+= 13 * atoi(s
);
173 else if (!strncmp(s
, "TITLE", 5)
174 || !strncmp(s
, "PERFORMER", 9)
175 || !strncmp(s
, "SONGWRITER", 10))
178 char *string
= get_string(s
);
184 case 'T': /* TITLE */
185 dest
= (cue
->track_count
<= 0) ? cue
->title
:
186 cue
->tracks
[cue
->track_count
-1].title
;
189 case 'P': /* PERFORMER */
190 dest
= (cue
->track_count
<= 0) ? cue
->performer
:
191 cue
->tracks
[cue
->track_count
-1].performer
;
194 case 'S': /* SONGWRITER */
195 dest
= (cue
->track_count
<= 0) ? cue
->songwriter
:
196 cue
->tracks
[cue
->track_count
-1].songwriter
;
204 dest
= iso_decode(string
, dest
, -1, MIN(strlen(string
), MAX_NAME
));
209 strncpy(dest
, string
, MAX_NAME
*3);
210 dest
[MAX_NAME
*3] = '\0';
217 /* If some songs don't have performer info, we copy the cuesheet performer */
219 for (i
= 0; i
< cue
->track_count
; i
++)
221 if (*(cue
->tracks
[i
].performer
) == '\0')
222 strncpy(cue
->tracks
[i
].performer
, cue
->performer
, MAX_NAME
*3);
224 if (*(cue
->tracks
[i
].songwriter
) == '\0')
225 strncpy(cue
->tracks
[i
].songwriter
, cue
->songwriter
, MAX_NAME
*3);
231 /* takes care of seeking to a track in a playlist
232 * returns false if audio isn't playing */
233 static bool seek(unsigned long pos
)
235 if (!(audio_status() & AUDIO_STATUS_PLAY
))
241 #if (CONFIG_CODEC == SWCODEC)
242 audio_pre_ff_rewind();
243 audio_ff_rewind(pos
);
246 audio_ff_rewind(pos
);
253 /* returns the index of the track currently being played
254 and updates the information about the current track. */
255 int cue_find_current_track(struct cuesheet
*cue
, unsigned long curpos
)
258 while (i
< cue
->track_count
-1 && cue
->tracks
[i
+1].offset
< curpos
)
261 cue
->curr_track_idx
= i
;
262 cue
->curr_track
= cue
->tracks
+ i
;
266 /* callback that gives list item titles for the cuesheet browser */
267 static char *list_get_name_cb(int selected_item
,
272 struct cuesheet
*cue
= (struct cuesheet
*)data
;
274 if (selected_item
& 1)
275 strncpy(buffer
, cue
->tracks
[selected_item
/2].title
, buffer_len
);
277 snprintf(buffer
, buffer_len
, "%02d. %s", selected_item
/2+1,
278 cue
->tracks
[selected_item
/2].performer
);
283 void browse_cuesheet(struct cuesheet
*cue
)
285 struct gui_synclist lists
;
289 char title
[MAX_PATH
];
290 char cuepath
[MAX_PATH
];
291 struct mp3entry
*id3
= audio_current_track();
293 snprintf(title
, MAX_PATH
, "%s: %s", cue
->performer
, cue
->title
);
294 gui_synclist_init(&lists
, list_get_name_cb
, cue
, false, 2, NULL
);
295 gui_synclist_set_nb_items(&lists
, 2*cue
->track_count
);
296 gui_synclist_set_title(&lists
, title
, 0);
298 if (id3
&& *id3
->path
&& strcmp(id3
->path
, "No file!"))
300 look_for_cuesheet_file(id3
->path
, cuepath
);
303 if (id3
&& id3
->cuesheet_type
&& !strcmp(cue
->path
, cuepath
))
305 gui_synclist_select_item(&lists
,
306 2*cue_find_current_track(cue
, id3
->elapsed
));
311 gui_synclist_draw(&lists
);
312 action
= get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
313 if (gui_synclist_do_button(&lists
, &action
, LIST_WRAP_UNLESS_HELD
))
318 id3
= audio_current_track();
319 if (id3
&& *id3
->path
&& strcmp(id3
->path
, "No file!"))
321 look_for_cuesheet_file(id3
->path
, cuepath
);
322 if (id3
->cuesheet_type
&& !strcmp(cue
->path
, cuepath
))
324 sel
= gui_synclist_get_sel_pos(&lists
);
325 seek(cue
->tracks
[sel
/2].offset
);
329 case ACTION_STD_CANCEL
:
335 bool display_cuesheet_content(char* filename
)
338 struct cuesheet
*cue
= (struct cuesheet
*)plugin_get_buffer(&bufsize
);
339 if (!cue
|| bufsize
< sizeof(struct cuesheet
))
342 if (!parse_cuesheet(filename
, cue
))
345 browse_cuesheet(cue
);
349 /* skips backwards or forward in the current cuesheet
350 * the return value indicates whether we're still in a cusheet after skipping
351 * it also returns false if we weren't in a cuesheet.
352 * direction should be 1 or -1.
354 bool curr_cuesheet_skip(int direction
, unsigned long curr_pos
)
356 int track
= cue_find_current_track(curr_cue
, curr_pos
);
358 if (direction
>= 0 && track
== curr_cue
->track_count
- 1)
360 /* we want to get out of the cuesheet */
365 if (!(direction
<= 0 && track
== 0))
368 seek(curr_cue
->tracks
[track
].offset
);
374 void cue_spoof_id3(struct cuesheet
*cue
, struct mp3entry
*id3
)
376 if (!cue
|| !cue
->curr_track
)
379 struct cue_track_info
*track
= cue
->curr_track
;
381 id3
->title
= *track
->title
? track
->title
: NULL
;
382 id3
->artist
= *track
->performer
? track
->performer
: NULL
;
383 id3
->composer
= *track
->songwriter
? track
->songwriter
: NULL
;
384 id3
->album
= *cue
->title
? cue
->title
: NULL
;
386 /* if the album artist is the same as the track artist, we hide it. */
387 if (strcmp(cue
->performer
, track
->performer
))
388 id3
->albumartist
= *cue
->performer
? cue
->performer
: NULL
;
390 id3
->albumartist
= NULL
;
392 int i
= cue
->curr_track_idx
;
394 if (id3
->track_string
)
395 snprintf(id3
->track_string
, 10, "%d/%d", i
+1, cue
->track_count
);
398 #ifdef HAVE_LCD_BITMAP
399 static inline void draw_veritcal_line_mark(struct screen
* screen
,
402 screen
->set_drawmode(DRMODE_COMPLEMENT
);
403 screen
->vline(x
, y
, y
+h
-1);
406 /* draw the cuesheet markers for a track of length "tracklen",
407 between (x1,y) and (x2,y) */
408 void cue_draw_markers(struct screen
*screen
, unsigned long tracklen
,
409 int x1
, int x2
, int y
, int h
)
413 for (i
=1; i
< curr_cue
->track_count
; i
++)
415 xi
= x1
+ (w
* curr_cue
->tracks
[i
].offset
)/tracklen
;
416 draw_veritcal_line_mark(screen
, xi
, y
, h
);