2002-06-04 21:43:38 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by wavey@wavey.org
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
2002-06-17 13:34:53 +00:00
|
|
|
|
2002-06-04 21:43:38 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "playlist.h"
|
|
|
|
#include <file.h>
|
|
|
|
#include "sprintf.h"
|
|
|
|
#include "debug.h"
|
2002-06-07 14:48:06 +00:00
|
|
|
#include "mpeg.h"
|
2002-06-13 15:16:41 +00:00
|
|
|
#include "lcd.h"
|
|
|
|
#include "kernel.h"
|
2002-06-27 10:10:30 +00:00
|
|
|
#include "settings.h"
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
playlist_info_t playlist;
|
|
|
|
|
2002-07-05 12:48:17 +00:00
|
|
|
char now_playing[MAX_PATH+1];
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2002-07-05 12:48:17 +00:00
|
|
|
char* playlist_next(int steps, char *dirname)
|
2002-06-04 21:43:38 +00:00
|
|
|
{
|
2002-06-26 23:25:03 +00:00
|
|
|
int seek;
|
2002-06-04 21:43:38 +00:00
|
|
|
int max;
|
|
|
|
int fd;
|
2002-06-17 10:17:50 +00:00
|
|
|
int i;
|
2002-07-05 12:48:17 +00:00
|
|
|
char buf[MAX_PATH+1];
|
2002-06-07 14:48:06 +00:00
|
|
|
|
2002-06-26 23:25:03 +00:00
|
|
|
playlist.index = (playlist.index+steps) % playlist.amount;
|
|
|
|
seek = playlist.indices[playlist.index];
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
fd = open(playlist.filename, O_RDONLY);
|
|
|
|
if(-1 != fd) {
|
|
|
|
lseek(fd, seek, SEEK_SET);
|
2002-07-05 12:48:17 +00:00
|
|
|
max = read(fd, buf, sizeof(buf)-1);
|
2002-06-04 21:43:38 +00:00
|
|
|
close(fd);
|
2002-06-10 09:02:13 +00:00
|
|
|
|
|
|
|
/* Zero-terminate the file name */
|
2002-06-04 21:43:38 +00:00
|
|
|
seek=0;
|
2002-07-05 12:48:17 +00:00
|
|
|
while((buf[seek] != '\n') &&
|
|
|
|
(buf[seek] != '\r') &&
|
2002-06-04 21:43:38 +00:00
|
|
|
(seek < max))
|
|
|
|
seek++;
|
2002-06-11 07:33:51 +00:00
|
|
|
|
2002-06-27 08:20:09 +00:00
|
|
|
/* Now work back killing white space */
|
2002-07-05 12:48:17 +00:00
|
|
|
while((buf[seek-1] == ' ') ||
|
|
|
|
(buf[seek-1] == '\t'))
|
2002-06-27 08:20:09 +00:00
|
|
|
seek--;
|
|
|
|
|
2002-07-05 12:48:17 +00:00
|
|
|
buf[seek]=0;
|
2002-06-17 10:17:50 +00:00
|
|
|
|
|
|
|
/* replace backslashes with forward slashes */
|
2002-07-20 22:47:25 +00:00
|
|
|
for ( i=0; i<seek; i++ )
|
2002-07-05 12:48:17 +00:00
|
|
|
if ( buf[i] == '\\' )
|
|
|
|
buf[i] = '/';
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2002-07-05 12:48:17 +00:00
|
|
|
if('/' == buf[0]) {
|
|
|
|
strcpy(now_playing, &buf[0]);
|
|
|
|
return now_playing;
|
|
|
|
}
|
2002-06-12 07:09:33 +00:00
|
|
|
else {
|
2002-06-17 10:17:50 +00:00
|
|
|
/* handle dos style drive letter */
|
2002-07-05 12:48:17 +00:00
|
|
|
if ( ':' == buf[1] ) {
|
|
|
|
strcpy(now_playing, &buf[2]);
|
|
|
|
return now_playing;
|
|
|
|
}
|
2002-06-17 10:17:50 +00:00
|
|
|
else {
|
2002-07-05 12:48:17 +00:00
|
|
|
snprintf(now_playing, MAX_PATH+1, "%s/%s", dirname, buf);
|
2002-06-17 10:17:50 +00:00
|
|
|
return now_playing;
|
|
|
|
}
|
2002-06-12 07:09:33 +00:00
|
|
|
}
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
|
|
|
else
|
2002-06-12 07:07:45 +00:00
|
|
|
return NULL;
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void play_list(char *dir, char *file)
|
|
|
|
{
|
2002-06-12 07:07:45 +00:00
|
|
|
char *sep="";
|
2002-06-13 15:16:41 +00:00
|
|
|
|
|
|
|
lcd_clear_display();
|
|
|
|
lcd_puts(0,0,"Loading...");
|
|
|
|
lcd_update();
|
2002-06-04 21:43:38 +00:00
|
|
|
empty_playlist(&playlist);
|
|
|
|
|
2002-06-12 07:07:45 +00:00
|
|
|
/* If the dir does not end in trailing new line, we use a separator.
|
|
|
|
Otherwise we don't. */
|
|
|
|
if('/' != dir[strlen(dir)-1])
|
|
|
|
sep="/";
|
|
|
|
|
2002-06-04 21:43:38 +00:00
|
|
|
snprintf(playlist.filename, sizeof(playlist.filename),
|
2002-06-12 07:07:45 +00:00
|
|
|
"%s%s%s",
|
|
|
|
dir, sep, file);
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
/* add track indices to playlist data structure */
|
|
|
|
add_indices_to_playlist(&playlist);
|
|
|
|
|
2002-06-27 10:10:30 +00:00
|
|
|
if(global_settings.playlist_shuffle) {
|
2002-06-17 10:36:42 +00:00
|
|
|
lcd_puts(0,0,"Shuffling...");
|
|
|
|
lcd_update();
|
|
|
|
randomise_playlist( &playlist, current_tick );
|
|
|
|
}
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2002-06-17 10:36:42 +00:00
|
|
|
lcd_puts(0,0,"Playing... ");
|
2002-06-13 15:16:41 +00:00
|
|
|
lcd_update();
|
2002-06-04 21:43:38 +00:00
|
|
|
/* also make the first song get playing */
|
2002-07-05 12:48:17 +00:00
|
|
|
mpeg_play(playlist_next(0, dir));
|
2002-06-13 15:16:41 +00:00
|
|
|
sleep(HZ);
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove any filename and indices associated with the playlist
|
|
|
|
*/
|
|
|
|
void empty_playlist( playlist_info_t *playlist )
|
|
|
|
{
|
|
|
|
playlist->filename[0] = '\0';
|
|
|
|
playlist->index = 0;
|
|
|
|
playlist->amount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calculate track offsets within a playlist file
|
|
|
|
*/
|
|
|
|
void add_indices_to_playlist( playlist_info_t *playlist )
|
|
|
|
{
|
|
|
|
int nread;
|
|
|
|
int fd;
|
2002-06-11 07:30:31 +00:00
|
|
|
int i = 0;
|
|
|
|
int store_index = 0;
|
|
|
|
int count = 0;
|
2002-06-13 21:27:42 +00:00
|
|
|
int next_tick = current_tick + HZ;
|
2002-06-11 07:30:31 +00:00
|
|
|
|
2002-06-11 07:33:51 +00:00
|
|
|
unsigned char *p;
|
2002-06-13 15:16:41 +00:00
|
|
|
unsigned char buf[512];
|
|
|
|
char line[16];
|
2002-06-11 07:33:51 +00:00
|
|
|
|
2002-06-04 21:43:38 +00:00
|
|
|
fd = open(playlist->filename, O_RDONLY);
|
|
|
|
if(-1 == fd)
|
|
|
|
return; /* failure */
|
|
|
|
|
2002-06-11 07:30:31 +00:00
|
|
|
store_index = 1;
|
|
|
|
|
|
|
|
while((nread = read(fd, &buf, sizeof(buf))) != 0)
|
|
|
|
{
|
|
|
|
p = buf;
|
|
|
|
|
|
|
|
for(count=0; count < nread; count++,p++) {
|
|
|
|
|
|
|
|
/* Are we on a new line? */
|
2002-06-13 23:06:30 +00:00
|
|
|
if((*p == '\n') || (*p == '\r'))
|
2002-06-11 07:30:31 +00:00
|
|
|
{
|
|
|
|
store_index = 1;
|
|
|
|
}
|
2002-06-15 14:31:01 +00:00
|
|
|
else if((*p == '#') && store_index)
|
|
|
|
{
|
|
|
|
/* If the first character on a new line is a hash
|
|
|
|
sign, we treat it as a comment. So called winamp
|
|
|
|
style playlist. */
|
|
|
|
store_index = 0;
|
|
|
|
}
|
2002-06-11 07:30:31 +00:00
|
|
|
else if(store_index)
|
|
|
|
{
|
2002-07-05 12:48:17 +00:00
|
|
|
|
2002-06-11 07:30:31 +00:00
|
|
|
/* Store a new entry */
|
|
|
|
playlist->indices[ playlist->amount ] = i+count;
|
|
|
|
playlist->amount++;
|
2002-06-13 13:53:22 +00:00
|
|
|
if ( playlist->amount >= MAX_PLAYLIST_SIZE ) {
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2002-06-11 07:30:31 +00:00
|
|
|
|
|
|
|
store_index = 0;
|
2002-06-13 21:27:42 +00:00
|
|
|
if ( current_tick >= next_tick ) {
|
|
|
|
next_tick = current_tick + HZ;
|
|
|
|
snprintf(line, sizeof line, "%d files", playlist->amount);
|
2002-06-13 15:16:41 +00:00
|
|
|
lcd_puts(0,1,line);
|
|
|
|
lcd_update();
|
|
|
|
}
|
2002-06-11 07:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i+= count;
|
|
|
|
}
|
2002-06-13 21:27:42 +00:00
|
|
|
snprintf(line, sizeof line, "%d files", playlist->amount);
|
2002-06-13 15:16:41 +00:00
|
|
|
lcd_puts(0,1,line);
|
|
|
|
lcd_update();
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* randomly rearrange the array of indices for the playlist
|
|
|
|
*/
|
|
|
|
void randomise_playlist( playlist_info_t *playlist, unsigned int seed )
|
|
|
|
{
|
2002-07-22 15:36:39 +00:00
|
|
|
int count;
|
2002-06-04 21:43:38 +00:00
|
|
|
int candidate;
|
|
|
|
int store;
|
|
|
|
|
|
|
|
/* seed with the given seed */
|
2002-07-17 23:07:45 +00:00
|
|
|
srand( seed );
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
/* randomise entire indices list */
|
2002-07-22 15:36:39 +00:00
|
|
|
for(count = playlist->amount - 1; count ; count--)
|
2002-06-04 21:43:38 +00:00
|
|
|
{
|
|
|
|
/* the rand is from 0 to RAND_MAX, so adjust to our value range */
|
2002-07-22 15:36:39 +00:00
|
|
|
candidate = rand() % (count + 1);
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
/* now swap the values at the 'count' and 'candidate' positions */
|
|
|
|
store = playlist->indices[candidate];
|
|
|
|
playlist->indices[candidate] = playlist->indices[count];
|
|
|
|
playlist->indices[count] = store;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------
|
|
|
|
* local variables:
|
|
|
|
* eval: (load-file "../firmware/rockbox-mode.el")
|
|
|
|
* end:
|
|
|
|
*/
|