Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 Miika Pekkarinen |
| 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 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <ctype.h> |
| 23 | |
| 24 | #include "system.h" |
| 25 | #include "thread.h" |
| 26 | #include "file.h" |
| 27 | #include "lcd.h" |
| 28 | #include "font.h" |
| 29 | #include "backlight.h" |
| 30 | #include "button.h" |
| 31 | #include "kernel.h" |
| 32 | #include "tree.h" |
| 33 | #include "debug.h" |
| 34 | #include "sprintf.h" |
| 35 | #include "settings.h" |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 36 | #include "codecs.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 37 | #include "wps.h" |
| 38 | #include "wps-display.h" |
| 39 | #include "audio.h" |
| 40 | #include "logf.h" |
| 41 | #include "mp3_playback.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 42 | #include "usb.h" |
| 43 | #include "status.h" |
| 44 | #include "main_menu.h" |
| 45 | #include "ata.h" |
| 46 | #include "screens.h" |
| 47 | #include "playlist.h" |
| 48 | #include "playback.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 49 | #include "pcmbuf.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 50 | #include "pcm_playback.h" |
| 51 | #include "buffer.h" |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 52 | #include "dsp.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 53 | #ifdef HAVE_LCD_BITMAP |
| 54 | #include "icons.h" |
| 55 | #include "peakmeter.h" |
| 56 | #include "action.h" |
| 57 | #endif |
| 58 | #include "lang.h" |
| 59 | #include "bookmark.h" |
| 60 | #include "misc.h" |
| 61 | #include "sound.h" |
Dave Chapman | 3ad485b | 2005-06-14 22:27:57 +0000 | [diff] [blame] | 62 | #include "metadata.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 63 | |
Miika Pekkarinen | 84d6f9e | 2005-06-29 20:50:58 +0000 | [diff] [blame] | 64 | static volatile bool codec_loaded; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 65 | static volatile bool playing; |
| 66 | static volatile bool paused; |
| 67 | |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 68 | #define CODEC_VORBIS "/.rockbox/codecs/vorbis.codec"; |
| 69 | #define CODEC_MPA_L3 "/.rockbox/codecs/mpa.codec"; |
| 70 | #define CODEC_FLAC "/.rockbox/codecs/flac.codec"; |
| 71 | #define CODEC_WAV "/.rockbox/codecs/wav.codec"; |
| 72 | #define CODEC_A52 "/.rockbox/codecs/a52.codec"; |
| 73 | #define CODEC_MPC "/.rockbox/codecs/mpc.codec"; |
| 74 | #define CODEC_WAVPACK "/.rockbox/codecs/wavpack.codec"; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 75 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 76 | #define AUDIO_FILL_CYCLE (1024*256) |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 77 | #define AUDIO_DEFAULT_WATERMARK (1024*512) |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 78 | #define AUDIO_DEFAULT_FILECHUNK (1024*32) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 79 | |
| 80 | #define AUDIO_PLAY 1 |
| 81 | #define AUDIO_STOP 2 |
| 82 | #define AUDIO_PAUSE 3 |
| 83 | #define AUDIO_RESUME 4 |
| 84 | #define AUDIO_NEXT 5 |
| 85 | #define AUDIO_PREV 6 |
| 86 | #define AUDIO_FF_REWIND 7 |
| 87 | #define AUDIO_FLUSH_RELOAD 8 |
| 88 | #define AUDIO_CODEC_DONE 9 |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 89 | #define AUDIO_FLUSH 10 |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 90 | #define AUDIO_TRACK_CHANGED 11 |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 91 | |
| 92 | #define CODEC_LOAD 1 |
| 93 | #define CODEC_LOAD_DISK 2 |
| 94 | |
| 95 | /* As defined in plugins/lib/xxx2wav.h */ |
| 96 | #define MALLOC_BUFSIZE (512*1024) |
| 97 | #define GUARD_BUFSIZE (8*1024) |
| 98 | |
| 99 | /* TODO: |
| 100 | Handle playlist_peek in mpeg.c |
| 101 | Track changing |
| 102 | */ |
| 103 | |
| 104 | extern bool audio_is_initialized; |
| 105 | |
| 106 | /* Buffer control thread. */ |
| 107 | static struct event_queue audio_queue; |
| 108 | static long audio_stack[(DEFAULT_STACK_SIZE + 0x1000)/sizeof(long)]; |
| 109 | static const char audio_thread_name[] = "audio"; |
| 110 | |
| 111 | /* Codec thread. */ |
| 112 | static struct event_queue codec_queue; |
Jens Arnold | bb3ed3c | 2005-07-03 15:09:11 +0000 | [diff] [blame] | 113 | static long codec_stack[(DEFAULT_STACK_SIZE + 0x2500)/sizeof(long)] IDATA_ATTR; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 114 | static const char codec_thread_name[] = "codec"; |
| 115 | |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 116 | static struct mutex mutex_bufferfill; |
| 117 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 118 | /* Is file buffer currently being refilled? */ |
| 119 | static volatile bool filling; |
| 120 | |
| 121 | /* Ring buffer where tracks and codecs are loaded. */ |
Linus Nielsen Feltzing | ecf2f747 | 2005-06-22 02:47:54 +0000 | [diff] [blame] | 122 | static char *codecbuf; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 123 | |
| 124 | /* Total size of the ring buffer. */ |
Linus Nielsen Feltzing | b5a0f70 | 2005-06-22 14:03:04 +0000 | [diff] [blame] | 125 | int codecbuflen; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 126 | |
| 127 | /* Bytes available in the buffer. */ |
Linus Nielsen Feltzing | b5a0f70 | 2005-06-22 14:03:04 +0000 | [diff] [blame] | 128 | int codecbufused; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 129 | |
| 130 | /* Ring buffer read and write indexes. */ |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 131 | static volatile int buf_ridx; |
| 132 | static volatile int buf_widx; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 133 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 134 | static int last_peek_offset; |
| 135 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 136 | /* Track information (count in file buffer, read/write indexes for |
| 137 | track ring structure. */ |
Linus Nielsen Feltzing | 17098e1 | 2005-06-22 20:37:31 +0000 | [diff] [blame] | 138 | int track_count; |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 139 | static volatile int track_ridx; |
| 140 | static volatile int track_widx; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 141 | static bool track_changed; |
| 142 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 143 | /* Partially loaded song's file handle to continue buffering later. */ |
| 144 | static int current_fd; |
| 145 | |
| 146 | /* Information about how many bytes left on the buffer re-fill run. */ |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 147 | static long fill_bytesleft; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 148 | |
| 149 | /* Track info structure about songs in the file buffer. */ |
| 150 | static struct track_info tracks[MAX_TRACK]; |
| 151 | |
| 152 | /* Pointer to track info structure about current song playing. */ |
Miika Pekkarinen | 7d6d122 | 2005-06-29 21:36:30 +0000 | [diff] [blame] | 153 | static struct track_info *cur_ti; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 154 | |
| 155 | /* Codec API including function callbacks. */ |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 156 | extern struct codec_api ci; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 157 | |
| 158 | /* When we change a song and buffer is not in filling state, this |
| 159 | variable keeps information about whether to go a next/previous track. */ |
| 160 | static int new_track; |
| 161 | |
Miika Pekkarinen | 7d6d122 | 2005-06-29 21:36:30 +0000 | [diff] [blame] | 162 | /* Callback function to call when current track has really changed. */ |
| 163 | void (*track_changed_callback)(struct track_info *ti); |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 164 | void (*track_buffer_callback)(struct mp3entry *id3, bool last_track); |
Miika Pekkarinen | 9bde038 | 2005-07-03 18:36:24 +0000 | [diff] [blame] | 165 | void (*track_unbuffer_callback)(struct mp3entry *id3, bool last_track); |
Miika Pekkarinen | 7d6d122 | 2005-06-29 21:36:30 +0000 | [diff] [blame] | 166 | |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 167 | /* Configuration */ |
| 168 | static int conf_bufferlimit; |
| 169 | static int conf_watermark; |
| 170 | static int conf_filechunk; |
Miika Pekkarinen | 2326bea | 2005-06-10 13:43:12 +0000 | [diff] [blame] | 171 | |
Linus Nielsen Feltzing | 4aaa321 | 2005-06-06 00:35:21 +0000 | [diff] [blame] | 172 | static bool v1first = false; |
| 173 | |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 174 | static void mp3_set_elapsed(struct mp3entry* id3); |
| 175 | int mp3_get_file_pos(void); |
| 176 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 177 | bool codec_pcmbuf_insert_callback(char *buf, long length) |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 178 | { |
| 179 | char *dest; |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 180 | long realsize; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 181 | int factor; |
| 182 | int next_channel = 0; |
| 183 | int processed_length; |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 184 | int mono = 0; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 185 | |
| 186 | /* If non-interleaved stereo mode. */ |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 187 | if (dsp_config.stereo_mode == STEREO_NONINTERLEAVED) |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 188 | next_channel = length / 2; |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 189 | else if (dsp_config.stereo_mode == STEREO_MONO) { |
| 190 | length *= 2; |
| 191 | mono = 1; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (dsp_config.sample_depth > 16) { |
| 195 | length /= 2; |
| 196 | factor = 1; |
| 197 | } else { |
| 198 | factor = 0; |
| 199 | } |
| 200 | |
| 201 | while (length > 0) { |
| 202 | /* Request a few extra bytes for resampling. */ |
| 203 | /* FIXME: Required extra bytes SHOULD be calculated. */ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 204 | while ((dest = pcmbuf_request_buffer(length+16384, &realsize)) == NULL) |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 205 | yield(); |
| 206 | |
| 207 | if (realsize < 16384) { |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 208 | pcmbuf_flush_buffer(0); |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 209 | continue ; |
| 210 | } |
| 211 | |
| 212 | realsize -= 16384; |
| 213 | |
| 214 | if (next_channel) { |
| 215 | processed_length = dsp_process(dest, buf, realsize / 4) * 2; |
| 216 | dsp_process(dest, buf + next_channel, realsize / 4); |
| 217 | } else { |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 218 | processed_length = dsp_process(dest, buf, realsize >> (mono + 1)); |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 219 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 220 | pcmbuf_flush_buffer(processed_length); |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 221 | length -= realsize; |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 222 | buf += realsize << (factor + mono); |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return true; |
| 226 | } |
| 227 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 228 | bool codec_pcmbuf_insert_split_callback(void *ch1, void *ch2, |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 229 | long length) |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 230 | { |
| 231 | char *dest; |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 232 | long realsize; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 233 | int factor; |
| 234 | int processed_length; |
| 235 | |
| 236 | /* non-interleaved stereo mode. */ |
| 237 | if (dsp_config.sample_depth > 16) { |
| 238 | factor = 0; |
| 239 | } else { |
| 240 | length /= 2; |
| 241 | factor = 1; |
| 242 | } |
| 243 | |
| 244 | while (length > 0) { |
| 245 | /* Request a few extra bytes for resampling. */ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 246 | while ((dest = pcmbuf_request_buffer(length+4096, &realsize)) == NULL) |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 247 | yield(); |
| 248 | |
| 249 | if (realsize < 4096) { |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 250 | pcmbuf_flush_buffer(0); |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 251 | continue ; |
| 252 | } |
| 253 | |
| 254 | realsize -= 4096; |
| 255 | |
| 256 | processed_length = dsp_process(dest, ch1, realsize / 4) * 2; |
| 257 | dsp_process(dest, ch2, realsize / 4); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 258 | pcmbuf_flush_buffer(processed_length); |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 259 | length -= realsize; |
| 260 | ch1 += realsize >> factor; |
| 261 | ch2 += realsize >> factor; |
| 262 | } |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 267 | void* get_codec_memory_callback(long *size) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 268 | { |
| 269 | *size = MALLOC_BUFSIZE; |
| 270 | return &audiobuf[0]; |
| 271 | } |
| 272 | |
| 273 | void codec_set_elapsed_callback(unsigned int value) |
| 274 | { |
| 275 | unsigned int latency; |
| 276 | |
Miika Pekkarinen | cf18f96 | 2005-06-20 06:49:21 +0000 | [diff] [blame] | 277 | if (ci.stop_codec) |
| 278 | return ; |
| 279 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 280 | latency = pcmbuf_get_latency(); |
Miika Pekkarinen | 92ea04a | 2005-06-15 12:53:50 +0000 | [diff] [blame] | 281 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 282 | if (value < latency) { |
| 283 | cur_ti->id3.elapsed = 0; |
| 284 | } else if (value - latency > cur_ti->id3.elapsed |
| 285 | || value - latency < cur_ti->id3.elapsed - 2) { |
| 286 | cur_ti->id3.elapsed = value - latency; |
| 287 | } |
| 288 | } |
| 289 | |
Ryan Jackson | d191756 | 2005-07-12 16:45:38 +0000 | [diff] [blame] | 290 | void codec_set_offset_callback(unsigned int value) |
| 291 | { |
| 292 | unsigned int latency; |
| 293 | |
| 294 | if (ci.stop_codec) |
| 295 | return ; |
| 296 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 297 | /* The 1000 here is a hack. pcmbuf_get_latency() should |
Ryan Jackson | d191756 | 2005-07-12 16:45:38 +0000 | [diff] [blame] | 298 | * be more accurate |
| 299 | */ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 300 | latency = (pcmbuf_get_latency() + 1000) * cur_ti->id3.bitrate / 8; |
Ryan Jackson | d191756 | 2005-07-12 16:45:38 +0000 | [diff] [blame] | 301 | |
| 302 | if (value < latency) { |
| 303 | cur_ti->id3.offset = 0; |
| 304 | } else if (value - latency > (unsigned int)cur_ti->id3.offset ) { |
| 305 | cur_ti->id3.offset = value - latency; |
| 306 | } |
| 307 | } |
| 308 | |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 309 | long codec_filebuf_callback(void *ptr, long size) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 310 | { |
| 311 | char *buf = (char *)ptr; |
| 312 | int copy_n; |
| 313 | int part_n; |
| 314 | |
| 315 | if (ci.stop_codec || !playing) |
| 316 | return 0; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 317 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 318 | copy_n = MIN((off_t)size, (off_t)cur_ti->available + cur_ti->filerem); |
| 319 | |
| 320 | while (copy_n > cur_ti->available) { |
| 321 | yield(); |
| 322 | if (ci.stop_codec) |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | if (copy_n == 0) |
| 327 | return 0; |
| 328 | |
| 329 | part_n = MIN(copy_n, codecbuflen - buf_ridx); |
| 330 | memcpy(buf, &codecbuf[buf_ridx], part_n); |
| 331 | if (part_n < copy_n) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 332 | memcpy(&buf[part_n], &codecbuf[0], copy_n - part_n); |
| 333 | } |
| 334 | |
| 335 | buf_ridx += copy_n; |
| 336 | if (buf_ridx >= codecbuflen) |
| 337 | buf_ridx -= codecbuflen; |
| 338 | ci.curpos += copy_n; |
| 339 | cur_ti->available -= copy_n; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 340 | codecbufused -= copy_n; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 341 | |
| 342 | return copy_n; |
| 343 | } |
| 344 | |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 345 | void* codec_request_buffer_callback(long *realsize, long reqsize) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 346 | { |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 347 | long part_n; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 348 | |
| 349 | if (ci.stop_codec || !playing) { |
| 350 | *realsize = 0; |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | *realsize = MIN((off_t)reqsize, (off_t)cur_ti->available + cur_ti->filerem); |
| 355 | if (*realsize == 0) { |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | while ((int)*realsize > cur_ti->available) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 360 | yield(); |
| 361 | if (ci.stop_codec) { |
Miika Pekkarinen | 9ff373c | 2005-06-10 20:29:35 +0000 | [diff] [blame] | 362 | *realsize = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 363 | return NULL; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | part_n = MIN((int)*realsize, codecbuflen - buf_ridx); |
| 368 | if (part_n < *realsize) { |
| 369 | part_n += GUARD_BUFSIZE; |
| 370 | if (part_n < *realsize) |
| 371 | *realsize = part_n; |
| 372 | memcpy(&codecbuf[codecbuflen], &codecbuf[0], *realsize - |
| 373 | (codecbuflen - buf_ridx)); |
| 374 | } |
| 375 | |
| 376 | return (char *)&codecbuf[buf_ridx]; |
| 377 | } |
| 378 | |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 379 | static bool rebuffer_and_seek(int newpos) |
| 380 | { |
| 381 | int fd; |
| 382 | |
| 383 | logf("Re-buffering song"); |
| 384 | mutex_lock(&mutex_bufferfill); |
| 385 | |
| 386 | /* (Re-)open current track's file handle. */ |
| 387 | fd = open(playlist_peek(0), O_RDONLY); |
| 388 | if (fd < 0) { |
| 389 | logf("Open failed!"); |
| 390 | mutex_unlock(&mutex_bufferfill); |
| 391 | return false; |
| 392 | } |
| 393 | if (current_fd >= 0) |
| 394 | close(current_fd); |
| 395 | current_fd = fd; |
| 396 | |
| 397 | /* Clear codec buffer. */ |
| 398 | audio_invalidate_tracks(); |
| 399 | codecbufused = 0; |
| 400 | buf_ridx = buf_widx = 0; |
| 401 | cur_ti->filerem = cur_ti->filesize - newpos; |
| 402 | cur_ti->filepos = newpos; |
| 403 | cur_ti->start_pos = newpos; |
| 404 | ci.curpos = newpos; |
| 405 | cur_ti->available = 0; |
| 406 | lseek(current_fd, newpos, SEEK_SET); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 407 | pcmbuf_flush_audio(); |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 408 | |
| 409 | mutex_unlock(&mutex_bufferfill); |
| 410 | |
Miika Pekkarinen | 2724d0b | 2005-07-01 21:00:02 +0000 | [diff] [blame] | 411 | while (cur_ti->available == 0 && cur_ti->filerem > 0) { |
| 412 | yield(); |
| 413 | if (ci.stop_codec) |
| 414 | return false; |
| 415 | } |
Linus Nielsen Feltzing | c4b7c67 | 2005-07-11 06:47:35 +0000 | [diff] [blame] | 416 | |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 417 | return true; |
| 418 | } |
| 419 | |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 420 | void codec_advance_buffer_callback(long amount) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 421 | { |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 422 | if (amount > cur_ti->available + cur_ti->filerem) |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 423 | amount = cur_ti->available + cur_ti->filerem; |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 424 | |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 425 | if (amount > cur_ti->available) { |
| 426 | if (!rebuffer_and_seek(ci.curpos + amount)) |
| 427 | ci.stop_codec = true; |
| 428 | return ; |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 429 | } |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 430 | |
| 431 | buf_ridx += amount; |
| 432 | if (buf_ridx >= codecbuflen) |
| 433 | buf_ridx -= codecbuflen; |
| 434 | cur_ti->available -= amount; |
| 435 | codecbufused -= amount; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 436 | ci.curpos += amount; |
Ryan Jackson | d191756 | 2005-07-12 16:45:38 +0000 | [diff] [blame] | 437 | codec_set_offset_callback(ci.curpos); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void codec_advance_buffer_loc_callback(void *ptr) |
| 441 | { |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 442 | long amount; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 443 | |
| 444 | amount = (int)ptr - (int)&codecbuf[buf_ridx]; |
| 445 | codec_advance_buffer_callback(amount); |
| 446 | } |
| 447 | |
| 448 | off_t codec_mp3_get_filepos_callback(int newtime) |
| 449 | { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 450 | off_t newpos; |
| 451 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 452 | cur_ti->id3.elapsed = newtime; |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 453 | newpos = mp3_get_file_pos(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 454 | |
| 455 | return newpos; |
| 456 | } |
| 457 | |
| 458 | bool codec_seek_buffer_callback(off_t newpos) |
| 459 | { |
| 460 | int difference; |
| 461 | |
| 462 | if (newpos < 0) |
| 463 | newpos = 0; |
| 464 | |
| 465 | if (newpos >= cur_ti->filesize) |
| 466 | newpos = cur_ti->filesize - 1; |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 467 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 468 | difference = newpos - ci.curpos; |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 469 | /* Seeking forward */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 470 | if (difference >= 0) { |
| 471 | logf("seek: +%d", difference); |
| 472 | codec_advance_buffer_callback(difference); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 473 | if (!pcmbuf_is_crossfade_active()) |
| 474 | pcmbuf_play_stop(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 475 | return true; |
| 476 | } |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 477 | |
| 478 | /* Seeking backward */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 479 | difference = -difference; |
| 480 | if (ci.curpos - difference < 0) |
| 481 | difference = ci.curpos; |
| 482 | |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 483 | /* We need to reload the song. */ |
| 484 | if (newpos < cur_ti->start_pos) |
| 485 | return rebuffer_and_seek(newpos); |
| 486 | |
| 487 | /* Seeking inside buffer space. */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 488 | logf("seek: -%d", difference); |
| 489 | codecbufused += difference; |
| 490 | cur_ti->available += difference; |
| 491 | buf_ridx -= difference; |
| 492 | if (buf_ridx < 0) |
| 493 | buf_ridx = codecbuflen + buf_ridx; |
| 494 | ci.curpos -= difference; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 495 | if (!pcmbuf_is_crossfade_active()) |
| 496 | pcmbuf_play_stop(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 497 | |
| 498 | return true; |
| 499 | } |
| 500 | |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 501 | void codec_configure_callback(int setting, void *value) |
| 502 | { |
| 503 | switch (setting) { |
| 504 | case CODEC_SET_FILEBUF_WATERMARK: |
| 505 | conf_watermark = (unsigned int)value; |
| 506 | break; |
| 507 | |
| 508 | case CODEC_SET_FILEBUF_CHUNKSIZE: |
| 509 | conf_filechunk = (unsigned int)value; |
| 510 | break; |
| 511 | |
| 512 | case CODEC_SET_FILEBUF_LIMIT: |
| 513 | conf_bufferlimit = (unsigned int)value; |
| 514 | break; |
| 515 | |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 516 | case CODEC_DSP_ENABLE: |
| 517 | if ((bool)value) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 518 | ci.pcmbuf_insert = codec_pcmbuf_insert_callback; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 519 | else |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 520 | ci.pcmbuf_insert = pcmbuf_insert_buffer; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 521 | break ; |
| 522 | |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 523 | default: |
Miika Pekkarinen | 65b840d | 2005-06-26 20:01:33 +0000 | [diff] [blame] | 524 | if (!dsp_configure(setting, value)) { |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 525 | logf("Illegal key: %d", setting); |
Miika Pekkarinen | 65b840d | 2005-06-26 20:01:33 +0000 | [diff] [blame] | 526 | } |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 530 | void audio_set_track_buffer_event(void (*handler)(struct mp3entry *id3, |
| 531 | bool last_track)) |
| 532 | { |
| 533 | track_buffer_callback = handler; |
| 534 | } |
| 535 | |
| 536 | void audio_set_track_unbuffer_event(void (*handler)(struct mp3entry *id3, |
Miika Pekkarinen | 9bde038 | 2005-07-03 18:36:24 +0000 | [diff] [blame] | 537 | bool last_track)) |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 538 | { |
| 539 | track_unbuffer_callback = handler; |
| 540 | } |
| 541 | |
Miika Pekkarinen | 7d6d122 | 2005-06-29 21:36:30 +0000 | [diff] [blame] | 542 | void audio_set_track_changed_event(void (*handler)(struct track_info *ti)) |
| 543 | { |
| 544 | track_changed_callback = handler; |
| 545 | } |
| 546 | |
Miika Pekkarinen | c3fed62 | 2005-06-15 18:59:04 +0000 | [diff] [blame] | 547 | void codec_track_changed(void) |
| 548 | { |
| 549 | track_changed = true; |
Miika Pekkarinen | 7d6d122 | 2005-06-29 21:36:30 +0000 | [diff] [blame] | 550 | queue_post(&audio_queue, AUDIO_TRACK_CHANGED, 0); |
Miika Pekkarinen | c3fed62 | 2005-06-15 18:59:04 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Miika Pekkarinen | 3e33a0f | 2005-07-05 15:51:59 +0000 | [diff] [blame] | 553 | /* Give codecs or file buffering the right amount of processing time |
| 554 | to prevent pcm audio buffer from going empty. */ |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 555 | void yield_codecs(void) |
| 556 | { |
Miika Pekkarinen | 61716dd | 2005-06-07 18:03:33 +0000 | [diff] [blame] | 557 | yield(); |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 558 | if (!pcm_is_playing()) |
| 559 | sleep(5); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 560 | while ((pcmbuf_is_crossfade_active() || pcmbuf_is_lowdata()) |
Miika Pekkarinen | 3e33a0f | 2005-07-05 15:51:59 +0000 | [diff] [blame] | 561 | && !ci.stop_codec && playing && queue_empty(&audio_queue) |
| 562 | && codecbufused > (128*1024)) |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 563 | yield(); |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Linus Nielsen Feltzing | df80798 | 2005-07-07 09:53:02 +0000 | [diff] [blame] | 566 | /* FIXME: This code should be made more generic and move to metadata.c */ |
| 567 | void strip_id3v1_tag(void) |
| 568 | { |
| 569 | int i; |
| 570 | static const unsigned char tag[] = "TAG"; |
| 571 | int tagptr; |
| 572 | bool found = true; |
| 573 | |
| 574 | if (codecbufused >= 128) |
| 575 | { |
| 576 | tagptr = buf_widx - 128; |
| 577 | if (tagptr < 0) |
| 578 | tagptr += codecbuflen; |
| 579 | |
| 580 | for(i = 0;i < 3;i++) |
| 581 | { |
| 582 | if(tagptr >= codecbuflen) |
| 583 | tagptr -= codecbuflen; |
| 584 | |
| 585 | if(codecbuf[tagptr] != tag[i]) |
| 586 | { |
| 587 | found = false; |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | tagptr++; |
| 592 | } |
| 593 | |
| 594 | if(found) |
| 595 | { |
| 596 | /* Skip id3v1 tag */ |
| 597 | logf("Skipping ID3v1 tag\n"); |
| 598 | buf_widx -= 128; |
| 599 | tracks[track_widx].available -= 128; |
| 600 | codecbufused -= 128; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 605 | void audio_fill_file_buffer(void) |
| 606 | { |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 607 | long i, size; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 608 | int rc; |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 609 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 610 | if (current_fd < 0) |
| 611 | return ; |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 612 | |
| 613 | /* Throw away buffered codec. */ |
| 614 | if (tracks[track_widx].start_pos != 0) |
| 615 | tracks[track_widx].codecsize = 0; |
| 616 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 617 | i = 0; |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 618 | size = MIN(tracks[track_widx].filerem, AUDIO_FILL_CYCLE); |
| 619 | while (i < size) { |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 620 | /* Give codecs some processing time. */ |
| 621 | yield_codecs(); |
| 622 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 623 | if (fill_bytesleft == 0) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 624 | break ; |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 625 | rc = MIN(conf_filechunk, codecbuflen - buf_widx); |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 626 | rc = MIN(rc, fill_bytesleft); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 627 | rc = read(current_fd, &codecbuf[buf_widx], rc); |
| 628 | if (rc <= 0) { |
| 629 | tracks[track_widx].filerem = 0; |
Linus Nielsen Feltzing | df80798 | 2005-07-07 09:53:02 +0000 | [diff] [blame] | 630 | strip_id3v1_tag(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 631 | break ; |
| 632 | } |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 633 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 634 | buf_widx += rc; |
Miika Pekkarinen | 2ed0b19 | 2005-06-10 10:58:45 +0000 | [diff] [blame] | 635 | if (buf_widx >= codecbuflen) |
| 636 | buf_widx -= codecbuflen; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 637 | i += rc; |
| 638 | tracks[track_widx].available += rc; |
Miika Pekkarinen | 3eb962d | 2005-07-07 07:15:05 +0000 | [diff] [blame] | 639 | tracks[track_widx].filerem -= rc; |
| 640 | tracks[track_widx].filepos += rc; |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 641 | codecbufused += rc; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 642 | fill_bytesleft -= rc; |
| 643 | } |
| 644 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 645 | /*logf("Filled:%d/%d", tracks[track_widx].available, |
| 646 | tracks[track_widx].filerem);*/ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | bool loadcodec(const char *trackname, bool start_play) |
| 650 | { |
| 651 | char msgbuf[80]; |
| 652 | off_t size; |
| 653 | int filetype; |
| 654 | int fd; |
| 655 | int i, rc; |
| 656 | const char *codec_path; |
| 657 | int copy_n; |
| 658 | int prev_track; |
| 659 | |
| 660 | filetype = probe_file_format(trackname); |
| 661 | switch (filetype) { |
| 662 | case AFMT_OGG_VORBIS: |
| 663 | logf("Codec: Vorbis"); |
| 664 | codec_path = CODEC_VORBIS; |
| 665 | break; |
Jens Arnold | e2cd581 | 2005-06-18 09:51:10 +0000 | [diff] [blame] | 666 | case AFMT_MPA_L1: |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 667 | case AFMT_MPA_L2: |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 668 | case AFMT_MPA_L3: |
Jens Arnold | e2cd581 | 2005-06-18 09:51:10 +0000 | [diff] [blame] | 669 | logf("Codec: MPA L1/L2/L3"); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 670 | codec_path = CODEC_MPA_L3; |
| 671 | break; |
| 672 | case AFMT_PCM_WAV: |
| 673 | logf("Codec: PCM WAV"); |
| 674 | codec_path = CODEC_WAV; |
| 675 | break; |
| 676 | case AFMT_FLAC: |
| 677 | logf("Codec: FLAC"); |
| 678 | codec_path = CODEC_FLAC; |
| 679 | break; |
Dave Chapman | 55ed7d7 | 2005-06-11 10:08:17 +0000 | [diff] [blame] | 680 | case AFMT_A52: |
| 681 | logf("Codec: A52"); |
| 682 | codec_path = CODEC_A52; |
| 683 | break; |
Dave Chapman | 2f2d7d4 | 2005-06-11 12:40:27 +0000 | [diff] [blame] | 684 | case AFMT_MPC: |
| 685 | logf("Codec: Musepack"); |
| 686 | codec_path = CODEC_MPC; |
| 687 | break; |
Dave Bryant | 57c6f6e | 2005-06-13 06:00:35 +0000 | [diff] [blame] | 688 | case AFMT_WAVPACK: |
| 689 | logf("Codec: WAVPACK"); |
| 690 | codec_path = CODEC_WAVPACK; |
| 691 | break; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 692 | default: |
| 693 | logf("Codec: Unsupported"); |
| 694 | snprintf(msgbuf, sizeof(msgbuf)-1, "No codec for: %s", trackname); |
| 695 | splash(HZ*2, true, msgbuf); |
| 696 | codec_path = NULL; |
| 697 | } |
| 698 | |
Dave Chapman | 961c9a3 | 2005-06-18 16:24:27 +0000 | [diff] [blame] | 699 | tracks[track_widx].id3.codectype = filetype; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 700 | tracks[track_widx].codecsize = 0; |
| 701 | if (codec_path == NULL) |
| 702 | return false; |
| 703 | |
| 704 | if (!start_play) { |
| 705 | prev_track = track_widx - 1; |
| 706 | if (prev_track < 0) |
| 707 | prev_track = MAX_TRACK-1; |
Dave Chapman | 961c9a3 | 2005-06-18 16:24:27 +0000 | [diff] [blame] | 708 | if (track_count > 0 && filetype == tracks[prev_track].id3.codectype) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 709 | logf("Reusing prev. codec"); |
| 710 | return true; |
| 711 | } |
| 712 | } else { |
| 713 | /* Load the codec directly from disk and save some memory. */ |
| 714 | cur_ti = &tracks[track_widx]; |
| 715 | ci.filesize = cur_ti->filesize; |
| 716 | ci.id3 = (struct mp3entry *)&cur_ti->id3; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 717 | ci.taginfo_ready = (bool *)&cur_ti->taginfo_ready; |
| 718 | ci.curpos = 0; |
| 719 | playing = true; |
| 720 | logf("Starting codec"); |
| 721 | queue_post(&codec_queue, CODEC_LOAD_DISK, (void *)codec_path); |
| 722 | return true; |
| 723 | } |
| 724 | |
| 725 | fd = open(codec_path, O_RDONLY); |
| 726 | if (fd < 0) { |
| 727 | logf("Codec doesn't exist!"); |
| 728 | snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", codec_path); |
| 729 | splash(HZ*2, true, msgbuf); |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | size = filesize(fd); |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 734 | if ((off_t)fill_bytesleft < size + conf_watermark) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 735 | logf("Not enough space"); |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 736 | fill_bytesleft = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 737 | close(fd); |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | i = 0; |
| 742 | while (i < size) { |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 743 | yield_codecs(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 744 | |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 745 | copy_n = MIN(conf_filechunk, codecbuflen - buf_widx); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 746 | rc = read(fd, &codecbuf[buf_widx], copy_n); |
| 747 | if (rc < 0) |
| 748 | return false; |
| 749 | buf_widx += rc; |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 750 | codecbufused += rc; |
| 751 | fill_bytesleft -= rc; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 752 | if (buf_widx >= codecbuflen) |
Miika Pekkarinen | 2ed0b19 | 2005-06-10 10:58:45 +0000 | [diff] [blame] | 753 | buf_widx -= codecbuflen; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 754 | i += rc; |
| 755 | } |
| 756 | close(fd); |
| 757 | logf("Done: %dB", i); |
| 758 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 759 | tracks[track_widx].codecsize = size; |
| 760 | |
| 761 | return true; |
| 762 | } |
| 763 | |
Miika Pekkarinen | f46c9f2 | 2005-07-03 18:03:20 +0000 | [diff] [blame] | 764 | bool read_next_metadata(void) |
| 765 | { |
| 766 | int fd; |
| 767 | char *trackname; |
| 768 | int next_track; |
| 769 | int status; |
| 770 | |
Miika Pekkarinen | 527ce40 | 2005-07-10 08:46:12 +0000 | [diff] [blame] | 771 | next_track = track_widx; |
| 772 | if (tracks[track_widx].taginfo_ready) |
| 773 | next_track++; |
| 774 | |
| 775 | if (next_track >= MAX_TRACK) |
| 776 | next_track -= MAX_TRACK; |
| 777 | |
| 778 | if (tracks[next_track].taginfo_ready) |
| 779 | return true; |
| 780 | |
Miika Pekkarinen | f46c9f2 | 2005-07-03 18:03:20 +0000 | [diff] [blame] | 781 | trackname = playlist_peek(last_peek_offset); |
| 782 | if (!trackname) |
| 783 | return false; |
| 784 | |
| 785 | fd = open(trackname, O_RDONLY); |
| 786 | if (fd < 0) |
| 787 | return false; |
Miika Pekkarinen | 527ce40 | 2005-07-10 08:46:12 +0000 | [diff] [blame] | 788 | |
Miika Pekkarinen | f46c9f2 | 2005-07-03 18:03:20 +0000 | [diff] [blame] | 789 | /* Start buffer refilling also because we need to spin-up the disk. */ |
| 790 | filling = true; |
| 791 | status = get_metadata(&tracks[next_track],fd,trackname,v1first); |
Miika Pekkarinen | 8ad60cc | 2005-07-04 06:06:30 +0000 | [diff] [blame] | 792 | track_changed = true; |
Miika Pekkarinen | f46c9f2 | 2005-07-03 18:03:20 +0000 | [diff] [blame] | 793 | close(fd); |
| 794 | |
| 795 | return status; |
| 796 | } |
| 797 | |
Miika Pekkarinen | a9ac3d1 | 2005-06-08 10:45:40 +0000 | [diff] [blame] | 798 | bool audio_load_track(int offset, bool start_play, int peek_offset) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 799 | { |
| 800 | char *trackname; |
| 801 | int fd; |
| 802 | off_t size; |
| 803 | int rc, i; |
| 804 | int copy_n; |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 805 | |
Miika Pekkarinen | b288dda | 2005-07-10 08:38:16 +0000 | [diff] [blame] | 806 | /* Stop buffer filling if there is no free track entries. |
| 807 | Don't fill up the last track entry (we wan't to store next track |
| 808 | metadata there). */ |
| 809 | if (track_count >= MAX_TRACK - 1) { |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 810 | fill_bytesleft = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 811 | return false; |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /* Don't start loading track if the current write position already |
| 815 | contains a BUFFERED track. The entry may contain the metadata |
| 816 | which is ok. */ |
| 817 | if (tracks[track_widx].filesize != 0) |
| 818 | return false; |
| 819 | |
| 820 | /* Get track name from current playlist read position. */ |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 821 | logf("Buffering track:%d/%d", track_widx, track_ridx); |
Miika Pekkarinen | a9ac3d1 | 2005-06-08 10:45:40 +0000 | [diff] [blame] | 822 | trackname = playlist_peek(peek_offset); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 823 | if (!trackname) { |
Miika Pekkarinen | c3fed62 | 2005-06-15 18:59:04 +0000 | [diff] [blame] | 824 | logf("End-of-playlist"); |
| 825 | conf_watermark = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 826 | return false; |
| 827 | } |
| 828 | |
| 829 | fd = open(trackname, O_RDONLY); |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 830 | if (fd < 0) { |
| 831 | logf("Open failed"); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 832 | return false; |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 833 | } |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 834 | |
| 835 | /* Initialize track entry. */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 836 | size = filesize(fd); |
| 837 | tracks[track_widx].filerem = size; |
| 838 | tracks[track_widx].filesize = size; |
| 839 | tracks[track_widx].filepos = 0; |
| 840 | tracks[track_widx].available = 0; |
| 841 | tracks[track_widx].taginfo_ready = false; |
| 842 | tracks[track_widx].playlist_offset = offset; |
| 843 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 844 | if (buf_widx >= codecbuflen) |
Miika Pekkarinen | 2ed0b19 | 2005-06-10 10:58:45 +0000 | [diff] [blame] | 845 | buf_widx -= codecbuflen; |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 846 | |
| 847 | /* Set default values */ |
| 848 | if (start_play) { |
| 849 | conf_bufferlimit = 0; |
| 850 | conf_watermark = AUDIO_DEFAULT_WATERMARK; |
| 851 | conf_filechunk = AUDIO_DEFAULT_FILECHUNK; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 852 | dsp_configure(DSP_RESET, 0); |
| 853 | ci.configure(CODEC_DSP_ENABLE, false); |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 854 | } |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 855 | |
| 856 | /* Load the codec. */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 857 | tracks[track_widx].codecbuf = &codecbuf[buf_widx]; |
| 858 | if (!loadcodec(trackname, start_play)) { |
| 859 | close(fd); |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 860 | /* Stop buffer filling if codec load failed. */ |
| 861 | fill_bytesleft = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 862 | return false; |
| 863 | } |
Miika Pekkarinen | d94cba6 | 2005-06-13 15:26:53 +0000 | [diff] [blame] | 864 | // tracks[track_widx].filebuf = &codecbuf[buf_widx]; |
| 865 | tracks[track_widx].start_pos = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 866 | |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 867 | /* Get track metadata if we don't already have it. */ |
Miika Pekkarinen | f46c9f2 | 2005-07-03 18:03:20 +0000 | [diff] [blame] | 868 | if (!tracks[track_widx].taginfo_ready) { |
| 869 | if (!get_metadata(&tracks[track_widx],fd,trackname,v1first)) { |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 870 | logf("Metadata error!"); |
| 871 | tracks[track_widx].filesize = 0; |
Miika Pekkarinen | f46c9f2 | 2005-07-03 18:03:20 +0000 | [diff] [blame] | 872 | close(fd); |
| 873 | return false; |
| 874 | } |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 875 | } |
Miika Pekkarinen | aa30f04 | 2005-07-05 07:25:55 +0000 | [diff] [blame] | 876 | tracks[track_widx].id3.elapsed = 0; |
Dave Chapman | 3ad485b | 2005-06-14 22:27:57 +0000 | [diff] [blame] | 877 | |
| 878 | /* Starting playback from an offset is only support in MPA at the moment */ |
| 879 | if (offset > 0) { |
Miika Pekkarinen | 3eb962d | 2005-07-07 07:15:05 +0000 | [diff] [blame] | 880 | switch (tracks[track_widx].id3.codectype) { |
| 881 | case AFMT_MPA_L2: |
| 882 | case AFMT_MPA_L3: |
| 883 | lseek(fd, offset, SEEK_SET); |
| 884 | tracks[track_widx].id3.offset = offset; |
| 885 | mp3_set_elapsed(&tracks[track_widx].id3); |
| 886 | tracks[track_widx].filepos = offset; |
| 887 | tracks[track_widx].filerem = tracks[track_widx].filesize - offset; |
| 888 | ci.curpos = offset; |
| 889 | tracks[track_widx].start_pos = offset; |
| 890 | break; |
| 891 | |
| 892 | case AFMT_WAVPACK: |
| 893 | lseek(fd, offset, SEEK_SET); |
| 894 | tracks[track_widx].id3.offset = offset; |
| 895 | tracks[track_widx].id3.elapsed = tracks[track_widx].id3.length / 2; |
| 896 | tracks[track_widx].filepos = offset; |
| 897 | tracks[track_widx].filerem = tracks[track_widx].filesize - offset; |
| 898 | ci.curpos = offset; |
| 899 | tracks[track_widx].start_pos = offset; |
| 900 | break; |
Linus Nielsen Feltzing | c4b7c67 | 2005-07-11 06:47:35 +0000 | [diff] [blame] | 901 | case AFMT_OGG_VORBIS: |
| 902 | tracks[track_widx].id3.offset = offset; |
| 903 | break; |
Miika Pekkarinen | 3eb962d | 2005-07-07 07:15:05 +0000 | [diff] [blame] | 904 | } |
| 905 | } |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 906 | |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 907 | if (start_play) { |
| 908 | track_count++; |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 909 | codec_track_changed(); |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 910 | } |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 911 | |
| 912 | /* Do some initial file buffering. */ |
Miika Pekkarinen | d94cba6 | 2005-06-13 15:26:53 +0000 | [diff] [blame] | 913 | i = tracks[track_widx].start_pos; |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 914 | size = MIN(size, AUDIO_FILL_CYCLE); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 915 | while (i < size) { |
| 916 | /* Give codecs some processing time to prevent glitches. */ |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 917 | yield_codecs(); |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 918 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 919 | if (fill_bytesleft == 0) |
| 920 | break ; |
| 921 | |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 922 | copy_n = MIN(conf_filechunk, codecbuflen - buf_widx); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 923 | copy_n = MIN(size - i, copy_n); |
| 924 | copy_n = MIN((int)fill_bytesleft, copy_n); |
| 925 | rc = read(fd, &codecbuf[buf_widx], copy_n); |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 926 | if (rc < copy_n) { |
Miika Pekkarinen | fe468b1 | 2005-06-09 19:31:35 +0000 | [diff] [blame] | 927 | logf("File error!"); |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 928 | tracks[track_widx].filesize = 0; |
| 929 | tracks[track_widx].filerem = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 930 | close(fd); |
| 931 | return false; |
| 932 | } |
| 933 | buf_widx += rc; |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 934 | if (buf_widx >= codecbuflen) |
Miika Pekkarinen | 2ed0b19 | 2005-06-10 10:58:45 +0000 | [diff] [blame] | 935 | buf_widx -= codecbuflen; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 936 | i += rc; |
| 937 | tracks[track_widx].available += rc; |
| 938 | tracks[track_widx].filerem -= rc; |
Miika Pekkarinen | 2ed0b19 | 2005-06-10 10:58:45 +0000 | [diff] [blame] | 939 | codecbufused += rc; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 940 | fill_bytesleft -= rc; |
| 941 | } |
| 942 | |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 943 | if (!start_play) |
| 944 | track_count++; |
| 945 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 946 | tracks[track_widx].filepos = i; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 947 | |
Miika Pekkarinen | ef72f99 | 2005-06-14 14:36:46 +0000 | [diff] [blame] | 948 | if (current_fd >= 0) { |
| 949 | close(current_fd); |
| 950 | current_fd = -1; |
| 951 | } |
| 952 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 953 | /* Leave the file handle open for faster buffer refill. */ |
| 954 | if (tracks[track_widx].filerem != 0) { |
| 955 | current_fd = fd; |
| 956 | logf("Partially buf:%d", tracks[track_widx].available); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 957 | } else { |
| 958 | logf("Completely buf."); |
| 959 | close(fd); |
Linus Nielsen Feltzing | df80798 | 2005-07-07 09:53:02 +0000 | [diff] [blame] | 960 | |
| 961 | strip_id3v1_tag(); |
| 962 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 963 | if (++track_widx >= MAX_TRACK) { |
| 964 | track_widx = 0; |
| 965 | } |
| 966 | tracks[track_widx].filerem = 0; |
| 967 | } |
| 968 | |
| 969 | return true; |
| 970 | } |
| 971 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 972 | void audio_play_start(int offset) |
| 973 | { |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 974 | if (current_fd >= 0) { |
| 975 | close(current_fd); |
| 976 | current_fd = -1; |
| 977 | } |
Miika Pekkarinen | 0d63cbb | 2005-07-10 20:37:36 +0000 | [diff] [blame] | 978 | |
Miika Pekkarinen | fe468b1 | 2005-06-09 19:31:35 +0000 | [diff] [blame] | 979 | memset(&tracks, 0, sizeof(struct track_info) * MAX_TRACK); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 980 | sound_set(SOUND_VOLUME, global_settings.volume); |
| 981 | track_count = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 982 | track_widx = 0; |
| 983 | track_ridx = 0; |
| 984 | buf_ridx = 0; |
| 985 | buf_widx = 0; |
| 986 | codecbufused = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 987 | pcmbuf_set_boost_mode(true); |
Miika Pekkarinen | cf18f96 | 2005-06-20 06:49:21 +0000 | [diff] [blame] | 988 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 989 | fill_bytesleft = codecbuflen; |
| 990 | filling = true; |
| 991 | last_peek_offset = 0; |
| 992 | if (audio_load_track(offset, true, 0)) { |
| 993 | last_peek_offset++; |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 994 | if (track_buffer_callback) { |
| 995 | cur_ti->event_sent = true; |
| 996 | track_buffer_callback(&cur_ti->id3, true); |
| 997 | } |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 998 | } else { |
| 999 | logf("Failure"); |
| 1000 | } |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1001 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1002 | pcmbuf_set_boost_mode(false); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1005 | void audio_clear_track_entries(void) |
| 1006 | { |
Miika Pekkarinen | 9bde038 | 2005-07-03 18:36:24 +0000 | [diff] [blame] | 1007 | int cur_idx, event_count; |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1008 | int i; |
| 1009 | |
| 1010 | cur_idx = track_widx; |
Miika Pekkarinen | 9bde038 | 2005-07-03 18:36:24 +0000 | [diff] [blame] | 1011 | event_count = 0; |
| 1012 | for (i = 0; i < MAX_TRACK - track_count; i++) { |
| 1013 | if (++cur_idx >= MAX_TRACK) |
| 1014 | cur_idx = 0; |
| 1015 | |
| 1016 | if (tracks[cur_idx].event_sent) |
| 1017 | event_count++; |
| 1018 | |
| 1019 | if (!track_unbuffer_callback) |
| 1020 | memset(&tracks[cur_idx], 0, sizeof(struct track_info)); |
| 1021 | } |
| 1022 | |
| 1023 | if (!track_unbuffer_callback) |
| 1024 | return ; |
| 1025 | |
| 1026 | cur_idx = track_widx; |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1027 | for (i = 0; i < MAX_TRACK - track_count; i++) { |
| 1028 | if (++cur_idx >= MAX_TRACK) |
| 1029 | cur_idx = 0; |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1030 | |
Miika Pekkarinen | 9bde038 | 2005-07-03 18:36:24 +0000 | [diff] [blame] | 1031 | /* Send an event to notify that track has finished. */ |
| 1032 | if (tracks[cur_idx].event_sent) { |
| 1033 | tracks[cur_idx].event_sent = true; |
| 1034 | event_count--; |
| 1035 | track_unbuffer_callback(&tracks[cur_idx].id3, event_count == 0); |
| 1036 | } |
| 1037 | |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1038 | memset(&tracks[cur_idx], 0, sizeof(struct track_info)); |
| 1039 | } |
| 1040 | } |
| 1041 | |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1042 | /* Send callback events to notify about new tracks. */ |
| 1043 | static void generate_postbuffer_events(void) |
| 1044 | { |
| 1045 | int i; |
| 1046 | int cur_ridx, event_count; |
| 1047 | |
| 1048 | if (!track_buffer_callback) |
| 1049 | return ; |
| 1050 | |
| 1051 | /* At first determine how many unsent events we have. */ |
| 1052 | cur_ridx = track_ridx; |
| 1053 | event_count = 0; |
| 1054 | for (i = 0; i < track_count; i++) { |
| 1055 | if (!tracks[cur_ridx].event_sent) |
| 1056 | event_count++; |
| 1057 | if (++cur_ridx >= MAX_TRACK) |
| 1058 | cur_ridx -= MAX_TRACK; |
| 1059 | } |
| 1060 | |
| 1061 | /* Now sent these events. */ |
| 1062 | cur_ridx = track_ridx; |
| 1063 | for (i = 0; i < track_count; i++) { |
| 1064 | if (!tracks[cur_ridx].event_sent) { |
| 1065 | tracks[cur_ridx].event_sent = true; |
| 1066 | event_count--; |
| 1067 | track_buffer_callback(&tracks[cur_ridx].id3, event_count == 0); |
| 1068 | } |
| 1069 | if (++cur_ridx >= MAX_TRACK) |
| 1070 | cur_ridx -= MAX_TRACK; |
| 1071 | } |
| 1072 | } |
| 1073 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1074 | void initialize_buffer_fill(void) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1075 | { |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1076 | int cur_idx, i; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1077 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1078 | |
| 1079 | fill_bytesleft = codecbuflen - codecbufused; |
Miika Pekkarinen | c520d69 | 2005-07-01 18:38:10 +0000 | [diff] [blame] | 1080 | cur_ti->start_pos = ci.curpos; |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 1081 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1082 | pcmbuf_set_boost_mode(true); |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1083 | |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 1084 | if (filling) |
| 1085 | return ; |
| 1086 | |
| 1087 | filling = true; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1088 | |
| 1089 | /* Calculate real track count after throwing away old tracks. */ |
| 1090 | cur_idx = track_ridx; |
| 1091 | for (i = 0; i < track_count; i++) { |
| 1092 | if (cur_idx == track_widx) |
| 1093 | break ; |
| 1094 | |
| 1095 | if (++cur_idx >= MAX_TRACK) |
| 1096 | cur_idx = 0; |
| 1097 | } |
| 1098 | |
| 1099 | track_count = i; |
Miika Pekkarinen | 8d5822d | 2005-06-14 18:59:34 +0000 | [diff] [blame] | 1100 | if (tracks[track_widx].filesize != 0) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1101 | track_count++; |
| 1102 | |
| 1103 | /* Mark all other entries null. */ |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1104 | audio_clear_track_entries(); |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | void audio_check_buffer(void) |
| 1108 | { |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1109 | /* Start buffer filling as necessary. */ |
| 1110 | if ((codecbufused > conf_watermark || !queue_empty(&audio_queue) |
| 1111 | || !playing || ci.stop_codec || ci.reload_codec) && !filling) |
| 1112 | return ; |
| 1113 | |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 1114 | initialize_buffer_fill(); |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1115 | |
| 1116 | /* Limit buffering size at first run. */ |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 1117 | if (conf_bufferlimit && fill_bytesleft > conf_bufferlimit |
| 1118 | - codecbufused) { |
| 1119 | fill_bytesleft = MAX(0, conf_bufferlimit - codecbufused); |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1120 | } |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1121 | |
| 1122 | /* Try to load remainings of the file. */ |
| 1123 | if (tracks[track_widx].filerem > 0) |
| 1124 | audio_fill_file_buffer(); |
| 1125 | |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1126 | /* Increase track write index as necessary. */ |
| 1127 | if (tracks[track_widx].filerem == 0 && tracks[track_widx].filesize != 0) { |
| 1128 | if (++track_widx == MAX_TRACK) |
| 1129 | track_widx = 0; |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1132 | /* Load new files to fill the entire buffer. */ |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1133 | if (audio_load_track(0, false, last_peek_offset)) { |
| 1134 | last_peek_offset++; |
| 1135 | } else if (tracks[track_widx].filerem == 0 || fill_bytesleft == 0) { |
Miika Pekkarinen | 527ce40 | 2005-07-10 08:46:12 +0000 | [diff] [blame] | 1136 | /* Read next unbuffered track's metadata as necessary. */ |
| 1137 | read_next_metadata(); |
Miika Pekkarinen | f46c9f2 | 2005-07-03 18:03:20 +0000 | [diff] [blame] | 1138 | |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1139 | generate_postbuffer_events(); |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1140 | filling = false; |
Miika Pekkarinen | 84d6f9e | 2005-06-29 20:50:58 +0000 | [diff] [blame] | 1141 | conf_bufferlimit = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1142 | pcmbuf_set_boost_mode(false); |
| 1143 | |
| 1144 | #ifndef SIMULATOR |
Miika Pekkarinen | b288dda | 2005-07-10 08:38:16 +0000 | [diff] [blame] | 1145 | if (playing) |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1146 | ata_sleep(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1147 | #endif |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1148 | } |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | void audio_update_trackinfo(void) |
| 1152 | { |
| 1153 | if (new_track >= 0) { |
| 1154 | buf_ridx += cur_ti->available; |
| 1155 | codecbufused -= cur_ti->available; |
| 1156 | |
| 1157 | cur_ti = &tracks[track_ridx]; |
| 1158 | buf_ridx += cur_ti->codecsize; |
Miika Pekkarinen | 2ed0b19 | 2005-06-10 10:58:45 +0000 | [diff] [blame] | 1159 | codecbufused -= cur_ti->codecsize; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1160 | if (buf_ridx >= codecbuflen) |
| 1161 | buf_ridx -= codecbuflen; |
Miika Pekkarinen | 92ea04a | 2005-06-15 12:53:50 +0000 | [diff] [blame] | 1162 | |
Miika Pekkarinen | b4bc106 | 2005-06-09 07:19:16 +0000 | [diff] [blame] | 1163 | if (!filling) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1164 | pcmbuf_set_boost_mode(false); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1165 | } else { |
Miika Pekkarinen | 1c5b392 | 2005-06-11 13:47:01 +0000 | [diff] [blame] | 1166 | buf_ridx -= ci.curpos + cur_ti->codecsize; |
| 1167 | codecbufused += ci.curpos + cur_ti->codecsize; |
Miika Pekkarinen | f472410 | 2005-06-07 21:51:29 +0000 | [diff] [blame] | 1168 | cur_ti->available = cur_ti->filesize; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1169 | |
| 1170 | cur_ti = &tracks[track_ridx]; |
Miika Pekkarinen | 1c5b392 | 2005-06-11 13:47:01 +0000 | [diff] [blame] | 1171 | buf_ridx -= cur_ti->filesize; |
| 1172 | codecbufused += cur_ti->filesize; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1173 | cur_ti->available = cur_ti->filesize; |
| 1174 | if (buf_ridx < 0) |
| 1175 | buf_ridx = codecbuflen + buf_ridx; |
| 1176 | } |
Miika Pekkarinen | aa30f04 | 2005-07-05 07:25:55 +0000 | [diff] [blame] | 1177 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1178 | ci.filesize = cur_ti->filesize; |
Miika Pekkarinen | a380d90 | 2005-06-11 18:05:16 +0000 | [diff] [blame] | 1179 | cur_ti->id3.elapsed = 0; |
| 1180 | cur_ti->id3.offset = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1181 | ci.id3 = (struct mp3entry *)&cur_ti->id3; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1182 | ci.curpos = 0; |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 1183 | cur_ti->start_pos = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1184 | ci.taginfo_ready = (bool *)&cur_ti->taginfo_ready; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1185 | if (!pcmbuf_crossfade_init()) |
| 1186 | pcmbuf_add_event(codec_track_changed); |
Miika Pekkarinen | b469e73 | 2005-07-02 17:45:12 +0000 | [diff] [blame] | 1187 | else |
| 1188 | codec_track_changed(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Miika Pekkarinen | 9c70b1b | 2005-07-02 17:03:19 +0000 | [diff] [blame] | 1191 | static void audio_stop_playback(void) |
| 1192 | { |
| 1193 | paused = false; |
| 1194 | playing = false; |
| 1195 | ci.stop_codec = true; |
| 1196 | if (current_fd >= 0) { |
| 1197 | close(current_fd); |
| 1198 | current_fd = -1; |
| 1199 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1200 | pcmbuf_play_stop(); |
Miika Pekkarinen | 9c70b1b | 2005-07-02 17:03:19 +0000 | [diff] [blame] | 1201 | pcm_play_pause(true); |
| 1202 | track_count = 0; |
Miika Pekkarinen | 9c70b1b | 2005-07-02 17:03:19 +0000 | [diff] [blame] | 1203 | audio_clear_track_entries(); |
| 1204 | filling = false; |
| 1205 | } |
| 1206 | |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 1207 | /* Request the next track with new codec. */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1208 | void audio_change_track(void) |
| 1209 | { |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1210 | logf("change track"); |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 1211 | |
| 1212 | /* Wait for new track data. */ |
Miika Pekkarinen | 22960c3 | 2005-07-06 15:44:59 +0000 | [diff] [blame] | 1213 | while (track_count <= 1 && filling) |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 1214 | yield(); |
| 1215 | |
| 1216 | /* If we are not filling, then it must be end-of-playlist. */ |
Miika Pekkarinen | 22960c3 | 2005-07-06 15:44:59 +0000 | [diff] [blame] | 1217 | if (track_count <= 1) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1218 | logf("No more tracks"); |
Miika Pekkarinen | c3fed62 | 2005-06-15 18:59:04 +0000 | [diff] [blame] | 1219 | while (pcm_is_playing()) |
| 1220 | yield(); |
Miika Pekkarinen | 9c70b1b | 2005-07-02 17:03:19 +0000 | [diff] [blame] | 1221 | audio_stop_playback(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1222 | return ; |
| 1223 | } |
| 1224 | |
| 1225 | if (++track_ridx >= MAX_TRACK) |
| 1226 | track_ridx = 0; |
| 1227 | |
| 1228 | audio_update_trackinfo(); |
| 1229 | queue_post(&codec_queue, CODEC_LOAD, 0); |
| 1230 | } |
| 1231 | |
Miika Pekkarinen | 645a2e1 | 2005-07-10 16:33:03 +0000 | [diff] [blame] | 1232 | static int get_codec_base_type(int type) |
| 1233 | { |
| 1234 | switch (type) { |
| 1235 | case AFMT_MPA_L1: |
| 1236 | case AFMT_MPA_L2: |
| 1237 | case AFMT_MPA_L3: |
| 1238 | return AFMT_MPA_L3; |
| 1239 | } |
| 1240 | |
| 1241 | return type; |
| 1242 | } |
| 1243 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1244 | bool codec_request_next_track_callback(void) |
| 1245 | { |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 1246 | if (ci.stop_codec || !playing) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1247 | return false; |
| 1248 | |
| 1249 | logf("Request new track"); |
| 1250 | |
| 1251 | /* Advance to next track. */ |
| 1252 | if (ci.reload_codec && new_track > 0) { |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 1253 | if (!playlist_check(new_track)) |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1254 | return false; |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 1255 | last_peek_offset--; |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 1256 | playlist_next(new_track); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1257 | if (++track_ridx == MAX_TRACK) |
| 1258 | track_ridx = 0; |
Miika Pekkarinen | d6e7942 | 2005-07-05 15:19:22 +0000 | [diff] [blame] | 1259 | |
| 1260 | /* Wait for new track data (codectype 0 is invalid). When a correct |
| 1261 | codectype is set, we can assume that the filesize is correct. */ |
| 1262 | while (tracks[track_ridx].id3.codectype == 0 && filling |
| 1263 | && !ci.stop_codec) |
| 1264 | yield(); |
| 1265 | |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 1266 | if (tracks[track_ridx].filesize == 0) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1267 | logf("Loading from disk..."); |
| 1268 | new_track = 0; |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 1269 | queue_post(&audio_queue, AUDIO_PLAY, 0); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1270 | return false; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | /* Advance to previous track. */ |
| 1275 | else if (ci.reload_codec && new_track < 0) { |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 1276 | if (!playlist_check(new_track)) |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1277 | return false; |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 1278 | last_peek_offset++; |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 1279 | playlist_next(new_track); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1280 | if (--track_ridx < 0) |
| 1281 | track_ridx = MAX_TRACK-1; |
| 1282 | if (tracks[track_ridx].filesize == 0 || |
| 1283 | codecbufused+ci.curpos+tracks[track_ridx].filesize |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 1284 | /*+ (off_t)tracks[track_ridx].codecsize*/ > codecbuflen) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1285 | logf("Loading from disk..."); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1286 | new_track = 0; |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 1287 | queue_post(&audio_queue, AUDIO_PLAY, 0); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1288 | return false; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | /* Codec requested track change (next track). */ |
| 1293 | else { |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1294 | if (!playlist_check(1)) |
| 1295 | return false; |
Miika Pekkarinen | 2d79df5 | 2005-07-05 13:34:52 +0000 | [diff] [blame] | 1296 | last_peek_offset--; |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 1297 | playlist_next(1); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1298 | if (++track_ridx >= MAX_TRACK) |
| 1299 | track_ridx = 0; |
| 1300 | |
Miika Pekkarinen | d6e7942 | 2005-07-05 15:19:22 +0000 | [diff] [blame] | 1301 | /* Wait for new track data (codectype 0 is invalid). When a correct |
| 1302 | codectype is set, we can assume that the filesize is correct. */ |
| 1303 | while (tracks[track_ridx].id3.codectype == 0 && filling |
| 1304 | && !ci.stop_codec) |
| 1305 | yield(); |
| 1306 | |
| 1307 | if (tracks[track_ridx].filesize == 0) { |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1308 | logf("No more tracks [2]"); |
| 1309 | ci.stop_codec = true; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1310 | new_track = 0; |
Miika Pekkarinen | 0d63cbb | 2005-07-10 20:37:36 +0000 | [diff] [blame] | 1311 | queue_post(&audio_queue, AUDIO_PLAY, 0); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1312 | return false; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | ci.reload_codec = false; |
| 1317 | |
Miika Pekkarinen | 645a2e1 | 2005-07-10 16:33:03 +0000 | [diff] [blame] | 1318 | /* Check if the next codec is the same file. */ |
| 1319 | if (get_codec_base_type(cur_ti->id3.codectype) != |
| 1320 | get_codec_base_type(tracks[track_ridx].id3.codectype)) { |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1321 | logf("New codec:%d/%d", cur_ti->id3.codectype, |
| 1322 | tracks[track_ridx].id3.codectype); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1323 | if (--track_ridx < 0) |
| 1324 | track_ridx = MAX_TRACK-1; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1325 | new_track = 0; |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | logf("On-the-fly change"); |
| 1330 | audio_update_trackinfo(); |
| 1331 | new_track = 0; |
| 1332 | |
| 1333 | return true; |
| 1334 | } |
| 1335 | |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1336 | /* Invalidates all but currently playing track. */ |
| 1337 | void audio_invalidate_tracks(void) |
| 1338 | { |
| 1339 | if (track_count == 0) { |
| 1340 | queue_post(&audio_queue, AUDIO_PLAY, 0); |
| 1341 | return ; |
| 1342 | } |
| 1343 | |
| 1344 | track_count = 1; |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 1345 | last_peek_offset = 1; |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1346 | track_widx = track_ridx; |
| 1347 | audio_clear_track_entries(); |
| 1348 | codecbufused = cur_ti->available; |
| 1349 | buf_widx = buf_ridx + cur_ti->available; |
| 1350 | if (buf_widx >= codecbuflen) |
| 1351 | buf_widx -= codecbuflen; |
Miika Pekkarinen | f46c9f2 | 2005-07-03 18:03:20 +0000 | [diff] [blame] | 1352 | read_next_metadata(); |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1355 | static void initiate_track_change(int peek_index) |
| 1356 | { |
| 1357 | if (!playlist_check(peek_index)) |
| 1358 | return ; |
| 1359 | |
| 1360 | new_track = peek_index; |
| 1361 | ci.reload_codec = true; |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1362 | |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1363 | /* Detect if disk is spinning.. */ |
| 1364 | if (filling) { |
| 1365 | ci.stop_codec = true; |
| 1366 | playlist_next(peek_index); |
| 1367 | queue_post(&audio_queue, AUDIO_PLAY, 0); |
| 1368 | } |
| 1369 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1370 | else if (!pcmbuf_crossfade_init()) |
| 1371 | pcmbuf_flush_audio(); |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1372 | |
| 1373 | codec_track_changed(); |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1376 | void audio_thread(void) |
| 1377 | { |
| 1378 | struct event ev; |
| 1379 | |
| 1380 | while (1) { |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1381 | yield_codecs(); |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 1382 | |
| 1383 | mutex_lock(&mutex_bufferfill); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1384 | audio_check_buffer(); |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 1385 | mutex_unlock(&mutex_bufferfill); |
| 1386 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1387 | queue_wait_w_tmo(&audio_queue, &ev, 0); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1388 | switch (ev.id) { |
| 1389 | case AUDIO_PLAY: |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1390 | logf("starting..."); |
Miika Pekkarinen | 3b90707 | 2005-06-30 16:28:40 +0000 | [diff] [blame] | 1391 | playing = true; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1392 | ci.stop_codec = true; |
| 1393 | ci.reload_codec = false; |
| 1394 | ci.seek_time = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1395 | if (!pcmbuf_crossfade_init() && !pcmbuf_is_crossfade_active()) |
| 1396 | pcmbuf_flush_audio(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1397 | audio_play_start((int)ev.data); |
Hardeep Sidhu | 839dbca | 2005-07-04 22:50:57 +0000 | [diff] [blame] | 1398 | playlist_update_resume_info(audio_current_track()); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1399 | break ; |
| 1400 | |
| 1401 | case AUDIO_STOP: |
Hardeep Sidhu | 839dbca | 2005-07-04 22:50:57 +0000 | [diff] [blame] | 1402 | if (playing) |
| 1403 | playlist_update_resume_info(audio_current_track()); |
Miika Pekkarinen | 9c70b1b | 2005-07-02 17:03:19 +0000 | [diff] [blame] | 1404 | audio_stop_playback(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1405 | break ; |
| 1406 | |
| 1407 | case AUDIO_PAUSE: |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1408 | logf("audio_pause"); |
| 1409 | pcm_play_pause(false); |
| 1410 | paused = true; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1411 | break ; |
| 1412 | |
| 1413 | case AUDIO_RESUME: |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1414 | logf("audio_resume"); |
| 1415 | pcm_play_pause(true); |
| 1416 | paused = false; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1417 | break ; |
| 1418 | |
| 1419 | case AUDIO_NEXT: |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1420 | logf("audio_next"); |
| 1421 | initiate_track_change(1); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1422 | break ; |
| 1423 | |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1424 | case AUDIO_PREV: |
| 1425 | logf("audio_prev"); |
| 1426 | initiate_track_change(-1); |
| 1427 | break; |
| 1428 | |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1429 | case AUDIO_FLUSH: |
| 1430 | audio_invalidate_tracks(); |
| 1431 | break ; |
| 1432 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1433 | case AUDIO_TRACK_CHANGED: |
Miika Pekkarinen | 7d6d122 | 2005-06-29 21:36:30 +0000 | [diff] [blame] | 1434 | if (track_changed_callback) |
| 1435 | track_changed_callback(cur_ti); |
Hardeep Sidhu | 839dbca | 2005-07-04 22:50:57 +0000 | [diff] [blame] | 1436 | playlist_update_resume_info(audio_current_track()); |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1437 | break ; |
| 1438 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1439 | case AUDIO_CODEC_DONE: |
| 1440 | //if (playing) |
| 1441 | // audio_change_track(); |
| 1442 | break ; |
| 1443 | |
| 1444 | #ifndef SIMULATOR |
| 1445 | case SYS_USB_CONNECTED: |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1446 | logf("USB Connection"); |
Miika Pekkarinen | 9c70b1b | 2005-07-02 17:03:19 +0000 | [diff] [blame] | 1447 | audio_stop_playback(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1448 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 1449 | usb_wait_for_disconnect(&audio_queue); |
| 1450 | break ; |
| 1451 | #endif |
Hardeep Sidhu | 839dbca | 2005-07-04 22:50:57 +0000 | [diff] [blame] | 1452 | case SYS_TIMEOUT: |
| 1453 | if (playing) |
| 1454 | playlist_update_resume_info(audio_current_track()); |
| 1455 | break; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | void codec_thread(void) |
| 1461 | { |
| 1462 | struct event ev; |
Miika Pekkarinen | 85f4973 | 2005-06-27 19:29:49 +0000 | [diff] [blame] | 1463 | long codecsize; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1464 | int status; |
| 1465 | int wrap; |
| 1466 | |
| 1467 | while (1) { |
| 1468 | status = 0; |
| 1469 | queue_wait(&codec_queue, &ev); |
| 1470 | switch (ev.id) { |
| 1471 | case CODEC_LOAD_DISK: |
| 1472 | ci.stop_codec = false; |
Miika Pekkarinen | 84d6f9e | 2005-06-29 20:50:58 +0000 | [diff] [blame] | 1473 | codec_loaded = true; |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 1474 | status = codec_load_file((char *)ev.data); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1475 | break ; |
| 1476 | |
| 1477 | case CODEC_LOAD: |
| 1478 | logf("Codec start"); |
| 1479 | codecsize = cur_ti->codecsize; |
| 1480 | if (codecsize == 0) { |
| 1481 | logf("Codec slot is empty!"); |
Miika Pekkarinen | 3e33f85 | 2005-07-10 06:58:02 +0000 | [diff] [blame] | 1482 | /* Wait for the pcm buffer to go empty */ |
| 1483 | while (pcm_is_playing()) |
| 1484 | yield(); |
Miika Pekkarinen | 9c70b1b | 2005-07-02 17:03:19 +0000 | [diff] [blame] | 1485 | audio_stop_playback(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1486 | break ; |
| 1487 | } |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1488 | |
| 1489 | ci.stop_codec = false; |
| 1490 | wrap = (int)&codecbuf[codecbuflen] - (int)cur_ti->codecbuf; |
Miika Pekkarinen | 84d6f9e | 2005-06-29 20:50:58 +0000 | [diff] [blame] | 1491 | codec_loaded = true; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1492 | status = codec_load_ram(cur_ti->codecbuf, codecsize, |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 1493 | &codecbuf[0], wrap); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1494 | break ; |
| 1495 | |
| 1496 | #ifndef SIMULATOR |
| 1497 | case SYS_USB_CONNECTED: |
| 1498 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 1499 | usb_wait_for_disconnect(&codec_queue); |
| 1500 | break ; |
| 1501 | #endif |
| 1502 | } |
Miika Pekkarinen | 84d6f9e | 2005-06-29 20:50:58 +0000 | [diff] [blame] | 1503 | |
| 1504 | codec_loaded = false; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1505 | |
| 1506 | switch (ev.id) { |
| 1507 | case CODEC_LOAD_DISK: |
| 1508 | case CODEC_LOAD: |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 1509 | if (status != CODEC_OK) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1510 | logf("Codec failure"); |
Miika Pekkarinen | e0037ad | 2005-06-12 18:16:35 +0000 | [diff] [blame] | 1511 | splash(HZ*2, true, "Codec failure"); |
Miika Pekkarinen | 9c70b1b | 2005-07-02 17:03:19 +0000 | [diff] [blame] | 1512 | audio_stop_playback(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1513 | } else { |
| 1514 | logf("Codec finished"); |
| 1515 | } |
| 1516 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1517 | if (playing && !ci.stop_codec && !ci.reload_codec) { |
| 1518 | audio_change_track(); |
Miika Pekkarinen | fe468b1 | 2005-06-09 19:31:35 +0000 | [diff] [blame] | 1519 | continue ; |
| 1520 | } else if (ci.stop_codec) { |
| 1521 | //playing = false; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1522 | } |
Miika Pekkarinen | fe468b1 | 2005-06-09 19:31:35 +0000 | [diff] [blame] | 1523 | //queue_post(&audio_queue, AUDIO_CODEC_DONE, (void *)status); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | struct mp3entry* audio_current_track(void) |
| 1529 | { |
Miika Pekkarinen | aa30f04 | 2005-07-05 07:25:55 +0000 | [diff] [blame] | 1530 | // logf("audio_current_track"); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1531 | |
| 1532 | if (track_count > 0 && cur_ti->taginfo_ready) |
| 1533 | return (struct mp3entry *)&cur_ti->id3; |
| 1534 | else |
| 1535 | return NULL; |
| 1536 | } |
| 1537 | |
| 1538 | struct mp3entry* audio_next_track(void) |
| 1539 | { |
| 1540 | int next_idx = track_ridx + 1; |
| 1541 | |
| 1542 | if (track_count == 0) |
| 1543 | return NULL; |
| 1544 | |
| 1545 | if (next_idx >= MAX_TRACK) |
| 1546 | next_idx = 0; |
| 1547 | |
| 1548 | if (!tracks[next_idx].taginfo_ready) |
| 1549 | return NULL; |
| 1550 | |
| 1551 | //logf("audio_next_track"); |
| 1552 | |
| 1553 | return &tracks[next_idx].id3; |
| 1554 | } |
| 1555 | |
| 1556 | bool audio_has_changed_track(void) |
| 1557 | { |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 1558 | if (track_changed && track_count > 0 && playing) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1559 | if (!cur_ti->taginfo_ready) |
| 1560 | return false; |
| 1561 | track_changed = false; |
| 1562 | return true; |
| 1563 | } |
| 1564 | |
| 1565 | return false; |
| 1566 | } |
| 1567 | |
| 1568 | void audio_play(int offset) |
| 1569 | { |
| 1570 | logf("audio_play"); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1571 | ci.stop_codec = true; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1572 | if (!pcmbuf_crossfade_init()) |
| 1573 | pcmbuf_flush_audio(); |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1574 | codec_track_changed(); |
Miika Pekkarinen | d50ed12 | 2005-07-02 17:55:51 +0000 | [diff] [blame] | 1575 | |
Miika Pekkarinen | b4bc106 | 2005-06-09 07:19:16 +0000 | [diff] [blame] | 1576 | pcm_play_pause(true); |
Miika Pekkarinen | 9e200e3 | 2005-06-09 20:01:09 +0000 | [diff] [blame] | 1577 | paused = false; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1578 | queue_post(&audio_queue, AUDIO_PLAY, (void *)offset); |
| 1579 | } |
| 1580 | |
| 1581 | void audio_stop(void) |
| 1582 | { |
| 1583 | logf("audio_stop"); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1584 | queue_post(&audio_queue, AUDIO_STOP, 0); |
Miika Pekkarinen | 84d6f9e | 2005-06-29 20:50:58 +0000 | [diff] [blame] | 1585 | while (playing || codec_loaded) |
| 1586 | yield(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | void audio_pause(void) |
| 1590 | { |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1591 | queue_post(&audio_queue, AUDIO_PAUSE, 0); |
Miika Pekkarinen | 1ed292c | 2005-07-10 17:31:12 +0000 | [diff] [blame] | 1592 | while (!paused && playing) |
| 1593 | yield(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | void audio_resume(void) |
| 1597 | { |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1598 | queue_post(&audio_queue, AUDIO_RESUME, 0); |
Miika Pekkarinen | 1ed292c | 2005-07-10 17:31:12 +0000 | [diff] [blame] | 1599 | while (paused && playing) |
| 1600 | yield(); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1603 | void audio_next(void) |
| 1604 | { |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1605 | queue_post(&audio_queue, AUDIO_NEXT, 0); |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1608 | void audio_prev(void) |
| 1609 | { |
Linus Nielsen Feltzing | ffd207f | 2005-07-06 19:40:17 +0000 | [diff] [blame] | 1610 | queue_post(&audio_queue, AUDIO_PREV, 0); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | void audio_ff_rewind(int newpos) |
| 1614 | { |
| 1615 | logf("rewind: %d", newpos); |
Miika Pekkarinen | 2724d0b | 2005-07-01 21:00:02 +0000 | [diff] [blame] | 1616 | if (playing) { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1617 | ci.seek_time = newpos+1; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1618 | pcmbuf_play_stop(); |
Miika Pekkarinen | 2f6781d | 2005-07-11 20:43:02 +0000 | [diff] [blame] | 1619 | paused = false; |
Miika Pekkarinen | 2724d0b | 2005-07-01 21:00:02 +0000 | [diff] [blame] | 1620 | } |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | void audio_flush_and_reload_tracks(void) |
| 1624 | { |
| 1625 | logf("flush & reload"); |
Miika Pekkarinen | 58af47c | 2005-06-13 22:09:12 +0000 | [diff] [blame] | 1626 | queue_post(&audio_queue, AUDIO_FLUSH, 0); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | void audio_error_clear(void) |
| 1630 | { |
| 1631 | } |
| 1632 | |
| 1633 | int audio_status(void) |
| 1634 | { |
| 1635 | int ret = 0; |
| 1636 | |
| 1637 | if (playing) |
| 1638 | ret |= AUDIO_STATUS_PLAY; |
| 1639 | |
| 1640 | if (paused) |
| 1641 | ret |= AUDIO_STATUS_PAUSE; |
| 1642 | |
| 1643 | return ret; |
| 1644 | } |
| 1645 | |
Linus Nielsen Feltzing | 4aaa321 | 2005-06-06 00:35:21 +0000 | [diff] [blame] | 1646 | int audio_get_file_pos(void) |
| 1647 | { |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 1651 | |
| 1652 | /* Copied from mpeg.c. Should be moved somewhere else. */ |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 1653 | static void mp3_set_elapsed(struct mp3entry* id3) |
| 1654 | { |
| 1655 | if ( id3->vbr ) { |
| 1656 | if ( id3->has_toc ) { |
| 1657 | /* calculate elapsed time using TOC */ |
| 1658 | int i; |
| 1659 | unsigned int remainder, plen, relpos, nextpos; |
| 1660 | |
| 1661 | /* find wich percent we're at */ |
| 1662 | for (i=0; i<100; i++ ) |
| 1663 | { |
| 1664 | if ( id3->offset < (int)(id3->toc[i] * (id3->filesize / 256)) ) |
| 1665 | { |
| 1666 | break; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | i--; |
| 1671 | if (i < 0) |
| 1672 | i = 0; |
| 1673 | |
| 1674 | relpos = id3->toc[i]; |
| 1675 | |
| 1676 | if (i < 99) |
| 1677 | { |
| 1678 | nextpos = id3->toc[i+1]; |
| 1679 | } |
| 1680 | else |
| 1681 | { |
| 1682 | nextpos = 256; |
| 1683 | } |
| 1684 | |
| 1685 | remainder = id3->offset - (relpos * (id3->filesize / 256)); |
| 1686 | |
| 1687 | /* set time for this percent (divide before multiply to prevent |
| 1688 | overflow on long files. loss of precision is negligible on |
| 1689 | short files) */ |
| 1690 | id3->elapsed = i * (id3->length / 100); |
| 1691 | |
| 1692 | /* calculate remainder time */ |
| 1693 | plen = (nextpos - relpos) * (id3->filesize / 256); |
| 1694 | id3->elapsed += (((remainder * 100) / plen) * |
| 1695 | (id3->length / 10000)); |
| 1696 | } |
| 1697 | else { |
| 1698 | /* no TOC exists. set a rough estimate using average bitrate */ |
| 1699 | int tpk = id3->length / (id3->filesize / 1024); |
| 1700 | id3->elapsed = id3->offset / 1024 * tpk; |
| 1701 | } |
| 1702 | } |
| 1703 | else |
| 1704 | /* constant bitrate == simple frame calculation */ |
| 1705 | id3->elapsed = id3->offset / id3->bpf * id3->tpf; |
| 1706 | } |
| 1707 | |
| 1708 | /* Copied from mpeg.c. Should be moved somewhere else. */ |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 1709 | int mp3_get_file_pos(void) |
| 1710 | { |
| 1711 | int pos = -1; |
| 1712 | struct mp3entry *id3 = audio_current_track(); |
| 1713 | |
| 1714 | if (id3->vbr) |
| 1715 | { |
| 1716 | if (id3->has_toc) |
| 1717 | { |
| 1718 | /* Use the TOC to find the new position */ |
| 1719 | unsigned int percent, remainder; |
| 1720 | int curtoc, nexttoc, plen; |
| 1721 | |
| 1722 | percent = (id3->elapsed*100)/id3->length; |
| 1723 | if (percent > 99) |
| 1724 | percent = 99; |
| 1725 | |
| 1726 | curtoc = id3->toc[percent]; |
| 1727 | |
| 1728 | if (percent < 99) |
| 1729 | nexttoc = id3->toc[percent+1]; |
| 1730 | else |
| 1731 | nexttoc = 256; |
| 1732 | |
| 1733 | pos = (id3->filesize/256)*curtoc; |
| 1734 | |
| 1735 | /* Use the remainder to get a more accurate position */ |
| 1736 | remainder = (id3->elapsed*100)%id3->length; |
| 1737 | remainder = (remainder*100)/id3->length; |
| 1738 | plen = (nexttoc - curtoc)*(id3->filesize/256); |
| 1739 | pos += (plen/100)*remainder; |
| 1740 | } |
| 1741 | else |
| 1742 | { |
| 1743 | /* No TOC exists, estimate the new position */ |
| 1744 | pos = (id3->filesize / (id3->length / 1000)) * |
| 1745 | (id3->elapsed / 1000); |
| 1746 | } |
| 1747 | } |
| 1748 | else if (id3->bpf && id3->tpf) |
| 1749 | pos = (id3->elapsed/id3->tpf)*id3->bpf; |
| 1750 | else |
| 1751 | { |
| 1752 | return -1; |
| 1753 | } |
| 1754 | |
| 1755 | if (pos >= (int)(id3->filesize - id3->id3v1len)) |
| 1756 | { |
| 1757 | /* Don't seek right to the end of the file so that we can |
| 1758 | transition properly to the next song */ |
| 1759 | pos = id3->filesize - id3->id3v1len - 1; |
| 1760 | } |
| 1761 | else if (pos < (int)id3->first_frame_offset) |
| 1762 | { |
| 1763 | /* skip past id3v2 tag and other leading garbage */ |
| 1764 | pos = id3->first_frame_offset; |
| 1765 | } |
| 1766 | return pos; |
| 1767 | } |
| 1768 | |
Linus Nielsen Feltzing | 4aaa321 | 2005-06-06 00:35:21 +0000 | [diff] [blame] | 1769 | #ifndef SIMULATOR |
| 1770 | void audio_set_buffer_margin(int seconds) |
| 1771 | { |
| 1772 | (void)seconds; |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 1773 | logf("bufmargin: %d", seconds); |
Linus Nielsen Feltzing | 4aaa321 | 2005-06-06 00:35:21 +0000 | [diff] [blame] | 1774 | } |
| 1775 | #endif |
| 1776 | |
| 1777 | void mpeg_id3_options(bool _v1first) |
| 1778 | { |
| 1779 | v1first = _v1first; |
| 1780 | } |
| 1781 | |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1782 | void test_buffer_event(struct mp3entry *id3, bool last_track) |
| 1783 | { |
Miika Pekkarinen | e21cf84 | 2005-07-03 19:20:40 +0000 | [diff] [blame] | 1784 | (void)id3; |
| 1785 | (void)last_track; |
| 1786 | |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1787 | logf("be:%d%s", last_track, id3->path); |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Miika Pekkarinen | 9bde038 | 2005-07-03 18:36:24 +0000 | [diff] [blame] | 1790 | void test_unbuffer_event(struct mp3entry *id3, bool last_track) |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1791 | { |
Miika Pekkarinen | e21cf84 | 2005-07-03 19:20:40 +0000 | [diff] [blame] | 1792 | (void)id3; |
| 1793 | (void)last_track; |
| 1794 | |
Miika Pekkarinen | 5d9e053 | 2005-07-08 20:01:06 +0000 | [diff] [blame] | 1795 | logf("ube:%d%s", last_track, id3->path); |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1796 | } |
Miika Pekkarinen | 9bde038 | 2005-07-03 18:36:24 +0000 | [diff] [blame] | 1797 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1798 | void audio_init(void) |
| 1799 | { |
| 1800 | logf("audio api init"); |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 1801 | codecbuflen = audiobufend - audiobuf - PCMBUF_SIZE - PCMBUF_GUARD |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1802 | - MALLOC_BUFSIZE - GUARD_BUFSIZE; |
| 1803 | //codecbuflen = 2*512*1024; |
| 1804 | codecbufused = 0; |
Miika Pekkarinen | b4bc106 | 2005-06-09 07:19:16 +0000 | [diff] [blame] | 1805 | filling = false; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1806 | codecbuf = &audiobuf[MALLOC_BUFSIZE]; |
| 1807 | playing = false; |
Miika Pekkarinen | 84d6f9e | 2005-06-29 20:50:58 +0000 | [diff] [blame] | 1808 | codec_loaded = false; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1809 | paused = false; |
| 1810 | track_changed = false; |
Miika Pekkarinen | 94b917e | 2005-06-11 18:14:41 +0000 | [diff] [blame] | 1811 | current_fd = -1; |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1812 | track_buffer_callback = NULL; |
| 1813 | track_unbuffer_callback = NULL; |
Miika Pekkarinen | 7d6d122 | 2005-06-29 21:36:30 +0000 | [diff] [blame] | 1814 | track_changed_callback = NULL; |
Miika Pekkarinen | 3eb962d | 2005-07-07 07:15:05 +0000 | [diff] [blame] | 1815 | /* Just to prevent cur_ti never be anything random. */ |
| 1816 | cur_ti = &tracks[0]; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1817 | |
Linus Nielsen Feltzing | 4406fa6 | 2005-06-05 23:28:40 +0000 | [diff] [blame] | 1818 | logf("abuf:%0x", PCMBUF_SIZE); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1819 | logf("fbuf:%0x", codecbuflen); |
| 1820 | logf("mbuf:%0x", MALLOC_BUFSIZE); |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1821 | |
Miika Pekkarinen | d54811f | 2005-07-02 16:52:30 +0000 | [diff] [blame] | 1822 | audio_set_track_buffer_event(test_buffer_event); |
| 1823 | audio_set_track_unbuffer_event(test_unbuffer_event); |
Miika Pekkarinen | e21cf84 | 2005-07-03 19:20:40 +0000 | [diff] [blame] | 1824 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1825 | /* Initialize codec api. */ |
| 1826 | ci.read_filebuf = codec_filebuf_callback; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame^] | 1827 | ci.pcmbuf_insert = pcmbuf_insert_buffer; |
| 1828 | ci.pcmbuf_insert_split = codec_pcmbuf_insert_split_callback; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1829 | ci.get_codec_memory = get_codec_memory_callback; |
| 1830 | ci.request_buffer = codec_request_buffer_callback; |
| 1831 | ci.advance_buffer = codec_advance_buffer_callback; |
| 1832 | ci.advance_buffer_loc = codec_advance_buffer_loc_callback; |
| 1833 | ci.request_next_track = codec_request_next_track_callback; |
| 1834 | ci.mp3_get_filepos = codec_mp3_get_filepos_callback; |
| 1835 | ci.seek_buffer = codec_seek_buffer_callback; |
| 1836 | ci.set_elapsed = codec_set_elapsed_callback; |
Ryan Jackson | d191756 | 2005-07-12 16:45:38 +0000 | [diff] [blame] | 1837 | ci.set_offset = codec_set_offset_callback; |
Miika Pekkarinen | 68b9acd | 2005-06-10 15:02:10 +0000 | [diff] [blame] | 1838 | ci.configure = codec_configure_callback; |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 1839 | |
| 1840 | mutex_init(&mutex_bufferfill); |
Miika Pekkarinen | 82c2927 | 2005-06-07 06:34:54 +0000 | [diff] [blame] | 1841 | queue_init(&audio_queue); |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1842 | queue_init(&codec_queue); |
| 1843 | |
| 1844 | create_thread(codec_thread, codec_stack, sizeof(codec_stack), |
| 1845 | codec_thread_name); |
| 1846 | create_thread(audio_thread, audio_stack, sizeof(audio_stack), |
| 1847 | audio_thread_name); |
Miika Pekkarinen | e941289 | 2005-06-15 15:27:52 +0000 | [diff] [blame] | 1848 | #ifndef SIMULATOR |
| 1849 | audio_is_initialized = true; |
| 1850 | #endif |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | |