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 ****************************************************************************/
44 #define CUE_DIR ROCKBOX_DIR "/cue"
46 bool look_for_cuesheet_file(const char *trackpath
, char *found_cue_path
)
48 /* DEBUGF("look for cue file\n"); */
50 char cuepath
[MAX_PATH
];
53 slash
= strrchr(trackpath
, '/');
56 found_cue_path
= NULL
;
60 strlcpy(cuepath
, trackpath
, MAX_PATH
);
61 dot
= strrchr(cuepath
, '.');
64 if (!file_exists(cuepath
))
66 strcpy(cuepath
, CUE_DIR
);
67 strcat(cuepath
, slash
);
68 char *dot
= strrchr(cuepath
, '.');
70 if (!file_exists(cuepath
))
73 found_cue_path
= NULL
;
79 strlcpy(found_cue_path
, cuepath
, MAX_PATH
);
83 static char *get_string(const char *line
)
87 start
= strchr(line
, '"');
90 start
= strchr(line
, ' ');
96 end
= strchr(++start
, '"');
103 /* parse cuesheet "file" and store the information in "cue" */
104 bool parse_cuesheet(char *file
, struct cuesheet
*cue
)
110 int fd
= open_utf8(file
,O_RDONLY
);
113 /* couln't open the file */
116 if(lseek(fd
, 0, SEEK_CUR
) > 0)
120 memset(cue
, 0, sizeof(struct cuesheet
));
121 strcpy(cue
->path
, file
);
122 cue
->curr_track
= cue
->tracks
;
124 while ( read_line(fd
,line
,MAX_PATH
) && cue
->track_count
< MAX_TRACKS
)
126 s
= skip_whitespace(line
);
128 if (!strncmp(s
, "TRACK", 5))
132 else if (!strncmp(s
, "INDEX 01", 8))
135 s
= skip_whitespace(s
);
137 s
= skip_whitespace(s
);
138 cue
->tracks
[cue
->track_count
-1].offset
= 60*1000 * atoi(s
);
139 s
= strchr(s
,':') + 1;
140 cue
->tracks
[cue
->track_count
-1].offset
+= 1000 * atoi(s
);
141 s
= strchr(s
,':') + 1;
142 cue
->tracks
[cue
->track_count
-1].offset
+= 13 * atoi(s
);
144 else if (!strncmp(s
, "TITLE", 5)
145 || !strncmp(s
, "PERFORMER", 9)
146 || !strncmp(s
, "SONGWRITER", 10))
149 char *string
= get_string(s
);
155 case 'T': /* TITLE */
156 dest
= (cue
->track_count
<= 0) ? cue
->title
:
157 cue
->tracks
[cue
->track_count
-1].title
;
160 case 'P': /* PERFORMER */
161 dest
= (cue
->track_count
<= 0) ? cue
->performer
:
162 cue
->tracks
[cue
->track_count
-1].performer
;
165 case 'S': /* SONGWRITER */
166 dest
= (cue
->track_count
<= 0) ? cue
->songwriter
:
167 cue
->tracks
[cue
->track_count
-1].songwriter
;
175 dest
= iso_decode(string
, dest
, -1, MIN(strlen(string
), MAX_NAME
));
180 strlcpy(dest
, string
, MAX_NAME
*3 + 1);
187 /* If some songs don't have performer info, we copy the cuesheet performer */
189 for (i
= 0; i
< cue
->track_count
; i
++)
191 if (*(cue
->tracks
[i
].performer
) == '\0')
192 strlcpy(cue
->tracks
[i
].performer
, cue
->performer
, MAX_NAME
*3);
194 if (*(cue
->tracks
[i
].songwriter
) == '\0')
195 strlcpy(cue
->tracks
[i
].songwriter
, cue
->songwriter
, MAX_NAME
*3);
201 /* takes care of seeking to a track in a playlist
202 * returns false if audio isn't playing */
203 static bool seek(unsigned long pos
)
205 if (!(audio_status() & AUDIO_STATUS_PLAY
))
211 #if (CONFIG_CODEC == SWCODEC)
212 audio_pre_ff_rewind();
213 audio_ff_rewind(pos
);
216 audio_ff_rewind(pos
);
223 /* returns the index of the track currently being played
224 and updates the information about the current track. */
225 int cue_find_current_track(struct cuesheet
*cue
, unsigned long curpos
)
228 while (i
< cue
->track_count
-1 && cue
->tracks
[i
+1].offset
< curpos
)
231 cue
->curr_track_idx
= i
;
232 cue
->curr_track
= cue
->tracks
+ i
;
236 /* callback that gives list item titles for the cuesheet browser */
237 static const char* list_get_name_cb(int selected_item
,
242 struct cuesheet
*cue
= (struct cuesheet
*)data
;
244 if (selected_item
& 1)
245 strlcpy(buffer
, cue
->tracks
[selected_item
/2].title
, buffer_len
);
247 snprintf(buffer
, buffer_len
, "%02d. %s", selected_item
/2+1,
248 cue
->tracks
[selected_item
/2].performer
);
253 void browse_cuesheet(struct cuesheet
*cue
)
255 struct gui_synclist lists
;
259 char title
[MAX_PATH
];
260 char cuepath
[MAX_PATH
];
261 struct mp3entry
*id3
= audio_current_track();
263 snprintf(title
, MAX_PATH
, "%s: %s", cue
->performer
, cue
->title
);
264 gui_synclist_init(&lists
, list_get_name_cb
, cue
, false, 2, NULL
);
265 gui_synclist_set_nb_items(&lists
, 2*cue
->track_count
);
266 gui_synclist_set_title(&lists
, title
, 0);
271 gui_synclist_select_item(&lists
,
272 2*cue_find_current_track(cue
, id3
->elapsed
));
277 gui_synclist_draw(&lists
);
278 action
= get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
279 if (gui_synclist_do_button(&lists
, &action
, LIST_WRAP_UNLESS_HELD
))
284 id3
= audio_current_track();
285 if (id3
&& *id3
->path
&& strcmp(id3
->path
, "No file!"))
287 look_for_cuesheet_file(id3
->path
, cuepath
);
288 if (id3
->cuesheet
&& !strcmp(cue
->path
, cuepath
))
290 sel
= gui_synclist_get_sel_pos(&lists
);
291 seek(cue
->tracks
[sel
/2].offset
);
295 case ACTION_STD_CANCEL
:
301 bool display_cuesheet_content(char* filename
)
304 struct cuesheet
*cue
= (struct cuesheet
*)plugin_get_buffer(&bufsize
);
305 if (!cue
|| bufsize
< sizeof(struct cuesheet
))
308 if (!parse_cuesheet(filename
, cue
))
311 browse_cuesheet(cue
);
315 /* skips backwards or forward in the current cuesheet
316 * the return value indicates whether we're still in a cusheet after skipping
317 * it also returns false if we weren't in a cuesheet.
318 * direction should be 1 or -1.
320 bool curr_cuesheet_skip(struct cuesheet
*cue
, int direction
, unsigned long curr_pos
)
322 int track
= cue_find_current_track(cue
, curr_pos
);
324 if (direction
>= 0 && track
== cue
->track_count
- 1)
326 /* we want to get out of the cuesheet */
331 if (!(direction
<= 0 && track
== 0))
333 /* If skipping forward, skip to next cuesheet segment. If skipping
334 backward before DEFAULT_SKIP_TRESH milliseconds have elapsed, skip
335 to previous cuesheet segment. If skipping backward after
336 DEFAULT_SKIP_TRESH seconds have elapsed, skip to the start of the
337 current cuesheet segment */
338 if (direction
== 1 ||
339 ((curr_pos
- cue
->tracks
[track
].offset
) < DEFAULT_SKIP_TRESH
))
345 seek(cue
->tracks
[track
].offset
);
351 void cue_spoof_id3(struct cuesheet
*cue
, struct mp3entry
*id3
)
353 if (!cue
|| !cue
->curr_track
)
356 struct cue_track_info
*track
= cue
->curr_track
;
358 id3
->title
= *track
->title
? track
->title
: NULL
;
359 id3
->artist
= *track
->performer
? track
->performer
: NULL
;
360 id3
->composer
= *track
->songwriter
? track
->songwriter
: NULL
;
361 id3
->album
= *cue
->title
? cue
->title
: NULL
;
363 /* if the album artist is the same as the track artist, we hide it. */
364 if (strcmp(cue
->performer
, track
->performer
))
365 id3
->albumartist
= *cue
->performer
? cue
->performer
: NULL
;
367 id3
->albumartist
= NULL
;
369 int i
= cue
->curr_track_idx
;
371 if (id3
->track_string
)
372 snprintf(id3
->track_string
, 10, "%d/%d", i
+1, cue
->track_count
);
375 #ifdef HAVE_LCD_BITMAP
376 static inline void draw_veritcal_line_mark(struct screen
* screen
,
379 screen
->set_drawmode(DRMODE_COMPLEMENT
);
380 screen
->vline(x
, y
, y
+h
-1);
383 /* draw the cuesheet markers for a track of length "tracklen",
384 between (x,y) and (x+w,y) */
385 void cue_draw_markers(struct screen
*screen
, struct cuesheet
*cue
,
386 unsigned long tracklen
,
387 int x
, int y
, int w
, int h
)
390 for (i
=1; i
< cue
->track_count
; i
++)
392 xi
= x
+ (w
* cue
->tracks
[i
].offset
)/tracklen
;
393 draw_veritcal_line_mark(screen
, xi
, y
, h
);
398 bool cuesheet_subtrack_changed(struct mp3entry
*id3
)
400 struct cuesheet
*cue
= id3
->cuesheet
;
401 if (cue
&& (id3
->elapsed
< cue
->curr_track
->offset
402 || (cue
->curr_track_idx
< cue
->track_count
- 1
403 && id3
->elapsed
>= (cue
->curr_track
+1)->offset
)))
405 cue_find_current_track(cue
, id3
->elapsed
);
406 cue_spoof_id3(cue
, id3
);