Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Miika Pekkarinen |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include "config.h" |
Michael Sevakis | 6c399b8 | 2009-02-19 20:40:03 +0000 | [diff] [blame] | 23 | #include "system.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 24 | #include "debug.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 25 | #include <kernel.h> |
Michael Sevakis | 6077e5b | 2007-10-06 22:27:27 +0000 | [diff] [blame] | 26 | #include "pcm.h" |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 27 | #include "pcm_mixer.h" |
| 28 | #include "pcmbuf.h" |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 29 | #include "playback.h" |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 30 | #include "codec_thread.h" |
Jeffrey Goode | faa47bf | 2009-10-22 00:59:42 +0000 | [diff] [blame] | 31 | |
| 32 | /* Define LOGF_ENABLE to enable logf output in this file */ |
| 33 | /*#define LOGF_ENABLE*/ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 34 | #include "logf.h" |
Thomas Martitz | 35e8b14 | 2010-06-21 16:53:00 +0000 | [diff] [blame] | 35 | #if (CONFIG_PLATFORM & PLATFORM_NATIVE) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 36 | #include "cpu.h" |
| 37 | #endif |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 38 | #include <string.h> |
Miika Pekkarinen | f090dc3 | 2005-07-21 11:44:00 +0000 | [diff] [blame] | 39 | #include "settings.h" |
| 40 | #include "audio.h" |
Jeffrey Goode | d0ac045 | 2009-11-09 05:58:02 +0000 | [diff] [blame] | 41 | #include "voice_thread.h" |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 42 | #include "dsp.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 43 | |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 44 | #define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks |
| 45 | on the pcm buffer */ |
| 46 | #define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of |
| 47 | chunks on the pcm buffer (or we run out |
| 48 | of buffer descriptors, which is |
| 49 | non-fatal) */ |
| 50 | #define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than |
| 51 | this to the DMA */ |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 52 | #define CROSSFADE_BUFSIZE 8192 /* Size of the crossfade buffer */ |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 53 | |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 54 | /* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */ |
| 55 | #define BYTERATE (NATIVE_FREQUENCY * 4) |
| 56 | |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 57 | #if MEMORYSIZE > 2 |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 58 | /* Keep watermark high for iPods at least (2s) */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 59 | #define PCMBUF_WATERMARK (BYTERATE * 2) |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 60 | #else |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 61 | #define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */ |
Björn Stenberg | 6427d12 | 2009-01-10 21:10:56 +0000 | [diff] [blame] | 62 | #endif |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 63 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 64 | /* Structure we can use to queue pcm chunks in memory to be played |
| 65 | * by the driver code. */ |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 66 | struct chunkdesc |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 67 | { |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 68 | unsigned char *addr; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 69 | size_t size; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 70 | struct chunkdesc* link; |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 71 | /* true if last chunk in the track */ |
| 72 | bool end_of_track; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 73 | }; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 74 | |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 75 | #define NUM_CHUNK_DESCS(bufsize) \ |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 76 | ((bufsize) / PCMBUF_MINAVG_CHUNK) |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 77 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 78 | /* Size of the PCM buffer. */ |
| 79 | static size_t pcmbuf_size IDATA_ATTR = 0; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 80 | static char *pcmbuf_bufend IDATA_ATTR; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 81 | static char *pcmbuffer IDATA_ATTR; |
| 82 | /* Current PCM buffer write index. */ |
| 83 | static size_t pcmbuffer_pos IDATA_ATTR; |
| 84 | /* Amount pcmbuffer_pos will be increased.*/ |
| 85 | static size_t pcmbuffer_fillpos IDATA_ATTR; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 86 | |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 87 | static struct chunkdesc *first_desc; |
| 88 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 89 | /* Gapless playback */ |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 90 | static bool track_transition IDATA_ATTR; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 91 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 92 | /* Fade effect */ |
| 93 | static unsigned int fade_vol = MIX_AMP_UNITY; |
| 94 | |
| 95 | /* Voice */ |
| 96 | static bool soft_mode = false; |
| 97 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 98 | #ifdef HAVE_CROSSFADE |
| 99 | /* Crossfade buffer */ |
| 100 | static char *fadebuf IDATA_ATTR; |
| 101 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 102 | /* Crossfade related state */ |
| 103 | static bool crossfade_enabled; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 104 | static bool crossfade_enable_request; |
Miika Pekkarinen | c9a1b4e | 2006-05-14 14:08:26 +0000 | [diff] [blame] | 105 | static bool crossfade_mixmode; |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 106 | static bool crossfade_auto_skip; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 107 | static bool crossfade_active IDATA_ATTR; |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 108 | static bool crossfade_track_change_started IDATA_ATTR; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 109 | |
| 110 | /* Track the current location for processing crossfade */ |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 111 | static struct chunkdesc *crossfade_chunk IDATA_ATTR; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 112 | static size_t crossfade_sample IDATA_ATTR; |
| 113 | |
| 114 | /* Counters for fading in new data */ |
| 115 | static size_t crossfade_fade_in_total IDATA_ATTR; |
| 116 | static size_t crossfade_fade_in_rem IDATA_ATTR; |
Michael Giacomelli | 6469926 | 2009-08-11 02:05:38 +0000 | [diff] [blame] | 117 | #endif |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 118 | |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 119 | static struct chunkdesc *read_chunk IDATA_ATTR; |
| 120 | static struct chunkdesc *read_end_chunk IDATA_ATTR; |
| 121 | static struct chunkdesc *write_chunk IDATA_ATTR; |
| 122 | static struct chunkdesc *write_end_chunk IDATA_ATTR; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 123 | static size_t last_chunksize IDATA_ATTR; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 124 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 125 | static size_t pcmbuf_unplayed_bytes IDATA_ATTR; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 126 | static size_t pcmbuf_watermark IDATA_ATTR; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 127 | |
Miika Pekkarinen | 4408b6b | 2006-02-07 19:17:51 +0000 | [diff] [blame] | 128 | static bool low_latency_mode = false; |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 129 | static bool flush_pcmbuf = false; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 130 | |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 131 | #ifdef HAVE_PRIORITY_SCHEDULING |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 132 | static int codec_thread_priority = PRIORITY_PLAYBACK; |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 133 | #endif |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 134 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 135 | /* Helpful macros for use in conditionals this assumes some of the above |
| 136 | * static variable names */ |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 137 | #define COMMIT_IF_NEEDED if(pcmbuffer_fillpos > PCMBUF_TARGET_CHUNK || \ |
| 138 | (pcmbuffer_pos + pcmbuffer_fillpos) >= pcmbuf_size) commit_chunk(false) |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 139 | #define LOW_DATA(quarter_secs) \ |
| 140 | (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs) |
| 141 | |
Jeffrey Goode | e9f389a | 2009-11-06 04:25:28 +0000 | [diff] [blame] | 142 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 143 | static void crossfade_start(void); |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 144 | static void write_to_crossfade(size_t length); |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 145 | static void pcmbuf_finish_crossfade_enable(void); |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 146 | #endif |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 147 | |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 148 | /* Callbacks into playback.c */ |
| 149 | extern void audio_pcmbuf_position_callback(unsigned int time); |
| 150 | extern void audio_pcmbuf_track_change(bool pcmbuf); |
| 151 | extern bool audio_pcmbuf_may_play(void); |
| 152 | |
Jeffrey Goode | b6f15f2 | 2009-11-08 04:27:27 +0000 | [diff] [blame] | 153 | |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 154 | /**************************************/ |
| 155 | |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 156 | /* define this to show detailed chunkdesc usage information on the sim console */ |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 157 | /*#define DESC_DEBUG*/ |
| 158 | |
| 159 | #ifndef SIMULATOR |
| 160 | #undef DESC_DEBUG |
| 161 | #endif |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 162 | |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 163 | #ifdef DESC_DEBUG |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 164 | #define DISPLAY_DESC(caller) while(!show_desc(caller)) |
Jeffrey Goode | 84ca236 | 2009-11-18 20:56:19 +0000 | [diff] [blame] | 165 | #define DESC_IDX(desc) (desc ? desc - first_desc : -1) |
| 166 | #define SHOW_DESC(desc) if(DESC_IDX(desc)==-1) DEBUGF("--"); \ |
| 167 | else DEBUGF("%02d", DESC_IDX(desc)) |
| 168 | #define SHOW_DESC_LINK(desc) if(desc){SHOW_DESC(desc->link);DEBUGF(" ");} \ |
| 169 | else DEBUGF("-- ") |
| 170 | #define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \ |
| 171 | SHOW_DESC_LINK(desc) |
| 172 | #define SHOW_POINT(tag,desc) DEBUGF("%s",tag);SHOW_DETAIL(desc) |
| 173 | #define SHOW_NUM(num,desc) DEBUGF("%02d>",num);SHOW_DESC_LINK(desc) |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 174 | |
| 175 | static bool show_desc(char *caller) |
| 176 | { |
| 177 | if (show_desc_in_use) return false; |
| 178 | show_desc_in_use = true; |
| 179 | DEBUGF("%-14s\t", caller); |
Jeffrey Goode | 84ca236 | 2009-11-18 20:56:19 +0000 | [diff] [blame] | 180 | SHOW_POINT("r", read_chunk); |
| 181 | SHOW_POINT("re", read_end_chunk); |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 182 | DEBUGF(" "); |
Jeffrey Goode | 84ca236 | 2009-11-18 20:56:19 +0000 | [diff] [blame] | 183 | SHOW_POINT("w", write_chunk); |
| 184 | SHOW_POINT("we", write_end_chunk); |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 185 | DEBUGF("\n"); |
Jeffrey Goode | 84ca236 | 2009-11-18 20:56:19 +0000 | [diff] [blame] | 186 | int i; |
| 187 | for (i = 0; i < pcmbuf_descs(); i++) |
| 188 | { |
| 189 | SHOW_NUM(i, (first_desc + i)); |
| 190 | if (i%10 == 9) DEBUGF("\n"); |
| 191 | } |
| 192 | DEBUGF("\n\n"); |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 193 | show_desc_in_use = false; |
| 194 | return true; |
| 195 | } |
| 196 | #else |
| 197 | #define DISPLAY_DESC(caller) do{}while(0) |
| 198 | #endif |
| 199 | |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 200 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 201 | /** Accept new PCM data */ |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 202 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 203 | /* Commit PCM buffer samples as a new chunk for playback */ |
| 204 | static void commit_chunk(bool flush_next_time) |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 205 | { |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 206 | if (!pcmbuffer_fillpos) |
| 207 | return; |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 208 | |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 209 | /* Never use the last buffer descriptor */ |
| 210 | while (write_chunk == write_end_chunk) { |
| 211 | /* If this happens, something is being stupid */ |
| 212 | if (!pcm_is_playing()) { |
| 213 | logf("commit_chunk error"); |
| 214 | pcmbuf_play_start(); |
| 215 | } |
| 216 | /* Let approximately one chunk of data playback */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 217 | sleep(HZ * PCMBUF_TARGET_CHUNK / BYTERATE); |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 218 | } |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 219 | |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 220 | /* commit the chunk */ |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 221 | |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 222 | register size_t size = pcmbuffer_fillpos; |
| 223 | /* Grab the next description to write, and change the write pointer */ |
| 224 | register struct chunkdesc *pcmbuf_current = write_chunk; |
| 225 | write_chunk = pcmbuf_current->link; |
| 226 | /* Fill in the values in the new buffer chunk */ |
| 227 | pcmbuf_current->addr = &pcmbuffer[pcmbuffer_pos]; |
| 228 | pcmbuf_current->size = size; |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 229 | pcmbuf_current->end_of_track = false; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 230 | pcmbuf_current->link = NULL; |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 231 | |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 232 | if (read_chunk != NULL) |
| 233 | { |
Jeffrey Goode | d0ac045 | 2009-11-09 05:58:02 +0000 | [diff] [blame] | 234 | if (flush_pcmbuf) |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 235 | { |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 236 | /* Flush! Discard all data after the currently playing chunk, |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 237 | and make the current chunk play next */ |
Jeffrey Goode | 84ca236 | 2009-11-18 20:56:19 +0000 | [diff] [blame] | 238 | logf("commit_chunk: flush"); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 239 | pcm_play_lock(); |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 240 | write_end_chunk->link = read_chunk->link; |
| 241 | read_chunk->link = pcmbuf_current; |
| 242 | while (write_end_chunk->link) |
| 243 | { |
| 244 | write_end_chunk = write_end_chunk->link; |
| 245 | pcmbuf_unplayed_bytes -= write_end_chunk->size; |
| 246 | } |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 247 | |
| 248 | read_chunk->end_of_track = track_transition; |
| 249 | pcm_play_unlock(); |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 250 | } |
| 251 | /* If there is already a read buffer setup, add to it */ |
| 252 | else |
| 253 | read_end_chunk->link = pcmbuf_current; |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 254 | } |
| 255 | else |
| 256 | { |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 257 | /* Otherwise create the buffer */ |
| 258 | read_chunk = pcmbuf_current; |
| 259 | } |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 260 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 261 | /* If flush_next_time is true, then the current chunk will be thrown out |
| 262 | * and the next chunk to be committed will be the next to be played. |
| 263 | * This is used to empty the PCM buffer for a track change. */ |
| 264 | flush_pcmbuf = flush_next_time; |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 265 | |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 266 | /* This is now the last buffer to read */ |
| 267 | read_end_chunk = pcmbuf_current; |
| 268 | |
| 269 | /* Update bytes counters */ |
| 270 | pcmbuf_unplayed_bytes += size; |
| 271 | |
| 272 | pcmbuffer_pos += size; |
| 273 | if (pcmbuffer_pos >= pcmbuf_size) |
| 274 | pcmbuffer_pos -= pcmbuf_size; |
| 275 | |
| 276 | pcmbuffer_fillpos = 0; |
Jeffrey Goode | 73c4791 | 2009-11-09 06:53:22 +0000 | [diff] [blame] | 277 | DISPLAY_DESC("commit_chunk"); |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 280 | /* Set priority of the codec thread */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 281 | #ifdef HAVE_PRIORITY_SCHEDULING |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 282 | /* |
| 283 | * expects pcm_fill_state in tenth-% units (e.g. full pcm buffer is 10) */ |
| 284 | static void boost_codec_thread(int pcm_fill_state) |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 285 | { |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 286 | static const int prios[11] = { |
| 287 | PRIORITY_PLAYBACK_MAX, /* 0 - 10% */ |
| 288 | PRIORITY_PLAYBACK_MAX+1, /* 10 - 20% */ |
| 289 | PRIORITY_PLAYBACK_MAX+3, /* 20 - 30% */ |
| 290 | PRIORITY_PLAYBACK_MAX+5, /* 30 - 40% */ |
| 291 | PRIORITY_PLAYBACK_MAX+7, /* 40 - 50% */ |
| 292 | PRIORITY_PLAYBACK_MAX+8, /* 50 - 60% */ |
| 293 | PRIORITY_PLAYBACK_MAX+9, /* 60 - 70% */ |
| 294 | /* raiseing priority above 70% shouldn't be needed */ |
| 295 | PRIORITY_PLAYBACK, /* 70 - 80% */ |
| 296 | PRIORITY_PLAYBACK, /* 80 - 90% */ |
| 297 | PRIORITY_PLAYBACK, /* 90 -100% */ |
| 298 | PRIORITY_PLAYBACK, /* 100% */ |
| 299 | }; |
| 300 | int new_prio = prios[pcm_fill_state]; |
| 301 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 302 | /* Keep voice and codec threads at the same priority or else voice |
| 303 | * will starve if the codec thread's priority is boosted. */ |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 304 | if (new_prio != codec_thread_priority) |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 305 | { |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 306 | codec_thread_set_priority(new_prio); |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 307 | voice_thread_set_priority(new_prio); |
| 308 | codec_thread_priority = new_prio; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 311 | #else |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 312 | #define boost_codec_thread(pcm_fill_state) do{}while(0) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 313 | #endif /* HAVE_PRIORITY_SCHEDULING */ |
| 314 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 315 | /* Return true if the PCM buffer is able to receive new data. |
| 316 | * Also maintain buffer level above the watermark. */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 317 | static bool prepare_insert(size_t length) |
| 318 | { |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 319 | bool playing = mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED; |
| 320 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 321 | if (low_latency_mode) |
| 322 | { |
| 323 | /* 1/4s latency. */ |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 324 | if (!LOW_DATA(1) && playing) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | |
| 328 | /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */ |
| 329 | if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK) |
| 330 | return false; |
| 331 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 332 | /* Maintain the buffer level above the watermark */ |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 333 | if (playing) |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 334 | { |
| 335 | /* Only codec thread initiates boost - voice boosts the cpu when playing |
| 336 | a clip */ |
| 337 | #ifndef SIMULATOR |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 338 | if (is_codec_thread()) |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 339 | #endif /* SIMULATOR */ |
| 340 | { |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 341 | /* boost cpu if necessary */ |
| 342 | if (pcmbuf_unplayed_bytes < pcmbuf_watermark) |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 343 | trigger_cpu_boost(); |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 344 | boost_codec_thread(pcmbuf_unplayed_bytes*10/pcmbuf_size); |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 347 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 348 | /* Disable crossfade if < .5s of audio */ |
| 349 | if (LOW_DATA(2)) |
| 350 | { |
| 351 | crossfade_active = false; |
| 352 | } |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 353 | #endif |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 354 | } |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 355 | else /* !playing */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 356 | { |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 357 | /* Boost CPU for pre-buffer */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 358 | trigger_cpu_boost(); |
| 359 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 360 | /* If pre-buffered to the watermark, start playback */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 361 | #if MEMORYSIZE > 2 |
| 362 | if (!LOW_DATA(4)) |
| 363 | #else |
| 364 | if (pcmbuf_unplayed_bytes > pcmbuf_watermark) |
| 365 | #endif |
| 366 | { |
| 367 | logf("pcm starting"); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 368 | if (audio_pcmbuf_may_play()) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 369 | pcmbuf_play_start(); |
| 370 | } |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 371 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 372 | |
| 373 | return true; |
Jeffrey Goode | 04b01e1 | 2009-11-05 21:59:36 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 376 | /* Request space in the buffer for writing output samples */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 377 | void *pcmbuf_request_buffer(int *count) |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 378 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 379 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 380 | /* we're going to crossfade to a new track, which is now on its way */ |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 381 | if (crossfade_track_change_started) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 382 | crossfade_start(); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 383 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 384 | /* crossfade has begun, put the new track samples in fadebuf */ |
| 385 | if (crossfade_active) |
| 386 | { |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 387 | int cnt = MIN(*count, CROSSFADE_BUFSIZE/4); |
| 388 | if (prepare_insert(cnt << 2)) |
| 389 | { |
| 390 | *count = cnt; |
| 391 | return fadebuf; |
| 392 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 393 | } |
| 394 | else |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 395 | #endif |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 396 | /* if possible, reserve room in the PCM buffer for new samples */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 397 | { |
| 398 | if(prepare_insert(*count << 2)) |
| 399 | { |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 400 | size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos; |
| 401 | if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 402 | { |
| 403 | /* Usual case, there's space here */ |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 404 | return &pcmbuffer[pcmbuffer_index]; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 405 | } |
| 406 | else |
| 407 | { |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 408 | /* Wrap the buffer, the new samples go at the beginning */ |
| 409 | commit_chunk(false); |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 410 | pcmbuffer_pos = 0; |
| 411 | return &pcmbuffer[0]; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 414 | } |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 415 | /* PCM buffer not ready to receive new data yet */ |
Jeffrey Goode | d0ac045 | 2009-11-09 05:58:02 +0000 | [diff] [blame] | 416 | return NULL; |
Jeffrey Goode | 5ce8e2c | 2009-11-04 03:58:33 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 419 | /* Handle new samples to the buffer */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 420 | void pcmbuf_write_complete(int count) |
| 421 | { |
| 422 | size_t length = (size_t)(unsigned int)count << 2; |
| 423 | #ifdef HAVE_CROSSFADE |
| 424 | if (crossfade_active) |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 425 | write_to_crossfade(length); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 426 | else |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 427 | #endif |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 428 | { |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 429 | pcmbuffer_fillpos += length; |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 430 | COMMIT_IF_NEEDED; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 434 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 435 | /** Init */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 436 | |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 437 | static inline void init_pcmbuffers(void) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 438 | { |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 439 | first_desc = write_chunk; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 440 | struct chunkdesc *next = write_chunk; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 441 | next++; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 442 | write_end_chunk = write_chunk; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 443 | while ((void *)next < (void *)pcmbuf_bufend) { |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 444 | write_end_chunk->link=next; |
| 445 | write_end_chunk=next; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 446 | next++; |
| 447 | } |
| 448 | DISPLAY_DESC("init"); |
| 449 | } |
| 450 | |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 451 | static size_t get_next_required_pcmbuf_size(void) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 452 | { |
| 453 | size_t seconds = 1; |
| 454 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 455 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 456 | if (crossfade_enable_request) |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 457 | seconds += global_settings.crossfade_fade_out_delay + |
| 458 | global_settings.crossfade_fade_out_duration; |
| 459 | #endif |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 460 | |
| 461 | #if MEMORYSIZE > 2 |
| 462 | /* Buffer has to be at least 2s long. */ |
| 463 | seconds += 2; |
| 464 | #endif |
| 465 | logf("pcmbuf len: %ld", (long)seconds); |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 466 | return seconds * BYTERATE; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 469 | /* Initialize the pcmbuffer the structure looks like this: |
| 470 | * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */ |
| 471 | size_t pcmbuf_init(unsigned char *bufend) |
| 472 | { |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 473 | unsigned char *voicebuf; |
| 474 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 475 | pcmbuf_bufend = bufend; |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 476 | pcmbuf_size = get_next_required_pcmbuf_size(); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 477 | write_chunk = (struct chunkdesc *)pcmbuf_bufend - |
| 478 | NUM_CHUNK_DESCS(pcmbuf_size); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 479 | voicebuf = (unsigned char *)write_chunk - |
| 480 | voicebuf_init((unsigned char *)write_chunk); |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 481 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 482 | fadebuf = voicebuf - CROSSFADE_BUFSIZE; |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 483 | pcmbuffer = fadebuf - pcmbuf_size; |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 484 | #else |
| 485 | pcmbuffer = voicebuf - pcmbuf_size; |
| 486 | #endif |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 487 | |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 488 | init_pcmbuffers(); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 489 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 490 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 491 | pcmbuf_finish_crossfade_enable(); |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 492 | #else |
| 493 | pcmbuf_watermark = PCMBUF_WATERMARK; |
| 494 | #endif |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 495 | |
| 496 | pcmbuf_play_stop(); |
| 497 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 498 | pcmbuf_soft_mode(false); |
| 499 | |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 500 | return pcmbuf_bufend - pcmbuffer; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 504 | /** Track change */ |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 505 | void pcmbuf_monitor_track_change(bool monitor) |
| 506 | { |
| 507 | pcm_play_lock(); |
| 508 | |
| 509 | if (last_chunksize != 0) |
| 510 | { |
| 511 | /* If monitoring, wait until this track runs out. Place in |
| 512 | currently playing chunk. If not, cancel notification. */ |
| 513 | track_transition = monitor; |
| 514 | read_end_chunk->end_of_track = monitor; |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 515 | if (!monitor) |
| 516 | { |
| 517 | /* Clear all notifications */ |
| 518 | struct chunkdesc *desc = first_desc; |
| 519 | struct chunkdesc *end = desc + pcmbuf_descs(); |
| 520 | while (desc < end) |
| 521 | desc++->end_of_track = false; |
| 522 | } |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 523 | } |
| 524 | else |
| 525 | { |
| 526 | /* Post now if PCM stopped and last buffer was sent. */ |
| 527 | track_transition = false; |
| 528 | if (monitor) |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 529 | audio_pcmbuf_track_change(false); |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | pcm_play_unlock(); |
| 533 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 534 | |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 535 | bool pcmbuf_start_track_change(bool auto_skip) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 536 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 537 | bool crossfade = false; |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 538 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 539 | /* Determine whether this track change needs to crossfade */ |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 540 | if(crossfade_enabled && !pcmbuf_is_crossfade_active()) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 541 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 542 | switch(global_settings.crossfade) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 543 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 544 | case CROSSFADE_ENABLE_AUTOSKIP: |
| 545 | crossfade = auto_skip; |
| 546 | break; |
| 547 | case CROSSFADE_ENABLE_MANSKIP: |
| 548 | crossfade = !auto_skip; |
| 549 | break; |
| 550 | case CROSSFADE_ENABLE_SHUFFLE: |
| 551 | crossfade = global_settings.playlist_shuffle; |
| 552 | break; |
Jeffrey Goode | 29d2711 | 2009-11-12 15:42:37 +0000 | [diff] [blame] | 553 | case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP: |
| 554 | crossfade = global_settings.playlist_shuffle || !auto_skip; |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 555 | break; |
| 556 | case CROSSFADE_ENABLE_ALWAYS: |
| 557 | crossfade = true; |
| 558 | break; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 559 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 560 | } |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 561 | #endif |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 562 | |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 563 | if (!auto_skip || crossfade) |
| 564 | /* manual skip or crossfade */ |
| 565 | { |
| 566 | if (crossfade) |
| 567 | { logf(" crossfade track change"); } |
| 568 | else |
| 569 | { logf(" manual track change"); } |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 570 | |
| 571 | pcm_play_lock(); |
| 572 | |
| 573 | /* Cancel any pending automatic gapless transition */ |
| 574 | pcmbuf_monitor_track_change(false); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 575 | |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 576 | /* Can't do two crossfades at once and, no fade if pcm is off now */ |
| 577 | if ( |
| 578 | #ifdef HAVE_CROSSFADE |
| 579 | pcmbuf_is_crossfade_active() || |
| 580 | #endif |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 581 | mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED) |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 582 | { |
| 583 | pcmbuf_play_stop(); |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 584 | pcm_play_unlock(); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 585 | /* Notify playback that the track change starts now */ |
| 586 | return true; |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 589 | /* Not enough data, or not crossfading, flush the old data instead */ |
| 590 | if (LOW_DATA(2) || !crossfade || low_latency_mode) |
| 591 | { |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 592 | commit_chunk(true); |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 593 | } |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 594 | #ifdef HAVE_CROSSFADE |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 595 | else |
| 596 | { |
| 597 | /* Don't enable mix mode when skipping tracks manually. */ |
| 598 | crossfade_mixmode = auto_skip && |
| 599 | global_settings.crossfade_fade_out_mixmode; |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 600 | |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 601 | crossfade_auto_skip = auto_skip; |
| 602 | |
| 603 | crossfade_track_change_started = crossfade; |
| 604 | } |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 605 | #endif |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 606 | pcm_play_unlock(); |
| 607 | |
| 608 | /* Keep trigger outside the play lock or HW FIFO underruns can happen |
| 609 | since frequency scaling is *not* always fast */ |
| 610 | trigger_cpu_boost(); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 611 | |
| 612 | /* Notify playback that the track change starts now */ |
| 613 | return true; |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 614 | } |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 615 | else /* automatic and not crossfading, so do gapless track change */ |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 616 | { |
| 617 | /* The codec is moving on to the next track, but the current track will |
| 618 | * continue to play. Set a flag to make sure the elapsed time of the |
| 619 | * current track will be updated properly, and mark the current chunk |
| 620 | * as the last one in the track. */ |
| 621 | logf(" gapless track change"); |
Michael Sevakis | 6510973 | 2011-02-23 14:31:13 +0000 | [diff] [blame] | 622 | pcmbuf_monitor_track_change(true); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 623 | return false; |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 624 | } |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 628 | /** Playback */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 629 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 630 | /* PCM driver callback |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 631 | * This function has 3 major logical parts (separated by brackets both for |
| 632 | * readability and variable scoping). The first part performs the |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 633 | * operations related to finishing off the last chunk we fed to the DMA. |
| 634 | * The second part detects the end of playlist condition when the PCM |
| 635 | * buffer is empty except for uncommitted samples. Then they are committed |
| 636 | * and sent to the PCM driver for playback. The third part performs the |
| 637 | * operations involved in sending a new chunk to the DMA. */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 638 | static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR; |
| 639 | static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) |
| 640 | { |
| 641 | { |
| 642 | struct chunkdesc *pcmbuf_current = read_chunk; |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 643 | /* Take the finished chunk out of circulation */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 644 | read_chunk = pcmbuf_current->link; |
| 645 | |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 646 | /* if during a track transition, update the elapsed time in ms */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 647 | if (track_transition) |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 648 | audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE); |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 649 | |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 650 | /* if last chunk in the track, stop updates and notify audio thread */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 651 | if (pcmbuf_current->end_of_track) |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 652 | { |
| 653 | track_transition = false; |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 654 | audio_pcmbuf_track_change(true); |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 655 | } |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 656 | |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 657 | /* Put the finished chunk back into circulation */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 658 | write_end_chunk->link = pcmbuf_current; |
| 659 | write_end_chunk = pcmbuf_current; |
| 660 | |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 661 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 662 | /* If we've read over the crossfade chunk while it's still fading */ |
| 663 | if (pcmbuf_current == crossfade_chunk) |
| 664 | crossfade_chunk = read_chunk; |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 665 | #endif |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 666 | } |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 667 | |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 668 | { |
| 669 | /* Commit last samples at end of playlist */ |
| 670 | if (pcmbuffer_fillpos && !read_chunk) |
| 671 | { |
| 672 | logf("pcmbuf_pcm_callback: commit last samples"); |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 673 | commit_chunk(false); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
| 677 | { |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 678 | /* Send the new chunk to the DMA */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 679 | if(read_chunk) |
| 680 | { |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 681 | last_chunksize = read_chunk->size; |
| 682 | pcmbuf_unplayed_bytes -= last_chunksize; |
| 683 | *size = last_chunksize; |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 684 | *start = read_chunk->addr; |
| 685 | } |
| 686 | else |
| 687 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 688 | /* No more chunks */ |
| 689 | logf("pcmbuf_pcm_callback: no more chunks"); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 690 | last_chunksize = 0; |
| 691 | *size = 0; |
| 692 | *start = NULL; |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | DISPLAY_DESC("callback"); |
| 696 | } |
| 697 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 698 | /* Force playback */ |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 699 | void pcmbuf_play_start(void) |
| 700 | { |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 701 | if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED && |
| 702 | pcmbuf_unplayed_bytes && read_chunk != NULL) |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 703 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 704 | logf("pcmbuf_play_start"); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 705 | last_chunksize = read_chunk->size; |
| 706 | pcmbuf_unplayed_bytes -= last_chunksize; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 707 | mixer_channel_play_data(PCM_MIXER_CHAN_PLAYBACK, |
| 708 | pcmbuf_pcm_callback, NULL, 0); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | void pcmbuf_play_stop(void) |
| 713 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 714 | logf("pcmbuf_play_stop"); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 715 | mixer_channel_stop(PCM_MIXER_CHAN_PLAYBACK); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 716 | |
| 717 | pcmbuf_unplayed_bytes = 0; |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 718 | if (read_chunk) { |
| 719 | write_end_chunk->link = read_chunk; |
| 720 | write_end_chunk = read_end_chunk; |
| 721 | read_chunk = read_end_chunk = NULL; |
| 722 | } |
Michael Sevakis | ba8ce00 | 2011-02-25 03:51:52 +0000 | [diff] [blame] | 723 | last_chunksize = 0; |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 724 | pcmbuffer_pos = 0; |
| 725 | pcmbuffer_fillpos = 0; |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 726 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 727 | crossfade_track_change_started = false; |
| 728 | crossfade_active = false; |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 729 | #endif |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 730 | track_transition = false; |
Jeffrey Goode | 84ca236 | 2009-11-18 20:56:19 +0000 | [diff] [blame] | 731 | flush_pcmbuf = false; |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 732 | DISPLAY_DESC("play_stop"); |
| 733 | |
Thomas Martitz | 9afb55a | 2010-12-22 16:03:15 +0000 | [diff] [blame] | 734 | /* Can unboost the codec thread here no matter who's calling, |
| 735 | * pretend full pcm buffer to unboost */ |
| 736 | boost_codec_thread(10); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | void pcmbuf_pause(bool pause) |
| 740 | { |
Jeffrey Goode | 664dc90 | 2009-11-11 00:48:17 +0000 | [diff] [blame] | 741 | logf("pcmbuf_pause: %s", pause?"pause":"play"); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 742 | |
| 743 | if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED) |
| 744 | mixer_channel_play_pause(PCM_MIXER_CHAN_PLAYBACK, !pause); |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 745 | else if (!pause) |
| 746 | pcmbuf_play_start(); |
| 747 | } |
| 748 | |
| 749 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 750 | /** Crossfade */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 751 | |
Jeffrey Goode | b6f15f2 | 2009-11-08 04:27:27 +0000 | [diff] [blame] | 752 | /* Clip sample to signed 16 bit range */ |
| 753 | static inline int32_t clip_sample_16(int32_t sample) |
| 754 | { |
| 755 | if ((int16_t)sample != sample) |
| 756 | sample = 0x7fff ^ (sample >> 31); |
| 757 | return sample; |
| 758 | } |
| 759 | |
Michael Giacomelli | 6469926 | 2009-08-11 02:05:38 +0000 | [diff] [blame] | 760 | #ifdef HAVE_CROSSFADE |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 761 | /* Find the chunk that's (length) deep in the list. Return the position within |
| 762 | * the chunk, and leave the chunkdesc pointer pointing to the chunk. */ |
| 763 | static size_t find_chunk(size_t length, struct chunkdesc **chunk) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 764 | { |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 765 | while (*chunk && length >= (*chunk)->size) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 766 | { |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 767 | length -= (*chunk)->size; |
| 768 | *chunk = (*chunk)->link; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 769 | } |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 770 | return length; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 773 | /* Returns the number of bytes _NOT_ mixed/faded */ |
| 774 | static size_t crossfade_mix_fade(int factor, size_t length, const char *buf, |
| 775 | size_t *out_sample, struct chunkdesc **out_chunk) |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 776 | { |
Steve Bavin | b2dc7f0 | 2009-11-26 14:41:31 +0000 | [diff] [blame] | 777 | if (length == 0) |
| 778 | return 0; |
| 779 | |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 780 | const int16_t *input_buf = (const int16_t *)buf; |
| 781 | int16_t *output_buf = (int16_t *)((*out_chunk)->addr); |
| 782 | int16_t *chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size); |
| 783 | output_buf = &output_buf[*out_sample]; |
| 784 | int32_t sample; |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 785 | |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 786 | while (length) |
| 787 | { |
| 788 | /* fade left and right channel at once to keep buffer alignment */ |
| 789 | int i; |
| 790 | for (i = 0; i < 2; i++) |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 791 | { |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 792 | if (input_buf) |
| 793 | /* fade the input buffer and mix into the chunk */ |
| 794 | { |
| 795 | sample = *input_buf++; |
| 796 | sample = ((sample * factor) >> 8) + *output_buf; |
| 797 | *output_buf++ = clip_sample_16(sample); |
| 798 | } |
| 799 | else |
| 800 | /* fade the chunk only */ |
| 801 | { |
| 802 | sample = *output_buf; |
| 803 | *output_buf++ = (sample * factor) >> 8; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | length -= 4; /* 2 samples, each 16 bit -> 4 bytes */ |
| 808 | |
| 809 | /* move to next chunk as needed */ |
| 810 | if (output_buf >= chunk_end) |
| 811 | { |
| 812 | *out_chunk = (*out_chunk)->link; |
| 813 | if (!(*out_chunk)) |
| 814 | return length; |
| 815 | output_buf = (int16_t *)((*out_chunk)->addr); |
| 816 | chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size); |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 819 | *out_sample = output_buf - (int16_t *)((*out_chunk)->addr); |
| 820 | return 0; |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 823 | /* Initializes crossfader, calculates all necessary parameters and performs |
| 824 | * fade-out with the PCM buffer. */ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 825 | static void crossfade_start(void) |
| 826 | { |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 827 | size_t crossfade_rem; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 828 | size_t crossfade_need; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 829 | size_t fade_out_rem; |
| 830 | size_t fade_out_delay; |
| 831 | size_t fade_in_delay; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 832 | |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 833 | crossfade_track_change_started = false; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 834 | /* Reject crossfade if less than .5s of data */ |
| 835 | if (LOW_DATA(2)) { |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 836 | logf("crossfade rejected"); |
| 837 | pcmbuf_play_stop(); |
| 838 | return ; |
| 839 | } |
| 840 | |
| 841 | logf("crossfade_start"); |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 842 | commit_chunk(false); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 843 | crossfade_active = true; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 844 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 845 | /* Initialize the crossfade buffer size to all of the buffered data that |
| 846 | * has not yet been sent to the DMA */ |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 847 | crossfade_rem = pcmbuf_unplayed_bytes; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 848 | crossfade_chunk = read_chunk->link; |
Brandon Low | 8ef1827 | 2006-04-24 12:41:30 +0000 | [diff] [blame] | 849 | crossfade_sample = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 850 | |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 851 | /* Get fade out info from settings. */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 852 | fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE; |
| 853 | fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 854 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 855 | crossfade_need = fade_out_delay + fade_out_rem; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 856 | if (crossfade_rem > crossfade_need) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 857 | { |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 858 | if (crossfade_auto_skip) |
| 859 | /* Automatic track changes only modify the last part of the buffer, |
| 860 | * so find the right chunk and sample to start the crossfade */ |
| 861 | { |
| 862 | crossfade_sample = find_chunk(crossfade_rem - crossfade_need, |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 863 | &crossfade_chunk) / 2; |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 864 | crossfade_rem = crossfade_need; |
| 865 | } |
| 866 | else |
| 867 | /* Manual skips occur immediately, but give time to process */ |
| 868 | { |
| 869 | crossfade_rem -= crossfade_chunk->size; |
| 870 | crossfade_chunk = crossfade_chunk->link; |
| 871 | } |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 872 | } |
| 873 | /* Truncate fade out duration if necessary. */ |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 874 | if (crossfade_rem < crossfade_need) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 875 | { |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 876 | size_t crossfade_short = crossfade_need - crossfade_rem; |
| 877 | if (fade_out_rem >= crossfade_short) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 878 | fade_out_rem -= crossfade_short; |
| 879 | else |
| 880 | { |
| 881 | fade_out_delay -= crossfade_short - fade_out_rem; |
| 882 | fade_out_rem = 0; |
| 883 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 884 | } |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 885 | crossfade_rem -= fade_out_delay + fade_out_rem; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 886 | |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 887 | /* Completely process the crossfade fade-out effect with current PCM buffer */ |
| 888 | if (!crossfade_mixmode) |
| 889 | { |
| 890 | /* Fade out the specified amount of the already processed audio */ |
| 891 | size_t total_fade_out = fade_out_rem; |
| 892 | size_t fade_out_sample; |
| 893 | struct chunkdesc *fade_out_chunk = crossfade_chunk; |
| 894 | |
| 895 | /* Find the right chunk and sample to start fading out */ |
| 896 | fade_out_delay += crossfade_sample * 2; |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 897 | fade_out_sample = find_chunk(fade_out_delay, &fade_out_chunk) / 2; |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 898 | |
| 899 | while (fade_out_rem > 0) |
| 900 | { |
| 901 | /* Each 1/10 second of audio will have the same fade applied */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 902 | size_t block_rem = MIN(BYTERATE / 10, fade_out_rem); |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 903 | int factor = (fade_out_rem << 8) / total_fade_out; |
| 904 | |
| 905 | fade_out_rem -= block_rem; |
| 906 | |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 907 | crossfade_mix_fade(factor, block_rem, NULL, |
| 908 | &fade_out_sample, &fade_out_chunk); |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 909 | } |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 910 | |
Jeffrey Goode | d1963e1 | 2009-11-13 21:58:41 +0000 | [diff] [blame] | 911 | /* zero out the rest of the buffer */ |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 912 | crossfade_mix_fade(0, crossfade_rem, NULL, |
| 913 | &fade_out_sample, &fade_out_chunk); |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* Initialize fade-in counters */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 917 | crossfade_fade_in_total = global_settings.crossfade_fade_in_duration * BYTERATE; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 918 | crossfade_fade_in_rem = crossfade_fade_in_total; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 919 | |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 920 | fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 921 | |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 922 | /* Find the right chunk and sample to start fading in */ |
| 923 | fade_in_delay += crossfade_sample * 2; |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 924 | crossfade_sample = find_chunk(fade_in_delay, &crossfade_chunk) / 2; |
Jeffrey Goode | 15351e8 | 2009-11-12 04:24:41 +0000 | [diff] [blame] | 925 | logf("crossfade_start done!"); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 928 | /* Perform fade-in of new track */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 929 | static void write_to_crossfade(size_t length) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 930 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 931 | if (length) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 932 | { |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 933 | char *buf = fadebuf; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 934 | if (crossfade_fade_in_rem) |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 935 | { |
| 936 | size_t samples; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 937 | int16_t *input_buf; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 938 | |
| 939 | /* Fade factor for this packet */ |
| 940 | int factor = |
| 941 | ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) / |
| 942 | crossfade_fade_in_total; |
| 943 | /* Bytes to fade */ |
| 944 | size_t fade_rem = MIN(length, crossfade_fade_in_rem); |
| 945 | |
| 946 | /* We _will_ fade this many bytes */ |
| 947 | crossfade_fade_in_rem -= fade_rem; |
| 948 | |
| 949 | if (crossfade_chunk) |
| 950 | { |
| 951 | /* Mix the data */ |
| 952 | size_t fade_total = fade_rem; |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 953 | fade_rem = crossfade_mix_fade(factor, fade_rem, buf, |
| 954 | &crossfade_sample, &crossfade_chunk); |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 955 | length -= fade_total - fade_rem; |
| 956 | buf += fade_total - fade_rem; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 957 | if (!length) |
| 958 | return; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 961 | samples = fade_rem / 2; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 962 | input_buf = (int16_t *)buf; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 963 | /* Fade remaining samples in place */ |
Jeffrey Goode | 96bb9ef | 2009-10-23 01:01:00 +0000 | [diff] [blame] | 964 | while (samples--) |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 965 | { |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 966 | int32_t sample = *input_buf; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 967 | *input_buf++ = (sample * factor) >> 8; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 968 | } |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 969 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 970 | |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 971 | if (crossfade_chunk) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 972 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 973 | /* Mix the data */ |
| 974 | size_t mix_total = length; |
Jeffrey Goode | 873c5b6 | 2009-11-15 04:57:40 +0000 | [diff] [blame] | 975 | /* A factor of 256 means mix only, no fading */ |
| 976 | length = crossfade_mix_fade(256, length, buf, |
| 977 | &crossfade_sample, &crossfade_chunk); |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 978 | buf += mix_total - length; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 979 | if (!length) |
| 980 | return; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 981 | } |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 982 | |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 983 | while (length > 0) |
| 984 | { |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 985 | COMMIT_IF_NEEDED; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 986 | size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 987 | size_t copy_n = MIN(length, pcmbuf_size - pcmbuffer_index); |
| 988 | memcpy(&pcmbuffer[pcmbuffer_index], buf, copy_n); |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 989 | buf += copy_n; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 990 | pcmbuffer_fillpos += copy_n; |
Jeffrey Goode | 013fe35 | 2009-11-05 17:32:32 +0000 | [diff] [blame] | 991 | length -= copy_n; |
| 992 | } |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 993 | } |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 994 | /* if no more fading-in to do, stop the crossfade */ |
| 995 | if (!(crossfade_fade_in_rem || crossfade_chunk)) |
| 996 | crossfade_active = false; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 999 | static void pcmbuf_finish_crossfade_enable(void) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1000 | { |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1001 | /* Copy the pending setting over now */ |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 1002 | crossfade_enabled = crossfade_enable_request; |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 1003 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1004 | pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ? |
| 1005 | /* If crossfading, try to keep the buffer full other than 1 second */ |
Jeffrey Goode | ba9280d | 2009-11-16 04:42:34 +0000 | [diff] [blame] | 1006 | (pcmbuf_size - BYTERATE) : |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1007 | /* Otherwise, just use the default */ |
| 1008 | PCMBUF_WATERMARK; |
| 1009 | } |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 1010 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1011 | bool pcmbuf_is_crossfade_active(void) |
| 1012 | { |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 1013 | return crossfade_active || crossfade_track_change_started; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | void pcmbuf_request_crossfade_enable(bool on_off) |
| 1017 | { |
| 1018 | /* Next setting to be used, not applied now */ |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 1019 | crossfade_enable_request = on_off; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | bool pcmbuf_is_same_size(void) |
| 1023 | { |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 1024 | /* if pcmbuffer is NULL, then not set up yet even once so always */ |
| 1025 | bool same_size = pcmbuffer ? |
| 1026 | (get_next_required_pcmbuf_size() == pcmbuf_size) : true; |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 1027 | |
Jeffrey Goode | 5c69a42 | 2009-11-09 17:11:53 +0000 | [diff] [blame] | 1028 | /* no buffer change needed, so finish crossfade setup now */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1029 | if (same_size) |
| 1030 | pcmbuf_finish_crossfade_enable(); |
Jeffrey Goode | 8cb4b36 | 2011-05-09 21:52:06 +0000 | [diff] [blame] | 1031 | |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1032 | return same_size; |
| 1033 | } |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 1034 | #endif /* HAVE_CROSSFADE */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1035 | |
| 1036 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 1037 | /** Debug menu, other metrics */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1038 | |
| 1039 | /* Amount of bytes left in the buffer. */ |
| 1040 | size_t pcmbuf_free(void) |
| 1041 | { |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 1042 | if (read_chunk != NULL) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1043 | { |
Jeffrey Goode | c8944c0 | 2010-05-28 13:21:24 +0000 | [diff] [blame] | 1044 | void *read = (void *)read_chunk->addr; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 1045 | void *write = &pcmbuffer[pcmbuffer_pos + pcmbuffer_fillpos]; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1046 | if (read < write) |
| 1047 | return (size_t)(read - write) + pcmbuf_size; |
| 1048 | else |
| 1049 | return (size_t) (read - write); |
| 1050 | } |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 1051 | return pcmbuf_size - pcmbuffer_fillpos; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | size_t pcmbuf_get_bufsize(void) |
| 1055 | { |
| 1056 | return pcmbuf_size; |
| 1057 | } |
| 1058 | |
| 1059 | int pcmbuf_used_descs(void) |
| 1060 | { |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 1061 | struct chunkdesc *temp = read_chunk; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1062 | unsigned int i = 0; |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 1063 | while (temp) { |
| 1064 | temp = temp->link; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1065 | i++; |
| 1066 | } |
| 1067 | return i; |
| 1068 | } |
| 1069 | |
| 1070 | int pcmbuf_descs(void) |
| 1071 | { |
Jeffrey Goode | 874c911 | 2009-11-09 18:12:20 +0000 | [diff] [blame] | 1072 | return NUM_CHUNK_DESCS(pcmbuf_size); |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | #ifdef ROCKBOX_HAS_LOGF |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 1076 | unsigned char *pcmbuf_get_meminfo(size_t *length) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1077 | { |
Jeffrey Goode | 8edac6e | 2009-11-09 05:45:05 +0000 | [diff] [blame] | 1078 | *length = pcmbuf_bufend - pcmbuffer; |
| 1079 | return pcmbuffer; |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1080 | } |
| 1081 | #endif |
| 1082 | |
| 1083 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 1084 | /** Fading and channel volume control */ |
| 1085 | |
| 1086 | /* Sync the channel amplitude to all states */ |
| 1087 | static void pcmbuf_update_volume(void) |
| 1088 | { |
| 1089 | unsigned int vol = fade_vol; |
| 1090 | |
| 1091 | if (soft_mode) |
| 1092 | vol >>= 2; |
| 1093 | |
| 1094 | mixer_channel_set_amplitude(PCM_MIXER_CHAN_PLAYBACK, vol); |
| 1095 | } |
| 1096 | |
| 1097 | /* Quiet-down the channel if 'shhh' is true or else play at normal level */ |
| 1098 | void pcmbuf_soft_mode(bool shhh) |
| 1099 | { |
| 1100 | soft_mode = shhh; |
| 1101 | pcmbuf_update_volume(); |
| 1102 | } |
| 1103 | |
| 1104 | /* Fade channel in or out */ |
| 1105 | void pcmbuf_fade(bool fade, bool in) |
| 1106 | { |
| 1107 | if (!fade) |
| 1108 | { |
| 1109 | /* Simply set the level */ |
| 1110 | fade_vol = in ? MIX_AMP_UNITY : MIX_AMP_MUTE; |
| 1111 | } |
| 1112 | else |
| 1113 | { |
| 1114 | /* Start from the opposing end */ |
| 1115 | fade_vol = in ? MIX_AMP_MUTE : MIX_AMP_UNITY; |
| 1116 | } |
| 1117 | |
| 1118 | pcmbuf_update_volume(); |
| 1119 | |
| 1120 | if (fade) |
| 1121 | { |
| 1122 | /* Do this on thread for now */ |
| 1123 | int old_prio = thread_set_priority(thread_self(), PRIORITY_REALTIME); |
| 1124 | |
| 1125 | while (1) |
| 1126 | { |
| 1127 | /* Linear fade actually sounds better */ |
| 1128 | if (in) |
| 1129 | fade_vol += MIN(MIX_AMP_UNITY/16, MIX_AMP_UNITY - fade_vol); |
| 1130 | else |
| 1131 | fade_vol -= MIN(MIX_AMP_UNITY/16, fade_vol - MIX_AMP_MUTE); |
| 1132 | |
| 1133 | pcmbuf_update_volume(); |
| 1134 | |
| 1135 | if (fade_vol > MIX_AMP_MUTE && fade_vol < MIX_AMP_UNITY) |
| 1136 | { |
| 1137 | sleep(0); |
| 1138 | continue; |
| 1139 | } |
| 1140 | |
| 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | thread_set_priority(thread_self(), old_prio); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | |
Jeffrey Goode | 0db3308 | 2009-11-11 07:02:18 +0000 | [diff] [blame] | 1149 | /** Misc */ |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1150 | |
| 1151 | bool pcmbuf_is_lowdata(void) |
| 1152 | { |
Jeffrey Goode | 9e09534 | 2009-11-10 03:46:08 +0000 | [diff] [blame] | 1153 | if (!pcm_is_playing() || pcm_is_paused() |
| 1154 | #ifdef HAVE_CROSSFADE |
| 1155 | || pcmbuf_is_crossfade_active() |
| 1156 | #endif |
| 1157 | ) |
Jeffrey Goode | 37adbee | 2009-11-06 04:13:36 +0000 | [diff] [blame] | 1158 | return false; |
| 1159 | |
| 1160 | #if MEMORYSIZE > 2 |
| 1161 | /* 1 seconds of buffer is low data */ |
| 1162 | return LOW_DATA(4); |
| 1163 | #else |
| 1164 | /* under watermark is low data */ |
| 1165 | return (pcmbuf_unplayed_bytes < pcmbuf_watermark); |
| 1166 | #endif |
| 1167 | } |
| 1168 | |
| 1169 | void pcmbuf_set_low_latency(bool state) |
| 1170 | { |
| 1171 | low_latency_mode = state; |
| 1172 | } |
| 1173 | |
| 1174 | unsigned long pcmbuf_get_latency(void) |
| 1175 | { |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame^] | 1176 | return (pcmbuf_unplayed_bytes + |
| 1177 | mixer_channel_get_bytes_waiting(PCM_MIXER_CHAN_PLAYBACK)) * 1000 / BYTERATE; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1178 | } |