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 |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 11 | * Copyright (C) 2011 by Michael Sevakis |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +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. |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +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 | ****************************************************************************/ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include "config.h" |
Michael Sevakis | 6c399b8 | 2009-02-19 20:40:03 +0000 | [diff] [blame] | 24 | #include "system.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 25 | #include "debug.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 26 | #include <kernel.h> |
Michael Sevakis | 6077e5b | 2007-10-06 22:27:27 +0000 | [diff] [blame] | 27 | #include "pcm.h" |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 28 | #include "pcm_mixer.h" |
| 29 | #include "pcmbuf.h" |
Michael Sevakis | a43df15 | 2011-12-04 18:19:39 +0000 | [diff] [blame] | 30 | #include "dsp-util.h" |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 31 | #include "playback.h" |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 32 | #include "codec_thread.h" |
Jeffrey Goode | faa47bf | 2009-10-22 00:59:42 +0000 | [diff] [blame] | 33 | |
| 34 | /* Define LOGF_ENABLE to enable logf output in this file */ |
| 35 | /*#define LOGF_ENABLE*/ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 36 | #include "logf.h" |
Thomas Martitz | 35e8b14 | 2010-06-21 16:53:00 +0000 | [diff] [blame] | 37 | #if (CONFIG_PLATFORM & PLATFORM_NATIVE) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 38 | #include "cpu.h" |
| 39 | #endif |
Miika Pekkarinen | f090dc3 | 2005-07-21 11:44:00 +0000 | [diff] [blame] | 40 | #include "settings.h" |
| 41 | #include "audio.h" |
Jeffrey Goode | d0ac045 | 2009-11-09 05:58:02 +0000 | [diff] [blame] | 42 | #include "voice_thread.h" |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 43 | #include "dsp.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 44 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 45 | /* This is the target fill size of chunks on the pcm buffer |
| 46 | Can be any number of samples but power of two sizes make for faster and |
| 47 | smaller math - must be < 65536 bytes */ |
| 48 | #define PCMBUF_CHUNK_SIZE 8192u |
Michael Sevakis | 11cbffa | 2011-09-30 06:46:19 +0000 | [diff] [blame] | 49 | |
| 50 | /* Massive size is a nasty temp fix */ |
| 51 | #define PCMBUF_GUARD_SIZE (1024u*12*((NATIVE_FREQUENCY+7999)/8000)) |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 52 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 53 | /* Mnemonics for common data commit thresholds */ |
| 54 | #define COMMIT_CHUNKS PCMBUF_CHUNK_SIZE |
| 55 | #define COMMIT_ALL_DATA 1u |
| 56 | |
| 57 | /* Size of the crossfade buffer where codec data is written to be faded |
| 58 | on commit */ |
| 59 | #define CROSSFADE_BUFSIZE 8192u |
| 60 | |
Michael Sevakis | c7f3a0b | 2012-01-20 18:10:12 -0500 | [diff] [blame] | 61 | /* Return data level in 1/4-second increments */ |
| 62 | #define DATA_LEVEL(quarter_secs) (NATIVE_FREQUENCY * (quarter_secs)) |
| 63 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 64 | /* Number of bytes played per second: |
| 65 | (sample rate * 2 channels * 2 bytes/sample) */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 66 | #define BYTERATE (NATIVE_FREQUENCY * 4) |
| 67 | |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 68 | #if MEMORYSIZE > 2 |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 69 | /* Keep watermark high for large memory target - at least (2s) */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 70 | #define PCMBUF_WATERMARK (BYTERATE * 2) |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 71 | #define MIN_BUFFER_SIZE (BYTERATE * 3) |
Michael Sevakis | c7f3a0b | 2012-01-20 18:10:12 -0500 | [diff] [blame] | 72 | /* 1 seconds of buffer is low data */ |
| 73 | #define LOW_DATA DATA_LEVEL(4) |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 74 | #else |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 75 | #define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 76 | #define MIN_BUFFER_SIZE (BYTERATE * 1) |
Michael Sevakis | c7f3a0b | 2012-01-20 18:10:12 -0500 | [diff] [blame] | 77 | /* under watermark is low data */ |
| 78 | #define LOW_DATA pcmbuf_watermark |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 79 | #endif |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 80 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 81 | /* Describes each audio packet - keep it small since there are many of them */ |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 82 | struct chunkdesc |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 83 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 84 | uint16_t size; /* Actual size (0 < size <= PCMBUF_CHUNK_SIZE) */ |
| 85 | uint8_t is_end; /* Flag indicating end of track */ |
| 86 | uint8_t pos_key; /* Who put the position info in (0 = undefined) */ |
| 87 | unsigned long elapsed; /* Elapsed time to use */ |
| 88 | off_t offset; /* Offset to use */ |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 89 | }; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 90 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 91 | /* General PCM buffer data */ |
| 92 | #define INVALID_BUF_INDEX ((size_t)0 - (size_t)1) |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 93 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 94 | static unsigned char *pcmbuf_buffer; |
| 95 | static unsigned char *pcmbuf_guardbuf; |
| 96 | static size_t pcmbuf_size; |
| 97 | static struct chunkdesc *pcmbuf_descriptors; |
| 98 | static unsigned int pcmbuf_desc_count; |
| 99 | static unsigned int position_key = 1; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 100 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 101 | static size_t chunk_ridx; |
| 102 | static size_t chunk_widx; |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 103 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 104 | static size_t pcmbuf_bytes_waiting; |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 105 | static struct chunkdesc *current_desc; |
| 106 | |
Michael Sevakis | c7f3a0b | 2012-01-20 18:10:12 -0500 | [diff] [blame] | 107 | /* Only written if HAVE_CROSSFADE */ |
| 108 | static size_t pcmbuf_watermark = PCMBUF_WATERMARK; |
| 109 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 110 | static bool low_latency_mode = false; |
| 111 | |
| 112 | static bool pcmbuf_sync_position = false; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 113 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 114 | /* Fade effect */ |
| 115 | static unsigned int fade_vol = MIX_AMP_UNITY; |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 116 | static enum |
| 117 | { |
| 118 | PCM_NOT_FADING = 0, |
| 119 | PCM_FADING_IN, |
| 120 | PCM_FADING_OUT, |
| 121 | } fade_state = PCM_NOT_FADING; |
| 122 | static bool fade_out_complete = false; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 123 | |
| 124 | /* Voice */ |
| 125 | static bool soft_mode = false; |
| 126 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 127 | #ifdef HAVE_CROSSFADE |
| 128 | /* Crossfade buffer */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 129 | static unsigned char *crossfade_buffer; |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 130 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 131 | /* Crossfade related state */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 132 | static int crossfade_setting; |
| 133 | static int crossfade_enable_request; |
Miika Pekkarinen | c9a1b4e | 2006-05-14 14:08:26 +0000 | [diff] [blame] | 134 | static bool crossfade_mixmode; |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 135 | static bool crossfade_auto_skip; |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 136 | |
| 137 | static enum |
| 138 | { |
| 139 | CROSSFADE_INACTIVE = 0, |
| 140 | CROSSFADE_TRACK_CHANGE_STARTED, |
| 141 | CROSSFADE_ACTIVE, |
| 142 | } crossfade_status = CROSSFADE_INACTIVE; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 143 | |
| 144 | /* Track the current location for processing crossfade */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 145 | static size_t crossfade_index; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 146 | |
| 147 | /* Counters for fading in new data */ |
Michael Sevakis | 5ff641f | 2011-06-29 09:39:13 +0000 | [diff] [blame] | 148 | static size_t crossfade_fade_in_total; |
| 149 | static size_t crossfade_fade_in_rem; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 150 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 151 | /* Defines for operations on position info when mixing/fading - |
| 152 | passed in offset parameter */ |
| 153 | enum |
| 154 | { |
| 155 | MIXFADE_KEEP_POS = -1, /* Keep position info in chunk */ |
| 156 | MIXFADE_NULLIFY_POS = -2, /* Ignore position info in chunk */ |
| 157 | /* Positive values cause stamping/restamping */ |
| 158 | }; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 159 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 160 | static void crossfade_start(void); |
| 161 | static void write_to_crossfade(size_t size, unsigned long elapsed, |
| 162 | off_t offset); |
| 163 | static void pcmbuf_finish_crossfade_enable(void); |
| 164 | #endif /* HAVE_CROSSFADE */ |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 165 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 166 | /* Thread */ |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 167 | #ifdef HAVE_PRIORITY_SCHEDULING |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 168 | static int codec_thread_priority = PRIORITY_PLAYBACK; |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 169 | #endif |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 170 | |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 171 | /* Callbacks into playback.c */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 172 | extern void audio_pcmbuf_position_callback(unsigned long elapsed, |
| 173 | off_t offset, unsigned int key); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 174 | extern void audio_pcmbuf_track_change(bool pcmbuf); |
| 175 | extern bool audio_pcmbuf_may_play(void); |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 176 | extern void audio_pcmbuf_sync_position(void); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 177 | |
Jeffrey Goode | b6f15f2 | 2009-11-08 04:27:27 +0000 | [diff] [blame] | 178 | |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 179 | /**************************************/ |
| 180 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 181 | /* Return number of commited bytes in buffer (committed chunks count as |
| 182 | a full chunk even if only partially filled) */ |
| 183 | static size_t pcmbuf_unplayed_bytes(void) |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 184 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 185 | size_t ridx = chunk_ridx; |
| 186 | size_t widx = chunk_widx; |
| 187 | |
| 188 | if (ridx > widx) |
| 189 | widx += pcmbuf_size; |
| 190 | |
| 191 | return widx - ridx; |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 192 | } |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 193 | |
Michael Sevakis | c7f3a0b | 2012-01-20 18:10:12 -0500 | [diff] [blame] | 194 | /* Returns TRUE if amount of data is under the target fill size */ |
| 195 | static bool pcmbuf_data_critical(void) |
| 196 | { |
| 197 | return pcmbuf_unplayed_bytes() < LOW_DATA; |
| 198 | } |
| 199 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 200 | /* Return the next PCM chunk in the PCM buffer given a byte index into it */ |
| 201 | static size_t index_next(size_t index) |
| 202 | { |
| 203 | index = ALIGN_DOWN(index + PCMBUF_CHUNK_SIZE, PCMBUF_CHUNK_SIZE); |
| 204 | |
| 205 | if (index >= pcmbuf_size) |
| 206 | index -= pcmbuf_size; |
| 207 | |
| 208 | return index; |
| 209 | } |
| 210 | |
| 211 | /* Convert a byte offset in the PCM buffer into a pointer in the buffer */ |
| 212 | static FORCE_INLINE void * index_buffer(size_t index) |
| 213 | { |
| 214 | return pcmbuf_buffer + index; |
| 215 | } |
| 216 | |
| 217 | /* Convert a pointer in the buffer into an index offset */ |
| 218 | static FORCE_INLINE size_t buffer_index(void *p) |
| 219 | { |
| 220 | return (uintptr_t)p - (uintptr_t)pcmbuf_buffer; |
| 221 | } |
| 222 | |
| 223 | /* Return a chunk descriptor for a byte index in the buffer */ |
| 224 | static struct chunkdesc * index_chunkdesc(size_t index) |
| 225 | { |
| 226 | return &pcmbuf_descriptors[index / PCMBUF_CHUNK_SIZE]; |
| 227 | } |
| 228 | |
| 229 | /* Return a chunk descriptor for a byte index in the buffer, offset by 'offset' |
| 230 | chunks */ |
| 231 | static struct chunkdesc * index_chunkdesc_offs(size_t index, int offset) |
| 232 | { |
| 233 | int i = index / PCMBUF_CHUNK_SIZE; |
| 234 | |
| 235 | if (offset != 0) |
| 236 | { |
| 237 | i = (i + offset) % pcmbuf_desc_count; |
| 238 | |
| 239 | /* remainder => modulus */ |
| 240 | if (i < 0) |
| 241 | i += pcmbuf_desc_count; |
| 242 | } |
| 243 | |
| 244 | return &pcmbuf_descriptors[i]; |
| 245 | } |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 246 | |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 247 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 248 | /** Accept new PCM data */ |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 249 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 250 | /* Split the uncommitted data as needed into chunks, stopping when uncommitted |
| 251 | data is below the threshold */ |
| 252 | static void commit_chunks(size_t threshold) |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 253 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 254 | size_t index = chunk_widx; |
| 255 | size_t end_index = index + pcmbuf_bytes_waiting; |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 256 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 257 | /* Copy to the beginning of the buffer all data that must wrap */ |
| 258 | if (end_index > pcmbuf_size) |
| 259 | memcpy(pcmbuf_buffer, pcmbuf_guardbuf, end_index - pcmbuf_size); |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 260 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 261 | struct chunkdesc *desc = index_chunkdesc(index); |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 262 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 263 | do |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 264 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 265 | size_t size = MIN(pcmbuf_bytes_waiting, PCMBUF_CHUNK_SIZE); |
| 266 | pcmbuf_bytes_waiting -= size; |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 267 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 268 | /* Fill in the values in the new buffer chunk */ |
| 269 | desc->size = (uint16_t)size; |
| 270 | |
| 271 | /* Advance the current write chunk and make it available to the |
| 272 | PCM callback */ |
| 273 | chunk_widx = index = index_next(index); |
| 274 | desc = index_chunkdesc(index); |
| 275 | |
| 276 | /* Reset it before using it */ |
| 277 | desc->is_end = 0; |
| 278 | desc->pos_key = 0; |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 279 | } |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 280 | while (pcmbuf_bytes_waiting >= threshold); |
| 281 | } |
| 282 | |
| 283 | /* If uncommitted data count is above or equal to the threshold, commit it */ |
| 284 | static FORCE_INLINE void commit_if_needed(size_t threshold) |
| 285 | { |
| 286 | if (pcmbuf_bytes_waiting >= threshold) |
| 287 | commit_chunks(threshold); |
| 288 | } |
| 289 | |
| 290 | /* Place positioning information in the chunk */ |
| 291 | static void stamp_chunk(struct chunkdesc *desc, unsigned long elapsed, |
| 292 | off_t offset) |
| 293 | { |
| 294 | /* One-time stamping of a given chunk by the same track - new track may |
| 295 | overwrite */ |
| 296 | unsigned int key = position_key; |
| 297 | |
| 298 | if (desc->pos_key != key) |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 299 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 300 | desc->pos_key = key; |
| 301 | desc->elapsed = elapsed; |
| 302 | desc->offset = offset; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 303 | } |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 306 | /* Set priority of the codec thread */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 307 | #ifdef HAVE_PRIORITY_SCHEDULING |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 308 | /* |
| 309 | * expects pcm_fill_state in tenth-% units (e.g. full pcm buffer is 10) */ |
| 310 | static void boost_codec_thread(int pcm_fill_state) |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 311 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 312 | static const int8_t prios[11] = |
| 313 | { |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 314 | PRIORITY_PLAYBACK_MAX, /* 0 - 10% */ |
| 315 | PRIORITY_PLAYBACK_MAX+1, /* 10 - 20% */ |
| 316 | PRIORITY_PLAYBACK_MAX+3, /* 20 - 30% */ |
| 317 | PRIORITY_PLAYBACK_MAX+5, /* 30 - 40% */ |
| 318 | PRIORITY_PLAYBACK_MAX+7, /* 40 - 50% */ |
| 319 | PRIORITY_PLAYBACK_MAX+8, /* 50 - 60% */ |
| 320 | PRIORITY_PLAYBACK_MAX+9, /* 60 - 70% */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 321 | /* raising priority above 70% shouldn't be needed */ |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 322 | PRIORITY_PLAYBACK, /* 70 - 80% */ |
| 323 | PRIORITY_PLAYBACK, /* 80 - 90% */ |
| 324 | PRIORITY_PLAYBACK, /* 90 -100% */ |
| 325 | PRIORITY_PLAYBACK, /* 100% */ |
| 326 | }; |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 327 | |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 328 | int new_prio = prios[pcm_fill_state]; |
| 329 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 330 | /* Keep voice and codec threads at the same priority or else voice |
| 331 | * will starve if the codec thread's priority is boosted. */ |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 332 | if (new_prio != codec_thread_priority) |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 333 | { |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 334 | codec_thread_set_priority(new_prio); |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 335 | voice_thread_set_priority(new_prio); |
| 336 | codec_thread_priority = new_prio; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 339 | #else |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 340 | #define boost_codec_thread(pcm_fill_state) do{}while(0) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 341 | #endif /* HAVE_PRIORITY_SCHEDULING */ |
| 342 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 343 | /* Get the next available buffer and size - assumes adequate space exists */ |
| 344 | static void * get_write_buffer(size_t *size) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 345 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 346 | /* Obtain current chunk fill address */ |
| 347 | size_t index = chunk_widx + pcmbuf_bytes_waiting; |
| 348 | size_t index_end = pcmbuf_size + PCMBUF_GUARD_SIZE; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 349 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 350 | /* Get count to the end of the buffer where a wrap will happen + |
| 351 | the guard */ |
| 352 | size_t endsize = index_end - index; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 353 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 354 | /* Return available unwrapped space */ |
| 355 | *size = MIN(*size, endsize); |
| 356 | |
| 357 | return index_buffer(index); |
| 358 | } |
| 359 | |
| 360 | /* Commit outstanding data leaving less than a chunk size remaining and |
| 361 | write position info to the first chunk */ |
| 362 | static void commit_write_buffer(size_t size, unsigned long elapsed, off_t offset) |
| 363 | { |
| 364 | struct chunkdesc *desc = index_chunkdesc(chunk_widx); |
| 365 | stamp_chunk(desc, elapsed, offset); |
| 366 | |
| 367 | /* Add this data and commit if one or more chunks are ready */ |
| 368 | pcmbuf_bytes_waiting += size; |
| 369 | |
| 370 | commit_if_needed(COMMIT_CHUNKS); |
| 371 | } |
| 372 | |
| 373 | /* Request space in the buffer for writing output samples */ |
| 374 | void * pcmbuf_request_buffer(int *count) |
| 375 | { |
| 376 | size_t size = *count * 4; |
| 377 | |
| 378 | #ifdef HAVE_CROSSFADE |
| 379 | /* We're going to crossfade to a new track, which is now on its way */ |
| 380 | if (crossfade_status == CROSSFADE_TRACK_CHANGE_STARTED) |
| 381 | crossfade_start(); |
| 382 | |
| 383 | /* If crossfade has begun, put the new track samples in crossfade_buffer */ |
| 384 | if (crossfade_status != CROSSFADE_INACTIVE && size > CROSSFADE_BUFSIZE) |
| 385 | size = CROSSFADE_BUFSIZE; |
| 386 | #endif |
| 387 | |
| 388 | enum channel_status status = mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK); |
| 389 | size_t remaining = pcmbuf_unplayed_bytes(); |
| 390 | |
| 391 | /* Need to have length bytes to prevent wrapping overwriting - leave one |
| 392 | descriptor free to guard so that 0 != full in ring buffer */ |
| 393 | size_t freespace = pcmbuf_free(); |
| 394 | |
| 395 | if (pcmbuf_sync_position) |
| 396 | audio_pcmbuf_sync_position(); |
| 397 | |
| 398 | if (freespace < size + PCMBUF_CHUNK_SIZE) |
| 399 | return NULL; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 400 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 401 | /* Maintain the buffer level above the watermark */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 402 | if (status != CHANNEL_STOPPED) |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 403 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 404 | if (low_latency_mode) |
| 405 | { |
| 406 | /* 1/4s latency. */ |
| 407 | if (remaining > DATA_LEVEL(1)) |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | /* Boost CPU if necessary */ |
| 412 | size_t realrem = pcmbuf_size - freespace; |
| 413 | |
| 414 | if (realrem < pcmbuf_watermark) |
Michael Sevakis | 96f02a9 | 2011-07-21 22:25:09 +0000 | [diff] [blame] | 415 | trigger_cpu_boost(); |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 416 | |
| 417 | boost_codec_thread(realrem*10 / pcmbuf_size); |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 418 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 419 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 420 | /* Disable crossfade if < .5s of audio */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 421 | if (remaining < DATA_LEVEL(2)) |
| 422 | crossfade_status = CROSSFADE_INACTIVE; |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 423 | #endif |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 424 | } |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 425 | else /* !playing */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 426 | { |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 427 | /* Boost CPU for pre-buffer */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 428 | trigger_cpu_boost(); |
| 429 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 430 | /* If pre-buffered to the watermark, start playback */ |
Michael Sevakis | c7f3a0b | 2012-01-20 18:10:12 -0500 | [diff] [blame] | 431 | if (!pcmbuf_data_critical() && audio_pcmbuf_may_play()) |
| 432 | pcmbuf_play_start(); |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 433 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 434 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 435 | void *buf = |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 436 | #ifdef HAVE_CROSSFADE |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 437 | crossfade_status != CROSSFADE_INACTIVE ? crossfade_buffer : |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 438 | #endif |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 439 | get_write_buffer(&size); |
| 440 | |
| 441 | *count = size / 4; |
| 442 | return buf; |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 445 | /* Handle new samples to the buffer */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 446 | void pcmbuf_write_complete(int count, unsigned long elapsed, off_t offset) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 447 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 448 | size_t size = count * 4; |
| 449 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 450 | #ifdef HAVE_CROSSFADE |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 451 | if (crossfade_status != CROSSFADE_INACTIVE) |
| 452 | { |
| 453 | write_to_crossfade(size, elapsed, offset); |
| 454 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 455 | else |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 456 | #endif |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 457 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 458 | commit_write_buffer(size, elapsed, offset); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 459 | } |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 460 | |
| 461 | /* Revert to position updates by PCM */ |
| 462 | pcmbuf_sync_position = false; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 465 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 466 | /** Init */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 467 | static unsigned int get_next_required_pcmbuf_chunks(void) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 468 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 469 | size_t size = MIN_BUFFER_SIZE; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 470 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 471 | #ifdef HAVE_CROSSFADE |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 472 | if (crossfade_enable_request != CROSSFADE_ENABLE_OFF) |
| 473 | { |
| 474 | size_t seconds = global_settings.crossfade_fade_out_delay + |
| 475 | global_settings.crossfade_fade_out_duration; |
| 476 | size += seconds * BYTERATE; |
| 477 | } |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 478 | #endif |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 479 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 480 | logf("pcmbuf len: %lu", (unsigned long)(size / BYTERATE)); |
| 481 | return size / PCMBUF_CHUNK_SIZE; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 484 | /* Initialize the ringbuffer state */ |
| 485 | static void init_buffer_state(void) |
| 486 | { |
| 487 | /* Reset counters */ |
| 488 | chunk_ridx = chunk_widx = 0; |
| 489 | pcmbuf_bytes_waiting = 0; |
| 490 | |
| 491 | /* Reset first descriptor */ |
| 492 | struct chunkdesc *desc = pcmbuf_descriptors; |
| 493 | desc->is_end = 0; |
| 494 | desc->pos_key = 0; |
| 495 | } |
| 496 | |
| 497 | /* Initialize the PCM buffer. The structure looks like this: |
| 498 | * ...[|FADEBUF]|---------PCMBUF---------|GUARDBUF|DESCS| */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 499 | size_t pcmbuf_init(unsigned char *bufend) |
| 500 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 501 | unsigned char *bufstart; |
| 502 | |
| 503 | /* Set up the buffers */ |
| 504 | pcmbuf_desc_count = get_next_required_pcmbuf_chunks(); |
| 505 | pcmbuf_size = pcmbuf_desc_count * PCMBUF_CHUNK_SIZE; |
| 506 | pcmbuf_descriptors = (struct chunkdesc *)bufend - pcmbuf_desc_count; |
| 507 | |
| 508 | pcmbuf_buffer = (void *)pcmbuf_descriptors - |
| 509 | pcmbuf_size - PCMBUF_GUARD_SIZE; |
| 510 | |
| 511 | /* Mem-align buffer chunks for more efficient handling in lower layers */ |
| 512 | pcmbuf_buffer = ALIGN_DOWN(pcmbuf_buffer, (uintptr_t)MEM_ALIGN_SIZE); |
| 513 | |
| 514 | pcmbuf_guardbuf = pcmbuf_buffer + pcmbuf_size; |
| 515 | bufstart = pcmbuf_buffer; |
Michael Sevakis | a802eba | 2011-07-09 01:49:00 +0000 | [diff] [blame] | 516 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 517 | #ifdef HAVE_CROSSFADE |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 518 | /* Allocate FADEBUF if it will be needed */ |
| 519 | if (crossfade_enable_request != CROSSFADE_ENABLE_OFF) |
| 520 | { |
| 521 | bufstart -= CROSSFADE_BUFSIZE; |
| 522 | crossfade_buffer = bufstart; |
| 523 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 524 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 525 | pcmbuf_finish_crossfade_enable(); |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 526 | #endif /* HAVE_CROSSFADE */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 527 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 528 | init_buffer_state(); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 529 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 530 | pcmbuf_soft_mode(false); |
| 531 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 532 | return bufend - bufstart; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 536 | /** Track change */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 537 | |
| 538 | /* Place a track change notification in a specific descriptor or post it |
| 539 | immediately if the buffer is empty or the index is invalid */ |
| 540 | static void pcmbuf_monitor_track_change_ex(size_t index, int offset) |
| 541 | { |
| 542 | if (chunk_ridx != chunk_widx && index != INVALID_BUF_INDEX) |
| 543 | { |
| 544 | /* If monitoring, set flag in specified chunk */ |
| 545 | index_chunkdesc_offs(index, offset)->is_end = 1; |
| 546 | } |
| 547 | else |
| 548 | { |
| 549 | /* Post now if no outstanding buffers exist */ |
| 550 | audio_pcmbuf_track_change(false); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* Clear end of track and optionally the positioning info for all data */ |
| 555 | static void pcmbuf_cancel_track_change(bool position) |
| 556 | { |
| 557 | size_t index = chunk_ridx; |
| 558 | |
| 559 | while (1) |
| 560 | { |
| 561 | struct chunkdesc *desc = index_chunkdesc(index); |
| 562 | |
| 563 | desc->is_end = 0; |
| 564 | |
| 565 | if (position) |
| 566 | desc->pos_key = 0; |
| 567 | |
| 568 | if (index == chunk_widx) |
| 569 | break; |
| 570 | |
| 571 | index = index_next(index); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /* Place a track change notification at the end of the buffer or post it |
| 576 | immediately if the buffer is empty */ |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 577 | void pcmbuf_monitor_track_change(bool monitor) |
| 578 | { |
| 579 | pcm_play_lock(); |
| 580 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 581 | if (monitor) |
| 582 | pcmbuf_monitor_track_change_ex(chunk_widx, -1); |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 583 | else |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 584 | pcmbuf_cancel_track_change(false); |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 585 | |
| 586 | pcm_play_unlock(); |
| 587 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 588 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 589 | void pcmbuf_start_track_change(enum pcm_track_change_type type) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 590 | { |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 591 | #ifdef HAVE_CROSSFADE |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 592 | bool crossfade = false; |
| 593 | #endif |
| 594 | bool auto_skip = type != TRACK_CHANGE_MANUAL; |
| 595 | |
| 596 | /* Commit all outstanding data before starting next track - tracks don't |
| 597 | comingle inside a single buffer chunk */ |
| 598 | commit_if_needed(COMMIT_ALL_DATA); |
| 599 | |
| 600 | /* Update position key so that: |
| 601 | 1) Positions are keyed to the track to which they belong for sync |
| 602 | purposes |
| 603 | |
| 604 | 2) Buffers stamped with the outgoing track's positions are restamped |
| 605 | to the incoming track's positions when crossfading |
| 606 | */ |
| 607 | if (++position_key > UINT8_MAX) |
| 608 | position_key = 1; |
| 609 | |
| 610 | if (type == TRACK_CHANGE_END_OF_DATA) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 611 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 612 | /* If end of all data, force playback */ |
| 613 | if (audio_pcmbuf_may_play()) |
| 614 | pcmbuf_play_start(); |
| 615 | } |
| 616 | #ifdef HAVE_CROSSFADE |
| 617 | /* Determine whether this track change needs to crossfaded and how */ |
| 618 | else if (crossfade_setting != CROSSFADE_ENABLE_OFF && |
| 619 | !pcmbuf_is_crossfade_active() && |
| 620 | pcmbuf_unplayed_bytes() >= DATA_LEVEL(2) && |
| 621 | !low_latency_mode) |
| 622 | { |
| 623 | switch (crossfade_setting) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 624 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 625 | case CROSSFADE_ENABLE_AUTOSKIP: |
| 626 | crossfade = auto_skip; |
| 627 | break; |
| 628 | case CROSSFADE_ENABLE_MANSKIP: |
| 629 | crossfade = !auto_skip; |
| 630 | break; |
| 631 | case CROSSFADE_ENABLE_SHUFFLE: |
| 632 | crossfade = global_settings.playlist_shuffle; |
| 633 | break; |
| 634 | case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP: |
| 635 | crossfade = global_settings.playlist_shuffle || !auto_skip; |
| 636 | break; |
| 637 | case CROSSFADE_ENABLE_ALWAYS: |
| 638 | crossfade = true; |
| 639 | break; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 640 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 641 | } |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 642 | /* else crossfade is off, crossfade is already active, not enough data, |
| 643 | * pcm is off now (implying low data), not crossfading or low latency mode |
| 644 | */ |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 645 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 646 | if (crossfade) |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 647 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 648 | logf("crossfade track change"); |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 649 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 650 | /* Don't enable mix mode when skipping tracks manually */ |
| 651 | crossfade_mixmode = auto_skip && |
| 652 | global_settings.crossfade_fade_out_mixmode; |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 653 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 654 | crossfade_auto_skip = auto_skip; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 655 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 656 | crossfade_status = CROSSFADE_TRACK_CHANGE_STARTED; |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 657 | |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 658 | trigger_cpu_boost(); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 659 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 660 | /* Cancel any pending automatic gapless transition and if a manual |
| 661 | skip, stop position updates */ |
| 662 | pcm_play_lock(); |
| 663 | pcmbuf_cancel_track_change(!auto_skip); |
| 664 | pcm_play_unlock(); |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 665 | } |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 666 | else |
| 667 | #endif /* HAVE_CROSSFADE */ |
| 668 | if (auto_skip) |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 669 | { |
| 670 | /* The codec is moving on to the next track, but the current track will |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 671 | * continue to play, so mark the last write chunk as the last one in |
| 672 | * the track */ |
| 673 | logf("gapless track change"); |
| 674 | #ifdef HAVE_CROSSFADE |
| 675 | if (crossfade_status != CROSSFADE_INACTIVE) |
| 676 | { |
| 677 | /* Crossfade is still active but crossfade is not happening - for |
| 678 | * now, chicken-out and clear out the buffer (just like before) to |
| 679 | * avoid fade pile-up on short tracks fading-in over long ones */ |
| 680 | pcmbuf_play_stop(); |
| 681 | } |
| 682 | #endif |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 683 | pcmbuf_monitor_track_change(true); |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 684 | } |
| 685 | else |
| 686 | { |
| 687 | /* Discard old data; caller needs no transition notification */ |
| 688 | logf("manual track change"); |
| 689 | pcmbuf_play_stop(); |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 690 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 694 | /** Playback */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 695 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 696 | /* PCM driver callback */ |
| 697 | static void pcmbuf_pcm_callback(unsigned char **start, size_t *size) |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 698 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 699 | /*- Process the chunk that just finished -*/ |
| 700 | size_t index = chunk_ridx; |
| 701 | struct chunkdesc *desc = current_desc; |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 702 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 703 | if (desc) |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 704 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 705 | /* If last chunk in the track, notify of track change */ |
| 706 | if (desc->is_end != 0) |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 707 | audio_pcmbuf_track_change(true); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 708 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 709 | /* Free it for reuse */ |
| 710 | chunk_ridx = index = index_next(index); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 711 | } |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 712 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 713 | /*- Process the new one -*/ |
| 714 | if (index != chunk_widx && !fade_out_complete) |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 715 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 716 | current_desc = desc = index_chunkdesc(index); |
| 717 | |
| 718 | *start = index_buffer(index); |
| 719 | *size = desc->size; |
| 720 | |
| 721 | if (desc->pos_key != 0) |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 722 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 723 | /* Positioning chunk - notify playback */ |
| 724 | audio_pcmbuf_position_callback(desc->elapsed, desc->offset, |
| 725 | desc->pos_key); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 730 | /* Force playback */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 731 | void pcmbuf_play_start(void) |
| 732 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 733 | logf("pcmbuf_play_start"); |
| 734 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 735 | if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED && |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 736 | chunk_widx != chunk_ridx) |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 737 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 738 | current_desc = NULL; |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 739 | mixer_channel_play_data(PCM_MIXER_CHAN_PLAYBACK, pcmbuf_pcm_callback, |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 740 | NULL, 0); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 744 | /* Stop channel, empty and reset buffer */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 745 | void pcmbuf_play_stop(void) |
| 746 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 747 | logf("pcmbuf_play_stop"); |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 748 | |
| 749 | /* Reset channel */ |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 750 | mixer_channel_stop(PCM_MIXER_CHAN_PLAYBACK); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 751 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 752 | /* Reset buffer */ |
| 753 | init_buffer_state(); |
| 754 | |
| 755 | /* Revert to position updates by PCM */ |
| 756 | pcmbuf_sync_position = false; |
| 757 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 758 | #ifdef HAVE_CROSSFADE |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 759 | crossfade_status = CROSSFADE_INACTIVE; |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 760 | #endif |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 761 | |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 762 | /* Can unboost the codec thread here no matter who's calling, |
| 763 | * pretend full pcm buffer to unboost */ |
| 764 | boost_codec_thread(10); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | void pcmbuf_pause(bool pause) |
| 768 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 769 | logf("pcmbuf_pause: %s", pause?"pause":"play"); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 770 | |
| 771 | if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED) |
| 772 | mixer_channel_play_pause(PCM_MIXER_CHAN_PLAYBACK, !pause); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 773 | else if (!pause) |
| 774 | pcmbuf_play_start(); |
| 775 | } |
| 776 | |
| 777 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 778 | /** Crossfade */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 779 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 780 | #ifdef HAVE_CROSSFADE |
| 781 | /* Find the buffer index that's 'size' bytes away from 'index' */ |
| 782 | static size_t crossfade_find_index(size_t index, size_t size) |
| 783 | { |
| 784 | if (index != INVALID_BUF_INDEX) |
| 785 | { |
| 786 | size_t i = ALIGN_DOWN(index, PCMBUF_CHUNK_SIZE); |
| 787 | size += index - i; |
| 788 | |
| 789 | while (i != chunk_widx) |
| 790 | { |
| 791 | size_t desc_size = index_chunkdesc(i)->size; |
| 792 | |
| 793 | if (size < desc_size) |
| 794 | return i + size; |
| 795 | |
| 796 | size -= desc_size; |
| 797 | i = index_next(i); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | return INVALID_BUF_INDEX; |
| 802 | } |
| 803 | |
| 804 | /* Align the needed buffer area up to the end of existing data */ |
| 805 | static size_t crossfade_find_buftail(size_t buffer_rem, size_t buffer_need) |
| 806 | { |
| 807 | crossfade_index = chunk_ridx; |
| 808 | |
| 809 | if (buffer_rem > buffer_need) |
| 810 | { |
| 811 | size_t distance; |
| 812 | |
| 813 | if (crossfade_auto_skip) |
| 814 | { |
| 815 | /* Automatic track changes only modify the last part of the buffer, |
| 816 | * so find the right chunk and sample to start the crossfade */ |
| 817 | distance = buffer_rem - buffer_need; |
| 818 | buffer_rem = buffer_need; |
| 819 | } |
| 820 | else |
| 821 | { |
| 822 | /* Manual skips occur immediately, but give 1/5s to process */ |
| 823 | distance = BYTERATE / 5; |
| 824 | buffer_rem -= BYTERATE / 5; |
| 825 | } |
| 826 | |
| 827 | crossfade_index = crossfade_find_index(crossfade_index, distance); |
| 828 | } |
| 829 | |
| 830 | return buffer_rem; |
| 831 | } |
| 832 | |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 833 | /* Returns the number of bytes _NOT_ mixed/faded */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 834 | static int crossfade_mix_fade(int factor, size_t size, void *buf, size_t *out_index, |
| 835 | unsigned long elapsed, off_t offset) |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 836 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 837 | if (size == 0) |
Steve Bavin | b2dc7f0 | 2009-11-26 14:41:31 +0000 | [diff] [blame] | 838 | return 0; |
| 839 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 840 | size_t index = *out_index; |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 841 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 842 | if (index == INVALID_BUF_INDEX) |
| 843 | return size; |
| 844 | |
| 845 | const int16_t *input_buf = buf; |
| 846 | int16_t *output_buf = (int16_t *)index_buffer(index); |
| 847 | |
| 848 | while (size) |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 849 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 850 | struct chunkdesc *desc = index_chunkdesc(index); |
| 851 | |
| 852 | switch (offset) |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 853 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 854 | case MIXFADE_NULLIFY_POS: |
| 855 | /* Stop position updates for the chunk */ |
| 856 | desc->pos_key = 0; |
| 857 | break; |
| 858 | case MIXFADE_KEEP_POS: |
| 859 | /* Keep position info as it is */ |
| 860 | break; |
| 861 | default: |
| 862 | /* Replace position info */ |
| 863 | stamp_chunk(desc, elapsed, offset); |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 866 | size_t rem = desc->size - (index % PCMBUF_CHUNK_SIZE); |
| 867 | int16_t *chunk_end = SKIPBYTES(output_buf, rem); |
| 868 | |
| 869 | if (size < rem) |
| 870 | rem = size; |
| 871 | |
| 872 | size -= rem; |
| 873 | |
| 874 | do |
| 875 | { |
| 876 | /* fade left and right channel at once to keep buffer alignment */ |
| 877 | int32_t left = output_buf[0]; |
| 878 | int32_t right = output_buf[1]; |
| 879 | |
| 880 | if (input_buf) |
| 881 | { |
| 882 | /* fade the input buffer and mix into the chunk */ |
| 883 | left += *input_buf++ * factor >> 8; |
| 884 | right += *input_buf++ * factor >> 8; |
| 885 | left = clip_sample_16(left); |
| 886 | right = clip_sample_16(right); |
| 887 | } |
| 888 | else |
| 889 | { |
| 890 | /* fade the chunk only */ |
| 891 | left = left * factor >> 8; |
| 892 | right = right * factor >> 8; |
| 893 | } |
| 894 | |
| 895 | *output_buf++ = left; |
| 896 | *output_buf++ = right; |
| 897 | |
| 898 | rem -= 4; |
| 899 | } |
| 900 | while (rem); |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 901 | |
| 902 | /* move to next chunk as needed */ |
| 903 | if (output_buf >= chunk_end) |
| 904 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 905 | index = index_next(index); |
| 906 | |
| 907 | if (index == chunk_widx) |
| 908 | { |
| 909 | /* End of existing data */ |
| 910 | *out_index = INVALID_BUF_INDEX; |
| 911 | return size; |
| 912 | } |
| 913 | |
| 914 | output_buf = (int16_t *)index_buffer(index); |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 915 | } |
| 916 | } |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 917 | |
| 918 | *out_index = buffer_index(output_buf); |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 919 | return 0; |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 922 | /* Initializes crossfader, calculates all necessary parameters and performs |
| 923 | * fade-out with the PCM buffer. */ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 924 | static void crossfade_start(void) |
| 925 | { |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 926 | logf("crossfade_start"); |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 927 | |
| 928 | pcm_play_lock(); |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 929 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 930 | /* Initialize the crossfade buffer size to all of the buffered data that |
| 931 | * has not yet been sent to the DMA */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 932 | size_t unplayed = pcmbuf_unplayed_bytes(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 933 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 934 | /* Reject crossfade if less than .5s of data */ |
| 935 | if (unplayed < DATA_LEVEL(2)) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 936 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 937 | logf("crossfade rejected"); |
| 938 | |
| 939 | crossfade_status = CROSSFADE_INACTIVE; |
| 940 | |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 941 | if (crossfade_auto_skip) |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 942 | pcmbuf_monitor_track_change(true); |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 943 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 944 | pcm_play_unlock(); |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | /* Fading will happen */ |
| 949 | crossfade_status = CROSSFADE_ACTIVE; |
| 950 | |
| 951 | /* Get fade info from settings. */ |
| 952 | size_t fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE; |
| 953 | size_t fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE; |
| 954 | size_t fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE; |
| 955 | size_t fade_in_duration = global_settings.crossfade_fade_in_duration * BYTERATE; |
| 956 | |
| 957 | if (!crossfade_auto_skip) |
| 958 | { |
| 959 | /* Forego fade-in delay on manual skip - do the best to preserve auto skip |
| 960 | relationship */ |
| 961 | if (fade_out_delay > fade_in_delay) |
| 962 | fade_out_delay -= fade_in_delay; |
| 963 | else |
| 964 | fade_out_delay = 0; |
| 965 | |
| 966 | fade_in_delay = 0; |
| 967 | } |
| 968 | |
| 969 | size_t fade_out_need = fade_out_delay + fade_out_rem; |
| 970 | |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 971 | if (!crossfade_mixmode) |
| 972 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 973 | size_t buffer_rem = crossfade_find_buftail(unplayed, fade_out_need); |
| 974 | |
| 975 | pcm_play_unlock(); |
| 976 | |
| 977 | if (buffer_rem < fade_out_need) |
| 978 | { |
| 979 | /* Existing buffers are short */ |
| 980 | size_t fade_out_short = fade_out_need - buffer_rem; |
| 981 | |
| 982 | if (fade_out_rem >= fade_out_short) |
| 983 | { |
| 984 | /* Truncate fade-out duration */ |
| 985 | fade_out_rem -= fade_out_short; |
| 986 | } |
| 987 | else |
| 988 | { |
| 989 | /* Truncate fade-out and fade-out delay */ |
| 990 | fade_out_delay = fade_out_rem; |
| 991 | fade_out_rem = 0; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /* Completely process the crossfade fade-out effect with current PCM buffer */ |
| 996 | |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 997 | /* Fade out the specified amount of the already processed audio */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 998 | size_t fade_out_total = fade_out_rem; |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 999 | |
| 1000 | /* Find the right chunk and sample to start fading out */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1001 | size_t fade_out_index = crossfade_find_index(crossfade_index, fade_out_delay); |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 1002 | |
| 1003 | while (fade_out_rem > 0) |
| 1004 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1005 | /* Each 1/20 second of audio will have the same fade applied */ |
| 1006 | size_t block_rem = MIN(BYTERATE / 20, fade_out_rem); |
| 1007 | int factor = (fade_out_rem << 8) / fade_out_total; |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 1008 | |
| 1009 | fade_out_rem -= block_rem; |
| 1010 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1011 | crossfade_mix_fade(factor, block_rem, NULL, &fade_out_index, |
| 1012 | 0, MIXFADE_KEEP_POS); |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 1013 | } |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 1014 | |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 1015 | /* zero out the rest of the buffer */ |
Michael Sevakis | 9384cda | 2011-08-28 08:59:14 +0000 | [diff] [blame] | 1016 | crossfade_mix_fade(0, pcmbuf_size, NULL, &fade_out_index, |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1017 | 0, MIXFADE_NULLIFY_POS); |
| 1018 | |
| 1019 | pcm_play_lock(); |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /* Initialize fade-in counters */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1023 | crossfade_fade_in_total = fade_in_duration; |
| 1024 | crossfade_fade_in_rem = fade_in_duration; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 1025 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1026 | /* Find the right chunk and sample to start fading in - redo from read |
| 1027 | chunk in case original position were/was overrun in callback - the |
| 1028 | track change event _must not_ ever fail to happen */ |
| 1029 | unplayed = pcmbuf_unplayed_bytes() + fade_in_delay; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 1030 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1031 | crossfade_find_buftail(unplayed, fade_out_need); |
| 1032 | |
| 1033 | if (crossfade_auto_skip) |
| 1034 | pcmbuf_monitor_track_change_ex(crossfade_index, 0); |
| 1035 | |
| 1036 | pcm_play_unlock(); |
| 1037 | |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 1038 | logf("crossfade_start done!"); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 1041 | /* Perform fade-in of new track */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1042 | static void write_to_crossfade(size_t size, unsigned long elapsed, off_t offset) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 1043 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1044 | unsigned char *buf = crossfade_buffer; |
| 1045 | |
| 1046 | if (crossfade_fade_in_rem) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 1047 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1048 | /* Fade factor for this packet */ |
| 1049 | int factor = |
| 1050 | ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) / |
| 1051 | crossfade_fade_in_total; |
| 1052 | /* Bytes to fade */ |
| 1053 | size_t fade_rem = MIN(size, crossfade_fade_in_rem); |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 1054 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1055 | /* We _will_ fade this many bytes */ |
| 1056 | crossfade_fade_in_rem -= fade_rem; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 1057 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1058 | if (crossfade_index != INVALID_BUF_INDEX) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 1059 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 1060 | /* Mix the data */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1061 | size_t fade_total = fade_rem; |
| 1062 | fade_rem = crossfade_mix_fade(factor, fade_rem, buf, &crossfade_index, |
| 1063 | elapsed, offset); |
| 1064 | fade_total -= fade_rem; |
| 1065 | size -= fade_total; |
| 1066 | buf += fade_total; |
| 1067 | |
| 1068 | if (!size) |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 1069 | return; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 1070 | } |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 1071 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1072 | /* Fade remaining samples in place */ |
| 1073 | int samples = fade_rem / 4; |
| 1074 | int16_t *input_buf = (int16_t *)buf; |
| 1075 | |
| 1076 | while (samples--) |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 1077 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1078 | int32_t left = input_buf[0]; |
| 1079 | int32_t right = input_buf[1]; |
| 1080 | *input_buf++ = left * factor >> 8; |
| 1081 | *input_buf++ = right * factor >> 8; |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 1082 | } |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 1083 | } |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1084 | |
| 1085 | if (crossfade_index != INVALID_BUF_INDEX) |
| 1086 | { |
| 1087 | /* Mix the data */ |
| 1088 | size_t mix_total = size; |
| 1089 | |
| 1090 | /* A factor of 256 means mix only, no fading */ |
| 1091 | size = crossfade_mix_fade(256, size, buf, &crossfade_index, |
| 1092 | elapsed, offset); |
| 1093 | buf += mix_total - size; |
| 1094 | |
| 1095 | if (!size) |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | /* Data might remain in the fade buffer yet the fade-in has run its |
| 1100 | course - finish it off as normal chunks */ |
| 1101 | while (size > 0) |
| 1102 | { |
| 1103 | size_t copy_n = size; |
| 1104 | unsigned char *outbuf = get_write_buffer(©_n); |
| 1105 | memcpy(outbuf, buf, copy_n); |
| 1106 | commit_write_buffer(copy_n, elapsed, offset); |
| 1107 | buf += copy_n; |
| 1108 | size -= copy_n; |
| 1109 | } |
| 1110 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 1111 | /* if no more fading-in to do, stop the crossfade */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1112 | #if 0 |
| 1113 | /* This way (the previous way) can cause a sudden volume jump if mixable |
| 1114 | data is used up before the fade-in completes and that just sounds wrong |
| 1115 | -- jethead71 */ |
| 1116 | if (!crossfade_fade_in_rem || crossfade_index == INVALID_BUF_INDEX) |
| 1117 | #endif |
| 1118 | /* Let fade-in complete even if not fully overlapping the existing data */ |
| 1119 | if (!crossfade_fade_in_rem) |
| 1120 | crossfade_status = CROSSFADE_INACTIVE; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1123 | static void pcmbuf_finish_crossfade_enable(void) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1124 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1125 | /* Copy the pending setting over now */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1126 | crossfade_setting = crossfade_enable_request; |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 1127 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1128 | pcmbuf_watermark = (crossfade_setting != CROSSFADE_ENABLE_OFF && pcmbuf_size) ? |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1129 | /* If crossfading, try to keep the buffer full other than 1 second */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 1130 | (pcmbuf_size - BYTERATE) : |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1131 | /* Otherwise, just use the default */ |
| 1132 | PCMBUF_WATERMARK; |
| 1133 | } |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 1134 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1135 | bool pcmbuf_is_crossfade_active(void) |
| 1136 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1137 | return crossfade_status != CROSSFADE_INACTIVE; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1140 | void pcmbuf_request_crossfade_enable(int setting) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1141 | { |
| 1142 | /* Next setting to be used, not applied now */ |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1143 | crossfade_enable_request = setting; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | bool pcmbuf_is_same_size(void) |
| 1147 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1148 | /* if pcmbuf_buffer is NULL, then not set up yet even once so always */ |
| 1149 | bool same_size = pcmbuf_buffer ? |
| 1150 | (get_next_required_pcmbuf_chunks() == pcmbuf_desc_count) : true; |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 1151 | |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 1152 | /* no buffer change needed, so finish crossfade setup now */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1153 | if (same_size) |
| 1154 | pcmbuf_finish_crossfade_enable(); |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 1155 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1156 | return same_size; |
| 1157 | } |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 1158 | #endif /* HAVE_CROSSFADE */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1159 | |
| 1160 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 1161 | /** Debug menu, other metrics */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1162 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1163 | /* Amount of bytes left in the buffer, accounting for uncommitted bytes */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1164 | size_t pcmbuf_free(void) |
| 1165 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1166 | return pcmbuf_size - pcmbuf_unplayed_bytes() - pcmbuf_bytes_waiting; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1169 | /* Data bytes allocated for buffer */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1170 | size_t pcmbuf_get_bufsize(void) |
| 1171 | { |
| 1172 | return pcmbuf_size; |
| 1173 | } |
| 1174 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1175 | /* Number of committed descriptors */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1176 | int pcmbuf_used_descs(void) |
| 1177 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1178 | return pcmbuf_unplayed_bytes() / PCMBUF_CHUNK_SIZE; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1181 | /* Total number of descriptors allocated */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1182 | int pcmbuf_descs(void) |
| 1183 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1184 | return pcmbuf_desc_count; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1187 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 1188 | /** Fading and channel volume control */ |
| 1189 | |
| 1190 | /* Sync the channel amplitude to all states */ |
| 1191 | static void pcmbuf_update_volume(void) |
| 1192 | { |
| 1193 | unsigned int vol = fade_vol; |
| 1194 | |
| 1195 | if (soft_mode) |
| 1196 | vol >>= 2; |
| 1197 | |
| 1198 | mixer_channel_set_amplitude(PCM_MIXER_CHAN_PLAYBACK, vol); |
| 1199 | } |
| 1200 | |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 1201 | /* Tick that does the fade for the playback channel */ |
| 1202 | static void pcmbuf_fade_tick(void) |
| 1203 | { |
| 1204 | /* ~1/3 second for full range fade */ |
| 1205 | const unsigned int fade_step = MIX_AMP_UNITY / (HZ / 3); |
| 1206 | |
| 1207 | if (fade_state == PCM_FADING_IN) |
| 1208 | fade_vol += MIN(fade_step, MIX_AMP_UNITY - fade_vol); |
| 1209 | else if (fade_state == PCM_FADING_OUT) |
| 1210 | fade_vol -= MIN(fade_step, fade_vol - MIX_AMP_MUTE); |
| 1211 | |
| 1212 | pcmbuf_update_volume(); |
| 1213 | |
| 1214 | if (fade_vol == MIX_AMP_MUTE || fade_vol == MIX_AMP_UNITY) |
| 1215 | { |
| 1216 | /* Fade is complete */ |
| 1217 | tick_remove_task(pcmbuf_fade_tick); |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 1218 | if (fade_state == PCM_FADING_OUT) |
| 1219 | { |
| 1220 | /* Tell PCM to stop at its earliest convenience */ |
| 1221 | fade_out_complete = true; |
| 1222 | } |
| 1223 | |
| 1224 | fade_state = PCM_NOT_FADING; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Michael Sevakis | 4d903f2 | 2011-08-23 05:58:28 +0000 | [diff] [blame] | 1228 | /* Fade channel in or out in the background */ |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 1229 | void pcmbuf_fade(bool fade, bool in) |
| 1230 | { |
Michael Sevakis | 4d903f2 | 2011-08-23 05:58:28 +0000 | [diff] [blame] | 1231 | /* Must pause any active fade */ |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 1232 | pcm_play_lock(); |
| 1233 | |
| 1234 | if (fade_state != PCM_NOT_FADING) |
| 1235 | tick_remove_task(pcmbuf_fade_tick); |
| 1236 | |
| 1237 | fade_out_complete = false; |
| 1238 | |
| 1239 | pcm_play_unlock(); |
| 1240 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 1241 | if (!fade) |
| 1242 | { |
| 1243 | /* Simply set the level */ |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 1244 | fade_state = PCM_NOT_FADING; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 1245 | fade_vol = in ? MIX_AMP_UNITY : MIX_AMP_MUTE; |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 1246 | pcmbuf_update_volume(); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 1247 | } |
| 1248 | else |
| 1249 | { |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 1250 | /* Set direction and resume fade from current point */ |
| 1251 | fade_state = in ? PCM_FADING_IN : PCM_FADING_OUT; |
| 1252 | tick_add_task(pcmbuf_fade_tick); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | |
Michael Sevakis | 5078d46 | 2011-08-23 01:37:59 +0000 | [diff] [blame] | 1256 | /* Return 'true' if fade is in progress */ |
| 1257 | bool pcmbuf_fading(void) |
| 1258 | { |
| 1259 | return fade_state != PCM_NOT_FADING; |
| 1260 | } |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 1261 | |
Michael Sevakis | 697aa7f | 2011-08-25 00:12:19 +0000 | [diff] [blame] | 1262 | /* Quiet-down the channel if 'shhh' is true or else play at normal level */ |
| 1263 | void pcmbuf_soft_mode(bool shhh) |
| 1264 | { |
| 1265 | /* Have to block the tick or improper order could leave volume in soft |
| 1266 | mode if fading reads the old value first but updates after us. */ |
| 1267 | int res = fade_state != PCM_NOT_FADING ? |
| 1268 | tick_remove_task(pcmbuf_fade_tick) : -1; |
| 1269 | |
| 1270 | soft_mode = shhh; |
| 1271 | pcmbuf_update_volume(); |
| 1272 | |
| 1273 | if (res == 0) |
| 1274 | tick_add_task(pcmbuf_fade_tick); |
| 1275 | } |
| 1276 | |
| 1277 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 1278 | /** Misc */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1279 | |
| 1280 | bool pcmbuf_is_lowdata(void) |
| 1281 | { |
Michael Sevakis | 3156730 | 2011-08-28 14:26:59 +0000 | [diff] [blame] | 1282 | enum channel_status status = mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK); |
| 1283 | |
| 1284 | if (status != CHANNEL_PLAYING || pcmbuf_is_crossfade_active()) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1285 | return false; |
| 1286 | |
Michael Sevakis | c7f3a0b | 2012-01-20 18:10:12 -0500 | [diff] [blame] | 1287 | return pcmbuf_data_critical(); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | void pcmbuf_set_low_latency(bool state) |
| 1291 | { |
| 1292 | low_latency_mode = state; |
| 1293 | } |
| 1294 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1295 | /* Return the current position key value */ |
| 1296 | unsigned int pcmbuf_get_position_key(void) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1297 | { |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame] | 1298 | return position_key; |
| 1299 | } |
| 1300 | |
| 1301 | /* Set position updates to be synchronous and immediate in addition to during |
| 1302 | PCM frames - cancelled upon first codec insert or upon stopping */ |
| 1303 | void pcmbuf_sync_position_update(void) |
| 1304 | { |
| 1305 | pcmbuf_sync_position = true; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1306 | } |