mpegplayer:
[kugel-rb.git] / apps / cuesheet.c
blobef1eff8a85b19bcdb75d2b64b679f6f952d94d47
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include "system.h"
28 #include "audio.h"
29 #include "kernel.h"
30 #include "logf.h"
31 #include "sprintf.h"
32 #include "misc.h"
33 #include "screens.h"
34 #include "list.h"
35 #include "action.h"
36 #include "lang.h"
37 #include "debug.h"
38 #include "settings.h"
39 #include "plugin.h"
40 #include "playback.h"
41 #include "cuesheet.h"
42 #include "gui/wps.h"
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];
51 char *dot, *slash;
53 slash = strrchr(trackpath, '/');
54 if (!slash)
56 found_cue_path = NULL;
57 return false;
60 strlcpy(cuepath, trackpath, MAX_PATH);
61 dot = strrchr(cuepath, '.');
62 strcpy(dot, ".cue");
64 if (!file_exists(cuepath))
66 strcpy(cuepath, CUE_DIR);
67 strcat(cuepath, slash);
68 char *dot = strrchr(cuepath, '.');
69 strcpy(dot, ".cue");
70 if (!file_exists(cuepath))
72 if (found_cue_path)
73 found_cue_path = NULL;
74 return false;
78 if (found_cue_path)
79 strlcpy(found_cue_path, cuepath, MAX_PATH);
80 return true;
83 static char *get_string(const char *line)
85 char *start, *end;
87 start = strchr(line, '"');
88 if (!start)
90 start = strchr(line, ' ');
92 if (!start)
93 return NULL;
96 end = strchr(++start, '"');
97 if (end)
98 *end = '\0';
100 return start;
103 /* parse cuesheet "file" and store the information in "cue" */
104 bool parse_cuesheet(char *file, struct cuesheet *cue)
106 char line[MAX_PATH];
107 char *s;
108 bool utf8 = false;
110 int fd = open_utf8(file,O_RDONLY);
111 if (fd < 0)
113 /* couln't open the file */
114 return false;
116 if(lseek(fd, 0, SEEK_CUR) > 0)
117 utf8 = true;
119 /* Initialization */
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))
130 cue->track_count++;
132 else if (!strncmp(s, "INDEX 01", 8))
134 s = strchr(s,' ');
135 s = skip_whitespace(s);
136 s = strchr(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))
148 char *dest = NULL;
149 char *string = get_string(s);
150 if (!string)
151 break;
153 switch (*s)
155 case 'T': /* TITLE */
156 dest = (cue->track_count <= 0) ? cue->title :
157 cue->tracks[cue->track_count-1].title;
158 break;
160 case 'P': /* PERFORMER */
161 dest = (cue->track_count <= 0) ? cue->performer :
162 cue->tracks[cue->track_count-1].performer;
163 break;
165 case 'S': /* SONGWRITER */
166 dest = (cue->track_count <= 0) ? cue->songwriter :
167 cue->tracks[cue->track_count-1].songwriter;
168 break;
171 if (dest)
173 if (!utf8)
175 dest = iso_decode(string, dest, -1, MIN(strlen(string), MAX_NAME));
176 *dest = '\0';
178 else
180 strlcpy(dest, string, MAX_NAME*3 + 1);
185 close(fd);
187 /* If some songs don't have performer info, we copy the cuesheet performer */
188 int i;
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);
198 return true;
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))
207 return false;
209 else
211 #if (CONFIG_CODEC == SWCODEC)
212 audio_pre_ff_rewind();
213 audio_ff_rewind(pos);
214 #else
215 audio_pause();
216 audio_ff_rewind(pos);
217 audio_resume();
218 #endif
219 return true;
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)
227 int i=0;
228 while (i < cue->track_count-1 && cue->tracks[i+1].offset < curpos)
229 i++;
231 cue->curr_track_idx = i;
232 cue->curr_track = cue->tracks + i;
233 return i;
236 /* callback that gives list item titles for the cuesheet browser */
237 static const char* list_get_name_cb(int selected_item,
238 void *data,
239 char *buffer,
240 size_t buffer_len)
242 struct cuesheet *cue = (struct cuesheet *)data;
244 if (selected_item & 1)
245 strlcpy(buffer, cue->tracks[selected_item/2].title, buffer_len);
246 else
247 snprintf(buffer, buffer_len, "%02d. %s", selected_item/2+1,
248 cue->tracks[selected_item/2].performer);
250 return buffer;
253 void browse_cuesheet(struct cuesheet *cue)
255 struct gui_synclist lists;
256 int action;
257 bool done = false;
258 int sel;
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);
269 if (id3)
271 gui_synclist_select_item(&lists,
272 2*cue_find_current_track(cue, id3->elapsed));
275 while (!done)
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))
280 continue;
281 switch (action)
283 case ACTION_STD_OK:
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);
294 break;
295 case ACTION_STD_CANCEL:
296 done = true;
301 bool display_cuesheet_content(char* filename)
303 size_t bufsize = 0;
304 struct cuesheet *cue = (struct cuesheet *)plugin_get_buffer(&bufsize);
305 if (!cue || bufsize < sizeof(struct cuesheet))
306 return false;
308 if (!parse_cuesheet(filename, cue))
309 return false;
311 browse_cuesheet(cue);
312 return true;
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 */
327 return false;
329 else
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))
341 track += direction;
345 seek(cue->tracks[track].offset);
346 return true;
351 void cue_spoof_id3(struct cuesheet *cue, struct mp3entry *id3)
353 if (!cue || !cue->curr_track)
354 return;
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;
366 else
367 id3->albumartist = NULL;
369 int i = cue->curr_track_idx;
370 id3->tracknum = i+1;
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,
377 int x, int y, int h)
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 (x1,y) and (x2,y) */
385 void cue_draw_markers(struct screen *screen, struct cuesheet *cue,
386 unsigned long tracklen,
387 int x1, int x2, int y, int h)
389 int i,xi;
390 int w = x2 - x1;
391 for (i=1; i < cue->track_count; i++)
393 xi = x1 + (w * cue->tracks[i].offset)/tracklen;
394 draw_veritcal_line_mark(screen, xi, y, h);
397 #endif
399 bool cuesheet_subtrack_changed(struct mp3entry *id3)
401 struct cuesheet *cue = id3->cuesheet;
402 if (cue && (id3->elapsed < cue->curr_track->offset
403 || (cue->curr_track_idx < cue->track_count - 1
404 && id3->elapsed >= (cue->curr_track+1)->offset)))
406 cue_find_current_track(cue, id3->elapsed);
407 cue_spoof_id3(cue, id3);
408 return true;
410 return false;