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 | ****************************************************************************/ |
| 21 | |
| 22 | #include <stdbool.h> |
| 23 | #include <stdio.h> |
| 24 | #include "config.h" |
| 25 | #include "debug.h" |
| 26 | #include "panic.h" |
| 27 | #include <kernel.h> |
| 28 | #include "pcmbuf.h" |
Michael Sevakis | 6077e5b | 2007-10-06 22:27:27 +0000 | [diff] [blame] | 29 | #include "pcm.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 30 | #include "logf.h" |
| 31 | #ifndef SIMULATOR |
| 32 | #include "cpu.h" |
| 33 | #endif |
| 34 | #include "system.h" |
| 35 | #include <string.h> |
| 36 | #include "buffer.h" |
Miika Pekkarinen | f090dc3 | 2005-07-21 11:44:00 +0000 | [diff] [blame] | 37 | #include "settings.h" |
| 38 | #include "audio.h" |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 39 | #include "voice_thread.h" |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 40 | #include "dsp.h" |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 41 | #include "thread.h" |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 42 | |
Michael Sevakis | 3bec38e | 2007-06-24 20:36:58 +0000 | [diff] [blame] | 43 | /* Define PCMBUF_MUTING if the codec requires muting to prevent pops */ |
Michael Sevakis | 405e90e | 2008-05-05 14:52:07 +0000 | [diff] [blame] | 44 | #if !defined(HAVE_UDA1380) && !defined(HAVE_TLV320) && !defined(HAVE_AS3514) \ |
| 45 | && !defined(HAVE_WM8978) |
Michael Sevakis | bf65676 | 2006-11-16 05:33:42 +0000 | [diff] [blame] | 46 | #define PCMBUF_MUTING |
| 47 | #endif |
| 48 | |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 49 | /* Clip sample to signed 16 bit range */ |
| 50 | static inline int32_t clip_sample_16(int32_t sample) |
| 51 | { |
| 52 | if ((int16_t)sample != sample) |
| 53 | sample = 0x7fff ^ (sample >> 31); |
| 54 | return sample; |
| 55 | } |
| 56 | |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 57 | /* Keep watermark high for iPods at least (2s) */ |
Brandon Low | 1f3360f | 2006-10-13 20:48:23 +0000 | [diff] [blame] | 58 | #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 59 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 60 | /* Structure we can use to queue pcm chunks in memory to be played |
| 61 | * by the driver code. */ |
| 62 | struct pcmbufdesc |
| 63 | { |
| 64 | void *addr; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 65 | size_t size; |
| 66 | struct pcmbufdesc* link; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 67 | /* Call this when the buffer has been played */ |
| 68 | void (*callback)(void); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 69 | }; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 70 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 71 | #define PCMBUF_DESCS(bufsize) \ |
| 72 | ((bufsize) / PCMBUF_MINAVG_CHUNK) |
| 73 | #define PCMBUF_DESCS_SIZE(bufsize) \ |
| 74 | (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc)) |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 75 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 76 | /* Size of the PCM buffer. */ |
| 77 | static size_t pcmbuf_size IDATA_ATTR = 0; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 78 | static char *pcmbuf_bufend IDATA_ATTR; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 79 | static char *audiobuffer IDATA_ATTR; |
| 80 | /* Current audio buffer write index. */ |
| 81 | static size_t audiobuffer_pos IDATA_ATTR; |
| 82 | /* Amount audiobuffer_pos will be increased.*/ |
| 83 | static size_t audiobuffer_fillpos IDATA_ATTR; |
| 84 | static char *fadebuf IDATA_ATTR; |
| 85 | static char *voicebuf IDATA_ATTR; |
| 86 | |
| 87 | static void (*pcmbuf_event_handler)(void) IDATA_ATTR; |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 88 | static void (*position_callback)(size_t size) IDATA_ATTR; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 89 | |
| 90 | /* Crossfade related state */ |
| 91 | static bool crossfade_enabled; |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 92 | static bool crossfade_enabled_pending; |
Miika Pekkarinen | c9a1b4e | 2006-05-14 14:08:26 +0000 | [diff] [blame] | 93 | static bool crossfade_mixmode; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 94 | static bool crossfade_active IDATA_ATTR; |
| 95 | static bool crossfade_init IDATA_ATTR; |
| 96 | |
| 97 | /* Track the current location for processing crossfade */ |
| 98 | static struct pcmbufdesc *crossfade_chunk IDATA_ATTR; |
| 99 | static size_t crossfade_sample IDATA_ATTR; |
| 100 | |
| 101 | /* Counters for fading in new data */ |
| 102 | static size_t crossfade_fade_in_total IDATA_ATTR; |
| 103 | static size_t crossfade_fade_in_rem IDATA_ATTR; |
| 104 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 105 | static struct pcmbufdesc *pcmbuf_read IDATA_ATTR; |
| 106 | static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR; |
| 107 | static struct pcmbufdesc *pcmbuf_write IDATA_ATTR; |
| 108 | static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR; |
| 109 | static size_t last_chunksize IDATA_ATTR; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 110 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 111 | static size_t pcmbuf_unplayed_bytes IDATA_ATTR; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 112 | static size_t pcmbuf_watermark IDATA_ATTR; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 113 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 114 | static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR; |
| 115 | static size_t pcmbuf_mix_sample IDATA_ATTR; |
Brandon Low | 2da61ff | 2006-04-24 01:32:28 +0000 | [diff] [blame] | 116 | |
Miika Pekkarinen | 4408b6b | 2006-02-07 19:17:51 +0000 | [diff] [blame] | 117 | static bool low_latency_mode = false; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 118 | static bool pcmbuf_flush; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 119 | |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 120 | #ifdef HAVE_PRIORITY_SCHEDULING |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 121 | static int codec_thread_priority = PRIORITY_PLAYBACK; |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 122 | #endif |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 123 | |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 124 | extern struct thread_entry *codec_thread_p; |
| 125 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 126 | /* Helpful macros for use in conditionals this assumes some of the above |
| 127 | * static variable names */ |
| 128 | #define NEED_FLUSH(position) \ |
| 129 | (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size) |
| 130 | #define LOW_DATA(quarter_secs) \ |
| 131 | (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs) |
| 132 | |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 133 | static bool prepare_insert(size_t length); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 134 | static void pcmbuf_under_watermark(void); |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 135 | static bool pcmbuf_flush_fillpos(void); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 136 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 137 | #define CALL_IF_EXISTS(function, args...) if (function) function(args) |
| 138 | /* This function has 2 major logical parts (separated by brackets both for |
| 139 | * readability and variable scoping). The first part performs the |
| 140 | * operastions related to finishing off the last buffer we fed to the DMA. |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 141 | * The second part performs the operations involved in sending a new buffer |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 142 | * to the DMA. Finally the function checks the status of the buffer and |
| 143 | * boosts if necessary */ |
| 144 | static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR; |
| 145 | static void pcmbuf_callback(unsigned char** start, size_t* size) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 146 | { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 147 | { |
| 148 | struct pcmbufdesc *pcmbuf_current = pcmbuf_read; |
| 149 | /* Take the finished buffer out of circulation */ |
| 150 | pcmbuf_read = pcmbuf_current->link; |
| 151 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 152 | /* The buffer is finished, call the callback functions */ |
| 153 | CALL_IF_EXISTS(position_callback, last_chunksize); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 154 | CALL_IF_EXISTS(pcmbuf_current->callback); |
| 155 | |
| 156 | /* Put the finished buffer back into circulation */ |
| 157 | pcmbuf_write_end->link = pcmbuf_current; |
| 158 | pcmbuf_write_end = pcmbuf_current; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 159 | |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 160 | /* If we've read over the mix chunk while it's still mixing there */ |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 161 | if (pcmbuf_current == pcmbuf_mix_chunk) |
| 162 | pcmbuf_mix_chunk = NULL; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 163 | /* If we've read over the crossfade chunk while it's still fading */ |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 164 | if (pcmbuf_current == crossfade_chunk) |
Brandon Low | 2306433 | 2006-04-25 16:15:11 +0000 | [diff] [blame] | 165 | crossfade_chunk = pcmbuf_read; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | { |
| 169 | /* Send the new buffer to the pcm */ |
| 170 | struct pcmbufdesc *pcmbuf_new = pcmbuf_read; |
| 171 | size_t *realsize = size; |
| 172 | unsigned char** realstart = start; |
| 173 | if(pcmbuf_new) |
| 174 | { |
| 175 | size_t current_size = pcmbuf_new->size; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 176 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 177 | pcmbuf_unplayed_bytes -= current_size; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 178 | last_chunksize = current_size; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 179 | *realsize = current_size; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 180 | *realstart = pcmbuf_new->addr; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | /* No more buffers */ |
| 185 | last_chunksize = 0; |
| 186 | *realsize = 0; |
| 187 | *realstart = NULL; |
| 188 | CALL_IF_EXISTS(pcmbuf_event_handler); |
| 189 | } |
| 190 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 193 | void pcmbuf_set_position_callback(void (*callback)(size_t size)) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 194 | { |
Brandon Low | a3868d3 | 2006-01-21 22:42:44 +0000 | [diff] [blame] | 195 | position_callback = callback; |
| 196 | } |
| 197 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 198 | static void pcmbuf_set_watermark_bytes(void) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 199 | { |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 200 | pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ? |
| 201 | /* If crossfading, try to keep the buffer full other than 1 second */ |
| 202 | (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) : |
| 203 | /* Otherwise, just keep it above 2 second */ |
| 204 | PCMBUF_WATERMARK; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 207 | /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep |
| 208 | * in a separate function for the moment */ |
| 209 | static inline void pcmbuf_add_chunk(void) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 210 | { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 211 | register size_t size = audiobuffer_fillpos; |
| 212 | /* Grab the next description to write, and change the write pointer */ |
| 213 | register struct pcmbufdesc *pcmbuf_current = pcmbuf_write; |
| 214 | pcmbuf_write = pcmbuf_current->link; |
| 215 | /* Fill in the values in the new buffer chunk */ |
| 216 | pcmbuf_current->addr = &audiobuffer[audiobuffer_pos]; |
| 217 | pcmbuf_current->size = size; |
| 218 | pcmbuf_current->callback = pcmbuf_event_handler; |
| 219 | pcmbuf_current->link = NULL; |
| 220 | /* This is single use only */ |
| 221 | pcmbuf_event_handler = NULL; |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 222 | if (pcmbuf_read != NULL) { |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 223 | if (pcmbuf_flush) |
| 224 | { |
| 225 | pcmbuf_write_end->link = pcmbuf_read->link; |
| 226 | pcmbuf_read->link = pcmbuf_current; |
| 227 | while (pcmbuf_write_end->link) |
| 228 | { |
| 229 | pcmbuf_write_end = pcmbuf_write_end->link; |
| 230 | pcmbuf_unplayed_bytes -= pcmbuf_write_end->size; |
| 231 | } |
| 232 | pcmbuf_flush = false; |
| 233 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 234 | /* If there is already a read buffer setup, add to it */ |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 235 | else |
| 236 | pcmbuf_read_end->link = pcmbuf_current; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 237 | } else { |
| 238 | /* Otherwise create the buffer */ |
| 239 | pcmbuf_read = pcmbuf_current; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 240 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 241 | /* This is now the last buffer to read */ |
| 242 | pcmbuf_read_end = pcmbuf_current; |
| 243 | |
| 244 | /* Update bytes counters */ |
| 245 | pcmbuf_unplayed_bytes += size; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 246 | |
| 247 | audiobuffer_pos += size; |
| 248 | if (audiobuffer_pos >= pcmbuf_size) |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 249 | audiobuffer_pos -= pcmbuf_size; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 250 | |
| 251 | audiobuffer_fillpos = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 254 | #ifdef HAVE_PRIORITY_SCHEDULING |
| 255 | static void boost_codec_thread(bool boost) |
| 256 | { |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 257 | /* Keep voice and codec threads at the same priority or else voice |
| 258 | * will starve if the codec thread's priority is boosted. */ |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 259 | if (boost) |
| 260 | { |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 261 | int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes |
| 262 | / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX; |
| 263 | |
| 264 | if (priority != codec_thread_priority) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 265 | { |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 266 | codec_thread_priority = priority; |
| 267 | thread_set_priority(codec_thread_p, priority); |
| 268 | voice_thread_set_priority(priority); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 269 | } |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 270 | } |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 271 | else if (codec_thread_priority != PRIORITY_PLAYBACK) |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 272 | { |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 273 | thread_set_priority(codec_thread_p, PRIORITY_PLAYBACK); |
| 274 | voice_thread_set_priority(PRIORITY_PLAYBACK); |
| 275 | codec_thread_priority = PRIORITY_PLAYBACK; |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | #endif /* HAVE_PRIORITY_SCHEDULING */ |
| 279 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 280 | static void pcmbuf_under_watermark(void) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 281 | { |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 282 | /* Only codec thread initiates boost - voice boosts the cpu when playing |
| 283 | a clip */ |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 284 | #ifndef SIMULATOR |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 285 | if (thread_get_current() == codec_thread_p) |
Michael Sevakis | 938593b | 2007-03-06 21:00:45 +0000 | [diff] [blame] | 286 | #endif /* SIMULATOR */ |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 287 | { |
Miika Pekkarinen | 7058752 | 2006-10-15 11:57:52 +0000 | [diff] [blame] | 288 | #ifdef HAVE_PRIORITY_SCHEDULING |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 289 | /* If buffer is critically low, override UI priority, else |
| 290 | set back to the original priority. */ |
| 291 | boost_codec_thread(LOW_DATA(2) && pcm_is_playing()); |
Miika Pekkarinen | 7058752 | 2006-10-15 11:57:52 +0000 | [diff] [blame] | 292 | #endif |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 293 | /* Fill audio buffer by boosting cpu */ |
| 294 | trigger_cpu_boost(); |
| 295 | } |
Miika Pekkarinen | 7058752 | 2006-10-15 11:57:52 +0000 | [diff] [blame] | 296 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 297 | /* Disable crossfade if < .5s of audio */ |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 298 | if (LOW_DATA(2)) |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 299 | { |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 300 | crossfade_active = false; |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 301 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 304 | void pcmbuf_set_event_handler(void (*event_handler)(void)) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 305 | { |
| 306 | pcmbuf_event_handler = event_handler; |
| 307 | } |
| 308 | |
| 309 | unsigned int pcmbuf_get_latency(void) |
| 310 | { |
Nicolas Pennequin | ca55f13 | 2008-01-08 14:33:17 +0000 | [diff] [blame] | 311 | /* Be careful how this calculation is rearranged, it's easy to overflow */ |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 312 | size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting(); |
| 313 | return bytes / 4 / (NATIVE_FREQUENCY/1000); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Miika Pekkarinen | 4408b6b | 2006-02-07 19:17:51 +0000 | [diff] [blame] | 316 | void pcmbuf_set_low_latency(bool state) |
| 317 | { |
| 318 | low_latency_mode = state; |
| 319 | } |
| 320 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 321 | bool pcmbuf_is_lowdata(void) |
| 322 | { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 323 | if (!pcm_is_playing() || pcm_is_paused() || |
| 324 | crossfade_init || crossfade_active) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 325 | return false; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 326 | |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 327 | /* 1 seconds of buffer is low data */ |
| 328 | return LOW_DATA(4); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 331 | /* Amount of bytes left in the buffer. */ |
| 332 | inline size_t pcmbuf_free(void) |
| 333 | { |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 334 | if (pcmbuf_read != NULL) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 335 | { |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 336 | void *read = pcmbuf_read->addr; |
| 337 | void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos]; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 338 | if (read < write) |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 339 | return (size_t)(read - write) + pcmbuf_size; |
| 340 | else |
| 341 | return (size_t) (read - write); |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 342 | } |
| 343 | return pcmbuf_size; |
| 344 | } |
| 345 | |
Miika Pekkarinen | a4f8d1c | 2006-01-27 16:25:44 +0000 | [diff] [blame] | 346 | bool pcmbuf_crossfade_init(bool manual_skip) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 347 | { |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 348 | /* Can't do two crossfades at once and, no fade if pcm is off now */ |
| 349 | if (crossfade_init || crossfade_active || !pcm_is_playing()) |
| 350 | { |
| 351 | pcmbuf_play_stop(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 352 | return false; |
| 353 | } |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 354 | |
Miika Pekkarinen | 7058752 | 2006-10-15 11:57:52 +0000 | [diff] [blame] | 355 | trigger_cpu_boost(); |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 356 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 357 | /* Not enough data, or crossfade disabled, flush the old data instead */ |
Miika Pekkarinen | f3add92 | 2006-08-02 18:35:24 +0000 | [diff] [blame] | 358 | if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 359 | { |
Brandon Low | b961551 | 2006-05-05 03:13:06 +0000 | [diff] [blame] | 360 | pcmbuf_flush_fillpos(); |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 361 | pcmbuf_flush = true; |
| 362 | return false; |
| 363 | } |
| 364 | |
Miika Pekkarinen | a4f8d1c | 2006-01-27 16:25:44 +0000 | [diff] [blame] | 365 | /* Don't enable mix mode when skipping tracks manually. */ |
Miika Pekkarinen | c9a1b4e | 2006-05-14 14:08:26 +0000 | [diff] [blame] | 366 | if (manual_skip) |
| 367 | crossfade_mixmode = false; |
| 368 | else |
| 369 | crossfade_mixmode = global_settings.crossfade_fade_out_mixmode; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 370 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 371 | crossfade_init = true; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 372 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 373 | return true; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 374 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void pcmbuf_play_stop(void) |
| 378 | { |
| 379 | pcm_play_stop(); |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 380 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 381 | pcmbuf_unplayed_bytes = 0; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 382 | pcmbuf_mix_chunk = NULL; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 383 | if (pcmbuf_read) { |
| 384 | pcmbuf_write_end->link = pcmbuf_read; |
| 385 | pcmbuf_write_end = pcmbuf_read_end; |
| 386 | pcmbuf_read = pcmbuf_read_end = NULL; |
| 387 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 388 | audiobuffer_pos = 0; |
| 389 | audiobuffer_fillpos = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 390 | crossfade_init = false; |
| 391 | crossfade_active = false; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 392 | pcmbuf_flush = false; |
Michael Sevakis | b425de7 | 2007-03-06 20:32:13 +0000 | [diff] [blame] | 393 | |
| 394 | #ifdef HAVE_PRIORITY_SCHEDULING |
| 395 | /* Can unboost the codec thread here no matter who's calling */ |
| 396 | boost_codec_thread(false); |
| 397 | #endif |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 400 | int pcmbuf_used_descs(void) { |
| 401 | struct pcmbufdesc *pcmbuf_temp = pcmbuf_read; |
| 402 | unsigned int i = 0; |
| 403 | while (pcmbuf_temp) { |
| 404 | pcmbuf_temp = pcmbuf_temp->link; |
| 405 | i++; |
| 406 | } |
| 407 | return i; |
| 408 | } |
| 409 | |
| 410 | int pcmbuf_descs(void) { |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 411 | return PCMBUF_DESCS(pcmbuf_size); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 414 | static void pcmbuf_init_pcmbuffers(void) { |
| 415 | struct pcmbufdesc *next = pcmbuf_write; |
| 416 | next++; |
| 417 | pcmbuf_write_end = pcmbuf_write; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 418 | while ((void *)next < (void *)pcmbuf_bufend) { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 419 | pcmbuf_write_end->link=next; |
| 420 | pcmbuf_write_end=next; |
| 421 | next++; |
| 422 | } |
| 423 | } |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 424 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 425 | static size_t pcmbuf_get_next_required_pcmbuf_size(void) |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 426 | { |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 427 | #if MEM > 1 |
| 428 | size_t seconds = 1; |
| 429 | |
| 430 | if (crossfade_enabled_pending) |
| 431 | seconds += global_settings.crossfade_fade_out_delay |
| 432 | + global_settings.crossfade_fade_out_duration; |
| 433 | |
| 434 | /* Buffer has to be at least 2s long. */ |
| 435 | seconds += 2; |
| 436 | logf("pcmbuf len: %ld", seconds); |
| 437 | return seconds * (NATIVE_FREQUENCY*4); |
| 438 | #else |
| 439 | return NATIVE_FREQUENCY*2; |
| 440 | #endif |
| 441 | } |
| 442 | |
| 443 | static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize) |
| 444 | { |
| 445 | return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 + |
| 446 | PCMBUF_DESCS_SIZE(bufsize)); |
| 447 | } |
| 448 | |
| 449 | bool pcmbuf_is_same_size(void) |
| 450 | { |
| 451 | if (audiobuffer == NULL) |
| 452 | return true; /* Not set up yet even once so always */ |
| 453 | |
| 454 | size_t bufsize = pcmbuf_get_next_required_pcmbuf_size(); |
| 455 | return pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 458 | /* Initialize the pcmbuffer the structure looks like this: |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 459 | * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */ |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 460 | size_t pcmbuf_init(unsigned char *bufend) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 461 | { |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 462 | pcmbuf_bufend = bufend; |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 463 | pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size(); |
| 464 | audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size); |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 465 | fadebuf = &audiobuffer[pcmbuf_size]; |
| 466 | voicebuf = &fadebuf[PCMBUF_MIX_CHUNK]; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 467 | pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK]; |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 468 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 469 | pcmbuf_init_pcmbuffers(); |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 470 | |
Brandon Low | ee6a95a | 2006-01-22 00:03:20 +0000 | [diff] [blame] | 471 | position_callback = NULL; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 472 | pcmbuf_event_handler = NULL; |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 473 | |
| 474 | pcmbuf_crossfade_enable_finished(); |
| 475 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 476 | pcmbuf_play_stop(); |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 477 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 478 | return pcmbuf_bufend - audiobuffer; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 481 | size_t pcmbuf_get_bufsize(void) |
Miika Pekkarinen | f090dc3 | 2005-07-21 11:44:00 +0000 | [diff] [blame] | 482 | { |
| 483 | return pcmbuf_size; |
| 484 | } |
| 485 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 486 | #ifdef ROCKBOX_HAS_LOGF |
| 487 | unsigned char * pcmbuf_get_meminfo(size_t *length) |
| 488 | { |
| 489 | *length = pcmbuf_bufend - audiobuffer; |
| 490 | return audiobuffer; |
| 491 | } |
| 492 | #endif |
| 493 | |
Michael Sevakis | 3bec38e | 2007-06-24 20:36:58 +0000 | [diff] [blame] | 494 | void pcmbuf_pause(bool pause) |
| 495 | { |
Michael Sevakis | bf65676 | 2006-11-16 05:33:42 +0000 | [diff] [blame] | 496 | #ifdef PCMBUF_MUTING |
Brandon Low | 8307d0b | 2006-03-24 02:38:57 +0000 | [diff] [blame] | 497 | if (pause) |
Michael Sevakis | bf65676 | 2006-11-16 05:33:42 +0000 | [diff] [blame] | 498 | pcm_mute(true); |
| 499 | #endif |
Michael Sevakis | 6077e5b | 2007-10-06 22:27:27 +0000 | [diff] [blame] | 500 | |
| 501 | if (pcm_is_playing()) |
| 502 | pcm_play_pause(!pause); |
| 503 | else if (!pause) |
| 504 | pcmbuf_play_start(); |
| 505 | |
Michael Sevakis | bf65676 | 2006-11-16 05:33:42 +0000 | [diff] [blame] | 506 | #ifdef PCMBUF_MUTING |
Brandon Low | 8307d0b | 2006-03-24 02:38:57 +0000 | [diff] [blame] | 507 | if (!pause) |
| 508 | pcm_mute(false); |
Michael Sevakis | bf65676 | 2006-11-16 05:33:42 +0000 | [diff] [blame] | 509 | #endif |
Miika Pekkarinen | 7058752 | 2006-10-15 11:57:52 +0000 | [diff] [blame] | 510 | trigger_cpu_boost(); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 513 | /* Force playback. */ |
| 514 | void pcmbuf_play_start(void) |
| 515 | { |
Michael Sevakis | 3bec38e | 2007-06-24 20:36:58 +0000 | [diff] [blame] | 516 | if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL) |
Miika Pekkarinen | 6d6ca6b | 2006-01-22 10:25:07 +0000 | [diff] [blame] | 517 | { |
Michael Sevakis | 3bec38e | 2007-06-24 20:36:58 +0000 | [diff] [blame] | 518 | last_chunksize = pcmbuf_read->size; |
| 519 | pcmbuf_unplayed_bytes -= last_chunksize; |
| 520 | pcm_play_data(pcmbuf_callback, |
| 521 | (unsigned char *)pcmbuf_read->addr, last_chunksize); |
Miika Pekkarinen | 6d6ca6b | 2006-01-22 10:25:07 +0000 | [diff] [blame] | 522 | } |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 525 | /** |
| 526 | * Commit samples waiting to the pcm buffer. |
| 527 | */ |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 528 | static bool pcmbuf_flush_fillpos(void) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 529 | { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 530 | if (audiobuffer_fillpos) { |
| 531 | /* Never use the last buffer descriptor */ |
| 532 | while (pcmbuf_write == pcmbuf_write_end) { |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 533 | /* If this happens, something is being stupid */ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 534 | if (!pcm_is_playing()) { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 535 | logf("pcmbuf_flush_fillpos error"); |
Miika Pekkarinen | 6d6ca6b | 2006-01-22 10:25:07 +0000 | [diff] [blame] | 536 | pcmbuf_play_start(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 537 | } |
Brandon Low | 86f1e2e | 2006-03-24 13:43:15 +0000 | [diff] [blame] | 538 | /* Let approximately one chunk of data playback */ |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 539 | sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4)); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 540 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 541 | pcmbuf_add_chunk(); |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 542 | return true; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 543 | } |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 544 | return false; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 547 | /** |
| 548 | * Completely process the crossfade fade out effect with current pcm buffer. |
| 549 | */ |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 550 | static void crossfade_process_buffer(size_t fade_in_delay, |
| 551 | size_t fade_out_delay, size_t fade_out_rem) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 552 | { |
Miika Pekkarinen | c9a1b4e | 2006-05-14 14:08:26 +0000 | [diff] [blame] | 553 | if (!crossfade_mixmode) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 554 | { |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 555 | /* Fade out the specified amount of the already processed audio */ |
| 556 | size_t total_fade_out = fade_out_rem; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 557 | size_t fade_out_sample; |
| 558 | struct pcmbufdesc *fade_out_chunk = crossfade_chunk; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 559 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 560 | /* Find the right chunk to start fading out */ |
Brandon Low | 9ca16a6 | 2006-04-24 03:43:43 +0000 | [diff] [blame] | 561 | fade_out_delay += crossfade_sample * 2; |
Steve Bavin | d5dcb49 | 2006-09-27 15:23:10 +0000 | [diff] [blame] | 562 | while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 563 | { |
| 564 | fade_out_delay -= fade_out_chunk->size; |
| 565 | fade_out_chunk = fade_out_chunk->link; |
| 566 | } |
| 567 | /* The start sample within the chunk */ |
| 568 | fade_out_sample = fade_out_delay / 2; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 569 | |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 570 | while (fade_out_rem > 0) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 571 | { |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 572 | /* Each 1/10 second of audio will have the same fade applied */ |
Brandon Low | 1924769 | 2006-04-25 10:39:56 +0000 | [diff] [blame] | 573 | size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem); |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 574 | int factor = (fade_out_rem << 8) / total_fade_out; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 575 | |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 576 | fade_out_rem -= block_rem; |
| 577 | |
| 578 | /* Fade this block */ |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 579 | while (block_rem > 0 && fade_out_chunk != NULL) |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 580 | { |
| 581 | /* Fade one sample */ |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 582 | int16_t *buf = (int16_t *)fade_out_chunk->addr; |
| 583 | int32_t sample = buf[fade_out_sample]; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 584 | buf[fade_out_sample++] = (sample * factor) >> 8; |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 585 | |
Brandon Low | 1924769 | 2006-04-25 10:39:56 +0000 | [diff] [blame] | 586 | block_rem -= 2; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 587 | /* Move to the next chunk as needed */ |
| 588 | if (fade_out_sample * 2 >= fade_out_chunk->size) |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 589 | { |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 590 | fade_out_chunk = fade_out_chunk->link; |
| 591 | fade_out_sample = 0; |
Brandon Low | dbcc9c2 | 2006-03-30 00:47:05 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 594 | } |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 597 | /* Find the right chunk and sample to start fading in */ |
Brandon Low | 9ca16a6 | 2006-04-24 03:43:43 +0000 | [diff] [blame] | 598 | fade_in_delay += crossfade_sample * 2; |
Steve Bavin | d5dcb49 | 2006-09-27 15:23:10 +0000 | [diff] [blame] | 599 | while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 600 | { |
| 601 | fade_in_delay -= crossfade_chunk->size; |
| 602 | crossfade_chunk = crossfade_chunk->link; |
| 603 | } |
| 604 | crossfade_sample = fade_in_delay / 2; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 605 | logf("process done!"); |
| 606 | } |
| 607 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 608 | /* Initializes crossfader, calculates all necessary parameters and |
| 609 | * performs fade-out with the pcm buffer. */ |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 610 | static void crossfade_start(void) |
| 611 | { |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 612 | size_t crossfade_rem; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 613 | size_t crossfade_need; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 614 | size_t fade_out_rem; |
| 615 | size_t fade_out_delay; |
| 616 | size_t fade_in_delay; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 617 | |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 618 | crossfade_init = false; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 619 | /* Reject crossfade if less than .5s of data */ |
| 620 | if (LOW_DATA(2)) { |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 621 | logf("crossfade rejected"); |
| 622 | pcmbuf_play_stop(); |
| 623 | return ; |
| 624 | } |
| 625 | |
| 626 | logf("crossfade_start"); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 627 | pcmbuf_flush_fillpos(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 628 | crossfade_active = true; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 629 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 630 | /* Initialize the crossfade buffer size to all of the buffered data that |
| 631 | * has not yet been sent to the DMA */ |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 632 | crossfade_rem = pcmbuf_unplayed_bytes; |
| 633 | crossfade_chunk = pcmbuf_read->link; |
Brandon Low | 8ef1827 | 2006-04-24 12:41:30 +0000 | [diff] [blame] | 634 | crossfade_sample = 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 635 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 636 | /* Get fade out delay from settings. */ |
| 637 | fade_out_delay = |
| 638 | NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 639 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 640 | /* Get fade out duration from settings. */ |
| 641 | fade_out_rem = |
| 642 | NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4; |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 643 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 644 | crossfade_need = fade_out_delay + fade_out_rem; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 645 | /* We want only to modify the last part of the buffer. */ |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 646 | if (crossfade_rem > crossfade_need) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 647 | { |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 648 | size_t crossfade_extra = crossfade_rem - crossfade_need; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 649 | while (crossfade_extra > crossfade_chunk->size) |
| 650 | { |
| 651 | crossfade_extra -= crossfade_chunk->size; |
| 652 | crossfade_chunk = crossfade_chunk->link; |
| 653 | } |
| 654 | crossfade_sample = crossfade_extra / 2; |
| 655 | } |
| 656 | /* Truncate fade out duration if necessary. */ |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 657 | else if (crossfade_rem < crossfade_need) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 658 | { |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 659 | size_t crossfade_short = crossfade_need - crossfade_rem; |
| 660 | if (fade_out_rem >= crossfade_short) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 661 | fade_out_rem -= crossfade_short; |
| 662 | else |
| 663 | { |
| 664 | fade_out_delay -= crossfade_short - fade_out_rem; |
| 665 | fade_out_rem = 0; |
| 666 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 667 | } |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 668 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 669 | /* Get also fade in duration and delays from settings. */ |
| 670 | crossfade_fade_in_total = |
| 671 | NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4; |
| 672 | crossfade_fade_in_rem = crossfade_fade_in_total; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 673 | |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 674 | fade_in_delay = |
| 675 | NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4; |
| 676 | |
| 677 | crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 680 | /* Returns the number of bytes _NOT_ mixed */ |
| 681 | static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 682 | { |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 683 | const int16_t *input_buf = (const int16_t *)buf; |
| 684 | int16_t *output_buf = (int16_t *)(crossfade_chunk->addr); |
| 685 | int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size); |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 686 | output_buf = &output_buf[crossfade_sample]; |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 687 | int32_t sample; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 688 | |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 689 | while (fade_rem) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 690 | { |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 691 | /* fade left and right channel at once to keep buffer alignment */ |
| 692 | sample = *input_buf++; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 693 | sample = ((sample * factor) >> 8) + *output_buf; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 694 | *output_buf++ = clip_sample_16(sample); |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 695 | |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 696 | sample = *input_buf++; |
| 697 | sample = ((sample * factor) >> 8) + *output_buf; |
| 698 | *output_buf++ = clip_sample_16(sample); |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 699 | |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 700 | fade_rem -= 4; /* 2 samples, each 16 bit -> 4 bytes */ |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 701 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 702 | if (output_buf >= chunk_end) |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 703 | { |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 704 | crossfade_chunk = crossfade_chunk->link; |
| 705 | if (!crossfade_chunk) |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 706 | return fade_rem; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 707 | output_buf = (int16_t *)crossfade_chunk->addr; |
| 708 | chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size); |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 709 | } |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 710 | } |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 711 | crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | /* Returns the number of bytes _NOT_ mixed */ |
| 716 | static size_t crossfade_mix(const char *buf, size_t length) |
| 717 | { |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 718 | const int16_t *input_buf = (const int16_t *)buf; |
| 719 | int16_t *output_buf = (int16_t *)crossfade_chunk->addr; |
| 720 | int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size); |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 721 | output_buf = &output_buf[crossfade_sample]; |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 722 | int32_t sample; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 723 | |
| 724 | while (length) |
| 725 | { |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 726 | /* fade left and right channel at once to keep buffer alignment */ |
| 727 | sample = *input_buf++ + *output_buf; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 728 | *output_buf++ = clip_sample_16(sample); |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 729 | |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 730 | sample = *input_buf++ + *output_buf; |
| 731 | *output_buf++ = clip_sample_16(sample); |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 732 | |
Andree Buschmann | 21870cf | 2008-03-30 10:11:31 +0000 | [diff] [blame] | 733 | length -= 4; /* 2 samples, each 16 bit -> 4 bytes */ |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 734 | |
| 735 | if (output_buf >= chunk_end) |
| 736 | { |
| 737 | crossfade_chunk = crossfade_chunk->link; |
| 738 | if (!crossfade_chunk) |
| 739 | return length; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 740 | |
Michael Sevakis | 18330a0 | 2007-11-16 14:34:15 +0000 | [diff] [blame] | 741 | output_buf = (int16_t *)crossfade_chunk->addr; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 742 | chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size); |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 745 | crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 746 | return 0; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 749 | static void pcmbuf_flush_buffer(const char *buf, size_t length) |
| 750 | { |
| 751 | size_t copy_n; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 752 | while (length > 0) { |
| 753 | size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; |
| 754 | if (NEED_FLUSH(audiobuffer_index)) |
| 755 | { |
| 756 | pcmbuf_flush_fillpos(); |
| 757 | audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; |
| 758 | } |
| 759 | copy_n = MIN(length, pcmbuf_size - audiobuffer_index); |
| 760 | memcpy(&audiobuffer[audiobuffer_index], buf, copy_n); |
| 761 | buf += copy_n; |
| 762 | audiobuffer_fillpos += copy_n; |
| 763 | length -= copy_n; |
| 764 | } |
| 765 | } |
| 766 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 767 | static void flush_crossfade(char *buf, size_t length) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 768 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 769 | if (length) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 770 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 771 | if (crossfade_fade_in_rem) |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 772 | { |
| 773 | size_t samples; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 774 | int16_t *input_buf; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 775 | |
| 776 | /* Fade factor for this packet */ |
| 777 | int factor = |
| 778 | ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) / |
| 779 | crossfade_fade_in_total; |
| 780 | /* Bytes to fade */ |
| 781 | size_t fade_rem = MIN(length, crossfade_fade_in_rem); |
| 782 | |
| 783 | /* We _will_ fade this many bytes */ |
| 784 | crossfade_fade_in_rem -= fade_rem; |
| 785 | |
| 786 | if (crossfade_chunk) |
| 787 | { |
| 788 | /* Mix the data */ |
| 789 | size_t fade_total = fade_rem; |
| 790 | fade_rem = crossfade_fade_mix(factor, buf, fade_rem); |
| 791 | length -= fade_total - fade_rem; |
| 792 | buf += fade_total - fade_rem; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 793 | if (!length) |
| 794 | return; |
| 795 | if (!fade_rem) |
| 796 | goto fade_done; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 799 | samples = fade_rem / 2; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 800 | input_buf = (int16_t *)buf; |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 801 | /* Fade remaining samples in place */ |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 802 | while (samples) |
| 803 | { |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 804 | int32_t sample = *input_buf; |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 805 | *input_buf++ = (sample * factor) >> 8; |
| 806 | samples--; |
| 807 | } |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 808 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 809 | |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 810 | fade_done: |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 811 | if (crossfade_chunk) |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 812 | { |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 813 | /* Mix the data */ |
| 814 | size_t mix_total = length; |
| 815 | length = crossfade_mix(buf, length); |
| 816 | buf += mix_total - length; |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 817 | if (!length) |
| 818 | return; |
Brandon Low | 6c0908b | 2006-04-23 22:54:34 +0000 | [diff] [blame] | 819 | } |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 820 | |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 821 | /* Flush samples to the buffer */ |
| 822 | while (!prepare_insert(length)) |
| 823 | sleep(1); |
| 824 | pcmbuf_flush_buffer(buf, length); |
Brandon Low | 9aa49a4 | 2006-04-25 02:28:21 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static bool prepare_insert(size_t length) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 830 | { |
Miika Pekkarinen | 4408b6b | 2006-02-07 19:17:51 +0000 | [diff] [blame] | 831 | if (low_latency_mode) |
| 832 | { |
| 833 | /* 1/4s latency. */ |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 834 | if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 2 |
Miika Pekkarinen | 4408b6b | 2006-02-07 19:17:51 +0000 | [diff] [blame] | 835 | && pcm_is_playing()) |
| 836 | return false; |
| 837 | } |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 838 | |
Brandon Low | 3bbd93b | 2006-02-13 17:08:53 +0000 | [diff] [blame] | 839 | /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */ |
Brandon Low | 9602dd7 | 2006-04-25 12:34:28 +0000 | [diff] [blame] | 840 | if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 841 | return false; |
Miika Pekkarinen | d83b659 | 2005-07-19 19:57:23 +0000 | [diff] [blame] | 842 | |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 843 | if (!pcm_is_playing()) |
| 844 | { |
Miika Pekkarinen | 7058752 | 2006-10-15 11:57:52 +0000 | [diff] [blame] | 845 | trigger_cpu_boost(); |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 846 | |
Miika Pekkarinen | 4408b6b | 2006-02-07 19:17:51 +0000 | [diff] [blame] | 847 | /* Pre-buffer 1s. */ |
Dave Chapman | 0a7ded3 | 2006-08-13 09:19:24 +0000 | [diff] [blame] | 848 | #if MEM <= 1 |
Tomasz Malesinski | 28910e7 | 2006-08-12 23:01:52 +0000 | [diff] [blame] | 849 | if (!LOW_DATA(1)) |
| 850 | #else |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 851 | if (!LOW_DATA(4)) |
Tomasz Malesinski | 28910e7 | 2006-08-12 23:01:52 +0000 | [diff] [blame] | 852 | #endif |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 853 | { |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 854 | logf("pcm starting"); |
Michael Sevakis | 6077e5b | 2007-10-06 22:27:27 +0000 | [diff] [blame] | 855 | if (!(audio_status() & AUDIO_STATUS_PAUSE)) |
| 856 | pcmbuf_play_start(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 857 | } |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 858 | } |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 859 | else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark) |
Brandon Low | 3a37fae | 2006-02-13 16:55:25 +0000 | [diff] [blame] | 860 | pcmbuf_under_watermark(); |
| 861 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 862 | return true; |
| 863 | } |
| 864 | |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 865 | void* pcmbuf_request_buffer(int *count) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 866 | { |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 867 | if (crossfade_init) |
| 868 | crossfade_start(); |
| 869 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 870 | if (crossfade_active) { |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 871 | *count = MIN(*count, PCMBUF_MIX_CHUNK/4); |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 872 | return fadebuf; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 873 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 874 | else |
| 875 | { |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 876 | if(prepare_insert(*count << 2)) |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 877 | { |
| 878 | size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; |
Brandon Low | 3bbd93b | 2006-02-13 17:08:53 +0000 | [diff] [blame] | 879 | if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK) |
| 880 | { |
| 881 | /* Usual case, there's space here */ |
| 882 | return &audiobuffer[audiobuffer_index]; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 883 | } |
| 884 | else |
| 885 | { |
Brandon Low | 3bbd93b | 2006-02-13 17:08:53 +0000 | [diff] [blame] | 886 | /* Flush and wrap the buffer */ |
| 887 | pcmbuf_flush_fillpos(); |
| 888 | audiobuffer_pos = 0; |
| 889 | return &audiobuffer[0]; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | else |
| 893 | { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 894 | return NULL; |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 899 | void * pcmbuf_request_voice_buffer(int *count) |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 900 | { |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 901 | /* A get-it-to-work-for-now hack (audio status could change by |
| 902 | completion) */ |
| 903 | if (audio_status() & AUDIO_STATUS_PLAY) |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 904 | { |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 905 | if (pcmbuf_read == NULL) |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 906 | { |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 907 | return NULL; |
| 908 | } |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 909 | else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 && |
| 910 | (pcmbuf_mix_chunk || pcmbuf_read->link)) |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 911 | { |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 912 | *count = MIN(*count, PCMBUF_MIX_CHUNK/4); |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 913 | return voicebuf; |
| 914 | } |
| 915 | else |
| 916 | { |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 917 | return NULL; |
| 918 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 919 | } |
| 920 | else |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 921 | { |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 922 | return pcmbuf_request_buffer(count); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 923 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | bool pcmbuf_is_crossfade_active(void) |
| 927 | { |
| 928 | return crossfade_active || crossfade_init; |
| 929 | } |
| 930 | |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 931 | void pcmbuf_write_complete(int count) |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 932 | { |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 933 | size_t length = (size_t)(unsigned int)count << 2; |
| 934 | |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 935 | if (crossfade_active) |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 936 | { |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 937 | flush_crossfade(fadebuf, length); |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 938 | if (!(crossfade_fade_in_rem || crossfade_chunk)) |
| 939 | crossfade_active = false; |
| 940 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 941 | else |
| 942 | { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 943 | audiobuffer_fillpos += length; |
| 944 | |
| 945 | if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos)) |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 946 | pcmbuf_flush_fillpos(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 947 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Jens Arnold | 2597a13 | 2006-12-25 14:01:47 +0000 | [diff] [blame] | 950 | #if 0 |
Steve Bavin | 135cc75 | 2008-03-28 12:51:33 +0000 | [diff] [blame] | 951 | bool pcmbuf_insert_buffer(char *buf, int count) |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 952 | { |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 953 | size_t length = (size_t)(unsigned int)count << 2; |
| 954 | |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 955 | if (crossfade_active) |
| 956 | { |
Brandon Low | 37faaab | 2006-04-20 02:30:59 +0000 | [diff] [blame] | 957 | flush_crossfade(buf, length); |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 958 | if (!(crossfade_fade_in_rem || crossfade_chunk)) |
| 959 | crossfade_active = false; |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 960 | } |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 961 | else |
| 962 | { |
Brandon Low | 08cdc43 | 2006-04-25 16:12:43 +0000 | [diff] [blame] | 963 | if (!prepare_insert(length)) |
| 964 | return false; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 965 | pcmbuf_flush_buffer(buf, length); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 966 | } |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 967 | return true; |
| 968 | } |
Jens Arnold | 2597a13 | 2006-12-25 14:01:47 +0000 | [diff] [blame] | 969 | #endif |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 970 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 971 | /* Generates a constant square wave sound with a given frequency |
| 972 | in Hertz for a duration in milliseconds. */ |
Brandon Low | 9535a9a | 2006-02-22 01:56:44 +0000 | [diff] [blame] | 973 | void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude) |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 974 | { |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 975 | unsigned int count = 0; |
| 976 | unsigned int i; |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 977 | unsigned int interval = NATIVE_FREQUENCY / frequency; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 978 | unsigned int samples = NATIVE_FREQUENCY / 1000 * duration; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 979 | int32_t sample; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 980 | int16_t *bufstart; |
| 981 | int16_t *bufptr; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 982 | int16_t *pcmbuf_end = (int16_t *)fadebuf; |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 983 | bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL; |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 984 | |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 985 | /* Find the insertion point and set bufstart to the start of it */ |
| 986 | if (mix) |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 987 | { |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 988 | /* Get the next chunk */ |
| 989 | char *pcmbuf_mix_buf = pcmbuf_read->link->addr; |
| 990 | /* Give 1/8s clearance. */ |
| 991 | bufstart = (int16_t *)&pcmbuf_mix_buf[NATIVE_FREQUENCY * 4 / 8]; |
Brandon Low | 9535a9a | 2006-02-22 01:56:44 +0000 | [diff] [blame] | 992 | } |
Brandon Low | 0fcd411 | 2006-04-05 13:00:31 +0000 | [diff] [blame] | 993 | else |
| 994 | { |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 995 | /* Use audiobuffer */ |
| 996 | bufstart = (int16_t *)audiobuffer; |
| 997 | } |
Brandon Low | 0fcd411 | 2006-04-05 13:00:31 +0000 | [diff] [blame] | 998 | |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 999 | /* Mix square wave into buffer */ |
| 1000 | bufptr = bufstart; |
| 1001 | for (i = 0; i < samples; ++i) |
| 1002 | { |
| 1003 | sample = mix ? *bufptr : 0; |
| 1004 | *bufptr++ = clip_sample_16(sample + amplitude); |
| 1005 | if (bufptr > pcmbuf_end) |
| 1006 | bufptr = (int16_t *)audiobuffer; |
| 1007 | sample = mix ? *bufptr : 0; |
| 1008 | *bufptr++ = clip_sample_16(sample + amplitude); |
| 1009 | if (bufptr > pcmbuf_end) |
| 1010 | bufptr = (int16_t *)audiobuffer; |
| 1011 | |
| 1012 | /* Toggle square wave edge */ |
| 1013 | if (++count >= interval) |
| 1014 | { |
| 1015 | count = 0; |
| 1016 | amplitude = -amplitude; |
Brandon Low | 0fcd411 | 2006-04-05 13:00:31 +0000 | [diff] [blame] | 1017 | } |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /* Kick off playback if required */ |
| 1021 | if (!pcm_is_playing()) |
| 1022 | { |
| 1023 | pcm_play_data(NULL, (unsigned char *)bufstart, samples * 4); |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
Steve Bavin | b24ed65 | 2008-04-03 14:24:37 +0000 | [diff] [blame] | 1027 | |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1028 | /* Returns pcm buffer usage in percents (0 to 100). */ |
| 1029 | int pcmbuf_usage(void) |
| 1030 | { |
| 1031 | return pcmbuf_unplayed_bytes * 100 / pcmbuf_size; |
| 1032 | } |
| 1033 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 1034 | int pcmbuf_mix_free(void) |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1035 | { |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 1036 | if (pcmbuf_mix_chunk) |
| 1037 | { |
| 1038 | size_t my_mix_end = |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 1039 | (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample]; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 1040 | size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos]; |
| 1041 | if (my_write_pos < my_mix_end) |
| 1042 | my_write_pos += pcmbuf_size; |
| 1043 | return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes; |
| 1044 | } |
| 1045 | return 100; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 1048 | void pcmbuf_write_voice_complete(int count) |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1049 | { |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 1050 | /* A get-it-to-work-for-now hack (audio status could have changed) */ |
| 1051 | if (!(audio_status() & AUDIO_STATUS_PLAY)) |
| 1052 | { |
| 1053 | pcmbuf_write_complete(count); |
| 1054 | return; |
| 1055 | } |
| 1056 | |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 1057 | int16_t *ibuf = (int16_t *)voicebuf; |
| 1058 | int16_t *obuf; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 1059 | size_t chunk_samples; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1060 | |
Miika Pekkarinen | 815684a | 2006-09-17 08:34:42 +0000 | [diff] [blame] | 1061 | if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL) |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 1062 | { |
| 1063 | pcmbuf_mix_chunk = pcmbuf_read->link; |
| 1064 | /* Start 1/8s into the next chunk */ |
| 1065 | pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16; |
| 1066 | } |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 1067 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 1068 | if (!pcmbuf_mix_chunk) |
| 1069 | return; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1070 | |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 1071 | obuf = (int16_t *)pcmbuf_mix_chunk->addr; |
Michael Sevakis | 18330a0 | 2007-11-16 14:34:15 +0000 | [diff] [blame] | 1072 | chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t); |
Zakk Roberts | 8bdd92b | 2006-03-30 05:56:19 +0000 | [diff] [blame] | 1073 | |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 1074 | count <<= 1; |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1075 | |
Michael Sevakis | 88c4748 | 2007-11-16 14:49:17 +0000 | [diff] [blame] | 1076 | while (count-- > 0) |
| 1077 | { |
| 1078 | int32_t sample = *ibuf++; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 1079 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 1080 | if (pcmbuf_mix_sample >= chunk_samples) |
| 1081 | { |
| 1082 | pcmbuf_mix_chunk = pcmbuf_mix_chunk->link; |
| 1083 | if (!pcmbuf_mix_chunk) |
| 1084 | return; |
| 1085 | pcmbuf_mix_sample = 0; |
| 1086 | obuf = pcmbuf_mix_chunk->addr; |
| 1087 | chunk_samples = pcmbuf_mix_chunk->size / 2; |
| 1088 | } |
Brandon Low | 920516c | 2006-04-23 05:30:52 +0000 | [diff] [blame] | 1089 | sample += obuf[pcmbuf_mix_sample] >> 2; |
Michael Sevakis | 5398125 | 2007-11-16 14:26:05 +0000 | [diff] [blame] | 1090 | obuf[pcmbuf_mix_sample++] = clip_sample_16(sample); |
Miika Pekkarinen | 159c52d | 2005-08-20 11:13:19 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1094 | void pcmbuf_crossfade_enable(bool on_off) |
| 1095 | { |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 1096 | #if MEM > 1 |
| 1097 | /* Next setting to be used, not applied now */ |
| 1098 | crossfade_enabled_pending = on_off; |
| 1099 | #endif |
| 1100 | (void)on_off; |
| 1101 | } |
Miika Pekkarinen | f090dc3 | 2005-07-21 11:44:00 +0000 | [diff] [blame] | 1102 | |
Michael Sevakis | e1dd10d | 2007-03-19 22:04:17 +0000 | [diff] [blame] | 1103 | void pcmbuf_crossfade_enable_finished(void) |
| 1104 | { |
| 1105 | /* Copy the pending setting over now */ |
| 1106 | crossfade_enabled = crossfade_enabled_pending; |
| 1107 | pcmbuf_set_watermark_bytes(); |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | bool pcmbuf_is_crossfade_enabled(void) |
| 1111 | { |
Miika Pekkarinen | e7461b3 | 2005-11-06 16:40:20 +0000 | [diff] [blame] | 1112 | if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE) |
| 1113 | return global_settings.playlist_shuffle; |
| 1114 | |
Miika Pekkarinen | 20b3897 | 2005-07-13 12:48:22 +0000 | [diff] [blame] | 1115 | return crossfade_enabled; |
| 1116 | } |