Accept FS#7228 by Dagni McPhee enable pitchscreen on sansa
[Rockbox.git] / apps / cuesheet.c
blob9bac3681f33ce0920072b90bb41fd4a8606f09c0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $$
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 #if CONFIG_CODEC != SWCODEC
45 /* special trickery because the hwcodec playback engine is in firmware/ */
46 static bool cuesheet_handler(const char *filename)
48 return cuesheet_is_enabled() && look_for_cuesheet_file(filename);
50 #endif
52 void cuesheet_init(void)
54 if (global_settings.cuesheet) {
55 curr_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
56 temp_cue = (struct cuesheet *)buffer_alloc(sizeof(struct cuesheet));
57 #if CONFIG_CODEC != SWCODEC
58 audio_set_cuesheet_callback(cuesheet_handler);
59 #endif
60 } else {
61 curr_cue = NULL;
62 temp_cue = NULL;
66 bool cuesheet_is_enabled(void)
68 return (curr_cue != NULL);
71 bool look_for_cuesheet_file(const char *trackpath)
73 char cuepath[MAX_PATH];
74 strncpy(cuepath, trackpath, MAX_PATH);
75 char *dot = strrchr(cuepath, '.');
76 strcpy(dot, ".cue");
78 int fd = open(cuepath,O_RDONLY);
79 if (fd < 0)
81 return false;
83 else
85 close(fd);
86 return true;
90 static char *skip_whitespace(char* buf)
92 char *r = buf;
93 while (*r && isspace(*r))
94 r++;
95 return r;
99 static char *get_string(const char *line)
101 char *start, *end;
103 start = strchr(line, '"');
104 if (!start)
106 start = strchr(line, ' ');
108 if (!start)
109 return NULL;
112 end = strchr(++start, '"');
113 if (end)
114 *end = '\0';
116 return start;
119 /* parse cuesheet "file" and store the information in "cue" */
120 bool parse_cuesheet(char *file, struct cuesheet *cue)
122 char line[MAX_PATH];
123 char *s;
125 int fd = open(file,O_RDONLY);
126 if (fd < 0)
128 /* couln't open the file */
129 return false;
132 /* Initialization */
133 memset(cue, 0, sizeof(struct cuesheet));
134 strcpy(cue->path, file);
135 cue->curr_track = cue->tracks;
137 while ( read_line(fd,line,MAX_PATH) && cue->track_count < MAX_TRACKS )
139 s = skip_whitespace(line);
141 if (!strncmp(s, "TRACK", 5))
143 cue->track_count++;
145 else if (!strncmp(s, "INDEX 01", 8))
147 s = strchr(s,' ');
148 s = skip_whitespace(s);
149 s = strchr(s,' ');
150 s = skip_whitespace(s);
151 cue->tracks[cue->track_count-1].offset = 60*1000 * atoi(s);
152 s = strchr(s,':') + 1;
153 cue->tracks[cue->track_count-1].offset += 1000 * atoi(s);
154 s = strchr(s,':') + 1;
155 cue->tracks[cue->track_count-1].offset += 13 * atoi(s);
157 else if (!strncmp(s, "TITLE", 5)
158 || !strncmp(s, "PERFORMER", 9)
159 || !strncmp(s, "SONGWRITER", 10))
161 char *dest = NULL;
162 char *string = get_string(s);
163 if (!string)
164 break;
166 switch (*s)
168 case 'T': /* TITLE */
169 dest = (cue->track_count <= 0) ? cue->title :
170 cue->tracks[cue->track_count-1].title;
171 break;
173 case 'P': /* PERFORMER */
174 dest = (cue->track_count <= 0) ? cue->performer :
175 cue->tracks[cue->track_count-1].performer;
176 break;
178 case 'S': /* SONGWRITER */
179 dest = (cue->track_count <= 0) ? cue->songwriter :
180 cue->tracks[cue->track_count-1].songwriter;
181 break;
184 if (dest)
185 strncpy(dest, string, MAX_NAME);
188 close(fd);
190 /* If some songs don't have performer info, we copy the cuesheet performer */
191 int i;
192 for (i = 0; i < cue->track_count; i++)
194 if (*(cue->tracks[i].performer) == '\0')
195 strncpy(cue->tracks[i].performer, cue->performer, MAX_NAME);
197 if (*(cue->tracks[i].songwriter) == '\0')
198 strncpy(cue->tracks[i].songwriter, cue->songwriter, MAX_NAME);
201 return true;
204 /* takes care of seeking to a track in a playlist
205 * returns false if audio isn't playing */
206 static bool seek(unsigned long pos)
208 if (!(audio_status() & AUDIO_STATUS_PLAY))
210 return false;
212 else
214 #if (CONFIG_CODEC == SWCODEC)
215 audio_pre_ff_rewind();
216 audio_ff_rewind(pos);
217 #else
218 audio_pause();
219 audio_ff_rewind(pos);
220 audio_resume();
221 #endif
222 return true;
226 /* returns the index of the track currently being played
227 and updates the information about the current track. */
228 int cue_find_current_track(struct cuesheet *cue, unsigned long curpos)
230 int i=0;
231 while (i < cue->track_count-1 && cue->tracks[i+1].offset < curpos)
232 i++;
234 cue->curr_track_idx = i;
235 cue->curr_track = cue->tracks + i;
236 return i;
239 /* callback that gives list item titles for the cuesheet browser */
240 static char *list_get_name_cb(int selected_item,
241 void *data,
242 char *buffer)
244 struct cuesheet *cue = (struct cuesheet *)data;
246 if (selected_item & 1)
247 snprintf(buffer, MAX_PATH, "%s",
248 cue->tracks[selected_item/2].title);
249 else
250 snprintf(buffer, MAX_PATH, "%02d. %s", selected_item/2+1,
251 cue->tracks[selected_item/2].performer);
253 return buffer;
256 void browse_cuesheet(struct cuesheet *cue)
258 struct gui_synclist lists;
259 int action;
260 bool done = false;
261 int sel;
262 char title[MAX_PATH];
263 char cuepath[MAX_PATH];
264 char *dot;
265 struct mp3entry *id3 = audio_current_track();
267 snprintf(title, MAX_PATH, "%s: %s", cue->performer, cue->title);
268 gui_synclist_init(&lists, list_get_name_cb, cue, false, 2);
269 gui_synclist_set_nb_items(&lists, 2*cue->track_count);
270 gui_synclist_set_title(&lists, title, 0);
272 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
274 strncpy(cuepath, id3->path, MAX_PATH);
275 dot = strrchr(cuepath, '.');
276 strcpy(dot, ".cue");
279 if (id3 && id3->cuesheet_type && !strcmp(cue->path, cuepath))
281 gui_synclist_select_item(&lists,
282 2*cue_find_current_track(cue, id3->elapsed));
285 while (!done)
287 gui_synclist_draw(&lists);
288 action = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
289 if (gui_synclist_do_button(&lists,action,LIST_WRAP_UNLESS_HELD))
290 continue;
291 switch (action)
293 case ACTION_STD_OK:
294 id3 = audio_current_track();
295 if (id3 && *id3->path && strcmp(id3->path, "No file!"))
297 strncpy(cuepath, id3->path, MAX_PATH);
298 dot = strrchr(cuepath, '.');
299 strcpy(dot, ".cue");
300 if (id3->cuesheet_type && !strcmp(cue->path, cuepath))
302 sel = gui_synclist_get_sel_pos(&lists);
303 seek(cue->tracks[sel/2].offset);
306 break;
307 case ACTION_STD_CANCEL:
308 done = true;
311 action_signalscreenchange();
314 bool display_cuesheet_content(char* filename)
316 size_t bufsize = 0;
317 struct cuesheet *cue = (struct cuesheet *)plugin_get_buffer(&bufsize);
318 if (!cue || bufsize < sizeof(struct cuesheet))
319 return false;
321 if (!parse_cuesheet(filename, cue))
322 return false;
324 browse_cuesheet(cue);
325 return true;
328 /* skips backwards or forward in the current cuesheet
329 * the return value indicates whether we're still in a cusheet after skipping
330 * it also returns false if we weren't in a cuesheet.
331 * direction should be 1 or -1.
333 bool curr_cuesheet_skip(int direction, unsigned long curr_pos)
335 int track = cue_find_current_track(curr_cue, curr_pos);
337 if (direction >= 0 && track == curr_cue->track_count - 1)
339 /* we want to get out of the cuesheet */
340 return false;
342 else
344 if (!(direction <= 0 && track == 0))
345 track += direction;
347 seek(curr_cue->tracks[track].offset);
348 return true;
353 void cue_spoof_id3(struct cuesheet *cue, struct mp3entry *id3)
355 if (!cue || !cue->curr_track)
356 return;
358 struct cue_track_info *track = cue->curr_track;
360 id3->title = *track->title ? track->title : NULL;
361 id3->artist = *track->performer ? track->performer : NULL;
362 id3->composer = *track->songwriter ? track->songwriter : NULL;
363 id3->album = *cue->title ? cue->title : NULL;
365 /* if the album artist is the same as the track artist, we hide it. */
366 if (strcmp(cue->performer, track->performer))
367 id3->albumartist = *cue->performer ? cue->performer : NULL;
368 else
369 id3->albumartist = NULL;
371 int i = cue->curr_track_idx;
372 id3->tracknum = i+1;
373 if (id3->track_string)
374 snprintf(id3->track_string, 10, "%d/%d", i+1, cue->track_count);
377 #ifdef HAVE_LCD_BITMAP
378 static inline void draw_veritcal_line_mark(struct screen * screen,
379 int x, int y, int h)
381 screen->set_drawmode(DRMODE_COMPLEMENT);
382 screen->vline(x, y, y+h-1);
385 /* draw the cuesheet markers for a track of length "tracklen",
386 between (x1,y) and (x2,y) */
387 void cue_draw_markers(struct screen *screen, unsigned long tracklen,
388 int x1, int x2, int y, int h)
390 int i,xi;
391 int w = x2 - x1;
392 for (i=1; i < curr_cue->track_count; i++)
394 xi = x1 + (w * curr_cue->tracks[i].offset)/tracklen;
395 draw_veritcal_line_mark(screen, xi, y, h);
398 #endif