diff options
author | Daniel Stenberg <daniel@haxx.se> | 2002-06-04 21:43:38 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2002-06-04 21:43:38 +0000 |
commit | 4d1fb48494284abc5a47bd70040d4f9ffe4649d4 (patch) | |
tree | 7ea9d0a64af75d12a4585cd16125cd44c7e618fb | |
parent | 9c5418648f538ba536645445febc69fdcd72c08d (diff) | |
download | rockbox-4d1fb48494284abc5a47bd70040d4f9ffe4649d4.tar.gz rockbox-4d1fb48494284abc5a47bd70040d4f9ffe4649d4.zip |
Moved playlist code here from the firmware directory, also adjusted it
and edited to support the mpeg thread request-style API. Believe it or not,
but this passes my basic tests for playlist funcionality.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@890 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/playlist.c | 164 | ||||
-rw-r--r-- | apps/playlist.h | 61 |
2 files changed, 225 insertions, 0 deletions
diff --git a/apps/playlist.c b/apps/playlist.c new file mode 100644 index 0000000000..60786f67ce --- /dev/null +++ b/apps/playlist.c | |||
@@ -0,0 +1,164 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by wavey@wavey.org | ||
11 | * | ||
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. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include <stdio.h> | ||
21 | #include <malloc.h> | ||
22 | #include <time.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include "playlist.h" | ||
26 | #include <file.h> | ||
27 | #include "sprintf.h" | ||
28 | #include "debug.h" | ||
29 | |||
30 | playlist_info_t playlist; | ||
31 | |||
32 | int index_array[1000]; | ||
33 | |||
34 | char now_playing[256]; | ||
35 | |||
36 | char* playlist_next(int type) | ||
37 | { | ||
38 | int seek = playlist.indices[playlist.index]; | ||
39 | int max; | ||
40 | int fd; | ||
41 | playlist.index = (playlist.index+1) % playlist.amount; | ||
42 | |||
43 | fd = open(playlist.filename, O_RDONLY); | ||
44 | if(-1 != fd) { | ||
45 | lseek(fd, seek, SEEK_SET); | ||
46 | max = read(fd, now_playing, sizeof(now_playing)); | ||
47 | close(fd); | ||
48 | |||
49 | seek=0; | ||
50 | while((now_playing[seek] != '\n') && | ||
51 | (now_playing[seek] != '\r') && | ||
52 | (seek < max)) | ||
53 | seek++; | ||
54 | if(seek == max) | ||
55 | seek = max-1; | ||
56 | now_playing[seek]=0; | ||
57 | |||
58 | return now_playing; | ||
59 | } | ||
60 | else | ||
61 | return NULL; | ||
62 | } | ||
63 | |||
64 | void play_list(char *dir, char *file) | ||
65 | { | ||
66 | empty_playlist(&playlist); | ||
67 | |||
68 | snprintf(playlist.filename, sizeof(playlist.filename), | ||
69 | "%s/%s", dir, file); | ||
70 | |||
71 | /* add track indices to playlist data structure */ | ||
72 | add_indices_to_playlist(&playlist); | ||
73 | |||
74 | /* if shuffle is wanted, this is where to do that */ | ||
75 | |||
76 | /* also make the first song get playing */ | ||
77 | mpeg_play(playlist_next(0)); | ||
78 | |||
79 | } | ||
80 | |||
81 | /* | ||
82 | * remove any filename and indices associated with the playlist | ||
83 | */ | ||
84 | void empty_playlist( playlist_info_t *playlist ) | ||
85 | { | ||
86 | playlist->filename[0] = '\0'; | ||
87 | playlist->index = 0; | ||
88 | playlist->amount = 0; | ||
89 | } | ||
90 | |||
91 | /* | ||
92 | * calculate track offsets within a playlist file | ||
93 | */ | ||
94 | void add_indices_to_playlist( playlist_info_t *playlist ) | ||
95 | { | ||
96 | char *p; | ||
97 | int i = 0; | ||
98 | unsigned char byte; | ||
99 | unsigned char lastbyte='\n'; | ||
100 | int nread; | ||
101 | |||
102 | int fd; | ||
103 | |||
104 | fd = open(playlist->filename, O_RDONLY); | ||
105 | if(-1 == fd) | ||
106 | return; /* failure */ | ||
107 | |||
108 | p = &byte; | ||
109 | |||
110 | /* loop thru buffer, store index whenever we get a new line */ | ||
111 | |||
112 | while((nread = read(fd, &byte, 1)) == 1) | ||
113 | { | ||
114 | /* move thru (any) newlines */ | ||
115 | |||
116 | if(( byte != '\n' ) && ( byte != '\r' ) && | ||
117 | ((lastbyte == '\n') || (lastbyte == '\r'))) { | ||
118 | /* we're now at the start of a new track filename. store index */ | ||
119 | |||
120 | DEBUGF("tune %d at position %d\n", playlist->amount, i); | ||
121 | playlist->indices [ playlist->amount ] = i; | ||
122 | playlist->amount++; | ||
123 | } | ||
124 | i++; | ||
125 | lastbyte = byte; | ||
126 | } | ||
127 | |||
128 | close(fd); | ||
129 | } | ||
130 | |||
131 | /* | ||
132 | * randomly rearrange the array of indices for the playlist | ||
133 | */ | ||
134 | void randomise_playlist( playlist_info_t *playlist, unsigned int seed ) | ||
135 | { | ||
136 | int count = 0; | ||
137 | int candidate; | ||
138 | int store; | ||
139 | |||
140 | /* seed with the given seed */ | ||
141 | srand( seed ); | ||
142 | |||
143 | /* randomise entire indices list */ | ||
144 | |||
145 | while( count < playlist->amount ) | ||
146 | { | ||
147 | /* the rand is from 0 to RAND_MAX, so adjust to our value range */ | ||
148 | candidate = rand() % ( playlist->amount ); | ||
149 | |||
150 | /* now swap the values at the 'count' and 'candidate' positions */ | ||
151 | store = playlist->indices[candidate]; | ||
152 | playlist->indices[candidate] = playlist->indices[count]; | ||
153 | playlist->indices[count] = store; | ||
154 | |||
155 | /* move along */ | ||
156 | count++; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | /* ----------------------------------------------------------------- | ||
161 | * local variables: | ||
162 | * eval: (load-file "../firmware/rockbox-mode.el") | ||
163 | * end: | ||
164 | */ | ||
diff --git a/apps/playlist.h b/apps/playlist.h new file mode 100644 index 0000000000..2271403920 --- /dev/null +++ b/apps/playlist.h | |||
@@ -0,0 +1,61 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by wavey@wavey.org | ||
11 | * | ||
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. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #ifndef __PLAYLIST_H__ | ||
21 | #define __PLAYLIST_H__ | ||
22 | |||
23 | /* playlist data */ | ||
24 | |||
25 | #define MAX_PLAYLIST_SIZE 1000 | ||
26 | typedef struct | ||
27 | { | ||
28 | char filename[256]; /* path name of m3u playlist on disk */ | ||
29 | int indices[MAX_PLAYLIST_SIZE]; /* array of indices */ | ||
30 | int index; /* index of *NEXT* track to play */ | ||
31 | int seed; /* random seed */ | ||
32 | int amount; /* number of tracks in the index */ | ||
33 | } playlist_info_t; | ||
34 | |||
35 | void play_list(char *dir, char *file); | ||
36 | |||
37 | void read_entire_file( char *buf, const char *filename ); | ||
38 | void load_playlist( playlist_info_t *playlist, const char *filename ); | ||
39 | void extract_playlist_indices( char *buf, playlist_info_t *playlist ); | ||
40 | void display_current_playlist( playlist_info_t *playlist ); | ||
41 | void get_indices_as_string( char *string, playlist_info_t *playlist ); | ||
42 | void empty_playlist( playlist_info_t *playlist ); | ||
43 | void add_indices_to_playlist( playlist_info_t *playlist ); | ||
44 | void extend_indices( playlist_info_t *playlist, int new_index ); | ||
45 | void randomise_playlist( playlist_info_t *playlist, unsigned int seed ); | ||
46 | int is_unused_random_in_list( int number, int *original_list, int count ); | ||
47 | |||
48 | /**********/ | ||
49 | |||
50 | |||
51 | |||
52 | |||
53 | |||
54 | |||
55 | int create_playlist( void ); | ||
56 | /*int add_to_playlist( track_t *track );*/ | ||
57 | int remove_from_playlist( int index ); | ||
58 | int set_playlist_position( void ); | ||
59 | /*track_t * get_previous_entry_in_playlist( void );*/ | ||
60 | |||
61 | #endif /* __PLAYLIST_H__ */ | ||