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