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