diff --git a/apps/playlist.c b/apps/playlist.c index fdd88a17bc..465470c069 100644 --- a/apps/playlist.c +++ b/apps/playlist.c @@ -33,157 +33,224 @@ playlist_info_t playlist; +#define PLAYLIST_BUFFER_SIZE (MAX_PATH*200) + +unsigned char playlist_buffer[PLAYLIST_BUFFER_SIZE]; +static int playlist_end_pos = 0; + char now_playing[MAX_PATH+1]; -char* playlist_next(int steps, char *dirname) +void playlist_clear(void) +{ + playlist_end_pos = 0; + playlist_buffer[0] = 0; +} + +int playlist_add(char *filename) +{ + int len = strlen(filename); + + if(len+2 > PLAYLIST_BUFFER_SIZE - playlist_end_pos) + return -1; + + strcpy(&playlist_buffer[playlist_end_pos], filename); + playlist_end_pos += len; + playlist_buffer[playlist_end_pos++] = '\n'; + playlist_buffer[playlist_end_pos] = '\0'; + return 0; +} + +char* playlist_next(int steps) { int seek; int max; int fd; int i; - char buf[MAX_PATH+1]; + char *buf; char dir_buf[MAX_PATH+1]; char *dir_end; playlist.index = (playlist.index+steps) % playlist.amount; seek = playlist.indices[playlist.index]; - fd = open(playlist.filename, O_RDONLY); - if(-1 != fd) { - lseek(fd, seek, SEEK_SET); - max = read(fd, buf, sizeof(buf)-1); - close(fd); - - /* Zero-terminate the file name */ - seek=0; - while((buf[seek] != '\n') && - (buf[seek] != '\r') && - (seek < max)) - seek++; - - /* Now work back killing white space */ - while((buf[seek-1] == ' ') || - (buf[seek-1] == '\t')) - seek--; - - buf[seek]=0; - - /* replace backslashes with forward slashes */ - for ( i=0; ifilename[0] = '\0'; - playlist->index = 0; - playlist->amount = 0; + 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 ) +void add_indices_to_playlist(void) { int nread; - int fd; + int fd = -1; int i = 0; int store_index = 0; int count = 0; int next_tick = current_tick + HZ; - unsigned char *p; - unsigned char buf[512]; + unsigned char *p = playlist_buffer; char line[16]; - fd = open(playlist->filename, O_RDONLY); - if(-1 == fd) - return; /* failure */ + if(!playlist.in_ram) { + fd = open(playlist.filename, O_RDONLY); + if(-1 == fd) + return; /* failure */ + } store_index = 1; - while((nread = read(fd, &buf, sizeof(buf))) != 0) + while(1) { - p = buf; + if(playlist.in_ram) { + nread = playlist_end_pos; + } else { + nread = read(fd, playlist_buffer, PLAYLIST_BUFFER_SIZE); + /* Terminate on EOF */ + if(nread <= 0) + break; + } + + p = playlist_buffer; for(count=0; count < nread; count++,p++) { @@ -203,36 +270,45 @@ void add_indices_to_playlist( playlist_info_t *playlist ) { /* Store a new entry */ - playlist->indices[ playlist->amount ] = i+count; - playlist->amount++; - if ( playlist->amount >= MAX_PLAYLIST_SIZE ) { - close(fd); + playlist.indices[ playlist.amount ] = i+count; + playlist.amount++; + if ( playlist.amount >= MAX_PLAYLIST_SIZE ) { + if(!playlist.in_ram) + close(fd); return; } store_index = 0; - if ( current_tick >= next_tick ) { - next_tick = current_tick + HZ; - snprintf(line, sizeof line, "%d files", playlist->amount); - lcd_puts(0,1,line); - lcd_update(); + /* Update the screen if it takes very long */ + if(!playlist.in_ram) { + if ( current_tick >= next_tick ) { + next_tick = current_tick + HZ; + snprintf(line, sizeof line, "%d files", + playlist.amount); + lcd_puts(0,1,line); + lcd_update(); + } } } } i+= count; - } - snprintf(line, sizeof line, "%d files", playlist->amount); - lcd_puts(0,1,line); - lcd_update(); - close(fd); + if(playlist.in_ram) + break; + } + if(!playlist.in_ram) { + snprintf(line, sizeof line, "%d files", playlist.amount); + lcd_puts(0,1,line); + lcd_update(); + close(fd); + } } /* * randomly rearrange the array of indices for the playlist */ -void randomise_playlist( playlist_info_t *playlist, unsigned int seed ) +void randomise_playlist( unsigned int seed ) { int count; int candidate; @@ -242,15 +318,15 @@ void randomise_playlist( playlist_info_t *playlist, unsigned int seed ) srand( seed ); /* randomise entire indices list */ - for(count = playlist->amount - 1; count >= 0; count--) + for(count = playlist.amount - 1; count >= 0; count--) { /* the rand is from 0 to RAND_MAX, so adjust to our value range */ candidate = rand() % (count + 1); /* now swap the values at the 'count' and 'candidate' positions */ - store = playlist->indices[candidate]; - playlist->indices[candidate] = playlist->indices[count]; - playlist->indices[count] = store; + store = playlist.indices[candidate]; + playlist.indices[candidate] = playlist.indices[count]; + playlist.indices[count] = store; } } @@ -265,11 +341,11 @@ static int compare(const void* p1, const void* p2) /* * sort the array of indices for the playlist */ -void sort_playlist( playlist_info_t *playlist ) +void sort_playlist(void) { - if (playlist->amount > 0) + if (playlist.amount > 0) { - qsort(&playlist->indices, playlist->amount, sizeof(playlist->indices[0]), compare); + qsort(&playlist.indices, playlist.amount, sizeof(playlist.indices[0]), compare); } } diff --git a/apps/playlist.h b/apps/playlist.h index 3d37c5da5f..a218209857 100644 --- a/apps/playlist.h +++ b/apps/playlist.h @@ -29,20 +29,24 @@ typedef struct { char filename[MAX_PATH]; /* path name of m3u playlist on disk */ + int dirlen; /* Length of the path to the playlist file */ int indices[MAX_PLAYLIST_SIZE]; /* array of indices */ int index; /* index of *NEXT* track to play */ int seed; /* random seed */ int amount; /* number of tracks in the index */ + bool in_ram; /* True if the playlist is RAM-based */ } playlist_info_t; -extern playlist_info_t playlist; +//extern playlist_info_t playlist; extern bool playlist_shuffle; void play_list(char *dir, char *file); -char* playlist_next(int steps, char *dirname); -void randomise_playlist( playlist_info_t *playlist, unsigned int seed ); -void sort_playlist( playlist_info_t *playlist ); -void empty_playlist( playlist_info_t *playlist ); -void add_indices_to_playlist( playlist_info_t *playlist ); +char* playlist_next(int steps); +void randomise_playlist( unsigned int seed ); +void sort_playlist(void); +void empty_playlist(void); +void add_indices_to_playlist(void); +void playlist_clear(void); +int playlist_add(char *filename); #endif /* __PLAYLIST_H__ */