added degree symbol
[Rockbox.git] / apps / cuesheet.c
blobe779c8f07dd22db10fc39160a34ef5eec88c505f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Nicolas Pennequin, Jonathan Gordon
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <atoi.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include "system.h"
27 #include "audio.h"
28 #include "kernel.h"
29 #include "logf.h"
30 #include "sprintf.h"
31 #include "misc.h"
32 #include "screens.h"
33 #include "splash.h"
34 #include "list.h"
35 #include "action.h"
36 #include "lang.h"
37 #include "debug.h"
38 #include "settings.h"
39 #include "buffer.h"
40 #include "plugin.h"
41 #include "playback.h"
42 #include "cuesheet.h"
44 #define CUE_DIR ROCKBOX_DIR "/cue"
46 struct cuesheet *curr_cue;
47 struct cuesheet *temp_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);
55 #endif
57 void cuesheet_init(void)
59 if (global_settings.cuesheet) {
60 curr_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
61 temp_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
62 #if CONFIG_CODEC != SWCODEC
63 audio_set_cuesheet_callback(cuesheet_handler);
64 #endif
65 } else {
66 curr_cue = NULL;
67 temp_cue = NULL;
71 bool cuesheet_is_enabled(void)
73 return (curr_cue != NULL);
76 bool look_for_cuesheet_file(const char *trackpath, char *found_cue_path)
78 DEBUGF("look for cue file\n");
79 char cuepath[MAX_PATH];
80 strncpy(cuepath, trackpath, MAX_PATH);
81 char *dot = strrchr(cuepath, '.');
82 strcpy(dot, ".cue");
84 int fd = open(cuepath,O_RDONLY);
85 if (fd < 0)
87 strcpy(cuepath, CUE_DIR);
88 strcat(cuepath, strrchr(trackpath, '/'));
89 char *dot = strrchr(cuepath, '.');
90 strcpy(dot, ".cue");
91 fd = open(cuepath,O_RDONLY);
92 if (fd < 0)
94 if (found_cue_path)
95 found_cue_path = NULL;
96 return false;
99 close(fd);
100 if (found_cue_path)
101 strncpy(found_cue_path, cuepath, MAX_PATH);
102 return true;
105 static char *skip_whitespace(char* buf)
107 char *r = buf;
108 while (*r && isspace(*r))
109 r++;
110 return r;
114 static char *get_string(const char *line)
116 char *start, *end;
118 start = strchr(line, '"');
119 if (!start)
121 start = strchr(line, ' ');
123 if (!start)
124 return NULL;
127 end = strchr(++start, '"');
128 if (end)
129 *end = '\0';
131 return start;
134 /* parse cuesheet "file" and store the information in "cue" */
135 bool parse_cuesheet(char *file, struct cuesheet *cue)
137 char line[MAX_PATH];
138 char *s;
140 DEBUGF("cue parse\n");
141 int fd = open(file,O_RDONLY);
142 if (fd < 0)
144 /* couln't open the file */
145 return false;
148 /* Initialization */
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))
159 cue->track_count++;
161 else if (!strncmp(s, "INDEX 01", 8))
163 s = strchr(s,' ');
164 s = skip_whitespace(s);
165 s = strchr(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))
177 char *dest = NULL;
178 char *string = get_string(s);
179 if (!string)
180 break;
182 switch (*s)
184 case 'T': /* TITLE */
185 dest = (cue->track_count <= 0) ? cue->title :
186 cue->tracks[cue->track_count-1].title;
187 break;
189 case 'P': /* PERFORMER */
190 dest = (cue->track_count <= 0) ? cue->performer :
191 cue->tracks[cue->track_count-1].performer;
192 break;
194 case 'S': /* SONGWRITER */
195 dest = (cue->track_count <= 0) ? cue->songwriter :
196 cue->tracks[cue->track_count-1].songwriter;
197 break;
200 if (dest) {
201 dest = iso_decode(string, dest, -1, MIN(strlen(string), MAX_NAME));
202 *dest = '\0';
206 close(fd);
208 /* If some songs don't have performer info, we copy the cuesheet performer */
209 int i;
210 for (i = 0; i < cue->track_count; i++)
212 if (*(cue->tracks[i].performer) == '\0')
213 strncpy(cue->tracks[i].performer, cue->performer, MAX_NAME);
215 if (*(cue->tracks[i].songwriter) == '\0')
216 strncpy(cue->tracks[i].songwriter, cue->songwriter, MAX_NAME);
219 return true;
222 /* takes care of seeking to a track in a playlist
223 * returns false if audio isn't playing */
224 static bool seek(unsigned long pos)
226 if (!(audio_status() & AUDIO_STATUS_PLAY))
228 return false;
230 else
232 #if (CONFIG_CODEC == SWCODEC)
233 audio_pre_ff_rewind();
234 audio_ff_rewind(pos);
235 #else
236 audio_pause();
237 audio_ff_rewind(pos);
238 audio_resume();
239 #endif
240 return true;
244 /* returns the index of the track currently being played
245 and updates the information about the current track. */
246 int cue_find_current_track(struct cuesheet *cue, unsigned long curpos)
248 int i=0;
249 while (i < cue->track_count-1 && cue->tracks[i+1].offset < curpos)
250 i++;
252 cue->curr_track_idx = i;
253 cue->curr_track = cue->tracks + i;
254 return i;
257 /* callback that gives list item titles for the cuesheet browser */
258 static char *list_get_name_cb(int selected_item,
259 void *data,
260 char *buffer)
262 struct cuesheet *cue = (struct cuesheet *)data;
264 if (selected_item & 1)
265 snprintf(buffer, MAX_PATH, "%s",
266 cue->tracks[selected_item/2].title);
267 else
268 snprintf(buffer, MAX_PATH, "%02d. %s", selected_item/2+1,
269 cue->tracks[selected_item/2].performer);
271 return buffer;
274 void browse_cuesheet(struct cuesheet *cue)
276 struct gui_synclist lists;
277 int action;
278 bool done = false;
279 int sel;
280 char title[MAX_PATH];
281 char cuepath[MAX_PATH];
282 struct mp3entry *id3 = audio_current_track();
284 snprintf(title, MAX_PATH, "%s: %s", cue->performer, cue->title);
285 gui_synclist_init(&lists, list_get_name_cb, cue, false, 2);
286 gui_synclist_set_nb_items(&lists, 2*cue->track_count);
287 gui_synclist_set_title(&lists, title, 0);
289 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
291 look_for_cuesheet_file(id3->path, cuepath);
294 if (id3 && id3->cuesheet_type && !strcmp(cue->path, cuepath))
296 gui_synclist_select_item(&lists,
297 2*cue_find_current_track(cue, id3->elapsed));
300 while (!done)
302 gui_synclist_draw(&lists);
303 action = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
304 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
305 continue;
306 switch (action)
308 case ACTION_STD_OK:
309 id3 = audio_current_track();
310 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
312 look_for_cuesheet_file(id3->path, cuepath);
313 if (id3->cuesheet_type && !strcmp(cue->path, cuepath))
315 sel = gui_synclist_get_sel_pos(&lists);
316 seek(cue->tracks[sel/2].offset);
319 break;
320 case ACTION_STD_CANCEL:
321 done = true;
326 bool display_cuesheet_content(char* filename)
328 size_t bufsize = 0;
329 struct cuesheet *cue = (struct cuesheet *)plugin_get_buffer(&bufsize);
330 if (!cue || bufsize < sizeof(struct cuesheet))
331 return false;
333 if (!parse_cuesheet(filename, cue))
334 return false;
336 browse_cuesheet(cue);
337 return true;
340 /* skips backwards or forward in the current cuesheet
341 * the return value indicates whether we're still in a cusheet after skipping
342 * it also returns false if we weren't in a cuesheet.
343 * direction should be 1 or -1.
345 bool curr_cuesheet_skip(int direction, unsigned long curr_pos)
347 int track = cue_find_current_track(curr_cue, curr_pos);
349 if (direction >= 0 && track == curr_cue->track_count - 1)
351 /* we want to get out of the cuesheet */
352 return false;
354 else
356 if (!(direction <= 0 && track == 0))
357 track += direction;
359 seek(curr_cue->tracks[track].offset);
360 return true;
365 void cue_spoof_id3(struct cuesheet *cue, struct mp3entry *id3)
367 if (!cue || !cue->curr_track)
368 return;
370 struct cue_track_info *track = cue->curr_track;
372 id3->title = *track->title ? track->title : NULL;
373 id3->artist = *track->performer ? track->performer : NULL;
374 id3->composer = *track->songwriter ? track->songwriter : NULL;
375 id3->album = *cue->title ? cue->title : NULL;
377 /* if the album artist is the same as the track artist, we hide it. */
378 if (strcmp(cue->performer, track->performer))
379 id3->albumartist = *cue->performer ? cue->performer : NULL;
380 else
381 id3->albumartist = NULL;
383 int i = cue->curr_track_idx;
384 id3->tracknum = i+1;
385 if (id3->track_string)
386 snprintf(id3->track_string, 10, "%d/%d", i+1, cue->track_count);
389 #ifdef HAVE_LCD_BITMAP
390 static inline void draw_veritcal_line_mark(struct screen * screen,
391 int x, int y, int h)
393 screen->set_drawmode(DRMODE_COMPLEMENT);
394 screen->vline(x, y, y+h-1);
397 /* draw the cuesheet markers for a track of length "tracklen",
398 between (x1,y) and (x2,y) */
399 void cue_draw_markers(struct screen *screen, unsigned long tracklen,
400 int x1, int x2, int y, int h)
402 int i,xi;
403 int w = x2 - x1;
404 for (i=1; i < curr_cue->track_count; i++)
406 xi = x1 + (w * curr_cue->tracks[i].offset)/tracklen;
407 draw_veritcal_line_mark(screen, xi, y, h);
410 #endif