2007-02-14 14:40:24 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2007-07-26 14:23:08 +00:00
|
|
|
* $Id$
|
2007-02-14 14:40:24 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Nicolas Pennequin, Jonathan Gordon
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-02-14 14:40:24 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2007-05-07 13:32:56 +00:00
|
|
|
#include <ctype.h>
|
2007-02-14 14:40:24 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "system.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#include "sprintf.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "screens.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "action.h"
|
|
|
|
#include "lang.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "playback.h"
|
|
|
|
#include "cuesheet.h"
|
2010-01-06 14:48:42 +00:00
|
|
|
#include "gui/wps.h"
|
2007-02-14 14:40:24 +00:00
|
|
|
|
2007-05-28 23:18:31 +00:00
|
|
|
#define CUE_DIR ROCKBOX_DIR "/cue"
|
|
|
|
|
|
|
|
bool look_for_cuesheet_file(const char *trackpath, char *found_cue_path)
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
2008-04-07 19:33:48 +00:00
|
|
|
/* DEBUGF("look for cue file\n"); */
|
2008-02-05 20:00:14 +00:00
|
|
|
|
2007-02-14 14:40:24 +00:00
|
|
|
char cuepath[MAX_PATH];
|
2008-02-05 20:00:14 +00:00
|
|
|
char *dot, *slash;
|
|
|
|
|
|
|
|
slash = strrchr(trackpath, '/');
|
|
|
|
if (!slash)
|
|
|
|
{
|
|
|
|
found_cue_path = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(cuepath, trackpath, MAX_PATH);
|
2008-02-05 20:00:14 +00:00
|
|
|
dot = strrchr(cuepath, '.');
|
2007-02-14 14:40:24 +00:00
|
|
|
strcpy(dot, ".cue");
|
|
|
|
|
2008-04-16 19:51:43 +00:00
|
|
|
if (!file_exists(cuepath))
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
2007-05-28 23:18:31 +00:00
|
|
|
strcpy(cuepath, CUE_DIR);
|
2008-02-05 20:00:14 +00:00
|
|
|
strcat(cuepath, slash);
|
2007-05-28 23:18:31 +00:00
|
|
|
char *dot = strrchr(cuepath, '.');
|
|
|
|
strcpy(dot, ".cue");
|
2008-04-16 19:51:43 +00:00
|
|
|
if (!file_exists(cuepath))
|
2007-05-28 23:18:31 +00:00
|
|
|
{
|
|
|
|
if (found_cue_path)
|
|
|
|
found_cue_path = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
2007-02-14 14:40:24 +00:00
|
|
|
}
|
2008-04-16 19:51:43 +00:00
|
|
|
|
2007-05-28 23:18:31 +00:00
|
|
|
if (found_cue_path)
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(found_cue_path, cuepath, MAX_PATH);
|
2007-05-28 23:18:31 +00:00
|
|
|
return true;
|
2007-02-14 14:40:24 +00:00
|
|
|
}
|
|
|
|
|
2007-03-28 19:43:03 +00:00
|
|
|
static char *get_string(const char *line)
|
|
|
|
{
|
|
|
|
char *start, *end;
|
|
|
|
|
|
|
|
start = strchr(line, '"');
|
|
|
|
if (!start)
|
|
|
|
{
|
|
|
|
start = strchr(line, ' ');
|
|
|
|
|
|
|
|
if (!start)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = strchr(++start, '"');
|
|
|
|
if (end)
|
|
|
|
*end = '\0';
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2007-02-14 14:40:24 +00:00
|
|
|
/* parse cuesheet "file" and store the information in "cue" */
|
|
|
|
bool parse_cuesheet(char *file, struct cuesheet *cue)
|
|
|
|
{
|
|
|
|
char line[MAX_PATH];
|
2007-03-28 19:43:03 +00:00
|
|
|
char *s;
|
2009-01-26 11:24:11 +00:00
|
|
|
bool utf8 = false;
|
2007-03-28 19:43:03 +00:00
|
|
|
|
2008-08-02 20:39:03 +00:00
|
|
|
int fd = open_utf8(file,O_RDONLY);
|
2007-02-14 14:40:24 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
/* couln't open the file */
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-26 11:24:11 +00:00
|
|
|
if(lseek(fd, 0, SEEK_CUR) > 0)
|
|
|
|
utf8 = true;
|
2007-02-14 14:40:24 +00:00
|
|
|
|
2007-03-28 19:43:03 +00:00
|
|
|
/* Initialization */
|
2007-02-14 14:40:24 +00:00
|
|
|
memset(cue, 0, sizeof(struct cuesheet));
|
|
|
|
strcpy(cue->path, file);
|
|
|
|
cue->curr_track = cue->tracks;
|
|
|
|
|
2007-03-28 19:43:03 +00:00
|
|
|
while ( read_line(fd,line,MAX_PATH) && cue->track_count < MAX_TRACKS )
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
s = skip_whitespace(line);
|
2007-03-28 19:43:03 +00:00
|
|
|
|
|
|
|
if (!strncmp(s, "TRACK", 5))
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
cue->track_count++;
|
|
|
|
}
|
2007-03-26 20:26:41 +00:00
|
|
|
else if (!strncmp(s, "INDEX 01", 8))
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
s = strchr(s,' ');
|
|
|
|
s = skip_whitespace(s);
|
|
|
|
s = strchr(s,' ');
|
|
|
|
s = skip_whitespace(s);
|
|
|
|
cue->tracks[cue->track_count-1].offset = 60*1000 * atoi(s);
|
|
|
|
s = strchr(s,':') + 1;
|
|
|
|
cue->tracks[cue->track_count-1].offset += 1000 * atoi(s);
|
|
|
|
s = strchr(s,':') + 1;
|
|
|
|
cue->tracks[cue->track_count-1].offset += 13 * atoi(s);
|
|
|
|
}
|
2007-03-28 19:43:03 +00:00
|
|
|
else if (!strncmp(s, "TITLE", 5)
|
|
|
|
|| !strncmp(s, "PERFORMER", 9)
|
|
|
|
|| !strncmp(s, "SONGWRITER", 10))
|
|
|
|
{
|
2007-03-28 19:55:41 +00:00
|
|
|
char *dest = NULL;
|
|
|
|
char *string = get_string(s);
|
2007-03-28 19:43:03 +00:00
|
|
|
if (!string)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (*s)
|
|
|
|
{
|
|
|
|
case 'T': /* TITLE */
|
|
|
|
dest = (cue->track_count <= 0) ? cue->title :
|
|
|
|
cue->tracks[cue->track_count-1].title;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P': /* PERFORMER */
|
|
|
|
dest = (cue->track_count <= 0) ? cue->performer :
|
|
|
|
cue->tracks[cue->track_count-1].performer;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S': /* SONGWRITER */
|
2007-03-29 18:39:04 +00:00
|
|
|
dest = (cue->track_count <= 0) ? cue->songwriter :
|
2007-03-28 19:43:03 +00:00
|
|
|
cue->tracks[cue->track_count-1].songwriter;
|
2007-03-28 19:55:41 +00:00
|
|
|
break;
|
2007-03-28 19:43:03 +00:00
|
|
|
}
|
|
|
|
|
2009-01-26 11:24:11 +00:00
|
|
|
if (dest)
|
|
|
|
{
|
|
|
|
if (!utf8)
|
|
|
|
{
|
|
|
|
dest = iso_decode(string, dest, -1, MIN(strlen(string), MAX_NAME));
|
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(dest, string, MAX_NAME*3 + 1);
|
2009-01-26 11:24:11 +00:00
|
|
|
}
|
2007-11-12 12:39:56 +00:00
|
|
|
}
|
2007-03-28 19:43:03 +00:00
|
|
|
}
|
2007-02-14 14:40:24 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* If some songs don't have performer info, we copy the cuesheet performer */
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < cue->track_count; i++)
|
|
|
|
{
|
|
|
|
if (*(cue->tracks[i].performer) == '\0')
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(cue->tracks[i].performer, cue->performer, MAX_NAME*3);
|
2007-03-29 18:39:04 +00:00
|
|
|
|
|
|
|
if (*(cue->tracks[i].songwriter) == '\0')
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(cue->tracks[i].songwriter, cue->songwriter, MAX_NAME*3);
|
2007-02-14 14:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* takes care of seeking to a track in a playlist
|
|
|
|
* returns false if audio isn't playing */
|
2007-02-16 18:48:36 +00:00
|
|
|
static bool seek(unsigned long pos)
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
if (!(audio_status() & AUDIO_STATUS_PLAY))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if (CONFIG_CODEC == SWCODEC)
|
|
|
|
audio_pre_ff_rewind();
|
2007-02-28 23:44:09 +00:00
|
|
|
audio_ff_rewind(pos);
|
2007-02-14 14:40:24 +00:00
|
|
|
#else
|
|
|
|
audio_pause();
|
|
|
|
audio_ff_rewind(pos);
|
2007-02-28 23:44:09 +00:00
|
|
|
audio_resume();
|
|
|
|
#endif
|
2007-02-14 14:40:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns the index of the track currently being played
|
|
|
|
and updates the information about the current track. */
|
|
|
|
int cue_find_current_track(struct cuesheet *cue, unsigned long curpos)
|
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
while (i < cue->track_count-1 && cue->tracks[i+1].offset < curpos)
|
|
|
|
i++;
|
2007-03-29 18:39:04 +00:00
|
|
|
|
2007-02-14 14:40:24 +00:00
|
|
|
cue->curr_track_idx = i;
|
|
|
|
cue->curr_track = cue->tracks + i;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* callback that gives list item titles for the cuesheet browser */
|
2009-08-20 16:47:44 +00:00
|
|
|
static const char* list_get_name_cb(int selected_item,
|
|
|
|
void *data,
|
|
|
|
char *buffer,
|
|
|
|
size_t buffer_len)
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
struct cuesheet *cue = (struct cuesheet *)data;
|
|
|
|
|
|
|
|
if (selected_item & 1)
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(buffer, cue->tracks[selected_item/2].title, buffer_len);
|
2007-02-14 14:40:24 +00:00
|
|
|
else
|
2008-04-09 15:25:17 +00:00
|
|
|
snprintf(buffer, buffer_len, "%02d. %s", selected_item/2+1,
|
2007-02-14 14:40:24 +00:00
|
|
|
cue->tracks[selected_item/2].performer);
|
2007-03-29 18:39:04 +00:00
|
|
|
|
2007-02-14 14:40:24 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2007-05-14 17:34:52 +00:00
|
|
|
void browse_cuesheet(struct cuesheet *cue)
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
struct gui_synclist lists;
|
|
|
|
int action;
|
|
|
|
bool done = false;
|
|
|
|
int sel;
|
|
|
|
char title[MAX_PATH];
|
|
|
|
char cuepath[MAX_PATH];
|
|
|
|
struct mp3entry *id3 = audio_current_track();
|
|
|
|
|
|
|
|
snprintf(title, MAX_PATH, "%s: %s", cue->performer, cue->title);
|
2008-03-26 03:35:24 +00:00
|
|
|
gui_synclist_init(&lists, list_get_name_cb, cue, false, 2, NULL);
|
2007-02-14 14:40:24 +00:00
|
|
|
gui_synclist_set_nb_items(&lists, 2*cue->track_count);
|
|
|
|
gui_synclist_set_title(&lists, title, 0);
|
|
|
|
|
|
|
|
|
2009-07-20 05:18:18 +00:00
|
|
|
if (id3)
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
gui_synclist_select_item(&lists,
|
|
|
|
2*cue_find_current_track(cue, id3->elapsed));
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
gui_synclist_draw(&lists);
|
|
|
|
action = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
|
2007-09-17 10:08:50 +00:00
|
|
|
if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
|
2007-02-14 14:40:24 +00:00
|
|
|
continue;
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case ACTION_STD_OK:
|
|
|
|
id3 = audio_current_track();
|
2007-03-21 23:35:26 +00:00
|
|
|
if (id3 && *id3->path && strcmp(id3->path, "No file!"))
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
2007-05-28 23:18:31 +00:00
|
|
|
look_for_cuesheet_file(id3->path, cuepath);
|
2009-07-20 05:18:18 +00:00
|
|
|
if (id3->cuesheet && !strcmp(cue->path, cuepath))
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
sel = gui_synclist_get_sel_pos(&lists);
|
|
|
|
seek(cue->tracks[sel/2].offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ACTION_STD_CANCEL:
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool display_cuesheet_content(char* filename)
|
|
|
|
{
|
2007-04-21 18:38:25 +00:00
|
|
|
size_t bufsize = 0;
|
2007-02-14 14:40:24 +00:00
|
|
|
struct cuesheet *cue = (struct cuesheet *)plugin_get_buffer(&bufsize);
|
2007-04-03 18:18:35 +00:00
|
|
|
if (!cue || bufsize < sizeof(struct cuesheet))
|
2007-02-14 14:40:24 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!parse_cuesheet(filename, cue))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
browse_cuesheet(cue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skips backwards or forward in the current cuesheet
|
|
|
|
* the return value indicates whether we're still in a cusheet after skipping
|
|
|
|
* it also returns false if we weren't in a cuesheet.
|
|
|
|
* direction should be 1 or -1.
|
|
|
|
*/
|
2009-07-24 01:13:30 +00:00
|
|
|
bool curr_cuesheet_skip(struct cuesheet *cue, int direction, unsigned long curr_pos)
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
2009-07-24 01:13:30 +00:00
|
|
|
int track = cue_find_current_track(cue, curr_pos);
|
2009-07-20 05:18:18 +00:00
|
|
|
|
2009-07-24 01:13:30 +00:00
|
|
|
if (direction >= 0 && track == cue->track_count - 1)
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
|
|
|
/* we want to get out of the cuesheet */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!(direction <= 0 && track == 0))
|
2010-01-06 14:48:42 +00:00
|
|
|
{
|
|
|
|
/* If skipping forward, skip to next cuesheet segment. If skipping
|
|
|
|
backward before DEFAULT_SKIP_TRESH milliseconds have elapsed, skip
|
|
|
|
to previous cuesheet segment. If skipping backward after
|
|
|
|
DEFAULT_SKIP_TRESH seconds have elapsed, skip to the start of the
|
|
|
|
current cuesheet segment */
|
|
|
|
if (direction == 1 ||
|
|
|
|
((curr_pos - cue->tracks[track].offset) < DEFAULT_SKIP_TRESH))
|
|
|
|
{
|
|
|
|
track += direction;
|
|
|
|
}
|
|
|
|
}
|
2007-02-14 14:40:24 +00:00
|
|
|
|
2009-07-24 01:13:30 +00:00
|
|
|
seek(cue->tracks[track].offset);
|
2007-02-14 14:40:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void cue_spoof_id3(struct cuesheet *cue, struct mp3entry *id3)
|
|
|
|
{
|
2007-04-02 19:40:53 +00:00
|
|
|
if (!cue || !cue->curr_track)
|
2007-02-14 14:40:24 +00:00
|
|
|
return;
|
|
|
|
|
2007-04-02 19:40:53 +00:00
|
|
|
struct cue_track_info *track = cue->curr_track;
|
2007-02-14 14:40:24 +00:00
|
|
|
|
2007-04-02 19:40:53 +00:00
|
|
|
id3->title = *track->title ? track->title : NULL;
|
|
|
|
id3->artist = *track->performer ? track->performer : NULL;
|
|
|
|
id3->composer = *track->songwriter ? track->songwriter : NULL;
|
|
|
|
id3->album = *cue->title ? cue->title : NULL;
|
2007-03-29 18:39:04 +00:00
|
|
|
|
|
|
|
/* if the album artist is the same as the track artist, we hide it. */
|
2007-04-02 19:40:53 +00:00
|
|
|
if (strcmp(cue->performer, track->performer))
|
|
|
|
id3->albumartist = *cue->performer ? cue->performer : NULL;
|
2007-03-29 18:39:04 +00:00
|
|
|
else
|
2007-04-02 19:40:53 +00:00
|
|
|
id3->albumartist = NULL;
|
2007-03-29 18:39:04 +00:00
|
|
|
|
2007-04-02 19:40:53 +00:00
|
|
|
int i = cue->curr_track_idx;
|
2007-02-14 14:40:24 +00:00
|
|
|
id3->tracknum = i+1;
|
|
|
|
if (id3->track_string)
|
|
|
|
snprintf(id3->track_string, 10, "%d/%d", i+1, cue->track_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
static inline void draw_veritcal_line_mark(struct screen * screen,
|
|
|
|
int x, int y, int h)
|
|
|
|
{
|
|
|
|
screen->set_drawmode(DRMODE_COMPLEMENT);
|
|
|
|
screen->vline(x, y, y+h-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw the cuesheet markers for a track of length "tracklen",
|
|
|
|
between (x1,y) and (x2,y) */
|
2009-07-24 01:13:30 +00:00
|
|
|
void cue_draw_markers(struct screen *screen, struct cuesheet *cue,
|
|
|
|
unsigned long tracklen,
|
2007-02-14 14:40:24 +00:00
|
|
|
int x1, int x2, int y, int h)
|
|
|
|
{
|
|
|
|
int i,xi;
|
|
|
|
int w = x2 - x1;
|
2009-07-24 01:13:30 +00:00
|
|
|
for (i=1; i < cue->track_count; i++)
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
2009-07-24 01:13:30 +00:00
|
|
|
xi = x1 + (w * cue->tracks[i].offset)/tracklen;
|
2007-02-14 14:40:24 +00:00
|
|
|
draw_veritcal_line_mark(screen, xi, y, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-07-20 05:18:18 +00:00
|
|
|
|
|
|
|
bool cuesheet_subtrack_changed(struct mp3entry *id3)
|
|
|
|
{
|
|
|
|
struct cuesheet *cue = id3->cuesheet;
|
|
|
|
if (cue && (id3->elapsed < cue->curr_track->offset
|
|
|
|
|| (cue->curr_track_idx < cue->track_count - 1
|
|
|
|
&& id3->elapsed >= (cue->curr_track+1)->offset)))
|
|
|
|
{
|
|
|
|
cue_find_current_track(cue, id3->elapsed);
|
|
|
|
cue_spoof_id3(cue, id3);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|