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