2002-04-30 19:16:15 +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>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "debug.h"
|
2002-05-05 11:59:14 +00:00
|
|
|
#include <file.h>
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* load playlist info from disk
|
|
|
|
*/
|
|
|
|
int reload_playlist_info( playlist_info_t *playlist )
|
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "reload_playlist_info()\n" );
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
/* this is a TEMP stub version */
|
|
|
|
|
|
|
|
/* return a dummy playlist entry */
|
|
|
|
|
2002-05-05 11:16:32 +00:00
|
|
|
strncpy( playlist->filename, "test.m3u", sizeof(playlist->filename) );
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
playlist->indices_count = 4;
|
|
|
|
|
|
|
|
playlist->indices = (void *)malloc( playlist->indices_count * sizeof( int ) );
|
|
|
|
|
|
|
|
playlist->indices[0] = 3;
|
|
|
|
playlist->indices[1] = 2;
|
|
|
|
playlist->indices[2] = 1;
|
|
|
|
playlist->indices[3] = 9;
|
|
|
|
|
|
|
|
playlist->index = 3;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read the contents of an m3u file from disk and store
|
|
|
|
* the indices of each listed track in an array
|
|
|
|
*/
|
|
|
|
void load_playlist( playlist_info_t *playlist, const char *filename ) {
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "load_playlist( %s )\n", filename );
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
/* store playlist filename */
|
2002-05-02 11:44:15 +00:00
|
|
|
strncpy( playlist->filename, filename, sizeof(playlist->filename) );
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
/* add track indices to playlist data structure */
|
2002-05-05 11:59:14 +00:00
|
|
|
add_indices_to_playlist( playlist );
|
2002-04-30 19:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove any filename and indices associated with the playlist
|
|
|
|
*/
|
|
|
|
void empty_playlist( playlist_info_t *playlist ) {
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "empty_playlist()\n" );
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
playlist->filename[0] = '\0';
|
|
|
|
playlist->indices_count = 0;
|
|
|
|
free( (void *)(playlist->indices) );
|
|
|
|
playlist->indices = NULL;
|
|
|
|
playlist->index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-05-05 11:59:14 +00:00
|
|
|
* calculate track offsets within a playlist file
|
2002-04-30 19:16:15 +00:00
|
|
|
*/
|
2002-05-05 11:59:14 +00:00
|
|
|
void add_indices_to_playlist( playlist_info_t *playlist )
|
2002-04-30 19:16:15 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int i = 0;
|
2002-05-05 11:59:14 +00:00
|
|
|
unsigned char byte;
|
|
|
|
unsigned char lastbyte='\n';
|
|
|
|
int nread;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(playlist->filename, O_RDONLY);
|
|
|
|
if(-1 == fd)
|
|
|
|
return; /* failure */
|
|
|
|
|
2002-04-30 19:16:15 +00:00
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
/*DEBUGF( "add_indices_to_playlist()\n" ); */
|
2002-04-30 19:16:15 +00:00
|
|
|
|
2002-05-05 11:59:14 +00:00
|
|
|
p = &byte;
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
/* loop thru buffer, store index whenever we get a new line */
|
|
|
|
|
2002-05-05 11:59:14 +00:00
|
|
|
while((nread = read(fd, &byte, 1)) == 1)
|
2002-04-30 19:16:15 +00:00
|
|
|
{
|
|
|
|
/* move thru (any) newlines */
|
|
|
|
|
2002-05-05 11:59:14 +00:00
|
|
|
if(( byte != '\n' ) && ( byte != '\r' ) &&
|
|
|
|
((lastbyte == '\n') || (lastbyte == '\r')))
|
|
|
|
/* we're now at the start of a new track filename. store index */
|
|
|
|
extend_indices( playlist, i );
|
2002-04-30 19:16:15 +00:00
|
|
|
|
2002-05-05 11:59:14 +00:00
|
|
|
lastbyte = byte;
|
|
|
|
i++;
|
2002-04-30 19:16:15 +00:00
|
|
|
}
|
2002-05-05 11:59:14 +00:00
|
|
|
|
|
|
|
close(fd);
|
2002-04-30 19:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extend the array of ints
|
|
|
|
*/
|
|
|
|
void extend_indices( playlist_info_t *playlist, int new_index )
|
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
/*DEBUGF( "extend_indices(%d)\n", new_index ); */
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
/* increase array size count */
|
|
|
|
|
|
|
|
playlist->indices_count++;
|
|
|
|
|
|
|
|
/* increase size of array to new count size */
|
|
|
|
|
|
|
|
playlist->indices = (int *)realloc( playlist->indices, (playlist->indices_count) * sizeof( int ) );
|
|
|
|
|
|
|
|
/* add new element to end of array */
|
|
|
|
|
|
|
|
playlist->indices[ playlist->indices_count - 1 ] = new_index;
|
|
|
|
}
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
track_t next_playlist_track( playlist_info_t *playlist )
|
|
|
|
{
|
2002-04-30 19:16:15 +00:00
|
|
|
track_t track;
|
2002-05-02 11:44:15 +00:00
|
|
|
strncpy( track.filename, "boogie", sizeof(track.filename) );
|
2002-04-30 19:16:15 +00:00
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* randomly rearrange the array of indices for the playlist
|
|
|
|
*/
|
2002-05-05 11:16:32 +00:00
|
|
|
void randomise_playlist( playlist_info_t *playlist, unsigned int seed )
|
2002-05-02 14:05:51 +00:00
|
|
|
{
|
2002-04-30 19:16:15 +00:00
|
|
|
int count = 0;
|
|
|
|
int candidate;
|
2002-05-05 11:16:32 +00:00
|
|
|
int store;
|
2002-04-30 19:16:15 +00:00
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "randomise_playlist()\n" );
|
2002-04-30 19:16:15 +00:00
|
|
|
|
2002-05-05 11:16:32 +00:00
|
|
|
/* seed with the given seed */
|
2002-04-30 19:16:15 +00:00
|
|
|
srand( seed );
|
|
|
|
|
|
|
|
/* randomise entire indices list */
|
|
|
|
|
|
|
|
while( count < playlist->indices_count )
|
|
|
|
{
|
2002-05-05 11:16:32 +00:00
|
|
|
/* the rand is from 0 to RAND_MAX, so adjust to our value range */
|
|
|
|
candidate = rand() % ( playlist->indices_count );
|
|
|
|
|
|
|
|
/* now swap the values at the 'count' and 'candidate' positions */
|
|
|
|
store = playlist->indices[candidate];
|
|
|
|
playlist->indices[candidate] = playlist->indices[count];
|
|
|
|
playlist->indices[count] = store;
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
/* move along */
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dump the details of a track to stdout
|
|
|
|
*/
|
|
|
|
void display_playlist_track( track_t *track )
|
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "track: %s\n", track->filename );
|
2002-04-30 19:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dump the current playlist info
|
|
|
|
*/
|
|
|
|
void display_current_playlist( playlist_info_t *playlist )
|
|
|
|
{
|
|
|
|
char indices[2048];
|
|
|
|
indices[0]='\0';
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
/*DEBUGF( "\ndisplay_current_playlist()\n" ); */
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
if( playlist->indices_count != 0 )
|
|
|
|
{
|
|
|
|
get_indices_as_string( indices, playlist );
|
|
|
|
}
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "\nfilename:\t%s\ntotal:\t\t%d\nindices:\t%s\ncurrent index:\t%d\n\n",
|
2002-04-30 19:16:15 +00:00
|
|
|
playlist->filename,
|
|
|
|
playlist->indices_count,
|
|
|
|
indices,
|
|
|
|
playlist->index );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* produce a string of the playlist indices
|
|
|
|
*/
|
|
|
|
void get_indices_as_string( char *string, playlist_info_t *playlist )
|
|
|
|
{
|
|
|
|
char tmp[8];
|
|
|
|
int count = 0;
|
|
|
|
int *p = playlist->indices;
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
/*DEBUGF( "get_indices_as_string()\n" ); */
|
2002-04-30 19:16:15 +00:00
|
|
|
|
|
|
|
while( count < playlist->indices_count ) {
|
|
|
|
|
|
|
|
if( strlen( string ) == 0 )
|
|
|
|
{
|
|
|
|
/* first iteration - no comma */
|
|
|
|
|
2002-05-02 11:44:15 +00:00
|
|
|
snprintf( tmp, sizeof(tmp), "%d", p[count] );
|
2002-04-30 19:16:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* normal iteration - insert comma */
|
|
|
|
|
2002-05-02 11:44:15 +00:00
|
|
|
snprintf( tmp, sizeof(tmp), ",%d", p[count] );
|
2002-04-30 19:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
strcat( string, tmp );
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Session Start (devlin.openprojects.net:alan): Thu Apr 25 15:13:27 2002
|
|
|
|
<alan> for shuffle mode, it's really easy to use the array as a random stack
|
|
|
|
<alan> just peek one randomly and exchange it with the last element in the stack
|
|
|
|
<alan> when stack is void, just grow the stack with its initial size : elements are still there but in a random order
|
|
|
|
<alan> note :
|
|
|
|
<alan> a stack is void when all songs were played
|
|
|
|
<alan> it is an O(1) algo
|
|
|
|
<alan> i don't see how you can do it with a list
|
|
|
|
*/
|
|
|
|
|
2002-05-05 11:16:32 +00:00
|
|
|
/* -----------------------------------------------------------------
|
|
|
|
* local variables:
|
|
|
|
* eval: (load-file "rockbox-mode.el")
|
|
|
|
* end:
|
|
|
|
*/
|