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 | * |
Nicolas Pennequin | ef5fa8e | 2008-05-05 09:40:22 +0000 | [diff] [blame] | 10 | * Copyright (C) 2005-2007 Miika Pekkarinen |
| 11 | * Copyright (C) 2007-2008 Nicolas Pennequin |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 12 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version 2 |
| 16 | * of the License, or (at your option) any later version. |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ****************************************************************************/ |
Brandon Low | c95044a | 2006-04-13 14:17:12 +0000 | [diff] [blame] | 22 | |
Brandon Low | 1f3360f | 2006-10-13 20:48:23 +0000 | [diff] [blame] | 23 | /* TODO: Pause should be handled in here, rather than PCMBUF so that voice can |
| 24 | * play whilst audio is paused */ |
Brandon Low | c95044a | 2006-04-13 14:17:12 +0000 | [diff] [blame] | 25 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <ctype.h> |
| 30 | |
| 31 | #include "system.h" |
| 32 | #include "thread.h" |
| 33 | #include "file.h" |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 34 | #include "panic.h" |
| 35 | #include "memory.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 36 | #include "lcd.h" |
| 37 | #include "font.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 38 | #include "button.h" |
| 39 | #include "kernel.h" |
| 40 | #include "tree.h" |
| 41 | #include "debug.h" |
| 42 | #include "sprintf.h" |
| 43 | #include "settings.h" |
Daniel Stenberg | 1dd672f | 2005-06-22 19:41:30 +0000 | [diff] [blame] | 44 | #include "codecs.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 45 | #include "audio.h" |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 46 | #include "buffering.h" |
Nicolas Pennequin | 33f522d | 2008-04-03 17:51:53 +0000 | [diff] [blame] | 47 | #include "events.h" |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 48 | #include "voice_thread.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 49 | #include "mp3_playback.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 50 | #include "usb.h" |
| 51 | #include "status.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 52 | #include "ata.h" |
| 53 | #include "screens.h" |
| 54 | #include "playlist.h" |
| 55 | #include "playback.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 56 | #include "pcmbuf.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 57 | #include "buffer.h" |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 58 | #include "dsp.h" |
Brandon Low | 8d5a660 | 2006-01-21 23:43:57 +0000 | [diff] [blame] | 59 | #include "abrepeat.h" |
Nicolas Pennequin | 9f4bd87 | 2007-02-14 14:40:24 +0000 | [diff] [blame] | 60 | #include "cuesheet.h" |
Jonathan Gordon | 710ccb7 | 2006-10-25 10:17:57 +0000 | [diff] [blame] | 61 | #ifdef HAVE_TAGCACHE |
Miika Pekkarinen | b725126 | 2006-03-26 16:37:18 +0000 | [diff] [blame] | 62 | #include "tagcache.h" |
Jonathan Gordon | 710ccb7 | 2006-10-25 10:17:57 +0000 | [diff] [blame] | 63 | #endif |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 64 | #ifdef HAVE_LCD_BITMAP |
| 65 | #include "icons.h" |
| 66 | #include "peakmeter.h" |
| 67 | #include "action.h" |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 68 | #include "albumart.h" |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 69 | #endif |
| 70 | #include "lang.h" |
| 71 | #include "bookmark.h" |
| 72 | #include "misc.h" |
| 73 | #include "sound.h" |
Dave Chapman | 3ad485b | 2005-06-14 22:27:57 +0000 | [diff] [blame] | 74 | #include "metadata.h" |
Kevin Ferrare | e991bee | 2005-11-16 15:12:15 +0000 | [diff] [blame] | 75 | #include "splash.h" |
Michael Sevakis | 4fc717a | 2006-08-28 22:38:41 +0000 | [diff] [blame] | 76 | #include "talk.h" |
Jonathan Gordon | 0b22795 | 2006-11-06 14:24:18 +0000 | [diff] [blame] | 77 | #include "ata_idle_notify.h" |
Michael Sevakis | 4fc717a | 2006-08-28 22:38:41 +0000 | [diff] [blame] | 78 | |
| 79 | #ifdef HAVE_RECORDING |
| 80 | #include "recording.h" |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 81 | #include "talk.h" |
Michael Sevakis | 4fc717a | 2006-08-28 22:38:41 +0000 | [diff] [blame] | 82 | #endif |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 83 | |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 84 | #define PLAYBACK_VOICE |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 85 | |
Brandon Low | 62ccbbb | 2006-04-11 03:55:58 +0000 | [diff] [blame] | 86 | /* default point to start buffer refill */ |
Miika Pekkarinen | de3b04e | 2005-06-29 14:46:27 +0000 | [diff] [blame] | 87 | #define AUDIO_DEFAULT_WATERMARK (1024*512) |
Brandon Low | 62ccbbb | 2006-04-11 03:55:58 +0000 | [diff] [blame] | 88 | /* amount of guess-space to allow for codecs that must hunt and peck |
| 89 | * for their correct seeek target, 32k seems a good size */ |
| 90 | #define AUDIO_REBUFFER_GUESS_SIZE (1024*32) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 91 | |
Nicolas Pennequin | fb70952 | 2007-10-24 22:06:36 +0000 | [diff] [blame] | 92 | /* Define LOGF_ENABLE to enable logf output in this file */ |
| 93 | /*#define LOGF_ENABLE*/ |
| 94 | #include "logf.h" |
| 95 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 96 | /* macros to enable logf for queues |
| 97 | logging on SYS_TIMEOUT can be disabled */ |
Michael Sevakis | 77771b0 | 2007-05-09 03:48:52 +0000 | [diff] [blame] | 98 | #ifdef SIMULATOR |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 99 | /* Define this for logf output of all queuing except SYS_TIMEOUT */ |
| 100 | #define PLAYBACK_LOGQUEUES |
| 101 | /* Define this to logf SYS_TIMEOUT messages */ |
Nicolas Pennequin | fb70952 | 2007-10-24 22:06:36 +0000 | [diff] [blame] | 102 | /*#define PLAYBACK_LOGQUEUES_SYS_TIMEOUT*/ |
Steve Bavin | a3087e4 | 2006-08-31 10:01:07 +0000 | [diff] [blame] | 103 | #endif |
| 104 | |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 105 | #ifdef PLAYBACK_LOGQUEUES |
Michael Sevakis | a21871c | 2007-05-08 23:13:46 +0000 | [diff] [blame] | 106 | #define LOGFQUEUE logf |
Steve Bavin | a3087e4 | 2006-08-31 10:01:07 +0000 | [diff] [blame] | 107 | #else |
Michael Sevakis | a21871c | 2007-05-08 23:13:46 +0000 | [diff] [blame] | 108 | #define LOGFQUEUE(...) |
Steve Bavin | a3087e4 | 2006-08-31 10:01:07 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 111 | #ifdef PLAYBACK_LOGQUEUES_SYS_TIMEOUT |
Michael Sevakis | a21871c | 2007-05-08 23:13:46 +0000 | [diff] [blame] | 112 | #define LOGFQUEUE_SYS_TIMEOUT logf |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 113 | #else |
Michael Sevakis | a21871c | 2007-05-08 23:13:46 +0000 | [diff] [blame] | 114 | #define LOGFQUEUE_SYS_TIMEOUT(...) |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 115 | #endif |
| 116 | |
| 117 | |
| 118 | /* Define one constant that includes recording related functionality */ |
| 119 | #if defined(HAVE_RECORDING) && !defined(SIMULATOR) |
| 120 | #define AUDIO_HAVE_RECORDING |
| 121 | #endif |
| 122 | |
Miika Pekkarinen | d319116 | 2006-01-27 11:39:46 +0000 | [diff] [blame] | 123 | enum { |
Michael Sevakis | d6f2a54 | 2007-11-19 11:59:52 +0000 | [diff] [blame] | 124 | Q_NULL = 0, |
Miika Pekkarinen | d319116 | 2006-01-27 11:39:46 +0000 | [diff] [blame] | 125 | Q_AUDIO_PLAY = 1, |
| 126 | Q_AUDIO_STOP, |
| 127 | Q_AUDIO_PAUSE, |
Brandon Low | 2736363 | 2006-04-06 21:06:37 +0000 | [diff] [blame] | 128 | Q_AUDIO_SKIP, |
Brandon Low | ab57025 | 2006-04-07 18:18:36 +0000 | [diff] [blame] | 129 | Q_AUDIO_PRE_FF_REWIND, |
Miika Pekkarinen | d319116 | 2006-01-27 11:39:46 +0000 | [diff] [blame] | 130 | Q_AUDIO_FF_REWIND, |
Brandon Low | 62ccbbb | 2006-04-11 03:55:58 +0000 | [diff] [blame] | 131 | Q_AUDIO_CHECK_NEW_TRACK, |
Miika Pekkarinen | d319116 | 2006-01-27 11:39:46 +0000 | [diff] [blame] | 132 | Q_AUDIO_FLUSH, |
| 133 | Q_AUDIO_TRACK_CHANGED, |
Brandon Low | 2736363 | 2006-04-06 21:06:37 +0000 | [diff] [blame] | 134 | Q_AUDIO_DIR_SKIP, |
Miika Pekkarinen | d319116 | 2006-01-27 11:39:46 +0000 | [diff] [blame] | 135 | Q_AUDIO_POSTINIT, |
Brandon Low | 930785c | 2006-04-06 16:21:31 +0000 | [diff] [blame] | 136 | Q_AUDIO_FILL_BUFFER, |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 137 | Q_AUDIO_FINISH_LOAD, |
Brandon Low | fd08424 | 2006-04-14 13:05:08 +0000 | [diff] [blame] | 138 | Q_CODEC_REQUEST_COMPLETE, |
Brandon Low | 0291a6e | 2006-04-14 14:03:43 +0000 | [diff] [blame] | 139 | Q_CODEC_REQUEST_FAILED, |
Brandon Low | fd08424 | 2006-04-14 13:05:08 +0000 | [diff] [blame] | 140 | |
Miika Pekkarinen | d319116 | 2006-01-27 11:39:46 +0000 | [diff] [blame] | 141 | Q_CODEC_LOAD, |
| 142 | Q_CODEC_LOAD_DISK, |
Michael Sevakis | 4fc717a | 2006-08-28 22:38:41 +0000 | [diff] [blame] | 143 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 144 | #ifdef AUDIO_HAVE_RECORDING |
Michael Sevakis | 4fc717a | 2006-08-28 22:38:41 +0000 | [diff] [blame] | 145 | Q_ENCODER_LOAD_DISK, |
| 146 | Q_ENCODER_RECORD, |
| 147 | #endif |
Miika Pekkarinen | d319116 | 2006-01-27 11:39:46 +0000 | [diff] [blame] | 148 | }; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 149 | |
Nicolas Pennequin | f68147e | 2008-03-28 20:18:53 +0000 | [diff] [blame] | 150 | enum filling_state { |
| 151 | STATE_IDLE, /* audio is stopped: nothing to do */ |
| 152 | STATE_FILLING, /* adding tracks to the buffer */ |
| 153 | STATE_FULL, /* can't add any more tracks */ |
| 154 | STATE_FINISHED, /* all remaining tracks have been added */ |
| 155 | }; |
| 156 | |
Bertrik Sikken | c97e503 | 2008-04-28 14:13:13 +0000 | [diff] [blame] | 157 | #if MEM > 1 |
| 158 | #define MAX_TRACK 128 |
| 159 | #else |
| 160 | #define MAX_TRACK 32 |
| 161 | #endif |
| 162 | |
| 163 | #define MAX_TRACK_MASK (MAX_TRACK-1) |
| 164 | |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 165 | /* As defined in plugins/lib/xxx2wav.h */ |
Dave Chapman | 0a7ded3 | 2006-08-13 09:19:24 +0000 | [diff] [blame] | 166 | #if MEM > 1 |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 167 | #define MALLOC_BUFSIZE (512*1024) |
Miika Pekkarinen | a4c190f | 2005-10-30 09:30:14 +0000 | [diff] [blame] | 168 | #define GUARD_BUFSIZE (32*1024) |
Tomasz Malesinski | 28910e7 | 2006-08-12 23:01:52 +0000 | [diff] [blame] | 169 | #else |
| 170 | #define MALLOC_BUFSIZE (100*1024) |
| 171 | #define GUARD_BUFSIZE (8*1024) |
| 172 | #endif |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 173 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 174 | /* As defined in plugin.lds */ |
Daniel Ankers | 242cbd5 | 2006-11-22 00:41:30 +0000 | [diff] [blame] | 175 | #if defined(CPU_PP) |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 176 | #define CODEC_IRAM_ORIGIN ((unsigned char *)0x4000c000) |
| 177 | #define CODEC_IRAM_SIZE ((size_t)0xc000) |
Jens Arnold | 8876018 | 2007-03-05 00:04:00 +0000 | [diff] [blame] | 178 | #elif defined(IAUDIO_X5) || defined(IAUDIO_M5) |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 179 | #define CODEC_IRAM_ORIGIN ((unsigned char *)0x10010000) |
| 180 | #define CODEC_IRAM_SIZE ((size_t)0x10000) |
Thom Johansen | a7b5a2c | 2006-03-22 15:19:59 +0000 | [diff] [blame] | 181 | #else |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 182 | #define CODEC_IRAM_ORIGIN ((unsigned char *)0x1000c000) |
| 183 | #define CODEC_IRAM_SIZE ((size_t)0xc000) |
Jens Arnold | 03adbd2 | 2006-11-10 07:45:30 +0000 | [diff] [blame] | 184 | #endif |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 185 | |
Steve Bavin | 7669103 | 2008-03-25 19:46:35 +0000 | [diff] [blame] | 186 | bool audio_is_initialized = false; |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 187 | static bool audio_thread_ready SHAREDBSS_ATTR = false; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 188 | |
Steve Bavin | 145ec81 | 2006-10-30 08:54:48 +0000 | [diff] [blame] | 189 | /* Variables are commented with the threads that use them: * |
| 190 | * A=audio, C=codec, V=voice. A suffix of - indicates that * |
| 191 | * the variable is read but not updated on that thread. */ |
Steve Bavin | 6a27006 | 2006-11-13 09:07:18 +0000 | [diff] [blame] | 192 | /* TBD: Split out "audio" and "playback" (ie. calling) threads */ |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 193 | |
Steve Bavin | 145ec81 | 2006-10-30 08:54:48 +0000 | [diff] [blame] | 194 | /* Main state control */ |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 195 | static volatile bool audio_codec_loaded SHAREDBSS_ATTR = false; /* Codec loaded? (C/A-) */ |
| 196 | static volatile bool playing SHAREDBSS_ATTR = false; /* Is audio playing? (A) */ |
| 197 | static volatile bool paused SHAREDBSS_ATTR = false; /* Is audio paused? (A/C-) */ |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 198 | |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 199 | /* Ring buffer where compressed audio and codecs are loaded */ |
Michael Sevakis | e4efe2f | 2007-03-09 13:41:33 +0000 | [diff] [blame] | 200 | static unsigned char *filebuf = NULL; /* Start of buffer (A/C-) */ |
Michael Sevakis | 1f8326b | 2007-03-11 00:37:58 +0000 | [diff] [blame] | 201 | static unsigned char *malloc_buf = NULL; /* Start of malloc buffer (A/C-) */ |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 202 | /* FIXME: make filebuflen static */ |
Michael Sevakis | e4efe2f | 2007-03-09 13:41:33 +0000 | [diff] [blame] | 203 | size_t filebuflen = 0; /* Size of buffer (A/C-) */ |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 204 | /* FIXME: make buf_ridx (C/A-) */ |
Miika Pekkarinen | bbd42ad | 2005-07-01 18:22:04 +0000 | [diff] [blame] | 205 | |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 206 | /* Possible arrangements of the buffer */ |
| 207 | #define BUFFER_STATE_TRASHED -1 /* trashed; must be reset */ |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 208 | #define BUFFER_STATE_INITIALIZED 0 /* voice+audio OR audio-only */ |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 209 | #define BUFFER_STATE_VOICED_ONLY 1 /* voice-only */ |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 210 | static int buffer_state = BUFFER_STATE_TRASHED; /* Buffer state */ |
| 211 | |
Nicolas Pennequin | 941600f | 2007-11-08 17:27:43 +0000 | [diff] [blame] | 212 | /* Used to keep the WPS up-to-date during track transtition */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 213 | static struct mp3entry prevtrack_id3; |
Nicolas Pennequin | 941600f | 2007-11-08 17:27:43 +0000 | [diff] [blame] | 214 | |
| 215 | /* Used to provide the codec with a pointer */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 216 | static struct mp3entry curtrack_id3; |
Nicolas Pennequin | 941600f | 2007-11-08 17:27:43 +0000 | [diff] [blame] | 217 | |
| 218 | /* Used to make next track info available while playing last track on buffer */ |
Nicolas Pennequin | 12d479f | 2007-11-08 16:52:29 +0000 | [diff] [blame] | 219 | static struct mp3entry lasttrack_id3; |
Steve Bavin | 73e2f7b | 2006-10-12 16:51:22 +0000 | [diff] [blame] | 220 | |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 221 | /* Track info structure about songs in the file buffer (A/C-) */ |
Steve Bavin | 4d34457 | 2007-10-02 07:47:43 +0000 | [diff] [blame] | 222 | struct track_info { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 223 | int audio_hid; /* The ID for the track's buffer handle */ |
| 224 | int id3_hid; /* The ID for the track's metadata handle */ |
| 225 | int codec_hid; /* The ID for the track's codec handle */ |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 226 | #ifdef HAVE_ALBUMART |
| 227 | int aa_hid; /* The ID for the track's album art handle */ |
| 228 | #endif |
Steve Bavin | 4d34457 | 2007-10-02 07:47:43 +0000 | [diff] [blame] | 229 | |
Steve Bavin | 4d34457 | 2007-10-02 07:47:43 +0000 | [diff] [blame] | 230 | size_t filesize; /* File total length */ |
Steve Bavin | 4d34457 | 2007-10-02 07:47:43 +0000 | [diff] [blame] | 231 | |
| 232 | bool taginfo_ready; /* Is metadata read */ |
Steve Bavin | 4d34457 | 2007-10-02 07:47:43 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 235 | static struct track_info tracks[MAX_TRACK]; |
Michael Sevakis | e4efe2f | 2007-03-09 13:41:33 +0000 | [diff] [blame] | 236 | static volatile int track_ridx = 0; /* Track being decoded (A/C-) */ |
| 237 | static int track_widx = 0; /* Track being buffered (A) */ |
Steve Bavin | 73e2f7b | 2006-10-12 16:51:22 +0000 | [diff] [blame] | 238 | |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 239 | #define CUR_TI (&tracks[track_ridx]) /* Playing track info pointer (A/C-) */ |
Nicolas Pennequin | 4963925 | 2007-11-12 00:17:55 +0000 | [diff] [blame] | 240 | static struct track_info *prev_ti = NULL; /* Pointer to the previously played |
| 241 | track */ |
Brandon Low | a553d5f | 2006-03-23 16:18:17 +0000 | [diff] [blame] | 242 | |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 243 | /* Set by the audio thread when the current track information has updated |
| 244 | * and the WPS may need to update its cached information */ |
Michael Sevakis | e4efe2f | 2007-03-09 13:41:33 +0000 | [diff] [blame] | 245 | static bool track_changed = false; |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 246 | |
| 247 | /* Information used only for filling the buffer */ |
| 248 | /* Playlist steps from playing track to next track to be buffered (A) */ |
Michael Sevakis | e4efe2f | 2007-03-09 13:41:33 +0000 | [diff] [blame] | 249 | static int last_peek_offset = 0; |
Miika Pekkarinen | 431e813 | 2005-06-19 18:41:53 +0000 | [diff] [blame] | 250 | |
Steve Bavin | 145ec81 | 2006-10-30 08:54:48 +0000 | [diff] [blame] | 251 | /* Scrobbler support */ |
Michael Sevakis | e4efe2f | 2007-03-09 13:41:33 +0000 | [diff] [blame] | 252 | static unsigned long prev_track_elapsed = 0; /* Previous track elapsed time (C/A-)*/ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 253 | |
Nicolas Pennequin | f68147e | 2008-03-28 20:18:53 +0000 | [diff] [blame] | 254 | static enum filling_state filling; |
| 255 | |
Steve Bavin | 145ec81 | 2006-10-30 08:54:48 +0000 | [diff] [blame] | 256 | /* Track change controls */ |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 257 | static bool automatic_skip = false; /* Who initiated in-progress skip? (C/A-) */ |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 258 | static bool dir_skip = false; /* Is a directory skip pending? (A) */ |
| 259 | static bool new_playlist = false; /* Are we starting a new playlist? (A) */ |
Steve Bavin | 93b3fb3 | 2007-07-31 07:57:59 +0000 | [diff] [blame] | 260 | static int wps_offset = 0; /* Pending track change offset, to keep WPS responsive (A) */ |
| 261 | static bool skipped_during_pause = false; /* Do we need to clear the PCM buffer when playback resumes (A) */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 262 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 263 | static bool start_play_g = false; /* Used by audio_load_track to notify |
| 264 | audio_finish_load_track about start_play */ |
| 265 | |
Magnus Holmgren | c8d2a24 | 2008-03-04 18:56:37 +0000 | [diff] [blame] | 266 | /* Set to true if the codec thread should send an audio stop request |
| 267 | * (typically because the end of the playlist has been reached). |
| 268 | */ |
| 269 | static bool codec_requested_stop = false; |
| 270 | |
Michael Sevakis | e4efe2f | 2007-03-09 13:41:33 +0000 | [diff] [blame] | 271 | static size_t buffer_margin = 0; /* Buffer margin aka anti-skip buffer (A/C-) */ |
Linus Nielsen Feltzing | 4aaa321 | 2005-06-06 00:35:21 +0000 | [diff] [blame] | 272 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 273 | /* Multiple threads */ |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 274 | /* Set the watermark to trigger buffer fill (A/C) FIXME */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 275 | static void set_filebuf_watermark(int seconds, size_t max); |
Miika Pekkarinen | 5899ed5 | 2005-06-08 10:33:01 +0000 | [diff] [blame] | 276 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 277 | /* Audio thread */ |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 278 | static struct event_queue audio_queue SHAREDBSS_ATTR; |
| 279 | static struct queue_sender_list audio_queue_sender_list SHAREDBSS_ATTR; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 280 | static long audio_stack[(DEFAULT_STACK_SIZE + 0x1000)/sizeof(long)]; |
| 281 | static const char audio_thread_name[] = "audio"; |
| 282 | |
| 283 | static void audio_thread(void); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 284 | static void audio_initiate_track_change(long direction); |
| 285 | static bool audio_have_tracks(void); |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 286 | static void audio_reset_buffer(void); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 287 | |
| 288 | /* Codec thread */ |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 289 | extern struct codec_api ci; |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 290 | static struct event_queue codec_queue SHAREDBSS_ATTR; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 291 | static struct queue_sender_list codec_queue_sender_list; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 292 | static long codec_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)] |
| 293 | IBSS_ATTR; |
| 294 | static const char codec_thread_name[] = "codec"; |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 295 | struct thread_entry *codec_thread_p; /* For modifying thread priority later. */ |
Steve Bavin | 145ec81 | 2006-10-30 08:54:48 +0000 | [diff] [blame] | 296 | |
Michael Sevakis | f38274f | 2008-02-28 22:37:46 +0000 | [diff] [blame] | 297 | /* PCM buffer messaging */ |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 298 | static struct event_queue pcmbuf_queue SHAREDBSS_ATTR; |
Michael Sevakis | f38274f | 2008-02-28 22:37:46 +0000 | [diff] [blame] | 299 | |
| 300 | /* Function to be called by pcm buffer callbacks. |
| 301 | * Permissible Context(s): Audio interrupt |
| 302 | */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 303 | static void pcmbuf_callback_queue_post(long id, intptr_t data) |
Michael Sevakis | f38274f | 2008-02-28 22:37:46 +0000 | [diff] [blame] | 304 | { |
| 305 | /* No lock since we're already in audio interrupt context */ |
| 306 | queue_post(&pcmbuf_queue, id, data); |
| 307 | } |
| 308 | |
| 309 | /* Scan the pcmbuf queue and return true if a message pulled. |
| 310 | * Permissible Context(s): Thread |
| 311 | */ |
| 312 | static bool pcmbuf_queue_scan(struct queue_event *ev) |
| 313 | { |
| 314 | if (!queue_empty(&pcmbuf_queue)) |
| 315 | { |
| 316 | /* Transfer message to audio queue */ |
| 317 | pcm_play_lock(); |
| 318 | /* Pull message - never, ever any blocking call! */ |
| 319 | queue_wait_w_tmo(&pcmbuf_queue, ev, 0); |
| 320 | pcm_play_unlock(); |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | /* Clear the pcmbuf queue of messages |
| 328 | * Permissible Context(s): Thread |
| 329 | */ |
| 330 | static void pcmbuf_queue_clear(void) |
| 331 | { |
| 332 | pcm_play_lock(); |
| 333 | queue_clear(&pcmbuf_queue); |
| 334 | pcm_play_unlock(); |
| 335 | } |
| 336 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 337 | /* --- Helper functions --- */ |
| 338 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 339 | static struct mp3entry *bufgetid3(int handle_id) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 340 | { |
| 341 | if (handle_id < 0) |
| 342 | return NULL; |
| 343 | |
| 344 | struct mp3entry *id3; |
| 345 | ssize_t ret = bufgetdata(handle_id, 0, (void *)&id3); |
| 346 | |
| 347 | if (ret < 0 || ret != sizeof(struct mp3entry)) |
| 348 | return NULL; |
| 349 | |
| 350 | return id3; |
| 351 | } |
| 352 | |
Steve Bavin | 530abe8 | 2007-10-26 09:39:05 +0000 | [diff] [blame] | 353 | static bool clear_track_info(struct track_info *track) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 354 | { |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 355 | /* bufclose returns true if the handle is not found, or if it is closed |
| 356 | * successfully, so these checks are safe on non-existant handles */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 357 | if (!track) |
| 358 | return false; |
| 359 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 360 | if (track->codec_hid >= 0) { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 361 | if (bufclose(track->codec_hid)) |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 362 | track->codec_hid = -1; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 363 | else |
| 364 | return false; |
| 365 | } |
| 366 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 367 | if (track->id3_hid >= 0) { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 368 | if (bufclose(track->id3_hid)) |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 369 | track->id3_hid = -1; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 370 | else |
| 371 | return false; |
| 372 | } |
| 373 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 374 | if (track->audio_hid >= 0) { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 375 | if (bufclose(track->audio_hid)) |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 376 | track->audio_hid = -1; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 377 | else |
| 378 | return false; |
| 379 | } |
| 380 | |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 381 | #ifdef HAVE_ALBUMART |
| 382 | if (track->aa_hid >= 0) { |
| 383 | if (bufclose(track->aa_hid)) |
| 384 | track->aa_hid = -1; |
| 385 | else |
| 386 | return false; |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 387 | } |
Nicolas Pennequin | cf37f4c | 2007-11-11 12:52:07 +0000 | [diff] [blame] | 388 | #endif |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 389 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 390 | track->filesize = 0; |
| 391 | track->taginfo_ready = false; |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 392 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 393 | return true; |
| 394 | } |
| 395 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 396 | /* --- External interfaces --- */ |
| 397 | |
Michael Sevakis | 9cd6394 | 2007-04-12 05:58:09 +0000 | [diff] [blame] | 398 | /* This sends a stop message and the audio thread will dump all it's |
| 399 | subsequenct messages */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 400 | void audio_hard_stop(void) |
Michael Sevakis | 9cd6394 | 2007-04-12 05:58:09 +0000 | [diff] [blame] | 401 | { |
| 402 | /* Stop playback */ |
| 403 | LOGFQUEUE("audio >| audio Q_AUDIO_STOP: 1"); |
| 404 | queue_send(&audio_queue, Q_AUDIO_STOP, 1); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 405 | #ifdef PLAYBACK_VOICE |
| 406 | voice_stop(); |
| 407 | #endif |
| 408 | } |
| 409 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 410 | bool audio_restore_playback(int type) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 411 | { |
| 412 | switch (type) |
| 413 | { |
| 414 | case AUDIO_WANT_PLAYBACK: |
| 415 | if (buffer_state != BUFFER_STATE_INITIALIZED) |
| 416 | audio_reset_buffer(); |
| 417 | return true; |
| 418 | case AUDIO_WANT_VOICE: |
| 419 | if (buffer_state == BUFFER_STATE_TRASHED) |
| 420 | audio_reset_buffer(); |
| 421 | return true; |
| 422 | default: |
| 423 | return false; |
| 424 | } |
Michael Sevakis | 9cd6394 | 2007-04-12 05:58:09 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 427 | unsigned char *audio_get_buffer(bool talk_buf, size_t *buffer_size) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 428 | { |
Michael Sevakis | acc29d9 | 2006-11-18 02:18:29 +0000 | [diff] [blame] | 429 | unsigned char *buf, *end; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 430 | |
Michael Sevakis | a1af87f | 2006-11-27 13:54:50 +0000 | [diff] [blame] | 431 | if (audio_is_initialized) |
Michael Sevakis | 97d1ca5 | 2006-11-28 15:00:56 +0000 | [diff] [blame] | 432 | { |
Michael Sevakis | 9cd6394 | 2007-04-12 05:58:09 +0000 | [diff] [blame] | 433 | audio_hard_stop(); |
Michael Sevakis | 97d1ca5 | 2006-11-28 15:00:56 +0000 | [diff] [blame] | 434 | } |
| 435 | /* else buffer_state will be BUFFER_STATE_TRASHED at this point */ |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 436 | |
Michael Sevakis | acc29d9 | 2006-11-18 02:18:29 +0000 | [diff] [blame] | 437 | if (buffer_size == NULL) |
| 438 | { |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 439 | /* Special case for talk_init to use since it already knows it's |
| 440 | trashed */ |
Michael Sevakis | acc29d9 | 2006-11-18 02:18:29 +0000 | [diff] [blame] | 441 | buffer_state = BUFFER_STATE_TRASHED; |
| 442 | return NULL; |
| 443 | } |
| 444 | |
Michael Sevakis | a1af87f | 2006-11-27 13:54:50 +0000 | [diff] [blame] | 445 | if (talk_buf || buffer_state == BUFFER_STATE_TRASHED |
| 446 | || !talk_voice_required()) |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 447 | { |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 448 | logf("get buffer: talk, audio"); |
| 449 | /* Ok to use everything from audiobuf to audiobufend - voice is loaded, |
| 450 | the talk buffer is not needed because voice isn't being used, or |
| 451 | could be BUFFER_STATE_TRASHED already. If state is |
| 452 | BUFFER_STATE_VOICED_ONLY, no problem as long as memory isn't written |
| 453 | without the caller knowing what's going on. Changing certain settings |
| 454 | may move it to a worse condition but the memory in use by something |
| 455 | else will remain undisturbed. |
| 456 | */ |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 457 | if (buffer_state != BUFFER_STATE_TRASHED) |
Michael Sevakis | fbac4f8 | 2006-11-22 09:13:14 +0000 | [diff] [blame] | 458 | { |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 459 | talk_buffer_steal(); |
Michael Sevakis | fbac4f8 | 2006-11-22 09:13:14 +0000 | [diff] [blame] | 460 | buffer_state = BUFFER_STATE_TRASHED; |
| 461 | } |
Michael Sevakis | 97d1ca5 | 2006-11-28 15:00:56 +0000 | [diff] [blame] | 462 | |
| 463 | buf = audiobuf; |
| 464 | end = audiobufend; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 465 | } |
| 466 | else |
| 467 | { |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 468 | /* Safe to just return this if already BUFFER_STATE_VOICED_ONLY or |
| 469 | still BUFFER_STATE_INITIALIZED */ |
| 470 | /* Skip talk buffer and move pcm buffer to end to maximize available |
| 471 | contiguous memory - no audio running means voice will not need the |
| 472 | swap space */ |
| 473 | logf("get buffer: audio"); |
Michael Sevakis | 97d1ca5 | 2006-11-28 15:00:56 +0000 | [diff] [blame] | 474 | buf = audiobuf + talk_get_bufsize(); |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 475 | end = audiobufend - pcmbuf_init(audiobufend); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 476 | buffer_state = BUFFER_STATE_VOICED_ONLY; |
| 477 | } |
| 478 | |
| 479 | *buffer_size = end - buf; |
| 480 | |
| 481 | return buf; |
| 482 | } |
| 483 | |
| 484 | #ifdef HAVE_RECORDING |
| 485 | unsigned char *audio_get_recording_buffer(size_t *buffer_size) |
| 486 | { |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 487 | /* Stop audio, voice and obtain all available buffer space */ |
Michael Sevakis | 9cd6394 | 2007-04-12 05:58:09 +0000 | [diff] [blame] | 488 | audio_hard_stop(); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 489 | talk_buffer_steal(); |
| 490 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 491 | unsigned char *end = audiobufend; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 492 | buffer_state = BUFFER_STATE_TRASHED; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 493 | *buffer_size = end - audiobuf; |
| 494 | |
| 495 | return (unsigned char *)audiobuf; |
| 496 | } |
| 497 | |
| 498 | bool audio_load_encoder(int afmt) |
| 499 | { |
| 500 | #ifndef SIMULATOR |
| 501 | const char *enc_fn = get_codec_filename(afmt | CODEC_TYPE_ENCODER); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 502 | if (!enc_fn) |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 503 | return false; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 504 | |
| 505 | audio_remove_encoder(); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 506 | ci.enc_codec_loaded = 0; /* clear any previous error condition */ |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 507 | |
Michael Sevakis | 598629c | 2007-03-04 04:16:53 +0000 | [diff] [blame] | 508 | LOGFQUEUE("codec > Q_ENCODER_LOAD_DISK"); |
| 509 | queue_post(&codec_queue, Q_ENCODER_LOAD_DISK, (intptr_t)enc_fn); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 510 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 511 | while (ci.enc_codec_loaded == 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 512 | yield(); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 513 | |
| 514 | logf("codec loaded: %d", ci.enc_codec_loaded); |
| 515 | |
| 516 | return ci.enc_codec_loaded > 0; |
| 517 | #else |
| 518 | (void)afmt; |
| 519 | return true; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 520 | #endif |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 521 | } /* audio_load_encoder */ |
| 522 | |
| 523 | void audio_remove_encoder(void) |
| 524 | { |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 525 | #ifndef SIMULATOR |
| 526 | /* force encoder codec unload (if currently loaded) */ |
| 527 | if (ci.enc_codec_loaded <= 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 528 | return; |
| 529 | |
Michael Sevakis | 598629c | 2007-03-04 04:16:53 +0000 | [diff] [blame] | 530 | ci.stop_encoder = true; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 531 | while (ci.enc_codec_loaded > 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 532 | yield(); |
| 533 | #endif |
| 534 | } /* audio_remove_encoder */ |
| 535 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 536 | #endif /* HAVE_RECORDING */ |
| 537 | |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 538 | #ifdef HAVE_ALBUMART |
| 539 | int audio_current_aa_hid(void) |
| 540 | { |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 541 | int cur_idx; |
| 542 | int offset = ci.new_track + wps_offset; |
| 543 | |
| 544 | cur_idx = track_ridx + offset; |
| 545 | cur_idx &= MAX_TRACK_MASK; |
| 546 | |
| 547 | return tracks[cur_idx].aa_hid; |
Nicolas Pennequin | 9d4bed7 | 2007-11-11 12:29:37 +0000 | [diff] [blame] | 548 | } |
| 549 | #endif |
| 550 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 551 | struct mp3entry* audio_current_track(void) |
| 552 | { |
| 553 | const char *filename; |
| 554 | const char *p; |
| 555 | static struct mp3entry temp_id3; |
Nicolas Pennequin | babd3be | 2008-04-15 20:59:47 +0000 | [diff] [blame] | 556 | struct playlist_track_info trackinfo; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 557 | int cur_idx; |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 558 | int offset = ci.new_track + wps_offset; |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 559 | |
Nicolas Pennequin | cfa6495 | 2007-11-19 15:52:25 +0000 | [diff] [blame] | 560 | cur_idx = (track_ridx + offset) & MAX_TRACK_MASK; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 561 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 562 | if (cur_idx == track_ridx && *curtrack_id3.path) |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 563 | { |
| 564 | /* The usual case */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 565 | return &curtrack_id3; |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 566 | } |
Nicolas Pennequin | b36d3c0 | 2008-04-15 19:20:57 +0000 | [diff] [blame] | 567 | else if (automatic_skip && offset == -1 && *prevtrack_id3.path) |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 568 | { |
| 569 | /* We're in a track transition. The codec has moved on to the nex track, |
| 570 | but the audio being played is still the same (now previous) track. |
| 571 | prevtrack_id3.elapsed is being updated in an ISR by |
| 572 | codec_pcmbuf_position_callback */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 573 | return &prevtrack_id3; |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 574 | } |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 575 | else if (tracks[cur_idx].id3_hid >= 0) |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 576 | { |
| 577 | /* Get the ID3 metadata from the main buffer */ |
Nicolas Pennequin | b36d3c0 | 2008-04-15 19:20:57 +0000 | [diff] [blame] | 578 | struct mp3entry *ret = bufgetid3(tracks[cur_idx].id3_hid); |
| 579 | if (ret) return ret; |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* We didn't find the ID3 metadata, so we fill temp_id3 with the little info |
| 583 | we have and return that. */ |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 584 | |
| 585 | memset(&temp_id3, 0, sizeof(struct mp3entry)); |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 586 | |
Nicolas Pennequin | babd3be | 2008-04-15 20:59:47 +0000 | [diff] [blame] | 587 | playlist_get_track_info(NULL, playlist_next(0)+wps_offset, &trackinfo); |
| 588 | filename = trackinfo.filename; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 589 | if (!filename) |
| 590 | filename = "No file!"; |
| 591 | |
Miika Pekkarinen | 6ab1c90 | 2008-03-09 20:33:19 +0000 | [diff] [blame] | 592 | #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 593 | if (tagcache_fill_tags(&temp_id3, filename)) |
| 594 | return &temp_id3; |
| 595 | #endif |
| 596 | |
| 597 | p = strrchr(filename, '/'); |
| 598 | if (!p) |
| 599 | p = filename; |
| 600 | else |
| 601 | p++; |
| 602 | |
| 603 | strncpy(temp_id3.path, p, sizeof(temp_id3.path)-1); |
| 604 | temp_id3.title = &temp_id3.path[0]; |
| 605 | |
| 606 | return &temp_id3; |
| 607 | } |
| 608 | |
| 609 | struct mp3entry* audio_next_track(void) |
| 610 | { |
Nicolas Pennequin | ea9e02b | 2008-02-26 17:15:30 +0000 | [diff] [blame] | 611 | int next_idx; |
| 612 | int offset = ci.new_track + wps_offset; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 613 | |
| 614 | if (!audio_have_tracks()) |
| 615 | return NULL; |
| 616 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 617 | if (wps_offset == -1 && *prevtrack_id3.path) |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 618 | { |
| 619 | /* We're in a track transition. The next track for the WPS is the one |
| 620 | currently being decoded. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 621 | return &curtrack_id3; |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 622 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 623 | |
Nicolas Pennequin | ea9e02b | 2008-02-26 17:15:30 +0000 | [diff] [blame] | 624 | next_idx = (track_ridx + offset + 1) & MAX_TRACK_MASK; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 625 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 626 | if (tracks[next_idx].id3_hid >= 0) |
| 627 | return bufgetid3(tracks[next_idx].id3_hid); |
| 628 | |
Nicolas Pennequin | 12d479f | 2007-11-08 16:52:29 +0000 | [diff] [blame] | 629 | if (next_idx == track_widx) |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 630 | { |
| 631 | /* The next track hasn't been buffered yet, so we return the static |
| 632 | version of its metadata. */ |
Nicolas Pennequin | 12d479f | 2007-11-08 16:52:29 +0000 | [diff] [blame] | 633 | return &lasttrack_id3; |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 634 | } |
Nicolas Pennequin | 12d479f | 2007-11-08 16:52:29 +0000 | [diff] [blame] | 635 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 636 | return NULL; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | bool audio_has_changed_track(void) |
| 640 | { |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 641 | if (track_changed) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 642 | { |
| 643 | track_changed = false; |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | return false; |
| 648 | } |
| 649 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 650 | void audio_play(long offset) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 651 | { |
| 652 | logf("audio_play"); |
Steve Bavin | 71dd94a | 2006-10-25 08:54:25 +0000 | [diff] [blame] | 653 | |
| 654 | #ifdef PLAYBACK_VOICE |
| 655 | /* Truncate any existing voice output so we don't have spelling |
| 656 | * etc. over the first part of the played track */ |
Steve Bavin | 32a9575 | 2007-10-19 15:31:42 +0000 | [diff] [blame] | 657 | talk_force_shutup(); |
Steve Bavin | 71dd94a | 2006-10-25 08:54:25 +0000 | [diff] [blame] | 658 | #endif |
| 659 | |
| 660 | /* Start playback */ |
Michael Sevakis | e486ed0 | 2007-05-09 03:44:44 +0000 | [diff] [blame] | 661 | LOGFQUEUE("audio >| audio Q_AUDIO_PLAY: %ld", offset); |
Michael Sevakis | d9839bc | 2007-05-08 22:59:25 +0000 | [diff] [blame] | 662 | /* Don't return until playback has actually started */ |
Michael Sevakis | 1c59b10 | 2007-05-08 22:56:12 +0000 | [diff] [blame] | 663 | queue_send(&audio_queue, Q_AUDIO_PLAY, offset); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void audio_stop(void) |
| 667 | { |
Steve Bavin | 71dd94a | 2006-10-25 08:54:25 +0000 | [diff] [blame] | 668 | /* Stop playback */ |
Michael Sevakis | d7e8e38 | 2007-05-03 21:49:59 +0000 | [diff] [blame] | 669 | LOGFQUEUE("audio >| audio Q_AUDIO_STOP"); |
Steve Bavin | 71dd94a | 2006-10-25 08:54:25 +0000 | [diff] [blame] | 670 | /* Don't return until playback has actually stopped */ |
Michael Sevakis | d7e8e38 | 2007-05-03 21:49:59 +0000 | [diff] [blame] | 671 | queue_send(&audio_queue, Q_AUDIO_STOP, 0); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | void audio_pause(void) |
| 675 | { |
Michael Sevakis | 1c59b10 | 2007-05-08 22:56:12 +0000 | [diff] [blame] | 676 | LOGFQUEUE("audio >| audio Q_AUDIO_PAUSE"); |
| 677 | /* Don't return until playback has actually paused */ |
| 678 | queue_send(&audio_queue, Q_AUDIO_PAUSE, true); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | void audio_resume(void) |
| 682 | { |
Michael Sevakis | 1c59b10 | 2007-05-08 22:56:12 +0000 | [diff] [blame] | 683 | LOGFQUEUE("audio >| audio Q_AUDIO_PAUSE resume"); |
| 684 | /* Don't return until playback has actually resumed */ |
| 685 | queue_send(&audio_queue, Q_AUDIO_PAUSE, false); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Nicolas Pennequin | f730133 | 2008-02-26 17:15:33 +0000 | [diff] [blame] | 688 | static void audio_skip(int direction) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 689 | { |
Nicolas Pennequin | f730133 | 2008-02-26 17:15:33 +0000 | [diff] [blame] | 690 | if (playlist_check(ci.new_track + wps_offset + direction)) |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 691 | { |
Steve Bavin | 081281f | 2006-09-26 07:19:43 +0000 | [diff] [blame] | 692 | if (global_settings.beep) |
| 693 | pcmbuf_beep(5000, 100, 2500*global_settings.beep); |
| 694 | |
Nicolas Pennequin | f730133 | 2008-02-26 17:15:33 +0000 | [diff] [blame] | 695 | LOGFQUEUE("audio > audio Q_AUDIO_SKIP %d", direction); |
| 696 | queue_post(&audio_queue, Q_AUDIO_SKIP, direction); |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 697 | /* Update wps while our message travels inside deep playback queues. */ |
Nicolas Pennequin | f730133 | 2008-02-26 17:15:33 +0000 | [diff] [blame] | 698 | wps_offset += direction; |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 699 | track_changed = true; |
| 700 | } |
| 701 | else |
| 702 | { |
| 703 | /* No more tracks. */ |
| 704 | if (global_settings.beep) |
Steve Bavin | 081281f | 2006-09-26 07:19:43 +0000 | [diff] [blame] | 705 | pcmbuf_beep(1000, 100, 1000*global_settings.beep); |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 706 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Nicolas Pennequin | f730133 | 2008-02-26 17:15:33 +0000 | [diff] [blame] | 709 | void audio_next(void) |
| 710 | { |
| 711 | audio_skip(1); |
| 712 | } |
| 713 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 714 | void audio_prev(void) |
| 715 | { |
Nicolas Pennequin | f730133 | 2008-02-26 17:15:33 +0000 | [diff] [blame] | 716 | audio_skip(-1); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void audio_next_dir(void) |
| 720 | { |
| 721 | LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP 1"); |
Michael Sevakis | 4b90267 | 2006-12-19 16:50:07 +0000 | [diff] [blame] | 722 | queue_post(&audio_queue, Q_AUDIO_DIR_SKIP, 1); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | void audio_prev_dir(void) |
| 726 | { |
| 727 | LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP -1"); |
Michael Sevakis | 4b90267 | 2006-12-19 16:50:07 +0000 | [diff] [blame] | 728 | queue_post(&audio_queue, Q_AUDIO_DIR_SKIP, -1); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | void audio_pre_ff_rewind(void) |
| 732 | { |
| 733 | LOGFQUEUE("audio > audio Q_AUDIO_PRE_FF_REWIND"); |
| 734 | queue_post(&audio_queue, Q_AUDIO_PRE_FF_REWIND, 0); |
| 735 | } |
| 736 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 737 | void audio_ff_rewind(long newpos) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 738 | { |
| 739 | LOGFQUEUE("audio > audio Q_AUDIO_FF_REWIND"); |
Michael Sevakis | 4b90267 | 2006-12-19 16:50:07 +0000 | [diff] [blame] | 740 | queue_post(&audio_queue, Q_AUDIO_FF_REWIND, newpos); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | void audio_flush_and_reload_tracks(void) |
| 744 | { |
| 745 | LOGFQUEUE("audio > audio Q_AUDIO_FLUSH"); |
| 746 | queue_post(&audio_queue, Q_AUDIO_FLUSH, 0); |
| 747 | } |
| 748 | |
| 749 | void audio_error_clear(void) |
| 750 | { |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 751 | #ifdef AUDIO_HAVE_RECORDING |
| 752 | pcm_rec_error_clear(); |
| 753 | #endif |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | int audio_status(void) |
| 757 | { |
| 758 | int ret = 0; |
| 759 | |
| 760 | if (playing) |
| 761 | ret |= AUDIO_STATUS_PLAY; |
| 762 | |
| 763 | if (paused) |
| 764 | ret |= AUDIO_STATUS_PAUSE; |
| 765 | |
| 766 | #ifdef HAVE_RECORDING |
| 767 | /* Do this here for constitency with mpeg.c version */ |
| 768 | ret |= pcm_rec_status(); |
| 769 | #endif |
| 770 | |
| 771 | return ret; |
| 772 | } |
| 773 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 774 | int audio_get_file_pos(void) |
| 775 | { |
| 776 | return 0; |
| 777 | } |
| 778 | |
Nils Wallménius | 0bfa3e7 | 2007-08-01 08:50:44 +0000 | [diff] [blame] | 779 | #ifndef HAVE_FLASH_STORAGE |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 780 | void audio_set_buffer_margin(int setting) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 781 | { |
| 782 | static const int lookup[] = {5, 15, 30, 60, 120, 180, 300, 600}; |
| 783 | buffer_margin = lookup[setting]; |
Nicolas Pennequin | 6631e36 | 2007-11-05 18:19:38 +0000 | [diff] [blame] | 784 | logf("buffer margin: %ld", (long)buffer_margin); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 785 | set_filebuf_watermark(buffer_margin, 0); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 786 | } |
Nils Wallménius | 0bfa3e7 | 2007-08-01 08:50:44 +0000 | [diff] [blame] | 787 | #endif |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 788 | |
Steve Bavin | c9df8fd | 2008-03-28 11:24:24 +0000 | [diff] [blame] | 789 | /* Take necessary steps to enable or disable the crossfade setting */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 790 | void audio_set_crossfade(int enable) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 791 | { |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 792 | size_t offset; |
| 793 | bool was_playing; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 794 | size_t size; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 795 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 796 | /* Tell it the next setting to use */ |
| 797 | pcmbuf_crossfade_enable(enable); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 798 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 799 | /* Return if size hasn't changed or this is too early to determine |
| 800 | which in the second case there's no way we could be playing |
| 801 | anything at all */ |
| 802 | if (pcmbuf_is_same_size()) |
| 803 | { |
| 804 | /* This function is a copout and just syncs some variables - |
| 805 | to be removed at a later date */ |
| 806 | pcmbuf_crossfade_enable_finished(); |
| 807 | return; |
| 808 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 809 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 810 | offset = 0; |
| 811 | was_playing = playing; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 812 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 813 | /* Playback has to be stopped before changing the buffer size */ |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 814 | if (was_playing) |
| 815 | { |
| 816 | /* Store the track resume position */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 817 | offset = curtrack_id3.offset; |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 818 | gui_syncsplash(0, str(LANG_RESTARTING_PLAYBACK)); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 821 | /* Blast it - audio buffer will have to be setup again next time |
| 822 | something plays */ |
| 823 | audio_get_buffer(true, &size); |
Steve Bavin | c9a7862 | 2006-09-28 15:17:59 +0000 | [diff] [blame] | 824 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 825 | /* Restart playback if audio was running previously */ |
| 826 | if (was_playing) |
Steve Bavin | faace1e | 2006-11-01 19:19:36 +0000 | [diff] [blame] | 827 | audio_play(offset); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 830 | /* --- Routines called from multiple threads --- */ |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 831 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 832 | static void set_filebuf_watermark(int seconds, size_t max) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 833 | { |
| 834 | size_t bytes; |
| 835 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 836 | if (!filebuf) |
| 837 | return; /* Audio buffers not yet set up */ |
| 838 | |
Brandon Low | f906498 | 2007-11-04 05:36:35 +0000 | [diff] [blame] | 839 | bytes = seconds?MAX(curtrack_id3.bitrate * seconds * (1000/8), max):max; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 840 | bytes = MIN(bytes, filebuflen / 2); |
Brandon Low | 3379440 | 2007-11-05 17:48:21 +0000 | [diff] [blame] | 841 | buf_set_watermark(bytes); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 844 | const char *get_codec_filename(int cod_spec) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 845 | { |
| 846 | const char *fname; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 847 | |
| 848 | #ifdef HAVE_RECORDING |
| 849 | /* Can choose decoder or encoder if one available */ |
| 850 | int type = cod_spec & CODEC_TYPE_MASK; |
| 851 | int afmt = cod_spec & CODEC_AFMT_MASK; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 852 | |
| 853 | if ((unsigned)afmt >= AFMT_NUM_CODECS) |
| 854 | type = AFMT_UNKNOWN | (type & CODEC_TYPE_MASK); |
| 855 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 856 | fname = (type == CODEC_TYPE_ENCODER) ? |
| 857 | audio_formats[afmt].codec_enc_root_fn : |
| 858 | audio_formats[afmt].codec_root_fn; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 859 | |
| 860 | logf("%s: %d - %s", |
| 861 | (type == CODEC_TYPE_ENCODER) ? "Encoder" : "Decoder", |
| 862 | afmt, fname ? fname : "<unknown>"); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 863 | #else /* !HAVE_RECORDING */ |
| 864 | /* Always decoder */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 865 | if ((unsigned)cod_spec >= AFMT_NUM_CODECS) |
| 866 | cod_spec = AFMT_UNKNOWN; |
| 867 | fname = audio_formats[cod_spec].codec_root_fn; |
| 868 | logf("Codec: %d - %s", cod_spec, fname ? fname : "<unknown>"); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 869 | #endif /* HAVE_RECORDING */ |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 870 | |
| 871 | return fname; |
| 872 | } /* get_codec_filename */ |
| 873 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 874 | /* --- Codec thread --- */ |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 875 | static bool codec_pcmbuf_insert_callback( |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 876 | const void *ch1, const void *ch2, int count) |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 877 | { |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 878 | const char *src[2] = { ch1, ch2 }; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 879 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 880 | while (count > 0) |
Brandon Low | 522ec27 | 2006-04-25 18:21:05 +0000 | [diff] [blame] | 881 | { |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 882 | int out_count = dsp_output_count(ci.dsp, count); |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 883 | int inp_count; |
| 884 | char *dest; |
| 885 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 886 | /* Prevent audio from a previous track from playing */ |
| 887 | if (ci.new_track || ci.stop_codec) |
| 888 | return true; |
| 889 | |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 890 | while ((dest = pcmbuf_request_buffer(&out_count)) == NULL) |
Miika Pekkarinen | d43bff9 | 2006-07-29 17:25:31 +0000 | [diff] [blame] | 891 | { |
Michael Sevakis | fadbf0a | 2007-11-20 03:44:25 +0000 | [diff] [blame] | 892 | cancel_cpu_boost(); |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 893 | sleep(1); |
| 894 | if (ci.seek_time || ci.new_track || ci.stop_codec) |
| 895 | return true; |
Magnus Holmgren | 08761aa | 2005-07-16 12:25:28 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Linus Nielsen Feltzing | 591d289 | 2005-08-10 23:17:55 +0000 | [diff] [blame] | 898 | /* Get the real input_size for output_size bytes, guarding |
| 899 | * against resampling buffer overflows. */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 900 | inp_count = dsp_input_count(ci.dsp, out_count); |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 901 | |
Michael Sevakis | 09186e3 | 2007-02-16 12:01:35 +0000 | [diff] [blame] | 902 | if (inp_count <= 0) |
| 903 | return true; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 904 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 905 | /* Input size has grown, no error, just don't write more than length */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 906 | if (inp_count > count) |
| 907 | inp_count = count; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 908 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 909 | out_count = dsp_process(ci.dsp, dest, src, inp_count); |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 910 | |
Michael Sevakis | 09186e3 | 2007-02-16 12:01:35 +0000 | [diff] [blame] | 911 | if (out_count <= 0) |
| 912 | return true; |
| 913 | |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 914 | pcmbuf_write_complete(out_count); |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 915 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 916 | count -= inp_count; |
Brandon Low | 522ec27 | 2006-04-25 18:21:05 +0000 | [diff] [blame] | 917 | } |
Magnus Holmgren | 08761aa | 2005-07-16 12:25:28 +0000 | [diff] [blame] | 918 | |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 919 | return true; |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 920 | } /* codec_pcmbuf_insert_callback */ |
Miika Pekkarinen | d8cb703 | 2005-06-26 19:41:29 +0000 | [diff] [blame] | 921 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 922 | static void* codec_get_memory_callback(size_t *size) |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 923 | { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 924 | *size = MALLOC_BUFSIZE; |
Michael Sevakis | 1f8326b | 2007-03-11 00:37:58 +0000 | [diff] [blame] | 925 | return malloc_buf; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Nicolas Pennequin | 496027d | 2007-11-11 15:50:52 +0000 | [diff] [blame] | 928 | /* Between the codec and PCM track change, we need to keep updating the |
| 929 | "elapsed" value of the previous (to the codec, but current to the |
| 930 | user/PCM/WPS) track, so that the progressbar reaches the end. |
| 931 | During that transition, the WPS will display prevtrack_id3. */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 932 | static void codec_pcmbuf_position_callback(size_t size) ICODE_ATTR; |
| 933 | static void codec_pcmbuf_position_callback(size_t size) |
Miika Pekkarinen | d43bff9 | 2006-07-29 17:25:31 +0000 | [diff] [blame] | 934 | { |
Steve Bavin | 34193e5 | 2006-10-26 12:13:28 +0000 | [diff] [blame] | 935 | /* This is called from an ISR, so be quick */ |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 936 | unsigned int time = size * 1000 / 4 / NATIVE_FREQUENCY + |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 937 | prevtrack_id3.elapsed; |
| 938 | |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 939 | if (time >= prevtrack_id3.length) |
Miika Pekkarinen | d43bff9 | 2006-07-29 17:25:31 +0000 | [diff] [blame] | 940 | { |
Brandon Low | a3868d3 | 2006-01-21 22:42:44 +0000 | [diff] [blame] | 941 | pcmbuf_set_position_callback(NULL); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 942 | prevtrack_id3.elapsed = prevtrack_id3.length; |
| 943 | } |
| 944 | else |
| 945 | prevtrack_id3.elapsed = time; |
Brandon Low | a3868d3 | 2006-01-21 22:42:44 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 948 | static void codec_set_elapsed_callback(unsigned int value) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 949 | { |
| 950 | unsigned int latency; |
Brandon Low | d461a3e | 2006-04-23 23:27:11 +0000 | [diff] [blame] | 951 | if (ci.seek_time) |
| 952 | return; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 953 | |
Brandon Low | 8d5a660 | 2006-01-21 23:43:57 +0000 | [diff] [blame] | 954 | #ifdef AB_REPEAT_ENABLE |
| 955 | ab_position_report(value); |
| 956 | #endif |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 957 | |
Brandon Low | d461a3e | 2006-04-23 23:27:11 +0000 | [diff] [blame] | 958 | latency = pcmbuf_get_latency(); |
Brandon Low | 87484fc | 2006-04-11 20:41:04 +0000 | [diff] [blame] | 959 | if (value < latency) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 960 | curtrack_id3.elapsed = 0; |
| 961 | else if (value - latency > curtrack_id3.elapsed || |
| 962 | value - latency < curtrack_id3.elapsed - 2) |
Miika Pekkarinen | d43bff9 | 2006-07-29 17:25:31 +0000 | [diff] [blame] | 963 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 964 | curtrack_id3.elapsed = value - latency; |
Miika Pekkarinen | d43bff9 | 2006-07-29 17:25:31 +0000 | [diff] [blame] | 965 | } |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 968 | static void codec_set_offset_callback(size_t value) |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 969 | { |
Brandon Low | d461a3e | 2006-04-23 23:27:11 +0000 | [diff] [blame] | 970 | unsigned int latency; |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 971 | |
Brandon Low | d461a3e | 2006-04-23 23:27:11 +0000 | [diff] [blame] | 972 | if (ci.seek_time) |
| 973 | return; |
| 974 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 975 | latency = pcmbuf_get_latency() * curtrack_id3.bitrate / 8; |
Brandon Low | 87484fc | 2006-04-11 20:41:04 +0000 | [diff] [blame] | 976 | if (value < latency) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 977 | curtrack_id3.offset = 0; |
Brandon Low | 87484fc | 2006-04-11 20:41:04 +0000 | [diff] [blame] | 978 | else |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 979 | curtrack_id3.offset = value - latency; |
Ryan Jackson | d191756 | 2005-07-12 16:45:38 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 982 | static void codec_advance_buffer_counters(size_t amount) |
Miika Pekkarinen | d43bff9 | 2006-07-29 17:25:31 +0000 | [diff] [blame] | 983 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 984 | bufadvance(CUR_TI->audio_hid, amount); |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 985 | ci.curpos += amount; |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /* copy up-to size bytes into ptr and return the actual size copied */ |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 989 | static size_t codec_filebuf_callback(void *ptr, size_t size) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 990 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 991 | ssize_t copy_n; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 992 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 993 | if (ci.stop_codec || !playing) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 994 | return 0; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 995 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 996 | copy_n = bufread(CUR_TI->audio_hid, size, ptr); |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 997 | |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 998 | /* Nothing requested OR nothing left */ |
| 999 | if (copy_n == 0) |
| 1000 | return 0; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 1001 | |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 1002 | /* Update read and other position pointers */ |
Steve Bavin | a3087e4 | 2006-08-31 10:01:07 +0000 | [diff] [blame] | 1003 | codec_advance_buffer_counters(copy_n); |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 1004 | |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 1005 | /* Return the actual amount of data copied to the buffer */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1006 | return copy_n; |
Michael Sevakis | 4fc717a | 2006-08-28 22:38:41 +0000 | [diff] [blame] | 1007 | } /* codec_filebuf_callback */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1008 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1009 | static void* codec_request_buffer_callback(size_t *realsize, size_t reqsize) |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1010 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1011 | size_t copy_n = reqsize; |
| 1012 | ssize_t ret; |
| 1013 | void *ptr; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1014 | |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1015 | if (!playing) |
Miika Pekkarinen | d43bff9 | 2006-07-29 17:25:31 +0000 | [diff] [blame] | 1016 | { |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1017 | *realsize = 0; |
| 1018 | return NULL; |
| 1019 | } |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 1020 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1021 | ret = bufgetdata(CUR_TI->audio_hid, reqsize, &ptr); |
| 1022 | if (ret >= 0) |
| 1023 | copy_n = MIN((size_t)ret, reqsize); |
| 1024 | |
| 1025 | if (copy_n == 0) |
Miika Pekkarinen | d43bff9 | 2006-07-29 17:25:31 +0000 | [diff] [blame] | 1026 | { |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 1027 | *realsize = 0; |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1028 | return NULL; |
| 1029 | } |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 1030 | |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 1031 | *realsize = copy_n; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1032 | |
| 1033 | return ptr; |
Michael Sevakis | 4fc717a | 2006-08-28 22:38:41 +0000 | [diff] [blame] | 1034 | } /* codec_request_buffer_callback */ |
Linus Nielsen Feltzing | 1c497e6 | 2005-06-05 23:05:10 +0000 | [diff] [blame] | 1035 | |
Brandon Low | 62ccbbb | 2006-04-11 03:55:58 +0000 | [diff] [blame] | 1036 | static int get_codec_base_type(int type) |
| 1037 | { |
| 1038 | switch (type) { |
| 1039 | case AFMT_MPA_L1: |
| 1040 | case AFMT_MPA_L2: |
| 1041 | case AFMT_MPA_L3: |
| 1042 | return AFMT_MPA_L3; |
| 1043 | } |
| 1044 | |
| 1045 | return type; |
| 1046 | } |
| 1047 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1048 | static void codec_advance_buffer_callback(size_t amount) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1049 | { |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1050 | codec_advance_buffer_counters(amount); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1051 | codec_set_offset_callback(ci.curpos); |
| 1052 | } |
| 1053 | |
| 1054 | static void codec_advance_buffer_loc_callback(void *ptr) |
| 1055 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1056 | size_t amount = buf_get_offset(CUR_TI->audio_hid, ptr); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1057 | codec_advance_buffer_callback(amount); |
| 1058 | } |
| 1059 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1060 | static void codec_seek_complete_callback(void) |
| 1061 | { |
| 1062 | logf("seek_complete"); |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1063 | if (pcm_is_paused()) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1064 | { |
| 1065 | /* If this is not a seamless seek, clear the buffer */ |
| 1066 | pcmbuf_play_stop(); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 1067 | dsp_configure(ci.dsp, DSP_FLUSH, 0); |
Michael Sevakis | bbef13e | 2006-11-26 12:02:47 +0000 | [diff] [blame] | 1068 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1069 | /* If playback was not 'deliberately' paused, unpause now */ |
| 1070 | if (!paused) |
| 1071 | pcmbuf_pause(false); |
| 1072 | } |
| 1073 | ci.seek_time = 0; |
| 1074 | } |
| 1075 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1076 | static bool codec_seek_buffer_callback(size_t newpos) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1077 | { |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1078 | logf("codec_seek_buffer_callback"); |
| 1079 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1080 | int ret = bufseek(CUR_TI->audio_hid, newpos); |
| 1081 | if (ret == 0) { |
| 1082 | ci.curpos = newpos; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1083 | return true; |
| 1084 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1085 | else { |
| 1086 | return false; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1087 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1090 | static void codec_configure_callback(int setting, intptr_t value) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1091 | { |
| 1092 | switch (setting) { |
| 1093 | case CODEC_SET_FILEBUF_WATERMARK: |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1094 | set_filebuf_watermark(buffer_margin, value); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1095 | break; |
| 1096 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1097 | default: |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 1098 | if (!dsp_configure(ci.dsp, setting, value)) |
| 1099 | { logf("Illegal key:%d", setting); } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | static void codec_track_changed(void) |
| 1104 | { |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1105 | LOGFQUEUE("codec > audio Q_AUDIO_TRACK_CHANGED"); |
| 1106 | queue_post(&audio_queue, Q_AUDIO_TRACK_CHANGED, 0); |
| 1107 | } |
| 1108 | |
| 1109 | static void codec_pcmbuf_track_changed_callback(void) |
| 1110 | { |
| 1111 | pcmbuf_set_position_callback(NULL); |
Michael Sevakis | f38274f | 2008-02-28 22:37:46 +0000 | [diff] [blame] | 1112 | pcmbuf_callback_queue_post(Q_AUDIO_TRACK_CHANGED, 0); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static void codec_discard_codec_callback(void) |
| 1116 | { |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1117 | if (CUR_TI->codec_hid >= 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1118 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1119 | bufclose(CUR_TI->codec_hid); |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1120 | CUR_TI->codec_hid = -1; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1121 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1124 | static inline void codec_gapless_track_change(void) |
| 1125 | { |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 1126 | /* callback keeps the progress bar moving while the pcmbuf empties */ |
| 1127 | pcmbuf_set_position_callback(codec_pcmbuf_position_callback); |
| 1128 | /* set the pcmbuf callback for when the track really changes */ |
| 1129 | pcmbuf_set_event_handler(codec_pcmbuf_track_changed_callback); |
| 1130 | } |
| 1131 | |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1132 | static inline void codec_crossfade_track_change(void) |
| 1133 | { |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 1134 | /* Initiate automatic crossfade mode */ |
| 1135 | pcmbuf_crossfade_init(false); |
| 1136 | /* Notify the wps that the track change starts now */ |
| 1137 | codec_track_changed(); |
| 1138 | } |
| 1139 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1140 | static void codec_track_skip_done(bool was_manual) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1141 | { |
| 1142 | /* Manual track change (always crossfade or flush audio). */ |
| 1143 | if (was_manual) |
| 1144 | { |
| 1145 | pcmbuf_crossfade_init(true); |
| 1146 | LOGFQUEUE("codec > audio Q_AUDIO_TRACK_CHANGED"); |
| 1147 | queue_post(&audio_queue, Q_AUDIO_TRACK_CHANGED, 0); |
| 1148 | } |
| 1149 | /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */ |
| 1150 | else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active() |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1151 | && global_settings.crossfade != CROSSFADE_ENABLE_TRACKSKIP) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1152 | { |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1153 | if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP) |
Jonathan Gordon | 3bceded | 2006-12-19 01:26:37 +0000 | [diff] [blame] | 1154 | { |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 1155 | if (global_settings.playlist_shuffle) |
| 1156 | /* shuffle mode is on, so crossfade: */ |
| 1157 | codec_crossfade_track_change(); |
| 1158 | else |
| 1159 | /* shuffle mode is off, so do a gapless track change */ |
| 1160 | codec_gapless_track_change(); |
Jonathan Gordon | 3bceded | 2006-12-19 01:26:37 +0000 | [diff] [blame] | 1161 | } |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 1162 | else |
| 1163 | /* normal crossfade: */ |
| 1164 | codec_crossfade_track_change(); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1165 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1166 | else |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 1167 | /* normal gapless playback. */ |
| 1168 | codec_gapless_track_change(); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1171 | static bool codec_load_next_track(void) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1172 | { |
Michael Sevakis | e486ed0 | 2007-05-09 03:44:44 +0000 | [diff] [blame] | 1173 | intptr_t result = Q_CODEC_REQUEST_FAILED; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1174 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1175 | prev_track_elapsed = curtrack_id3.elapsed; |
Linus Nielsen Feltzing | da153da | 2006-10-19 09:42:58 +0000 | [diff] [blame] | 1176 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1177 | #ifdef AB_REPEAT_ENABLE |
| 1178 | ab_end_of_track_report(); |
| 1179 | #endif |
| 1180 | |
| 1181 | logf("Request new track"); |
| 1182 | |
| 1183 | if (ci.new_track == 0) |
| 1184 | { |
| 1185 | ci.new_track++; |
| 1186 | automatic_skip = true; |
| 1187 | } |
Michael Sevakis | 42b0208 | 2006-12-16 20:19:58 +0000 | [diff] [blame] | 1188 | |
Michael Sevakis | e486ed0 | 2007-05-09 03:44:44 +0000 | [diff] [blame] | 1189 | if (!ci.stop_codec) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1190 | { |
Michael Sevakis | e486ed0 | 2007-05-09 03:44:44 +0000 | [diff] [blame] | 1191 | trigger_cpu_boost(); |
| 1192 | LOGFQUEUE("codec >| audio Q_AUDIO_CHECK_NEW_TRACK"); |
| 1193 | result = queue_send(&audio_queue, Q_AUDIO_CHECK_NEW_TRACK, 0); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1194 | } |
Miika Pekkarinen | 7058752 | 2006-10-15 11:57:52 +0000 | [diff] [blame] | 1195 | |
Michael Sevakis | 42b0208 | 2006-12-16 20:19:58 +0000 | [diff] [blame] | 1196 | switch (result) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1197 | { |
| 1198 | case Q_CODEC_REQUEST_COMPLETE: |
Michael Sevakis | 42b0208 | 2006-12-16 20:19:58 +0000 | [diff] [blame] | 1199 | LOGFQUEUE("codec |< Q_CODEC_REQUEST_COMPLETE"); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1200 | codec_track_skip_done(!automatic_skip); |
| 1201 | return true; |
| 1202 | |
| 1203 | case Q_CODEC_REQUEST_FAILED: |
Michael Sevakis | 42b0208 | 2006-12-16 20:19:58 +0000 | [diff] [blame] | 1204 | LOGFQUEUE("codec |< Q_CODEC_REQUEST_FAILED"); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1205 | ci.new_track = 0; |
| 1206 | ci.stop_codec = true; |
Magnus Holmgren | c8d2a24 | 2008-03-04 18:56:37 +0000 | [diff] [blame] | 1207 | codec_requested_stop = true; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1208 | return false; |
| 1209 | |
| 1210 | default: |
Michael Sevakis | 42b0208 | 2006-12-16 20:19:58 +0000 | [diff] [blame] | 1211 | LOGFQUEUE("codec |< default"); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1212 | ci.stop_codec = true; |
Magnus Holmgren | c8d2a24 | 2008-03-04 18:56:37 +0000 | [diff] [blame] | 1213 | codec_requested_stop = true; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1214 | return false; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | static bool codec_request_next_track_callback(void) |
| 1219 | { |
| 1220 | int prev_codectype; |
| 1221 | |
| 1222 | if (ci.stop_codec || !playing) |
| 1223 | return false; |
| 1224 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1225 | prev_codectype = get_codec_base_type(curtrack_id3.codectype); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1226 | |
| 1227 | if (!codec_load_next_track()) |
| 1228 | return false; |
| 1229 | |
Nicolas Pennequin | 3c96259 | 2008-04-14 10:52:16 +0000 | [diff] [blame] | 1230 | /* Seek to the beginning of the new track because if the struct |
| 1231 | mp3entry was buffered, "elapsed" might not be zero (if the track has |
| 1232 | been played already but not unbuffered) */ |
| 1233 | codec_seek_buffer_callback(curtrack_id3.first_frame_offset); |
| 1234 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1235 | /* Check if the next codec is the same file. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1236 | if (prev_codectype == get_codec_base_type(curtrack_id3.codectype)) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1237 | { |
| 1238 | logf("New track loaded"); |
| 1239 | codec_discard_codec_callback(); |
| 1240 | return true; |
| 1241 | } |
| 1242 | else |
| 1243 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1244 | logf("New codec:%d/%d", curtrack_id3.codectype, prev_codectype); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1245 | return false; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | static void codec_thread(void) |
| 1250 | { |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 1251 | struct queue_event ev; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1252 | int status; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1253 | |
| 1254 | while (1) { |
| 1255 | status = 0; |
Andree Buschmann | 63acf3e | 2008-03-30 10:08:08 +0000 | [diff] [blame] | 1256 | |
| 1257 | if (!pcmbuf_is_crossfade_active()) { |
| 1258 | cancel_cpu_boost(); |
| 1259 | } |
| 1260 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1261 | queue_wait(&codec_queue, &ev); |
Magnus Holmgren | c8d2a24 | 2008-03-04 18:56:37 +0000 | [diff] [blame] | 1262 | codec_requested_stop = false; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1263 | |
| 1264 | switch (ev.id) { |
| 1265 | case Q_CODEC_LOAD_DISK: |
| 1266 | LOGFQUEUE("codec < Q_CODEC_LOAD_DISK"); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1267 | queue_reply(&codec_queue, 1); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1268 | audio_codec_loaded = true; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1269 | ci.stop_codec = false; |
| 1270 | status = codec_load_file((const char *)ev.data, &ci); |
Steve Bavin | 2129d0f | 2007-08-02 13:19:35 +0000 | [diff] [blame] | 1271 | break; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1272 | |
| 1273 | case Q_CODEC_LOAD: |
| 1274 | LOGFQUEUE("codec < Q_CODEC_LOAD"); |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1275 | if (CUR_TI->codec_hid < 0) { |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1276 | logf("Codec slot is empty!"); |
| 1277 | /* Wait for the pcm buffer to go empty */ |
| 1278 | while (pcm_is_playing()) |
| 1279 | yield(); |
| 1280 | /* This must be set to prevent an infinite loop */ |
| 1281 | ci.stop_codec = true; |
| 1282 | LOGFQUEUE("codec > codec Q_AUDIO_PLAY"); |
| 1283 | queue_post(&codec_queue, Q_AUDIO_PLAY, 0); |
Steve Bavin | 2129d0f | 2007-08-02 13:19:35 +0000 | [diff] [blame] | 1284 | break; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | audio_codec_loaded = true; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1288 | ci.stop_codec = false; |
Brandon Low | 86919f4 | 2007-11-03 17:55:29 +0000 | [diff] [blame] | 1289 | status = codec_load_buf(CUR_TI->codec_hid, &ci); |
Steve Bavin | 2129d0f | 2007-08-02 13:19:35 +0000 | [diff] [blame] | 1290 | break; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1291 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 1292 | #ifdef AUDIO_HAVE_RECORDING |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1293 | case Q_ENCODER_LOAD_DISK: |
| 1294 | LOGFQUEUE("codec < Q_ENCODER_LOAD_DISK"); |
Michael Sevakis | 9d56f2d | 2006-09-17 18:52:31 +0000 | [diff] [blame] | 1295 | audio_codec_loaded = false; /* Not audio codec! */ |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 1296 | logf("loading encoder"); |
Michael Sevakis | 598629c | 2007-03-04 04:16:53 +0000 | [diff] [blame] | 1297 | ci.stop_encoder = false; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1298 | status = codec_load_file((const char *)ev.data, &ci); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 1299 | logf("encoder stopped"); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1300 | break; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 1301 | #endif /* AUDIO_HAVE_RECORDING */ |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1302 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1303 | default: |
| 1304 | LOGFQUEUE("codec < default"); |
| 1305 | } |
| 1306 | |
| 1307 | if (audio_codec_loaded) |
| 1308 | { |
| 1309 | if (ci.stop_codec) |
| 1310 | { |
| 1311 | status = CODEC_OK; |
| 1312 | if (!playing) |
| 1313 | pcmbuf_play_stop(); |
Steve Bavin | faace1e | 2006-11-01 19:19:36 +0000 | [diff] [blame] | 1314 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1315 | } |
| 1316 | audio_codec_loaded = false; |
| 1317 | } |
| 1318 | |
| 1319 | switch (ev.id) { |
| 1320 | case Q_CODEC_LOAD_DISK: |
| 1321 | case Q_CODEC_LOAD: |
| 1322 | LOGFQUEUE("codec < Q_CODEC_LOAD"); |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1323 | if (playing) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1324 | { |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1325 | if (ci.new_track || status != CODEC_OK) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1326 | { |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1327 | if (!ci.new_track) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1328 | { |
| 1329 | logf("Codec failure"); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 1330 | gui_syncsplash(HZ*2, "Codec failure"); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1331 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1332 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1333 | if (!codec_load_next_track()) |
| 1334 | { |
Magnus Holmgren | c8d2a24 | 2008-03-04 18:56:37 +0000 | [diff] [blame] | 1335 | LOGFQUEUE("codec > audio Q_AUDIO_STOP"); |
| 1336 | /* End of playlist */ |
| 1337 | queue_post(&audio_queue, Q_AUDIO_STOP, 0); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | } |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1340 | } |
| 1341 | else |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1342 | { |
| 1343 | logf("Codec finished"); |
| 1344 | if (ci.stop_codec) |
| 1345 | { |
| 1346 | /* Wait for the audio to stop playing before |
| 1347 | * triggering the WPS exit */ |
| 1348 | while(pcm_is_playing()) |
Steve Bavin | 004ee26 | 2006-10-19 07:43:07 +0000 | [diff] [blame] | 1349 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1350 | curtrack_id3.elapsed = |
| 1351 | curtrack_id3.length - pcmbuf_get_latency(); |
Steve Bavin | 2baaac1 | 2006-10-19 08:55:24 +0000 | [diff] [blame] | 1352 | sleep(1); |
Steve Bavin | 004ee26 | 2006-10-19 07:43:07 +0000 | [diff] [blame] | 1353 | } |
Magnus Holmgren | c8d2a24 | 2008-03-04 18:56:37 +0000 | [diff] [blame] | 1354 | |
| 1355 | if (codec_requested_stop) |
| 1356 | { |
| 1357 | LOGFQUEUE("codec > audio Q_AUDIO_STOP"); |
| 1358 | queue_post(&audio_queue, Q_AUDIO_STOP, 0); |
| 1359 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1360 | break; |
| 1361 | } |
| 1362 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1363 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1364 | if (CUR_TI->codec_hid >= 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1365 | { |
| 1366 | LOGFQUEUE("codec > codec Q_CODEC_LOAD"); |
| 1367 | queue_post(&codec_queue, Q_CODEC_LOAD, 0); |
| 1368 | } |
Steve Bavin | c985903 | 2007-10-29 13:57:29 +0000 | [diff] [blame] | 1369 | else |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1370 | { |
Brandon Low | be95ff4 | 2007-01-26 18:35:49 +0000 | [diff] [blame] | 1371 | const char *codec_fn = |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1372 | get_codec_filename(curtrack_id3.codectype); |
Nicolas Pennequin | ca243ce | 2008-01-09 20:37:36 +0000 | [diff] [blame] | 1373 | if (codec_fn) |
| 1374 | { |
| 1375 | LOGFQUEUE("codec > codec Q_CODEC_LOAD_DISK"); |
| 1376 | queue_post(&codec_queue, Q_CODEC_LOAD_DISK, |
| 1377 | (intptr_t)codec_fn); |
| 1378 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | break; |
| 1382 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 1383 | #ifdef AUDIO_HAVE_RECORDING |
| 1384 | case Q_ENCODER_LOAD_DISK: |
| 1385 | LOGFQUEUE("codec < Q_ENCODER_LOAD_DISK"); |
| 1386 | |
| 1387 | if (status == CODEC_OK) |
| 1388 | break; |
| 1389 | |
| 1390 | logf("Encoder failure"); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 1391 | gui_syncsplash(HZ*2, "Encoder failure"); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 1392 | |
| 1393 | if (ci.enc_codec_loaded < 0) |
| 1394 | break; |
| 1395 | |
| 1396 | logf("Encoder failed to load"); |
| 1397 | ci.enc_codec_loaded = -1; |
| 1398 | break; |
| 1399 | #endif /* AUDIO_HAVE_RECORDING */ |
| 1400 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1401 | default: |
| 1402 | LOGFQUEUE("codec < default"); |
| 1403 | |
| 1404 | } /* end switch */ |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | |
Nicolas Pennequin | 33f522d | 2008-04-03 17:51:53 +0000 | [diff] [blame] | 1409 | /* --- Buffering callbacks --- */ |
| 1410 | |
| 1411 | static void buffering_low_buffer_callback(void *data) |
| 1412 | { |
| 1413 | (void)data; |
| 1414 | logf("low buffer callback"); |
| 1415 | |
| 1416 | if (filling == STATE_FULL) { |
| 1417 | /* force a refill */ |
| 1418 | LOGFQUEUE("buffering > audio Q_AUDIO_FILL_BUFFER"); |
| 1419 | queue_post(&audio_queue, Q_AUDIO_FILL_BUFFER, 0); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | static void buffering_handle_rebuffer_callback(void *data) |
| 1424 | { |
| 1425 | (void)data; |
| 1426 | LOGFQUEUE("audio >| audio Q_AUDIO_FLUSH"); |
| 1427 | queue_post(&audio_queue, Q_AUDIO_FLUSH, 0); |
| 1428 | } |
| 1429 | |
| 1430 | static void buffering_handle_finished_callback(int *data) |
| 1431 | { |
| 1432 | logf("handle %d finished buffering", *data); |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1433 | |
| 1434 | if (*data == tracks[track_widx].id3_hid) |
| 1435 | { |
| 1436 | /* The metadata handle for the last loaded track has been buffered. |
| 1437 | We can ask the audio thread to load the rest of the track's data. */ |
| 1438 | LOGFQUEUE("audio >| audio Q_AUDIO_FINISH_LOAD"); |
| 1439 | queue_post(&audio_queue, Q_AUDIO_FINISH_LOAD, 0); |
| 1440 | } |
| 1441 | else |
| 1442 | { |
| 1443 | /* This is most likely an audio handle, so we strip the useless |
| 1444 | trailing tags that are left. */ |
| 1445 | strip_tags(*data); |
| 1446 | } |
Nicolas Pennequin | 33f522d | 2008-04-03 17:51:53 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1450 | /* --- Audio thread --- */ |
| 1451 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1452 | static bool audio_have_tracks(void) |
| 1453 | { |
Nicolas Pennequin | d3b8245 | 2007-11-19 16:37:52 +0000 | [diff] [blame] | 1454 | return (audio_track_count() != 0); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Nicolas Pennequin | d3b8245 | 2007-11-19 16:37:52 +0000 | [diff] [blame] | 1457 | static int audio_free_track_count(void) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1458 | { |
Nicolas Pennequin | d3b8245 | 2007-11-19 16:37:52 +0000 | [diff] [blame] | 1459 | /* Used tracks + free tracks adds up to MAX_TRACK - 1 */ |
| 1460 | return MAX_TRACK - 1 - audio_track_count(); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1461 | } |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1462 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1463 | int audio_track_count(void) |
| 1464 | { |
Nicolas Pennequin | d3b8245 | 2007-11-19 16:37:52 +0000 | [diff] [blame] | 1465 | /* Calculate difference from track_ridx to track_widx |
| 1466 | * taking into account a possible wrap-around. */ |
| 1467 | return (MAX_TRACK + track_widx - track_ridx) & MAX_TRACK_MASK; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Steve Bavin | 73e2f7b | 2006-10-12 16:51:22 +0000 | [diff] [blame] | 1470 | long audio_filebufused(void) |
Jens Arnold | 8c6920e | 2008-03-08 23:50:55 +0000 | [diff] [blame] | 1471 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1472 | return (long) buf_used(); |
Brandon Low | c95044a | 2006-04-13 14:17:12 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Nicolas Pennequin | 2ff5132 | 2007-11-19 17:30:46 +0000 | [diff] [blame] | 1475 | /* Update track info after successful a codec track change */ |
Brandon Low | 1d41f77 | 2006-04-13 21:47:00 +0000 | [diff] [blame] | 1476 | static void audio_update_trackinfo(void) |
Brandon Low | c95044a | 2006-04-13 14:17:12 +0000 | [diff] [blame] | 1477 | { |
Nicolas Pennequin | 2ff5132 | 2007-11-19 17:30:46 +0000 | [diff] [blame] | 1478 | /* Load the curent track's metadata into curtrack_id3 */ |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1479 | if (CUR_TI->id3_hid >= 0) |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1480 | copy_mp3entry(&curtrack_id3, bufgetid3(CUR_TI->id3_hid)); |
| 1481 | |
Nicolas Pennequin | 2ff5132 | 2007-11-19 17:30:46 +0000 | [diff] [blame] | 1482 | /* Reset current position */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1483 | curtrack_id3.elapsed = 0; |
| 1484 | curtrack_id3.offset = 0; |
Nicolas Pennequin | 2ff5132 | 2007-11-19 17:30:46 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* Update the codec API */ |
| 1487 | ci.filesize = CUR_TI->filesize; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1488 | ci.id3 = &curtrack_id3; |
Brandon Low | c95044a | 2006-04-13 14:17:12 +0000 | [diff] [blame] | 1489 | ci.curpos = 0; |
Steve Bavin | 5f70ad4 | 2006-10-12 15:54:36 +0000 | [diff] [blame] | 1490 | ci.taginfo_ready = &CUR_TI->taginfo_ready; |
Brandon Low | c95044a | 2006-04-13 14:17:12 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Nicolas Pennequin | c44d2bd | 2007-11-22 00:20:37 +0000 | [diff] [blame] | 1493 | /* Clear tracks between write and read, non inclusive */ |
Miika Pekkarinen | d8204fa | 2008-03-08 08:05:29 +0000 | [diff] [blame] | 1494 | static void audio_clear_track_entries(void) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1495 | { |
| 1496 | int cur_idx = track_widx; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1497 | |
Miika Pekkarinen | d8204fa | 2008-03-08 08:05:29 +0000 | [diff] [blame] | 1498 | logf("Clearing tracks:%d/%d", track_ridx, track_widx); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1499 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1500 | /* Loop over all tracks from write-to-read */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1501 | while (1) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1502 | { |
Nicolas Pennequin | cfa6495 | 2007-11-19 15:52:25 +0000 | [diff] [blame] | 1503 | cur_idx = (cur_idx + 1) & MAX_TRACK_MASK; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1504 | |
| 1505 | if (cur_idx == track_ridx) |
| 1506 | break; |
| 1507 | |
Miika Pekkarinen | d8204fa | 2008-03-08 08:05:29 +0000 | [diff] [blame] | 1508 | clear_track_info(&tracks[cur_idx]); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1509 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Nicolas Pennequin | c44d2bd | 2007-11-22 00:20:37 +0000 | [diff] [blame] | 1512 | /* Clear all tracks */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1513 | static bool audio_release_tracks(void) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1514 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1515 | int i, cur_idx; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1516 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1517 | logf("releasing all tracks"); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1518 | |
Nicolas Pennequin | aef008b | 2007-11-05 18:46:25 +0000 | [diff] [blame] | 1519 | for(i = 0; i < MAX_TRACK; i++) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1520 | { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1521 | cur_idx = (track_ridx + i) & MAX_TRACK_MASK; |
| 1522 | if (!clear_track_info(&tracks[cur_idx])) |
| 1523 | return false; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1524 | } |
Mark Arigo | 9b0ef15 | 2006-11-14 15:48:20 +0000 | [diff] [blame] | 1525 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1526 | return true; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1529 | static bool audio_loadcodec(bool start_play) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1530 | { |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1531 | int prev_track; |
| 1532 | char codec_path[MAX_PATH]; /* Full path to codec */ |
Nicolas Pennequin | 2cf6345 | 2008-04-16 16:41:35 +0000 | [diff] [blame] | 1533 | const struct mp3entry *id3, *prev_id3; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1534 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1535 | if (tracks[track_widx].id3_hid < 0) { |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1536 | return false; |
| 1537 | } |
| 1538 | |
Nicolas Pennequin | 2cf6345 | 2008-04-16 16:41:35 +0000 | [diff] [blame] | 1539 | id3 = bufgetid3(tracks[track_widx].id3_hid); |
| 1540 | if (!id3) |
| 1541 | return false; |
| 1542 | |
| 1543 | const char *codec_fn = get_codec_filename(id3->codectype); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1544 | if (codec_fn == NULL) |
| 1545 | return false; |
| 1546 | |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1547 | tracks[track_widx].codec_hid = -1; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1548 | |
| 1549 | if (start_play) |
| 1550 | { |
| 1551 | /* Load the codec directly from disk and save some memory. */ |
| 1552 | track_ridx = track_widx; |
Steve Bavin | 5f70ad4 | 2006-10-12 15:54:36 +0000 | [diff] [blame] | 1553 | ci.filesize = CUR_TI->filesize; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1554 | ci.id3 = &curtrack_id3; |
Steve Bavin | 5f70ad4 | 2006-10-12 15:54:36 +0000 | [diff] [blame] | 1555 | ci.taginfo_ready = &CUR_TI->taginfo_ready; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1556 | ci.curpos = 0; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1557 | LOGFQUEUE("codec > codec Q_CODEC_LOAD_DISK"); |
Michael Sevakis | 4b90267 | 2006-12-19 16:50:07 +0000 | [diff] [blame] | 1558 | queue_post(&codec_queue, Q_CODEC_LOAD_DISK, (intptr_t)codec_fn); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1559 | return true; |
| 1560 | } |
| 1561 | else |
| 1562 | { |
| 1563 | /* If we already have another track than this one buffered */ |
Steve Bavin | 7707f7f | 2007-10-29 13:55:34 +0000 | [diff] [blame] | 1564 | if (track_widx != track_ridx) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1565 | { |
| 1566 | prev_track = (track_widx - 1) & MAX_TRACK_MASK; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1567 | |
Nicolas Pennequin | 2cf6345 | 2008-04-16 16:41:35 +0000 | [diff] [blame] | 1568 | id3 = bufgetid3(tracks[track_widx].id3_hid); |
| 1569 | prev_id3 = bufgetid3(tracks[prev_track].id3_hid); |
| 1570 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1571 | /* If the previous codec is the same as this one, there is no need |
| 1572 | * to put another copy of it on the file buffer */ |
Nicolas Pennequin | 2cf6345 | 2008-04-16 16:41:35 +0000 | [diff] [blame] | 1573 | if (id3 && prev_id3 && |
| 1574 | get_codec_base_type(id3->codectype) == |
| 1575 | get_codec_base_type(prev_id3->codectype) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1576 | && audio_codec_loaded) |
| 1577 | { |
| 1578 | logf("Reusing prev. codec"); |
| 1579 | return true; |
| 1580 | } |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | codec_get_full_path(codec_path, codec_fn); |
| 1585 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1586 | tracks[track_widx].codec_hid = bufopen(codec_path, 0, TYPE_CODEC); |
| 1587 | if (tracks[track_widx].codec_hid < 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1588 | return false; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1589 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1590 | logf("Loaded codec"); |
Brandon Low | c2e1cc1 | 2007-11-03 21:48:08 +0000 | [diff] [blame] | 1591 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1592 | return true; |
| 1593 | } |
| 1594 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1595 | /* Load metadata for the next track (with bufopen). The rest of the track |
| 1596 | loading will be handled by audio_finish_load_track once the metadata has been |
| 1597 | actually loaded by the buffering thread. */ |
| 1598 | static bool audio_load_track(size_t offset, bool start_play) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1599 | { |
Steve Bavin | 38ed901 | 2008-03-25 19:26:00 +0000 | [diff] [blame] | 1600 | const char *trackname; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1601 | int fd = -1; |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1602 | |
| 1603 | start_play_g = start_play; /* will be read by audio_finish_load_track */ |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1604 | |
| 1605 | /* Stop buffer filling if there is no free track entries. |
| 1606 | Don't fill up the last track entry (we wan't to store next track |
| 1607 | metadata there). */ |
Nicolas Pennequin | d3b8245 | 2007-11-19 16:37:52 +0000 | [diff] [blame] | 1608 | if (!audio_free_track_count()) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1609 | { |
| 1610 | logf("No free tracks"); |
| 1611 | return false; |
| 1612 | } |
| 1613 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1614 | last_peek_offset++; |
Magnus Holmgren | 68f9e6b | 2008-02-23 17:46:33 +0000 | [diff] [blame] | 1615 | tracks[track_widx].taginfo_ready = false; |
| 1616 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1617 | logf("Buffering track:%d/%d", track_widx, track_ridx); |
| 1618 | /* Get track name from current playlist read position. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1619 | while ((trackname = playlist_peek(last_peek_offset)) != NULL) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1620 | { |
| 1621 | /* Handle broken playlists. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1622 | fd = open(trackname, O_RDONLY); |
| 1623 | if (fd < 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1624 | { |
| 1625 | logf("Open failed"); |
| 1626 | /* Skip invalid entry from playlist. */ |
| 1627 | playlist_skip_entry(NULL, last_peek_offset); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1628 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1629 | else |
| 1630 | break; |
| 1631 | } |
| 1632 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1633 | if (!trackname) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1634 | { |
| 1635 | logf("End-of-playlist"); |
Nicolas Pennequin | 9446226 | 2007-11-18 13:41:04 +0000 | [diff] [blame] | 1636 | memset(&lasttrack_id3, 0, sizeof(struct mp3entry)); |
Nicolas Pennequin | f68147e | 2008-03-28 20:18:53 +0000 | [diff] [blame] | 1637 | filling = STATE_FINISHED; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1638 | return false; |
| 1639 | } |
| 1640 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1641 | tracks[track_widx].filesize = filesize(fd); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1642 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1643 | if (offset > tracks[track_widx].filesize) |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1644 | offset = 0; |
Nicolas Pennequin | 659fe5a | 2008-01-08 23:48:51 +0000 | [diff] [blame] | 1645 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1646 | /* Set default values */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1647 | if (start_play) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1648 | { |
Brandon Low | 3379440 | 2007-11-05 17:48:21 +0000 | [diff] [blame] | 1649 | buf_set_watermark(AUDIO_DEFAULT_WATERMARK); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 1650 | dsp_configure(ci.dsp, DSP_RESET, 0); |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1651 | track_changed = true; |
| 1652 | playlist_update_resume_info(audio_current_track()); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /* Get track metadata if we don't already have it. */ |
Brandon Low | 31c1164 | 2007-11-04 19:01:02 +0000 | [diff] [blame] | 1656 | if (tracks[track_widx].id3_hid < 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1657 | { |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1658 | tracks[track_widx].id3_hid = bufopen(trackname, 0, TYPE_ID3); |
| 1659 | |
| 1660 | if (tracks[track_widx].id3_hid < 0) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1661 | { |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1662 | /* Buffer is full. */ |
| 1663 | get_metadata(&lasttrack_id3, fd, trackname); |
| 1664 | last_peek_offset--; |
Nicolas Pennequin | 9d05e50 | 2007-10-30 18:01:49 +0000 | [diff] [blame] | 1665 | close(fd); |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1666 | logf("buffer is full for now"); |
| 1667 | filling = STATE_FULL; |
| 1668 | return false; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1671 | if (track_widx == track_ridx) |
| 1672 | { |
| 1673 | buf_request_buffer_handle(tracks[track_widx].id3_hid); |
| 1674 | copy_mp3entry(&curtrack_id3, bufgetid3(tracks[track_widx].id3_hid)); |
| 1675 | curtrack_id3.offset = offset; |
| 1676 | } |
| 1677 | |
| 1678 | if (start_play) |
| 1679 | { |
| 1680 | track_changed = true; |
| 1681 | playlist_update_resume_info(audio_current_track()); |
| 1682 | } |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Nicolas Pennequin | b36d3c0 | 2008-04-15 19:20:57 +0000 | [diff] [blame] | 1685 | close(fd); |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1686 | return true; |
| 1687 | } |
| 1688 | |
| 1689 | /* Second part of the track loading: We now have the metadata available, so we |
| 1690 | can load the codec, the album art and finally the audio data. |
| 1691 | This is called on the audio thread after the buffering thread calls the |
| 1692 | buffering_handle_finished_callback callback. */ |
| 1693 | static void audio_finish_load_track(void) |
| 1694 | { |
| 1695 | char msgbuf[80]; |
| 1696 | size_t file_offset = 0; |
| 1697 | size_t offset = 0; |
| 1698 | bool start_play = start_play_g; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1699 | |
| 1700 | #if 0 |
Nicolas Pennequin | 9f4bd87 | 2007-02-14 14:40:24 +0000 | [diff] [blame] | 1701 | if (cuesheet_is_enabled() && tracks[track_widx].id3.cuesheet_type == 1) |
| 1702 | { |
| 1703 | char cuepath[MAX_PATH]; |
Nicolas Pennequin | 9f4bd87 | 2007-02-14 14:40:24 +0000 | [diff] [blame] | 1704 | |
| 1705 | struct cuesheet *cue = start_play ? curr_cue : temp_cue; |
| 1706 | |
Nicolas Pennequin | 6579818 | 2007-05-28 23:18:31 +0000 | [diff] [blame] | 1707 | if (look_for_cuesheet_file(trackname, cuepath) && |
| 1708 | parse_cuesheet(cuepath, cue)) |
Nicolas Pennequin | 9f4bd87 | 2007-02-14 14:40:24 +0000 | [diff] [blame] | 1709 | { |
| 1710 | strcpy((cue)->audio_filename, trackname); |
| 1711 | if (start_play) |
| 1712 | cue_spoof_id3(curr_cue, &tracks[track_widx].id3); |
| 1713 | } |
| 1714 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1715 | #endif |
Nicolas Pennequin | 9f4bd87 | 2007-02-14 14:40:24 +0000 | [diff] [blame] | 1716 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1717 | if (tracks[track_widx].id3_hid < 0) { |
| 1718 | logf("no metatdata"); |
| 1719 | return; |
| 1720 | } |
| 1721 | |
Nicolas Pennequin | 95db8f8 | 2007-11-11 20:09:13 +0000 | [diff] [blame] | 1722 | struct mp3entry *track_id3; |
| 1723 | |
| 1724 | if (track_widx == track_ridx) |
| 1725 | track_id3 = &curtrack_id3; |
| 1726 | else |
| 1727 | track_id3 = bufgetid3(tracks[track_widx].id3_hid); |
| 1728 | |
Nicolas Pennequin | de026dc | 2008-04-16 16:18:05 +0000 | [diff] [blame] | 1729 | if (track_id3->length == 0 && track_id3->filesize == 0) |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1730 | { |
| 1731 | logf("audio_finish_load_track: invalid metadata"); |
| 1732 | |
| 1733 | /* Invalid metadata */ |
| 1734 | bufclose(tracks[track_widx].id3_hid); |
| 1735 | tracks[track_widx].id3_hid = -1; |
| 1736 | |
| 1737 | /* Skip invalid entry from playlist. */ |
| 1738 | playlist_skip_entry(NULL, last_peek_offset--); |
| 1739 | |
| 1740 | /* load next track */ |
| 1741 | LOGFQUEUE("audio > audio Q_AUDIO_FILL_BUFFER %d", (int)start_play); |
| 1742 | queue_post(&audio_queue, Q_AUDIO_FILL_BUFFER, start_play); |
| 1743 | |
| 1744 | return; |
| 1745 | } |
| 1746 | |
Nicolas Pennequin | 95db8f8 | 2007-11-11 20:09:13 +0000 | [diff] [blame] | 1747 | #ifdef HAVE_ALBUMART |
Nicolas Pennequin | e71bc67 | 2007-11-11 21:27:18 +0000 | [diff] [blame] | 1748 | if (tracks[track_widx].aa_hid < 0 && gui_sync_wps_uses_albumart()) |
Nicolas Pennequin | 95db8f8 | 2007-11-11 20:09:13 +0000 | [diff] [blame] | 1749 | { |
| 1750 | char aa_path[MAX_PATH]; |
| 1751 | if (find_albumart(track_id3, aa_path, sizeof(aa_path))) |
| 1752 | tracks[track_widx].aa_hid = bufopen(aa_path, 0, TYPE_BITMAP); |
| 1753 | } |
| 1754 | #endif |
| 1755 | |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1756 | /* Load the codec. */ |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1757 | if (!audio_loadcodec(start_play)) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1758 | { |
Brandon Low | 0c97493 | 2007-11-03 21:55:24 +0000 | [diff] [blame] | 1759 | if (tracks[track_widx].codec_hid == ERR_BUFFER_FULL) |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1760 | { |
Brandon Low | 08a4cea | 2006-10-17 12:56:22 +0000 | [diff] [blame] | 1761 | /* No space for codec on buffer, not an error */ |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1762 | return; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1763 | } |
Brandon Low | 08a4cea | 2006-10-17 12:56:22 +0000 | [diff] [blame] | 1764 | |
| 1765 | /* This is an error condition, either no codec was found, or reading |
| 1766 | * the codec file failed part way through, either way, skip the track */ |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1767 | snprintf(msgbuf, sizeof(msgbuf)-1, "No codec for: %s", track_id3->path); |
Brandon Low | 08a4cea | 2006-10-17 12:56:22 +0000 | [diff] [blame] | 1768 | /* We should not use gui_syncplash from audio thread! */ |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 1769 | gui_syncsplash(HZ*2, msgbuf); |
Brandon Low | 08a4cea | 2006-10-17 12:56:22 +0000 | [diff] [blame] | 1770 | /* Skip invalid entry from playlist. */ |
| 1771 | playlist_skip_entry(NULL, last_peek_offset); |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1772 | return; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1775 | track_id3->elapsed = 0; |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1776 | offset = track_id3->offset; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1777 | |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1778 | enum data_type type = TYPE_PACKET_AUDIO; |
| 1779 | |
| 1780 | switch (track_id3->codectype) { |
| 1781 | case AFMT_MPA_L1: |
| 1782 | case AFMT_MPA_L2: |
| 1783 | case AFMT_MPA_L3: |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1784 | if (offset > 0) { |
| 1785 | file_offset = offset; |
| 1786 | track_id3->offset = offset; |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1787 | } |
| 1788 | break; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1789 | |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1790 | case AFMT_WAVPACK: |
| 1791 | if (offset > 0) { |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1792 | file_offset = offset; |
| 1793 | track_id3->offset = offset; |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1794 | track_id3->elapsed = track_id3->length / 2; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1795 | } |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1796 | break; |
| 1797 | |
| 1798 | case AFMT_OGG_VORBIS: |
| 1799 | case AFMT_SPEEX: |
| 1800 | case AFMT_FLAC: |
| 1801 | case AFMT_PCM_WAV: |
| 1802 | case AFMT_A52: |
| 1803 | case AFMT_AAC: |
| 1804 | case AFMT_MPC: |
| 1805 | case AFMT_APE: |
Michael Giacomelli | 9a946f6 | 2008-04-12 19:52:31 +0000 | [diff] [blame] | 1806 | case AFMT_WMA: |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 1807 | if (offset > 0) |
| 1808 | track_id3->offset = offset; |
Brandon Low | 11a3661 | 2007-11-03 06:21:32 +0000 | [diff] [blame] | 1809 | break; |
| 1810 | |
| 1811 | case AFMT_NSF: |
| 1812 | case AFMT_SPC: |
| 1813 | case AFMT_SID: |
| 1814 | logf("Loading atomic %d",track_id3->codectype); |
| 1815 | type = TYPE_ATOMIC_AUDIO; |
| 1816 | break; |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1817 | } |
Nicolas Pennequin | 3e3c43c | 2007-10-25 21:27:45 +0000 | [diff] [blame] | 1818 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1819 | logf("alt:%s", track_id3->path); |
Steve Bavin | 3cc46e7 | 2006-09-01 07:59:31 +0000 | [diff] [blame] | 1820 | |
Brandon Low | 1df1e7b | 2007-11-05 16:02:12 +0000 | [diff] [blame] | 1821 | if (file_offset > AUDIO_REBUFFER_GUESS_SIZE) |
| 1822 | file_offset -= AUDIO_REBUFFER_GUESS_SIZE; |
| 1823 | else if (track_id3->first_frame_offset) |
Brandon Low | d02b5c7 | 2007-11-05 01:56:12 +0000 | [diff] [blame] | 1824 | file_offset = track_id3->first_frame_offset; |
Brandon Low | 1df1e7b | 2007-11-05 16:02:12 +0000 | [diff] [blame] | 1825 | else |
| 1826 | file_offset = 0; |
Brandon Low | d02b5c7 | 2007-11-05 01:56:12 +0000 | [diff] [blame] | 1827 | |
Nicolas Pennequin | 4e2de44 | 2008-04-14 16:17:47 +0000 | [diff] [blame] | 1828 | |