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 ****************************************************************************/
43 #define CUE_DIR ROCKBOX_DIR "/cue"
45 bool look_for_cuesheet_file(const char *trackpath
, char *found_cue_path
)
47 /* DEBUGF("look for cue file\n"); */
49 char cuepath
[MAX_PATH
];
52 slash
= strrchr(trackpath
, '/');
55 found_cue_path
= NULL
;
59 strlcpy(cuepath
, trackpath
, MAX_PATH
);
60 dot
= strrchr(cuepath
, '.');
63 if (!file_exists(cuepath
))
65 strcpy(cuepath
, CUE_DIR
);
66 strcat(cuepath
, slash
);
67 char *dot
= strrchr(cuepath
, '.');
69 if (!file_exists(cuepath
))
72 found_cue_path
= NULL
;
78 strlcpy(found_cue_path
, cuepath
, MAX_PATH
);
82 static char *get_string(const char *line
)
86 start
= strchr(line
, '"');
89 start
= strchr(line
, ' ');
95 end
= strchr(++start
, '"');
102 /* parse cuesheet "file" and store the information in "cue" */
103 bool parse_cuesheet(char *file
, struct cuesheet
*cue
)
109 int fd
= open_utf8(file
,O_RDONLY
);
112 /* couln't open the file */
115 if(lseek(fd
, 0, SEEK_CUR
) > 0)
119 memset(cue
, 0, sizeof(struct cuesheet
));
120 strcpy(cue
->path
, file
);
121 cue
->curr_track
= cue
->tracks
;
123 while ( read_line(fd
,line
,MAX_PATH
) && cue
->track_count
< MAX_TRACKS
)
125 s
= skip_whitespace(line
);
127 if (!strncmp(s
, "TRACK", 5))
131 else if (!strncmp(s
, "INDEX 01", 8))
134 s
= skip_whitespace(s
);
136 s
= skip_whitespace(s
);
137 cue
->tracks
[cue
->track_count
-1].offset
= 60*1000 * atoi(s
);
138 s
= strchr(s
,':') + 1;
139 cue
->tracks
[cue
->track_count
-1].offset
+= 1000 * atoi(s
);
140 s
= strchr(s
,':') + 1;
141 cue
->tracks
[cue
->track_count
-1].offset
+= 13 * atoi(s
);
143 else if (!strncmp(s
, "TITLE", 5)
144 || !strncmp(s
, "PERFORMER", 9)
145 || !strncmp(s
, "SONGWRITER", 10))
148 char *string
= get_string(s
);
154 case 'T': /* TITLE */
155 dest
= (cue
->track_count
<= 0) ? cue
->title
:
156 cue
->tracks
[cue
->track_count
-1].title
;
159 case 'P': /* PERFORMER */
160 dest
= (cue
->track_count
<= 0) ? cue
->performer
:
161 cue
->tracks
[cue
->track_count
-1].performer
;
164 case 'S': /* SONGWRITER */
165 dest
= (cue
->track_count
<= 0) ? cue
->songwriter
:
166 cue
->tracks
[cue
->track_count
-1].songwriter
;
174 dest
= iso_decode(string
, dest
, -1, MIN(strlen(string
), MAX_NAME
));
179 strlcpy(dest
, string
, MAX_NAME
*3 + 1);
186 /* If some songs don't have performer info, we copy the cuesheet performer */
188 for (i
= 0; i
< cue
->track_count
; i
++)
190 if (*(cue
->tracks
[i
].performer
) == '\0')
191 strlcpy(cue
->tracks
[i
].performer
, cue
->performer
, MAX_NAME
*3);
193 if (*(cue
->tracks
[i
].songwriter
) == '\0')
194 strlcpy(cue
->tracks
[i
].songwriter
, cue
->songwriter
, MAX_NAME
*3);
200 /* takes care of seeking to a track in a playlist
201 * returns false if audio isn't playing */
202 static bool seek(unsigned long pos
)
204 if (!(audio_status() & AUDIO_STATUS_PLAY
))
210 #if (CONFIG_CODEC == SWCODEC)
211 audio_pre_ff_rewind();
212 audio_ff_rewind(pos
);
215 audio_ff_rewind(pos
);
222 /* returns the index of the track currently being played
223 and updates the information about the current track. */
224 int cue_find_current_track(struct cuesheet
*cue
, unsigned long curpos
)
227 while (i
< cue
->track_count
-1 && cue
->tracks
[i
+1].offset
< curpos
)
230 cue
->curr_track_idx
= i
;
231 cue
->curr_track
= cue
->tracks
+ i
;
235 /* callback that gives list item titles for the cuesheet browser */
236 static const char* list_get_name_cb(int selected_item
,
241 struct cuesheet
*cue
= (struct cuesheet
*)data
;
243 if (selected_item
& 1)
244 strlcpy(buffer
, cue
->tracks
[selected_item
/2].title
, buffer_len
);
246 snprintf(buffer
, buffer_len
, "%02d. %s", selected_item
/2+1,
247 cue
->tracks
[selected_item
/2].performer
);
252 void browse_cuesheet(struct cuesheet
*cue
)
254 struct gui_synclist lists
;
258 char title
[MAX_PATH
];
259 char cuepath
[MAX_PATH
];
260 struct mp3entry
*id3
= audio_current_track();
262 snprintf(title
, MAX_PATH
, "%s: %s", cue
->performer
, cue
->title
);
263 gui_synclist_init(&lists
, list_get_name_cb
, cue
, false, 2, NULL
);
264 gui_synclist_set_nb_items(&lists
, 2*cue
->track_count
);
265 gui_synclist_set_title(&lists
, title
, 0);
270 gui_synclist_select_item(&lists
,
271 2*cue_find_current_track(cue
, id3
->elapsed
));
276 gui_synclist_draw(&lists
);
277 action
= get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
278 if (gui_synclist_do_button(&lists
, &action
, LIST_WRAP_UNLESS_HELD
))
283 id3
= audio_current_track();
284 if (id3
&& *id3
->path
&& strcmp(id3
->path
, "No file!"))
286 look_for_cuesheet_file(id3
->path
, cuepath
);
287 if (id3
->cuesheet
&& !strcmp(cue
->path
, cuepath
))
289 sel
= gui_synclist_get_sel_pos(&lists
);
290 seek(cue
->tracks
[sel
/2].offset
);
294 case ACTION_STD_CANCEL
:
300 bool display_cuesheet_content(char* filename
)
303 struct cuesheet
*cue
= (struct cuesheet
*)plugin_get_buffer(&bufsize
);
304 if (!cue
|| bufsize
< sizeof(struct cuesheet
))
307 if (!parse_cuesheet(filename
, cue
))
310 browse_cuesheet(cue
);
314 /* skips backwards or forward in the current cuesheet
315 * the return value indicates whether we're still in a cusheet after skipping
316 * it also returns false if we weren't in a cuesheet.
317 * direction should be 1 or -1.
319 bool curr_cuesheet_skip(struct cuesheet
*cue
, int direction
, unsigned long curr_pos
)
321 int track
= cue_find_current_track(cue
, curr_pos
);
323 if (direction
>= 0 && track
== cue
->track_count
- 1)
325 /* we want to get out of the cuesheet */
330 if (!(direction
<= 0 && track
== 0))
332 /* If skipping forward, skip to next cuesheet segment. If skipping
333 backward before DEFAULT_SKIP_TRESH milliseconds have elapsed, skip
334 to previous cuesheet segment. If skipping backward after
335 DEFAULT_SKIP_TRESH seconds have elapsed, skip to the start of the
336 current cuesheet segment */
337 if (direction
== 1 ||
338 ((curr_pos
- cue
->tracks
[track
].offset
) < DEFAULT_SKIP_TRESH
))
344 seek(cue
->tracks
[track
].offset
);
349 const char *get_cuesheetid3_token(struct wps_token
*token
, struct mp3entry
*id3
,
350 int offset_tracks
, char *buf
, int buf_size
)
352 struct cuesheet
*cue
= id3
?id3
->cuesheet
:NULL
;
353 if (!cue
|| !cue
->curr_track
)
356 struct cue_track_info
*track
= cue
->curr_track
;
359 if (cue
->curr_track_idx
+offset_tracks
< cue
->track_count
)
360 track
+=offset_tracks
;
366 case WPS_TOKEN_METADATA_ARTIST
:
367 return *track
->performer
? track
->performer
: NULL
;
368 case WPS_TOKEN_METADATA_COMPOSER
:
369 return *track
->songwriter
? track
->songwriter
: NULL
;
370 case WPS_TOKEN_METADATA_ALBUM
:
371 return *cue
->title
? cue
->title
: NULL
;
372 case WPS_TOKEN_METADATA_ALBUM_ARTIST
:
373 return *cue
->performer
? cue
->performer
: NULL
;
374 case WPS_TOKEN_METADATA_TRACK_TITLE
:
375 return *track
->title
? track
->title
: NULL
;
376 case WPS_TOKEN_METADATA_TRACK_NUMBER
:
377 snprintf(buf
, buf_size
, "%d/%d",
378 cue
->curr_track_idx
+offset_tracks
+1, cue
->track_count
);
384 #ifdef HAVE_LCD_BITMAP
385 static inline void draw_veritcal_line_mark(struct screen
* screen
,
388 screen
->set_drawmode(DRMODE_COMPLEMENT
);
389 screen
->vline(x
, y
, y
+h
-1);
392 /* draw the cuesheet markers for a track of length "tracklen",
393 between (x,y) and (x+w,y) */
394 void cue_draw_markers(struct screen
*screen
, struct cuesheet
*cue
,
395 unsigned long tracklen
,
396 int x
, int y
, int w
, int h
)
399 for (i
=1; i
< cue
->track_count
; i
++)
401 xi
= x
+ (w
* cue
->tracks
[i
].offset
)/tracklen
;
402 draw_veritcal_line_mark(screen
, xi
, y
, h
);
407 bool cuesheet_subtrack_changed(struct mp3entry
*id3
)
409 struct cuesheet
*cue
= id3
->cuesheet
;
410 if (cue
&& (id3
->elapsed
< cue
->curr_track
->offset
411 || (cue
->curr_track_idx
< cue
->track_count
- 1
412 && id3
->elapsed
>= (cue
->curr_track
+1)->offset
)))
414 cue_find_current_track(cue
, id3
->elapsed
);