Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Miika Pekkarinen |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include "config.h" |
Michael Sevakis | 6c399b8 | 2009-02-19 20:40:03 +0000 | [diff] [blame] | 23 | #include "system.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 24 | #include "debug.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 25 | #include <kernel.h> |
| 26 | #include "pcmbuf.h" |
Michael Sevakis | 6077e5b | 2007-10-06 22:27:27 +0000 | [diff] [blame] | 27 | #include "pcm.h" |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 28 | #include "playback.h" |
Jeffrey Goode | faa47bf | 2009-10-22 00:59:42 +0000 | [diff] [blame] | 29 | |
| 30 | /* Define LOGF_ENABLE to enable logf output in this file */ |
| 31 | /*#define LOGF_ENABLE*/ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 32 | #include "logf.h" |
| 33 | #ifndef SIMULATOR |
| 34 | #include "cpu.h" |
| 35 | #endif |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include "buffer.h" |
Miika Pekkarinen | f090dc3 | 2005-07-21 11:44:00 +0000 | [diff] [blame] | 38 | #include "settings.h" |
| 39 | #include "audio.h" |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 40 | #include "voice_thread.h" |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 41 | #include "dsp.h" |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 42 | #include "thread.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 43 | |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 44 | /* Clip sample to signed 16 bit range */ |
| 45 | static inline int32_t clip_sample_16(int32_t sample) |
| 46 | { |
| 47 | if ((int16_t)sample != sample) |
| 48 | sample = 0x7fff ^ (sample >> 31); |
| 49 | return sample; |
| 50 | } |
| 51 | |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 52 | #define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks |
| 53 | on the pcm buffer */ |
| 54 | #define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of |
| 55 | chunks on the pcm buffer (or we run out |
| 56 | of buffer descriptors, which is |
| 57 | non-fatal) */ |
| 58 | #define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than |
| 59 | this to the DMA */ |
| 60 | #define PCMBUF_MIX_CHUNK 8192 /* This is the maximum size of one packet |
| 61 | for mixing (crossfade or voice) */ |
| 62 | |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 63 | #if MEMORYSIZE > 2 |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 64 | /* Keep watermark high for iPods at least (2s) */ |
Brandon Low | 1f3360f | 2006-10-13 20:48:23 +0000 | [diff] [blame] | 65 | #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2) |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 66 | #else |
| 67 | #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */ |
| 68 | #endif |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 69 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 70 | /* Structure we can use to queue pcm chunks in memory to be played |
| 71 | * by the driver code. */ |
| 72 | struct pcmbufdesc |
| 73 | { |
| 74 | void *addr; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 75 | size_t size; |
| 76 | struct pcmbufdesc* link; |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 77 | /* true if last chunk in the track */ |
| 78 | bool end_of_track; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 79 | }; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 80 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 81 | #define PCMBUF_DESCS(bufsize) \ |
| 82 | ((bufsize) / PCMBUF_MINAVG_CHUNK) |
| 83 | #define PCMBUF_DESCS_SIZE(bufsize) \ |
| 84 | (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc)) |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 85 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 86 | /* Size of the PCM buffer. */ |
| 87 | static size_t pcmbuf_size IDATA_ATTR = 0; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 88 | static char *pcmbuf_bufend IDATA_ATTR; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 89 | static char *audiobuffer IDATA_ATTR; |
| 90 | /* Current audio buffer write index. */ |
| 91 | static size_t audiobuffer_pos IDATA_ATTR; |
| 92 | /* Amount audiobuffer_pos will be increased.*/ |
| 93 | static size_t audiobuffer_fillpos IDATA_ATTR; |
| 94 | static char *fadebuf IDATA_ATTR; |
| 95 | static char *voicebuf IDATA_ATTR; |
| 96 | |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 97 | static bool end_of_track IDATA_ATTR; |
| 98 | bool track_transition IDATA_ATTR; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 99 | |
| 100 | /* Crossfade related state */ |
| 101 | static bool crossfade_enabled; |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 102 | static bool crossfade_enabled_pending; |
Miika Pekkarinen | c9a1b4e | 2006-05-14 14:08:26 +0000 | [diff] [blame] | 103 | static bool crossfade_mixmode; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 104 | static bool crossfade_active IDATA_ATTR; |
| 105 | static bool crossfade_init IDATA_ATTR; |
| 106 | |
| 107 | /* Track the current location for processing crossfade */ |
| 108 | static struct pcmbufdesc *crossfade_chunk IDATA_ATTR; |
Michael Giacomelli | 6469926 | 2009-08-11 02:05:38 +0000 | [diff] [blame] | 109 | #ifdef HAVE_CROSSFADE |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 110 | static size_t crossfade_sample IDATA_ATTR; |
| 111 | |
| 112 | /* Counters for fading in new data */ |
| 113 | static size_t crossfade_fade_in_total IDATA_ATTR; |
| 114 | static size_t crossfade_fade_in_rem IDATA_ATTR; |
Michael Giacomelli | 6469926 | 2009-08-11 02:05:38 +0000 | [diff] [blame] | 115 | #endif |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 116 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 117 | static struct pcmbufdesc *pcmbuf_read IDATA_ATTR; |
| 118 | static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR; |
| 119 | static struct pcmbufdesc *pcmbuf_write IDATA_ATTR; |
| 120 | static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR; |
| 121 | static size_t last_chunksize IDATA_ATTR; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 122 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 123 | static size_t pcmbuf_unplayed_bytes IDATA_ATTR; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 124 | static size_t pcmbuf_watermark IDATA_ATTR; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 125 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 126 | static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR; |
| 127 | static size_t pcmbuf_mix_sample IDATA_ATTR; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 128 | |
Miika Pekkarinen | 4408b6b | 2006-02-07 19:17:51 +0000 | [diff] [blame] | 129 | static bool low_latency_mode = false; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 130 | static bool pcmbuf_flush; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 131 | |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 132 | #ifdef HAVE_PRIORITY_SCHEDULING |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 133 | static int codec_thread_priority = PRIORITY_PLAYBACK; |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 134 | #endif |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 135 | |
Michael Sevakis | f2f3947 | 2008-12-10 09:11:43 +0000 | [diff] [blame] | 136 | extern unsigned int codec_thread_id; |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 137 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 138 | /* Helpful macros for use in conditionals this assumes some of the above |
| 139 | * static variable names */ |
| 140 | #define NEED_FLUSH(position) \ |
| 141 | (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size) |
| 142 | #define LOW_DATA(quarter_secs) \ |
| 143 | (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs) |
| 144 | |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 145 | static bool pcmbuf_flush_fillpos(void); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 146 | static void pcmbuf_finish_track_change(void); |
| 147 | static void crossfade_start(void); |
| 148 | static void flush_crossfade(char *buf, size_t length); |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 149 | static bool pcmbuf_crossfade_init(bool manual_skip); |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 150 | static void pcmbuf_finish_crossfade_enable(void); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 151 | static bool pcmbuf_is_crossfade_enabled(void); |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 152 | |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 153 | |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 154 | /**************************************/ |
| 155 | |
| 156 | /* define this to show detailed pcmbufdesc usage information on the sim console */ |
| 157 | /*#define DESC_DEBUG*/ |
| 158 | |
| 159 | #ifndef SIMULATOR |
| 160 | #undef DESC_DEBUG |
| 161 | #endif |
| 162 | #ifdef DESC_DEBUG |
| 163 | static struct pcmbufdesc *first_desc; |
| 164 | static bool show_desc_in_use = false; |
| 165 | #define DISPLAY_DESC(caller) while(!show_desc(caller)) |
| 166 | #define DESC_IDX(desc) (desc ? desc - first_desc : -1) |
| 167 | #define DESCL_IDX(desc) (desc && desc->link ? desc->link - first_desc : -1) |
| 168 | #define SHOW_1ST(desc) if(DESC_IDX (desc)==-1) DEBUGF(" -- "); \ |
| 169 | else DEBUGF(" %02d ", DESC_IDX(desc)) |
| 170 | #define SHOW_2ND(desc) if(DESCL_IDX(desc)==-1) DEBUGF("l -- "); \ |
| 171 | else DEBUGF("l %02d ", DESCL_IDX(desc)) |
| 172 | #define DESC_SHOW(tag, desc) DEBUGF(tag);SHOW_1ST(desc); \ |
| 173 | DEBUGF(tag);SHOW_2ND(desc) |
| 174 | |
| 175 | static bool show_desc(char *caller) |
| 176 | { |
| 177 | if (show_desc_in_use) return false; |
| 178 | show_desc_in_use = true; |
| 179 | DEBUGF("%-14s\t", caller); |
| 180 | DESC_SHOW("r", pcmbuf_read); |
| 181 | DESC_SHOW("re", pcmbuf_read_end); |
| 182 | DEBUGF(" "); |
| 183 | DESC_SHOW("w", pcmbuf_write); |
| 184 | DESC_SHOW("we", pcmbuf_write_end); |
| 185 | DEBUGF("\n"); |
| 186 | show_desc_in_use = false; |
| 187 | return true; |
| 188 | } |
| 189 | #else |
| 190 | #define DISPLAY_DESC(caller) do{}while(0) |
| 191 | #endif |
| 192 | |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 193 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 194 | /* Commit PCM data */ |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 195 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 196 | #ifdef HAVE_PRIORITY_SCHEDULING |
| 197 | static void boost_codec_thread(bool boost) |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 198 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 199 | /* Keep voice and codec threads at the same priority or else voice |
| 200 | * will starve if the codec thread's priority is boosted. */ |
| 201 | if (boost) |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 202 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 203 | int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes |
| 204 | / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX; |
| 205 | |
| 206 | if (priority != codec_thread_priority) |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 207 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 208 | codec_thread_priority = priority; |
| 209 | thread_set_priority(codec_thread_id, priority); |
| 210 | voice_thread_set_priority(priority); |
| 211 | } |
| 212 | } |
| 213 | else if (codec_thread_priority != PRIORITY_PLAYBACK) |
| 214 | { |
| 215 | thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK); |
| 216 | voice_thread_set_priority(PRIORITY_PLAYBACK); |
| 217 | codec_thread_priority = PRIORITY_PLAYBACK; |
| 218 | } |
| 219 | } |
| 220 | #endif /* HAVE_PRIORITY_SCHEDULING */ |
| 221 | |
| 222 | static void pcmbuf_under_watermark(bool under) |
| 223 | { |
| 224 | /* Only codec thread initiates boost - voice boosts the cpu when playing |
| 225 | a clip */ |
| 226 | #ifndef SIMULATOR |
| 227 | if (thread_get_current() == codec_thread_id) |
| 228 | #endif /* SIMULATOR */ |
| 229 | { |
| 230 | if (under) |
| 231 | { |
| 232 | /* Fill audio buffer by boosting cpu */ |
| 233 | trigger_cpu_boost(); |
| 234 | #ifdef HAVE_PRIORITY_SCHEDULING |
| 235 | /* If buffer is critically low, override UI priority, else |
| 236 | set back to the original priority. */ |
| 237 | boost_codec_thread(LOW_DATA(2) && pcm_is_playing()); |
| 238 | #endif |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 239 | } |
| 240 | else |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 241 | { |
| 242 | #ifdef HAVE_PRIORITY_SCHEDULING |
| 243 | boost_codec_thread(false); |
| 244 | #endif |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* Disable crossfade if < .5s of audio */ |
| 249 | if (LOW_DATA(2)) |
| 250 | { |
| 251 | crossfade_active = false; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static bool prepare_insert(size_t length) |
| 256 | { |
| 257 | if (low_latency_mode) |
| 258 | { |
| 259 | /* 1/4s latency. */ |
| 260 | if (!LOW_DATA(1) && pcm_is_playing()) |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */ |
| 265 | if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK) |
| 266 | return false; |
| 267 | |
| 268 | if (!pcm_is_playing()) |
| 269 | { |
| 270 | trigger_cpu_boost(); |
| 271 | |
| 272 | /* Pre-buffer up to watermark */ |
| 273 | #if MEMORYSIZE > 2 |
| 274 | if (!LOW_DATA(4)) |
| 275 | #else |
| 276 | if (pcmbuf_unplayed_bytes > pcmbuf_watermark) |
| 277 | #endif |
| 278 | { |
| 279 | logf("pcm starting"); |
| 280 | if (!(audio_status() & AUDIO_STATUS_PAUSE)) |
| 281 | pcmbuf_play_start(); |
| 282 | } |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 283 | } |
| 284 | else |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 285 | pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark); |
| 286 | |
| 287 | return true; |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 290 | void *pcmbuf_request_buffer(int *count) |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 291 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 292 | #ifdef HAVE_CROSSFADE |
| 293 | if (crossfade_init) |
| 294 | crossfade_start(); |
| 295 | #endif |
| 296 | |
| 297 | if (crossfade_active) { |
| 298 | *count = MIN(*count, PCMBUF_MIX_CHUNK/4); |
| 299 | return fadebuf; |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | if(prepare_insert(*count << 2)) |
| 304 | { |
| 305 | size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; |
| 306 | if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK) |
| 307 | { |
| 308 | /* Usual case, there's space here */ |
| 309 | return &audiobuffer[audiobuffer_index]; |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | /* Flush and wrap the buffer */ |
| 314 | pcmbuf_flush_fillpos(); |
| 315 | audiobuffer_pos = 0; |
| 316 | return &audiobuffer[0]; |
| 317 | } |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | return NULL; |
| 322 | } |
| 323 | } |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 326 | void pcmbuf_write_complete(int count) |
| 327 | { |
| 328 | size_t length = (size_t)(unsigned int)count << 2; |
| 329 | #ifdef HAVE_CROSSFADE |
| 330 | if (crossfade_active) |
| 331 | { |
| 332 | flush_crossfade(fadebuf, length); |
| 333 | if (!(crossfade_fade_in_rem || crossfade_chunk)) |
| 334 | crossfade_active = false; |
| 335 | } |
| 336 | else |
| 337 | #endif |
| 338 | { |
| 339 | audiobuffer_fillpos += length; |
| 340 | |
| 341 | if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos)) |
| 342 | pcmbuf_flush_fillpos(); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep |
| 347 | * in a separate function for the moment */ |
| 348 | static inline void pcmbuf_add_chunk(void) |
| 349 | { |
| 350 | register size_t size = audiobuffer_fillpos; |
| 351 | /* Grab the next description to write, and change the write pointer */ |
| 352 | register struct pcmbufdesc *pcmbuf_current = pcmbuf_write; |
| 353 | pcmbuf_write = pcmbuf_current->link; |
| 354 | /* Fill in the values in the new buffer chunk */ |
| 355 | pcmbuf_current->addr = &audiobuffer[audiobuffer_pos]; |
| 356 | pcmbuf_current->size = size; |
| 357 | pcmbuf_current->end_of_track = end_of_track; |
| 358 | pcmbuf_current->link = NULL; |
| 359 | end_of_track = false; /* This is single use only */ |
| 360 | if (pcmbuf_read != NULL) { |
| 361 | if (pcmbuf_flush) |
| 362 | { |
| 363 | pcmbuf_write_end->link = pcmbuf_read->link; |
| 364 | pcmbuf_read->link = pcmbuf_current; |
| 365 | while (pcmbuf_write_end->link) |
| 366 | { |
| 367 | pcmbuf_write_end = pcmbuf_write_end->link; |
| 368 | pcmbuf_unplayed_bytes -= pcmbuf_write_end->size; |
| 369 | } |
| 370 | pcmbuf_flush = false; |
| 371 | } |
| 372 | /* If there is already a read buffer setup, add to it */ |
| 373 | else |
| 374 | pcmbuf_read_end->link = pcmbuf_current; |
| 375 | } else { |
| 376 | /* Otherwise create the buffer */ |
| 377 | pcmbuf_read = pcmbuf_current; |
| 378 | } |
| 379 | /* This is now the last buffer to read */ |
| 380 | pcmbuf_read_end = pcmbuf_current; |
| 381 | |
| 382 | /* Update bytes counters */ |
| 383 | pcmbuf_unplayed_bytes += size; |
| 384 | |
| 385 | audiobuffer_pos += size; |
| 386 | if (audiobuffer_pos >= pcmbuf_size) |
| 387 | audiobuffer_pos -= pcmbuf_size; |
| 388 | |
| 389 | audiobuffer_fillpos = 0; |
| 390 | DISPLAY_DESC("add_chunk"); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Commit samples waiting to the pcm buffer. |
| 395 | */ |
| 396 | static bool pcmbuf_flush_fillpos(void) |
| 397 | { |
| 398 | if (audiobuffer_fillpos) { |
| 399 | /* Never use the last buffer descriptor */ |
| 400 | while (pcmbuf_write == pcmbuf_write_end) { |
| 401 | /* If this happens, something is being stupid */ |
| 402 | if (!pcm_is_playing()) { |
| 403 | logf("pcmbuf_flush_fillpos error"); |
| 404 | pcmbuf_play_start(); |
| 405 | } |
| 406 | /* Let approximately one chunk of data playback */ |
| 407 | sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4)); |
| 408 | } |
| 409 | pcmbuf_add_chunk(); |
| 410 | return true; |
| 411 | } |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /* Init */ |
| 417 | |
| 418 | static void pcmbuf_init_pcmbuffers(void) |
| 419 | { |
| 420 | #ifdef DESC_DEBUG |
| 421 | first_desc = pcmbuf_write; |
| 422 | #endif |
| 423 | struct pcmbufdesc *next = pcmbuf_write; |
| 424 | next++; |
| 425 | pcmbuf_write_end = pcmbuf_write; |
| 426 | while ((void *)next < (void *)pcmbuf_bufend) { |
| 427 | pcmbuf_write_end->link=next; |
| 428 | pcmbuf_write_end=next; |
| 429 | next++; |
| 430 | } |
| 431 | DISPLAY_DESC("init"); |
| 432 | } |
| 433 | |
| 434 | static size_t pcmbuf_get_next_required_pcmbuf_size(void) |
| 435 | { |
| 436 | size_t seconds = 1; |
| 437 | |
| 438 | if (crossfade_enabled_pending) |
| 439 | seconds += global_settings.crossfade_fade_out_delay |
| 440 | + global_settings.crossfade_fade_out_duration; |
| 441 | |
| 442 | #if MEMORYSIZE > 2 |
| 443 | /* Buffer has to be at least 2s long. */ |
| 444 | seconds += 2; |
| 445 | #endif |
| 446 | logf("pcmbuf len: %ld", (long)seconds); |
| 447 | return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */ |
| 448 | } |
| 449 | |
| 450 | static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize) |
| 451 | { |
| 452 | return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 + |
| 453 | PCMBUF_DESCS_SIZE(bufsize)); |
| 454 | } |
| 455 | |
| 456 | /* Initialize the pcmbuffer the structure looks like this: |
| 457 | * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */ |
| 458 | size_t pcmbuf_init(unsigned char *bufend) |
| 459 | { |
| 460 | pcmbuf_bufend = bufend; |
| 461 | pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size(); |
| 462 | audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size); |
| 463 | fadebuf = &audiobuffer[pcmbuf_size]; |
| 464 | voicebuf = &fadebuf[PCMBUF_MIX_CHUNK]; |
| 465 | pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK]; |
| 466 | |
| 467 | pcmbuf_init_pcmbuffers(); |
| 468 | |
| 469 | if(track_transition){logf("pcmbuf: (init) track transition false");} |
| 470 | end_of_track = false; |
| 471 | track_transition = false; |
| 472 | |
| 473 | pcmbuf_finish_crossfade_enable(); |
| 474 | |
| 475 | pcmbuf_play_stop(); |
| 476 | |
| 477 | return pcmbuf_bufend - audiobuffer; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /* Playback */ |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 482 | |
| 483 | /** PCM driver callback |
| 484 | * This function has 3 major logical parts (separated by brackets both for |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 485 | * readability and variable scoping). The first part performs the |
Jeffrey Goode | e8eefe9 | 2009-11-01 19:39:23 +0000 | [diff] [blame] | 486 | * operations related to finishing off the last buffer we fed to the DMA. |
| 487 | * The second part detects the end of playlist condition when the pcm |
| 488 | * buffer is empty except for uncommitted samples. Then they are committed. |
| 489 | * The third part performs the operations involved in sending a new buffer |
| 490 | * to the DMA. */ |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 491 | static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR; |
| 492 | static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 493 | { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 494 | { |
| 495 | struct pcmbufdesc *pcmbuf_current = pcmbuf_read; |
| 496 | /* Take the finished buffer out of circulation */ |
| 497 | pcmbuf_read = pcmbuf_current->link; |
| 498 | |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 499 | /* if during a track transition, update the elapsed time */ |
| 500 | if (track_transition) |
| 501 | audio_pcmbuf_position_callback(last_chunksize); |
| 502 | |
| 503 | /* if last buffer in the track, let the audio thread know */ |
| 504 | if (pcmbuf_current->end_of_track) |
| 505 | pcmbuf_finish_track_change(); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 506 | |
| 507 | /* Put the finished buffer back into circulation */ |
| 508 | pcmbuf_write_end->link = pcmbuf_current; |
| 509 | pcmbuf_write_end = pcmbuf_current; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 510 | |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 511 | /* If we've read over the mix chunk while it's still mixing there */ |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 512 | if (pcmbuf_current == pcmbuf_mix_chunk) |
| 513 | pcmbuf_mix_chunk = NULL; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 514 | /* If we've read over the crossfade chunk while it's still fading */ |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 515 | if (pcmbuf_current == crossfade_chunk) |
Brandon Low | 2306433 | 2006-04-25 16:15:11 +0000 | [diff] [blame] | 516 | crossfade_chunk = pcmbuf_read; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 517 | } |
Jeffrey Goode | e8eefe9 | 2009-11-01 19:39:23 +0000 | [diff] [blame] | 518 | |
| 519 | { |
| 520 | /* Commit last samples at end of playlist */ |
| 521 | if (audiobuffer_fillpos && !pcmbuf_read) |
| 522 | { |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 523 | logf("pcmbuf_pcm_callback: commit last samples"); |
Jeffrey Goode | e8eefe9 | 2009-11-01 19:39:23 +0000 | [diff] [blame] | 524 | pcmbuf_flush_fillpos(); |
| 525 | } |
| 526 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 527 | |
| 528 | { |
| 529 | /* Send the new buffer to the pcm */ |
| 530 | struct pcmbufdesc *pcmbuf_new = pcmbuf_read; |
| 531 | size_t *realsize = size; |
| 532 | unsigned char** realstart = start; |
| 533 | if(pcmbuf_new) |
| 534 | { |
| 535 | size_t current_size = pcmbuf_new->size; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 536 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 537 | pcmbuf_unplayed_bytes -= current_size; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 538 | last_chunksize = current_size; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 539 | *realsize = current_size; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 540 | *realstart = pcmbuf_new->addr; |
| 541 | } |
| 542 | else |
| 543 | { |
| 544 | /* No more buffers */ |
| 545 | last_chunksize = 0; |
| 546 | *realsize = 0; |
| 547 | *realstart = NULL; |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 548 | if (end_of_track) |
| 549 | pcmbuf_finish_track_change(); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 552 | DISPLAY_DESC("callback"); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 555 | /* Force playback. */ |
| 556 | void pcmbuf_play_start(void) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 557 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 558 | if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL) |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 559 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 560 | last_chunksize = pcmbuf_read->size; |
| 561 | pcmbuf_unplayed_bytes -= last_chunksize; |
| 562 | pcm_play_data(pcmbuf_pcm_callback, |
| 563 | (unsigned char *)pcmbuf_read->addr, last_chunksize); |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 564 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void pcmbuf_play_stop(void) |
| 568 | { |
| 569 | pcm_play_stop(); |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 570 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 571 | pcmbuf_unplayed_bytes = 0; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 572 | pcmbuf_mix_chunk = NULL; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 573 | if (pcmbuf_read) { |
| 574 | pcmbuf_write_end->link = pcmbuf_read; |
| 575 | pcmbuf_write_end = pcmbuf_read_end; |
| 576 | pcmbuf_read = pcmbuf_read_end = NULL; |
| 577 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 578 | audiobuffer_pos = 0; |
| 579 | audiobuffer_fillpos = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 580 | crossfade_init = false; |
| 581 | crossfade_active = false; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 582 | pcmbuf_flush = false; |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 583 | DISPLAY_DESC("play_stop"); |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 584 | |
| 585 | #ifdef HAVE_PRIORITY_SCHEDULING |
| 586 | /* Can unboost the codec thread here no matter who's calling */ |
| 587 | boost_codec_thread(false); |
| 588 | #endif |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Michael Sevakis | 3bec38e | 2007-06-24 20:36:58 +0000 | [diff] [blame] | 591 | void pcmbuf_pause(bool pause) |
| 592 | { |
Michael Sevakis | 6077e5b | 2007-10-06 22:27:27 +0000 | [diff] [blame] | 593 | if (pcm_is_playing()) |
| 594 | pcm_play_pause(!pause); |
| 595 | else if (!pause) |
| 596 | pcmbuf_play_start(); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 599 | |
| 600 | /* Track change */ |
| 601 | |
| 602 | /* The codec is moving on to the next track, but the current track is |
| 603 | * still playing. Set flags to make sure the elapsed time of the current |
| 604 | * track is updated properly, and mark the currently written chunk as the |
| 605 | * last one in the track. */ |
| 606 | static void pcmbuf_gapless_track_change(void) |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 607 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 608 | /* we're starting a track transition */ |
| 609 | track_transition = true; |
| 610 | |
| 611 | /* mark the last chunk in the track */ |
| 612 | end_of_track = true; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 615 | static void pcmbuf_crossfade_track_change(void) |
| 616 | { |
| 617 | /* Initiate automatic crossfade mode */ |
| 618 | pcmbuf_crossfade_init(false); |
| 619 | /* Notify the wps that the track change starts now */ |
| 620 | audio_post_track_change(false); |
| 621 | } |
| 622 | |
| 623 | void pcmbuf_start_track_change(bool manual_skip) |
| 624 | { |
| 625 | /* Manual track change (always crossfade or flush audio). */ |
| 626 | if (manual_skip) |
| 627 | { |
| 628 | pcmbuf_crossfade_init(true); |
| 629 | audio_post_track_change(false); |
| 630 | } |
| 631 | /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */ |
| 632 | else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active() |
| 633 | && global_settings.crossfade != CROSSFADE_ENABLE_TRACKSKIP) |
| 634 | { |
| 635 | if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP) |
| 636 | { |
| 637 | if (global_settings.playlist_shuffle) |
| 638 | /* shuffle mode is on, so crossfade: */ |
| 639 | pcmbuf_crossfade_track_change(); |
| 640 | else |
| 641 | /* shuffle mode is off, so normal gapless playback */ |
| 642 | pcmbuf_gapless_track_change(); |
| 643 | } |
| 644 | else |
| 645 | /* normal crossfade: */ |
| 646 | pcmbuf_crossfade_track_change(); |
| 647 | } |
| 648 | else |
| 649 | /* normal gapless playback. */ |
| 650 | pcmbuf_gapless_track_change(); |
| 651 | } |
| 652 | |
| 653 | /* Called when the last chunk in the track has been played */ |
| 654 | static void pcmbuf_finish_track_change(void) |
| 655 | { |
| 656 | /* not in a track transition anymore */ |
| 657 | if(track_transition){logf("pcmbuf: (finish change) track transition false");} |
| 658 | track_transition = false; |
| 659 | |
| 660 | /* notify playback that the track has just finished */ |
| 661 | audio_post_track_change(true); |
| 662 | } |
| 663 | |
| 664 | |
| 665 | /* Crossfade */ |
| 666 | |
Michael Giacomelli | 6469926 | 2009-08-11 02:05:38 +0000 | [diff] [blame] | 667 | /** |
| 668 | * Low memory targets don't have crossfade, so don't compile crossfade |
| 669 | * specific code in order to save some memory. */ |
| 670 | |
| 671 | #ifdef HAVE_CROSSFADE |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 672 | /** |
| 673 | * Completely process the crossfade fade out effect with current pcm buffer. |
| 674 | */ |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 675 | static void crossfade_process_buffer(size_t fade_in_delay, |
| 676 | size_t fade_out_delay, size_t fade_out_rem) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 677 | { |
Miika Pekkarinen | c9a1b4e | 2006-05-14 14:08:26 +0000 | [diff] [blame] | 678 | if (!crossfade_mixmode) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 679 | { |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 680 | /* Fade out the specified amount of the already processed audio */ |
| 681 | size_t total_fade_out = fade_out_rem; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 682 | size_t fade_out_sample; |
| 683 | struct pcmbufdesc *fade_out_chunk = crossfade_chunk; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 684 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 685 | /* Find the right chunk to start fading out */ |
Brandon Low | 9ca16a6 | 2006-04-24 03:43:43 +0000 | [diff] [blame] | 686 | fade_out_delay += crossfade_sample * 2; |
Steve Bavin | d5dcb49 | 2006-09-27 15:23:10 +0000 | [diff] [blame] | 687 | while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 688 | { |
| 689 | fade_out_delay -= fade_out_chunk->size; |
| 690 | fade_out_chunk = fade_out_chunk->link; |
| 691 | } |
| 692 | /* The start sample within the chunk */ |
| 693 | fade_out_sample = fade_out_delay / 2; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 694 | |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 695 | while (fade_out_rem > 0) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 696 | { |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 697 | /* Each 1/10 second of audio will have the same fade applied */ |
Brandon Low | 1924769 | 2006-04-25 10:39:56 +0000 | [diff] [blame] | 698 | size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem); |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 699 | int factor = (fade_out_rem << 8) / total_fade_out; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 700 | |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 701 | fade_out_rem -= block_rem; |
| 702 | |
| 703 | /* Fade this block */ |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 704 | while (block_rem > 0 && fade_out_chunk != NULL) |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 705 | { |
| 706 | /* Fade one sample */ |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 707 | int16_t *buf = (int16_t *)fade_out_chunk->addr; |
| 708 | int32_t sample = buf[fade_out_sample]; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 709 | buf[fade_out_sample++] = (sample * factor) >> 8; |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 710 | |
Brandon Low | 1924769 | 2006-04-25 10:39:56 +0000 | [diff] [blame] | 711 | block_rem -= 2; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 712 | /* Move to the next chunk as needed */ |
| 713 | if (fade_out_sample * 2 >= fade_out_chunk->size) |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 714 | { |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 715 | fade_out_chunk = fade_out_chunk->link; |
| 716 | fade_out_sample = 0; |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 719 | } |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 722 | /* Find the right chunk and sample to start fading in */ |
Brandon Low | 9ca16a6 | 2006-04-24 03:43:43 +0000 | [diff] [blame] | 723 | fade_in_delay += crossfade_sample * 2; |
Steve Bavin | d5dcb49 | 2006-09-27 15:23:10 +0000 | [diff] [blame] | 724 | while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 725 | { |
| 726 | fade_in_delay -= crossfade_chunk->size; |
| 727 | crossfade_chunk = crossfade_chunk->link; |
| 728 | } |
| 729 | crossfade_sample = fade_in_delay / 2; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 730 | logf("process done!"); |
| 731 | } |
| 732 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 733 | /* Initializes crossfader, calculates all necessary parameters and |
| 734 | * performs fade-out with the pcm buffer. */ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 735 | static void crossfade_start(void) |
| 736 | { |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 737 | size_t crossfade_rem; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 738 | size_t crossfade_need; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 739 | size_t fade_out_rem; |
| 740 | size_t fade_out_delay; |
| 741 | size_t fade_in_delay; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 742 | |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 743 | crossfade_init = false; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 744 | /* Reject crossfade if less than .5s of data */ |
| 745 | if (LOW_DATA(2)) { |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 746 | logf("crossfade rejected"); |
| 747 | pcmbuf_play_stop(); |
| 748 | return ; |
| 749 | } |
| 750 | |
| 751 | logf("crossfade_start"); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 752 | pcmbuf_flush_fillpos(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 753 | crossfade_active = true; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 754 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 755 | /* Initialize the crossfade buffer size to all of the buffered data that |
| 756 | * has not yet been sent to the DMA */ |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 757 | crossfade_rem = pcmbuf_unplayed_bytes; |
| 758 | crossfade_chunk = pcmbuf_read->link; |
Brandon Low | 8ef1827 | 2006-04-24 12:41:30 +0000 | [diff] [blame] | 759 | crossfade_sample = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 760 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 761 | /* Get fade out delay from settings. */ |
| 762 | fade_out_delay = |
| 763 | NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 764 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 765 | /* Get fade out duration from settings. */ |
| 766 | fade_out_rem = |
| 767 | NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 768 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 769 | crossfade_need = fade_out_delay + fade_out_rem; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 770 | /* We want only to modify the last part of the buffer. */ |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 771 | if (crossfade_rem > crossfade_need) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 772 | { |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 773 | size_t crossfade_extra = crossfade_rem - crossfade_need; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 774 | while (crossfade_extra > crossfade_chunk->size) |
| 775 | { |
| 776 | crossfade_extra -= crossfade_chunk->size; |
| 777 | crossfade_chunk = crossfade_chunk->link; |
| 778 | } |
| 779 | crossfade_sample = crossfade_extra / 2; |
| 780 | } |
| 781 | /* Truncate fade out duration if necessary. */ |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 782 | else if (crossfade_rem < crossfade_need) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 783 | { |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 784 | size_t crossfade_short = crossfade_need - crossfade_rem; |
| 785 | if (fade_out_rem >= crossfade_short) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 786 | fade_out_rem -= crossfade_short; |
| 787 | else |
| 788 | { |
| 789 | fade_out_delay -= crossfade_short - fade_out_rem; |
| 790 | fade_out_rem = 0; |
| 791 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 792 | } |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 793 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 794 | /* Get also fade in duration and delays from settings. */ |
| 795 | crossfade_fade_in_total = |
| 796 | NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4; |
| 797 | crossfade_fade_in_rem = crossfade_fade_in_total; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 798 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 799 | fade_in_delay = |
| 800 | NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4; |
| 801 | |
| 802 | crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 805 | /* Returns the number of bytes _NOT_ mixed */ |
Jeffrey Goode | 96bb9ef | 2009-10-23 01:01:00 +0000 | [diff] [blame] | 806 | static size_t crossfade_mix(int factor, const char *buf, size_t length) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 807 | { |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 808 | const int16_t *input_buf = (const int16_t *)buf; |
| 809 | int16_t *output_buf = (int16_t *)(crossfade_chunk->addr); |
| 810 | int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size); |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 811 | output_buf = &output_buf[crossfade_sample]; |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 812 | int32_t sample; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 813 | |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 814 | while (length) |
| 815 | { |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 816 | /* fade left and right channel at once to keep buffer alignment */ |
Jeffrey Goode | 96bb9ef | 2009-10-23 01:01:00 +0000 | [diff] [blame] | 817 | int i; |
| 818 | for (i = 0; i < 2; i++) |
| 819 | { |
| 820 | sample = *input_buf++; |
| 821 | sample = ((sample * factor) >> 8) + *output_buf; |
| 822 | *output_buf++ = clip_sample_16(sample); |
| 823 | } |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 824 | |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 825 | length -= 4; /* 2 samples, each 16 bit -> 4 bytes */ |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 826 | |
| 827 | if (output_buf >= chunk_end) |
| 828 | { |
| 829 | crossfade_chunk = crossfade_chunk->link; |
| 830 | if (!crossfade_chunk) |
| 831 | return length; |
Michael Sevakis | 18330a0 | 2007-11-16 14:34:15 +0000 | [diff] [blame] | 832 | output_buf = (int16_t *)crossfade_chunk->addr; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 833 | chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size); |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 836 | crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 837 | return 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 840 | static void flush_crossfade(char *buf, size_t length) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 841 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 842 | if (length) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 843 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 844 | if (crossfade_fade_in_rem) |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 845 | { |
| 846 | size_t samples; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 847 | int16_t *input_buf; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 848 | |
| 849 | /* Fade factor for this packet */ |
| 850 | int factor = |
| 851 | ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) / |
| 852 | crossfade_fade_in_total; |
| 853 | /* Bytes to fade */ |
| 854 | size_t fade_rem = MIN(length, crossfade_fade_in_rem); |
| 855 | |
| 856 | /* We _will_ fade this many bytes */ |
| 857 | crossfade_fade_in_rem -= fade_rem; |
| 858 | |
| 859 | if (crossfade_chunk) |
| 860 | { |
| 861 | /* Mix the data */ |
| 862 | size_t fade_total = fade_rem; |
Jeffrey Goode | 96bb9ef | 2009-10-23 01:01:00 +0000 | [diff] [blame] | 863 | fade_rem = crossfade_mix(factor, buf, fade_rem); |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 864 | length -= fade_total - fade_rem; |
| 865 | buf += fade_total - fade_rem; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 866 | if (!length) |
| 867 | return; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 870 | samples = fade_rem / 2; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 871 | input_buf = (int16_t *)buf; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 872 | /* Fade remaining samples in place */ |
Jeffrey Goode | 96bb9ef | 2009-10-23 01:01:00 +0000 | [diff] [blame] | 873 | while (samples--) |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 874 | { |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 875 | int32_t sample = *input_buf; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 876 | *input_buf++ = (sample * factor) >> 8; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 877 | } |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 878 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 879 | |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 880 | if (crossfade_chunk) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 881 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 882 | /* Mix the data */ |
| 883 | size_t mix_total = length; |
Jeffrey Goode | 96bb9ef | 2009-10-23 01:01:00 +0000 | [diff] [blame] | 884 | length = crossfade_mix(256, buf, length); |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 885 | buf += mix_total - length; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 886 | if (!length) |
| 887 | return; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 888 | } |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 889 | |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 890 | /* Flush samples to the buffer */ |
| 891 | while (!prepare_insert(length)) |
| 892 | sleep(1); |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 893 | while (length > 0) |
| 894 | { |
| 895 | size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; |
| 896 | if (NEED_FLUSH(audiobuffer_index)) |
| 897 | { |
| 898 | pcmbuf_flush_fillpos(); |
| 899 | audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; |
| 900 | } |
| 901 | size_t copy_n = MIN(length, pcmbuf_size - audiobuffer_index); |
| 902 | memcpy(&audiobuffer[audiobuffer_index], buf, copy_n); |
| 903 | buf += copy_n; |
| 904 | audiobuffer_fillpos += copy_n; |
| 905 | length -= copy_n; |
| 906 | } |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 907 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 908 | } |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 909 | #endif /* HAVE_CROSSFADE */ |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 910 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 911 | static bool pcmbuf_crossfade_init(bool manual_skip) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 912 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 913 | /* Can't do two crossfades at once and, no fade if pcm is off now */ |
| 914 | if (crossfade_init || crossfade_active || !pcm_is_playing()) |
Miika Pekkarinen | 4408b6b | 2006-02-07 19:17:51 +0000 | [diff] [blame] | 915 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 916 | pcmbuf_play_stop(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 917 | return false; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 918 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 919 | |
| 920 | trigger_cpu_boost(); |
| 921 | |
| 922 | /* Not enough data, or crossfade disabled, flush the old data instead */ |
| 923 | if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode) |
| 924 | { |
| 925 | pcmbuf_flush_fillpos(); |
| 926 | pcmbuf_flush = true; |
| 927 | return false; |
| 928 | } |
| 929 | |
| 930 | /* Don't enable mix mode when skipping tracks manually. */ |
| 931 | if (manual_skip) |
| 932 | crossfade_mixmode = false; |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 933 | else |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 934 | crossfade_mixmode = global_settings.crossfade_fade_out_mixmode; |
| 935 | |
| 936 | crossfade_init = true; |
Brandon Low | 3a37fae | 2006-02-13 16:55:25 +0000 | [diff] [blame] | 937 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 938 | return true; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 939 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 942 | static void pcmbuf_finish_crossfade_enable(void) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 943 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 944 | /* Copy the pending setting over now */ |
| 945 | crossfade_enabled = crossfade_enabled_pending; |
| 946 | |
| 947 | pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ? |
| 948 | /* If crossfading, try to keep the buffer full other than 1 second */ |
| 949 | (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) : |
| 950 | /* Otherwise, just use the default */ |
| 951 | PCMBUF_WATERMARK; |
| 952 | } |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 953 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 954 | static bool pcmbuf_is_crossfade_enabled(void) |
| 955 | { |
| 956 | if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE) |
| 957 | return global_settings.playlist_shuffle; |
| 958 | |
| 959 | return crossfade_enabled; |
| 960 | } |
| 961 | |
| 962 | bool pcmbuf_is_crossfade_active(void) |
| 963 | { |
| 964 | return crossfade_active || crossfade_init; |
| 965 | } |
| 966 | |
| 967 | void pcmbuf_request_crossfade_enable(bool on_off) |
| 968 | { |
| 969 | /* Next setting to be used, not applied now */ |
| 970 | crossfade_enabled_pending = on_off; |
| 971 | } |
| 972 | |
| 973 | bool pcmbuf_is_same_size(void) |
| 974 | { |
| 975 | bool same_size; |
| 976 | |
| 977 | if (audiobuffer == NULL) |
| 978 | same_size = true; /* Not set up yet even once so always */ |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 979 | else |
| 980 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 981 | size_t bufsize = pcmbuf_get_next_required_pcmbuf_size(); |
| 982 | same_size = pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer; |
| 983 | } |
| 984 | |
| 985 | if (same_size) |
| 986 | pcmbuf_finish_crossfade_enable(); |
| 987 | |
| 988 | return same_size; |
| 989 | } |
| 990 | |
| 991 | |
| 992 | /* Voice */ |
| 993 | |
| 994 | /* Returns pcm buffer usage in percents (0 to 100). */ |
| 995 | static int pcmbuf_usage(void) |
| 996 | { |
| 997 | return pcmbuf_unplayed_bytes * 100 / pcmbuf_size; |
| 998 | } |
| 999 | |
| 1000 | static int pcmbuf_mix_free(void) |
| 1001 | { |
| 1002 | if (pcmbuf_mix_chunk) |
| 1003 | { |
| 1004 | size_t my_mix_end = |
| 1005 | (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample]; |
| 1006 | size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos]; |
| 1007 | if (my_write_pos < my_mix_end) |
| 1008 | my_write_pos += pcmbuf_size; |
| 1009 | return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes; |
| 1010 | } |
| 1011 | return 100; |
| 1012 | } |
| 1013 | |
| 1014 | void *pcmbuf_request_voice_buffer(int *count) |
| 1015 | { |
| 1016 | /* A get-it-to-work-for-now hack (audio status could change by |
| 1017 | completion) */ |
| 1018 | if (audio_status() & AUDIO_STATUS_PLAY) |
| 1019 | { |
| 1020 | if (pcmbuf_read == NULL) |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 1021 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 1022 | return NULL; |
| 1023 | } |
| 1024 | else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 && |
| 1025 | (pcmbuf_mix_chunk || pcmbuf_read->link)) |
| 1026 | { |
| 1027 | *count = MIN(*count, PCMBUF_MIX_CHUNK/4); |
| 1028 | return voicebuf; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 1029 | } |
| 1030 | else |
| 1031 | { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 1032 | return NULL; |
| 1033 | } |
| 1034 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 1035 | else |
| 1036 | { |
| 1037 | return pcmbuf_request_buffer(count); |
| 1038 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 1041 | void pcmbuf_write_voice_complete(int count) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1042 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 1043 | /* A get-it-to-work-for-now hack (audio status could have changed) */ |
| 1044 | if (!(audio_status() & AUDIO_STATUS_PLAY)) |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 1045 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 1046 | pcmbuf_write_complete(count); |
| 1047 | return; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 1048 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 1049 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 1050 | int16_t *ibuf = (int16_t *)voicebuf; |
| 1051 | int16_t *obuf; |
| 1052 | size_t chunk_samples; |
| 1053 | |
| 1054 | if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL) |
| 1055 | { |
| 1056 | pcmbuf_mix_chunk = pcmbuf_read->link; |
| 1057 | /* Start 1/8s into the next chunk */ |
| 1058 | pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1059 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame^] | 1060 | |
| 1061 | if (!pcmbuf_mix_chunk) |
| 1062 | return; |
| 1063 | |
| 1064 | obuf = (int16_t *)pcmbuf_mix_chunk->addr; |
| 1065 | chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t); |
| 1066 | |
| 1067 | count <<= 1; |
| 1068 | |
| 1069 | while (count-- > 0) |
| 1070 | { |
| 1071 | int32_t sample = *ibuf++; |
| 1072 | |
| 1073 | if (pcmbuf_mix_sample >= chunk_samples) |
| 1074 | { |
| 1075 | pcmbuf_mix_chunk = pcmbuf_mix_chunk->link; |
| 1076 | if (!pcmbuf_mix_chunk) |
| 1077 | return; |
| 1078 | pcmbuf_mix_sample = 0; |
| 1079 | obuf = pcmbuf_mix_chunk->addr; |
| 1080 | chunk_samples = pcmbuf_mix_chunk->size / 2; |
| 1081 | } |
| 1082 | sample += obuf[pcmbuf_mix_sample] >> 2; |
| 1083 | obuf[pcmbuf_mix_sample++] = clip_sample_16(sample); |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | /* Debug menu, other metrics */ |
| 1089 | |
| 1090 | /* Amount of bytes left in the buffer. */ |
| 1091 | size_t pcmbuf_free(void) |
| 1092 | { |
| 1093 | if (pcmbuf_read != NULL) |
| 1094 | { |
| 1095 | void *read = pcmbuf_read->addr; |
| 1096 | void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos]; |
| 1097 | if (read < write) |
| 1098 | return (size_t)(read - write) + pcmbuf_size; |
| 1099 | else |
| 1100 | return (size_t) (read - write); |
| 1101 | } |
| 1102 | return pcmbuf_size; |
| 1103 | } |
| 1104 | |
| 1105 | size_t pcmbuf_get_bufsize(void) |
| 1106 | { |
| 1107 | return pcmbuf_size; |
| 1108 | } |
| 1109 | |
| 1110 | int pcmbuf_used_descs(void) |
| 1111 | { |
| 1112 | struct pcmbufdesc *pcmbuf_temp = pcmbuf_read; |
| 1113 | unsigned int i = 0; |
| 1114 | while (pcmbuf_temp) { |
| 1115 | pcmbuf_temp = pcmbuf_temp->link; |
| 1116 | i++; |
| 1117 | } |
| 1118 | return i; |
| 1119 | } |
| 1120 | |
| 1121 | int pcmbuf_descs(void) |
| 1122 | { |
| 1123 | return PCMBUF_DESCS(pcmbuf_size); |
| 1124 | } |
| 1125 | |
| 1126 | #ifdef ROCKBOX_HAS_LOGF |
| 1127 | unsigned char * pcmbuf_get_meminfo(size_t *length) |
| 1128 | { |
| 1129 | *length = pcmbuf_bufend - audiobuffer; |
| 1130 | return audiobuffer; |
| 1131 | } |
| 1132 | #endif |
| 1133 | |
| 1134 | |
| 1135 | /* Misc */ |
| 1136 | |
| 1137 | bool pcmbuf_is_lowdata(void) |
| 1138 | { |
| 1139 | if (!pcm_is_playing() || pcm_is_paused() || |
| 1140 | crossfade_init || crossfade_active) |
| 1141 | return false; |
| 1142 | |
| 1143 | #if MEMORYSIZE > 2 |
| 1144 | /* 1 seconds of buffer is low data */ |
| 1145 | return LOW_DATA(4); |
| 1146 | #else |
| 1147 | /* under watermark is low data */ |
| 1148 | return (pcmbuf_unplayed_bytes < pcmbuf_watermark); |
| 1149 | #endif |
| 1150 | } |
| 1151 | |
| 1152 | void pcmbuf_set_low_latency(bool state) |
| 1153 | { |
| 1154 | low_latency_mode = state; |
| 1155 | } |
| 1156 | |
| 1157 | unsigned long pcmbuf_get_latency(void) |
| 1158 | { |
| 1159 | /* Be careful how this calculation is rearranged, it's easy to overflow */ |
| 1160 | size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting(); |
| 1161 | return bytes / 4 * 1000 / NATIVE_FREQUENCY; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Michael Sevakis | 89da432 | 2008-12-07 00:07:47 +0000 | [diff] [blame] | 1164 | #ifndef HAVE_HARDWARE_BEEP |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1165 | #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION) |
| 1166 | #define MINIBUF_SIZE (MINIBUF_SAMPLES*4) |
| 1167 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1168 | /* Generates a constant square wave sound with a given frequency |
| 1169 | in Hertz for a duration in milliseconds. */ |
Brandon Low | 9535a9a | 2006-02-22 01:56:44 +0000 | [diff] [blame] | 1170 | void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude) |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1171 | { |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1172 | unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency; |
| 1173 | int32_t phase = 0; |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1174 | int16_t *bufptr, *bufstart, *bufend; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 1175 | int32_t sample; |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1176 | int nsamples = NATIVE_FREQUENCY / 1000 * duration; |
| 1177 | bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL; |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1178 | int i; |
| 1179 | |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1180 | bufend = SKIPBYTES((int16_t *)audiobuffer, pcmbuf_size); |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 1181 | |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1182 | /* Find the insertion point and set bufstart to the start of it */ |
| 1183 | if (mix) |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1184 | { |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1185 | /* Get the currently playing chunk at the current position. */ |
| 1186 | bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i); |
| 1187 | |
Michael Sevakis | 26bf32c | 2008-12-14 11:42:47 +0000 | [diff] [blame] | 1188 | /* If above isn't implemented or pcm is stopped, no beepeth. */ |
| 1189 | if (!bufstart || !pcm_is_playing()) |
| 1190 | return; |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1191 | |
| 1192 | /* Give 5ms clearance. */ |
| 1193 | bufstart += NATIVE_FREQUENCY * 4 / 200; |
| 1194 | |
Michael Sevakis | 94537f9 | 2009-02-08 22:32:41 +0000 | [diff] [blame] | 1195 | #ifdef HAVE_PCM_DMA_ADDRESS |
| 1196 | /* Returned peak addresses are DMA addresses */ |
| 1197 | bufend = pcm_dma_addr(bufend); |
| 1198 | #endif |
| 1199 | |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1200 | /* Wrapped above? */ |
| 1201 | if (bufstart >= bufend) |
| 1202 | bufstart -= pcmbuf_size; |
| 1203 | |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1204 | /* NOTE: On some targets using hardware DMA, cache range flushing may |
| 1205 | * be required or the writes may not be picked up by the controller. |
| 1206 | * An incremental flush should be done periodically during the mixdown. */ |
Brandon Low | 9535a9a | 2006-02-22 01:56:44 +0000 | [diff] [blame] | 1207 | } |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1208 | else if (nsamples <= MINIBUF_SAMPLES) |
| 1209 | { |
Michael Sevakis | e86ece4 | 2009-02-16 18:45:43 +0000 | [diff] [blame] | 1210 | static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4))); |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1211 | /* Use mini buffer */ |
| 1212 | bufstart = minibuf; |
| 1213 | bufend = SKIPBYTES(bufstart, MINIBUF_SIZE); |
| 1214 | } |
| 1215 | else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED) |
Brandon Low | 0fcd411 | 2006-04-05 13:00:31 +0000 | [diff] [blame] | 1216 | { |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1217 | /* Use audiobuffer */ |
| 1218 | bufstart = (int16_t *)audiobuffer; |
| 1219 | } |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1220 | else |
| 1221 | { |
| 1222 | /* No place */ |
| 1223 | return; |
| 1224 | } |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1225 | |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1226 | bufptr = bufstart; |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1227 | |
| 1228 | /* Mix square wave into buffer */ |
| 1229 | for (i = 0; i < nsamples; ++i) |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1230 | { |
Michael Sevakis | 89da432 | 2008-12-07 00:07:47 +0000 | [diff] [blame] | 1231 | int32_t amp = (phase >> 31) ^ (int32_t)amplitude; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1232 | sample = mix ? *bufptr : 0; |
Michael Sevakis | 89da432 | 2008-12-07 00:07:47 +0000 | [diff] [blame] | 1233 | *bufptr++ = clip_sample_16(sample + amp); |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1234 | if (bufptr >= bufend) |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1235 | bufptr = (int16_t *)audiobuffer; |
| 1236 | sample = mix ? *bufptr : 0; |
Michael Sevakis | 89da432 | 2008-12-07 00:07:47 +0000 | [diff] [blame] | 1237 | *bufptr++ = clip_sample_16(sample + amp); |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1238 | if (bufptr >= bufend) |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1239 | bufptr = (int16_t *)audiobuffer; |
| 1240 | |
Michael Sevakis | 89da432 | 2008-12-07 00:07:47 +0000 | [diff] [blame] | 1241 | phase += step; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1244 | pcm_play_lock(); |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1245 | #ifdef HAVE_RECORDING |
| 1246 | pcm_rec_lock(); |
| 1247 | #endif |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1248 | |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1249 | /* Kick off playback if required and it won't interfere */ |
| 1250 | if (!pcm_is_playing() |
| 1251 | #ifdef HAVE_RECORDING |
| 1252 | && !pcm_is_recording() |
| 1253 | #endif |
| 1254 | ) |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1255 | { |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1256 | pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4); |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1257 | } |
Michael Sevakis | 5e73f9f | 2008-12-13 01:32:39 +0000 | [diff] [blame] | 1258 | |
| 1259 | pcm_play_unlock(); |
Michael Sevakis | 93572e2 | 2008-12-13 06:01:08 +0000 | [diff] [blame] | 1260 | #ifdef HAVE_RECORDING |
| 1261 | pcm_rec_unlock(); |
| 1262 | #endif |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1263 | } |
Michael Sevakis | 89da432 | 2008-12-07 00:07:47 +0000 | [diff] [blame] | 1264 | #endif /* HAVE_HARDWARE_BEEP */ |