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