blob: 2075fc66e76ce0902f75ae025d44eef8892c0942 [file] [log] [blame]
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001/***************************************************************************
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 Pekkarinenf090dc32005-07-21 11:44:00 +000035#include "settings.h"
36#include "audio.h"
Miika Pekkarinen159c52d2005-08-20 11:13:19 +000037#include "dsp.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000038
Miika Pekkarinenf3add922006-08-02 18:35:24 +000039/* 1.5s low mark */
40#define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 6)
Miika Pekkarinen20b38972005-07-13 12:48:22 +000041
Miika Pekkarinen20b38972005-07-13 12:48:22 +000042/* Structure we can use to queue pcm chunks in memory to be played
43 * by the driver code. */
44struct pcmbufdesc
45{
46 void *addr;
Brandon Low413da2a2006-02-07 20:38:55 +000047 size_t size;
48 struct pcmbufdesc* link;
Miika Pekkarinen20b38972005-07-13 12:48:22 +000049 /* Call this when the buffer has been played */
50 void (*callback)(void);
Brandon Low413da2a2006-02-07 20:38:55 +000051};
Miika Pekkarinen20b38972005-07-13 12:48:22 +000052
Brandon Low6c0908b2006-04-23 22:54:34 +000053/* Size of the PCM buffer. */
54static size_t pcmbuf_size IDATA_ATTR = 0;
55
56static char *audiobuffer IDATA_ATTR;
57/* Current audio buffer write index. */
58static size_t audiobuffer_pos IDATA_ATTR;
59/* Amount audiobuffer_pos will be increased.*/
60static size_t audiobuffer_fillpos IDATA_ATTR;
61static char *fadebuf IDATA_ATTR;
62static char *voicebuf IDATA_ATTR;
63
64static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
65static void (*position_callback)(size_t size) IDATA_ATTR;
66
67/* Crossfade related state */
68static bool crossfade_enabled;
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +000069static bool crossfade_mixmode;
Brandon Low6c0908b2006-04-23 22:54:34 +000070static bool crossfade_active IDATA_ATTR;
71static bool crossfade_init IDATA_ATTR;
72
73/* Track the current location for processing crossfade */
74static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
75static size_t crossfade_sample IDATA_ATTR;
76
77/* Counters for fading in new data */
78static size_t crossfade_fade_in_total IDATA_ATTR;
79static size_t crossfade_fade_in_rem IDATA_ATTR;
80
Brandon Low413da2a2006-02-07 20:38:55 +000081static size_t pcmbuf_descsize;
82static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
83static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
84static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
85static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
86static size_t last_chunksize IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +000087
Brandon Low413da2a2006-02-07 20:38:55 +000088static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
Brandon Low413da2a2006-02-07 20:38:55 +000089static size_t pcmbuf_watermark IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +000090
Brandon Lowf3bc1ef2006-04-22 14:40:13 +000091static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
92static size_t pcmbuf_mix_sample IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +000093
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +000094static bool low_latency_mode = false;
Brandon Low6c0908b2006-04-23 22:54:34 +000095static bool pcmbuf_flush;
Miika Pekkarinen20b38972005-07-13 12:48:22 +000096
Brandon Low413da2a2006-02-07 20:38:55 +000097/* 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 Low9602dd72006-04-25 12:34:28 +0000104static bool prepare_insert(size_t length);
Brandon Low413da2a2006-02-07 20:38:55 +0000105static void pcmbuf_under_watermark(void);
Brandon Low37faaab2006-04-20 02:30:59 +0000106static bool pcmbuf_flush_fillpos(void);
Brandon Low413da2a2006-02-07 20:38:55 +0000107
108#if defined(HAVE_ADJUSTABLE_CPU_FREQ) && !defined(SIMULATOR)
Miika Pekkarinen29aad552005-08-28 14:16:03 +0000109void pcmbuf_boost(bool state)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000110{
111 static bool boost_state = false;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000112
Brandon Low0744e762006-04-13 17:30:54 +0000113 if (crossfade_init || crossfade_active)
Brandon Low413da2a2006-02-07 20:38:55 +0000114 return;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000115
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000116 if (state != boost_state) {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000117 cpu_boost(state);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000118 boost_state = state;
119 }
120}
Miika Pekkarinen65d43a22005-08-28 19:55:30 +0000121#endif
122
Brandon Low413da2a2006-02-07 20:38:55 +0000123#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 Roberts8bdd92b2006-03-30 05:56:19 +0000127 * The second part performs the operations involved in sending a new buffer
Brandon Low413da2a2006-02-07 20:38:55 +0000128 * to the DMA. Finally the function checks the status of the buffer and
129 * boosts if necessary */
130static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
131static void pcmbuf_callback(unsigned char** start, size_t* size)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000132{
Brandon Low413da2a2006-02-07 20:38:55 +0000133 {
134 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
135 /* Take the finished buffer out of circulation */
136 pcmbuf_read = pcmbuf_current->link;
137
Brandon Low6c0908b2006-04-23 22:54:34 +0000138 /* The buffer is finished, call the callback functions */
139 CALL_IF_EXISTS(position_callback, last_chunksize);
Brandon Low413da2a2006-02-07 20:38:55 +0000140 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 Lowf3bc1ef2006-04-22 14:40:13 +0000145
Brandon Low08cdc432006-04-25 16:12:43 +0000146 /* If we've read over the mix chunk while it's still mixing there */
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000147 if (pcmbuf_current == pcmbuf_mix_chunk)
148 pcmbuf_mix_chunk = NULL;
Brandon Low08cdc432006-04-25 16:12:43 +0000149 /* If we've read over the crossfade chunk while it's still fading */
Brandon Low9aa49a42006-04-25 02:28:21 +0000150 if (pcmbuf_current == crossfade_chunk)
Brandon Low23064332006-04-25 16:15:11 +0000151 crossfade_chunk = pcmbuf_read;
Brandon Low413da2a2006-02-07 20:38:55 +0000152 }
153
Brandon Low37faaab2006-04-20 02:30:59 +0000154process_new_buffer:
Brandon Low413da2a2006-02-07 20:38:55 +0000155 {
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 Lowf3bc1ef2006-04-22 14:40:13 +0000163
Brandon Low413da2a2006-02-07 20:38:55 +0000164 pcmbuf_unplayed_bytes -= current_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000165 last_chunksize = current_size;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000166 *realsize = current_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000167 *realstart = pcmbuf_new->addr;
168 }
169 else
170 {
Brandon Low37faaab2006-04-20 02:30:59 +0000171 /* There may be more data waiting to flush, try to use it */
172 if (pcmbuf_flush_fillpos())
173 goto process_new_buffer;
174
Brandon Low413da2a2006-02-07 20:38:55 +0000175 /* No more buffers */
176 last_chunksize = 0;
177 *realsize = 0;
178 *realstart = NULL;
179 CALL_IF_EXISTS(pcmbuf_event_handler);
180 }
181 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000182}
183
Brandon Low413da2a2006-02-07 20:38:55 +0000184void pcmbuf_set_position_callback(void (*callback)(size_t size))
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000185{
Brandon Lowa3868d32006-01-21 22:42:44 +0000186 position_callback = callback;
187}
188
Brandon Low413da2a2006-02-07 20:38:55 +0000189static void pcmbuf_set_watermark_bytes(size_t numbytes)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000190{
191 pcmbuf_watermark = numbytes;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000192}
193
Brandon Low413da2a2006-02-07 20:38:55 +0000194/* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
195 * in a separate function for the moment */
196static inline void pcmbuf_add_chunk(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000197{
Brandon Low413da2a2006-02-07 20:38:55 +0000198 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 Low6c0908b2006-04-23 22:54:34 +0000210 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 Low413da2a2006-02-07 20:38:55 +0000221 /* If there is already a read buffer setup, add to it */
Brandon Low6c0908b2006-04-23 22:54:34 +0000222 else
223 pcmbuf_read_end->link = pcmbuf_current;
Brandon Low413da2a2006-02-07 20:38:55 +0000224 } else {
225 /* Otherwise create the buffer */
226 pcmbuf_read = pcmbuf_current;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000227 }
Brandon Low413da2a2006-02-07 20:38:55 +0000228 /* 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 Low413da2a2006-02-07 20:38:55 +0000233
234 audiobuffer_pos += size;
235 if (audiobuffer_pos >= pcmbuf_size)
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000236 audiobuffer_pos -= pcmbuf_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000237
238 audiobuffer_fillpos = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000239}
240
Brandon Low413da2a2006-02-07 20:38:55 +0000241static void pcmbuf_under_watermark(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000242{
243 /* Fill audio buffer by boosting cpu */
244 pcmbuf_boost(true);
Brandon Low413da2a2006-02-07 20:38:55 +0000245 /* Disable crossfade if < .5s of audio */
Brandon Low6c0908b2006-04-23 22:54:34 +0000246 if (LOW_DATA(2))
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000247 crossfade_active = false;
248}
249
Brandon Low413da2a2006-02-07 20:38:55 +0000250void pcmbuf_set_event_handler(void (*event_handler)(void))
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000251{
252 pcmbuf_event_handler = event_handler;
253}
254
255unsigned int pcmbuf_get_latency(void)
256{
Brandon Low413da2a2006-02-07 20:38:55 +0000257 /* 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 Pekkarinen20b38972005-07-13 12:48:22 +0000260}
261
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000262void pcmbuf_set_low_latency(bool state)
263{
264 low_latency_mode = state;
265}
266
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000267bool pcmbuf_is_lowdata(void)
268{
Brandon Low413da2a2006-02-07 20:38:55 +0000269 if (!pcm_is_playing() || pcm_is_paused() ||
270 crossfade_init || crossfade_active)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000271 return false;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000272
Brandon Low413da2a2006-02-07 20:38:55 +0000273 /* 0.5 seconds of buffer is low data */
274 return LOW_DATA(2);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000275}
276
Brandon Low6c0908b2006-04-23 22:54:34 +0000277/* Amount of bytes left in the buffer. */
278inline 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 Pekkarinena4f8d1c2006-01-27 16:25:44 +0000292bool pcmbuf_crossfade_init(bool manual_skip)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000293{
Brandon Low6c0908b2006-04-23 22:54:34 +0000294 /* 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 Pekkarinen20b38972005-07-13 12:48:22 +0000298 return false;
299 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000300
301 /* Not enough data, or crossfade disabled, flush the old data instead */
Miika Pekkarinenf3add922006-08-02 18:35:24 +0000302 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
Brandon Low6c0908b2006-04-23 22:54:34 +0000303 {
304 pcmbuf_boost(true);
Brandon Lowb9615512006-05-05 03:13:06 +0000305 pcmbuf_flush_fillpos();
Brandon Low6c0908b2006-04-23 22:54:34 +0000306 pcmbuf_flush = true;
307 return false;
308 }
309
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000310 logf("pcmbuf_crossfade_init");
311 pcmbuf_boost(true);
Miika Pekkarinen90161c92005-07-22 16:46:27 +0000312
Miika Pekkarinena4f8d1c2006-01-27 16:25:44 +0000313 /* Don't enable mix mode when skipping tracks manually. */
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +0000314 if (manual_skip)
315 crossfade_mixmode = false;
316 else
317 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
318
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000319 crossfade_init = true;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000320
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000321 return true;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000322
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000323}
324
325void pcmbuf_play_stop(void)
326{
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000327 /** Prevent a very tiny pop from happening by muting audio
328 * until dma has been initialized. */
329 pcm_mute(true);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000330 pcm_play_stop();
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000331 pcm_mute(false);
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000332
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000333 pcmbuf_unplayed_bytes = 0;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000334 pcmbuf_mix_chunk = NULL;
Brandon Low413da2a2006-02-07 20:38:55 +0000335 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 Pekkarinen20b38972005-07-13 12:48:22 +0000340 audiobuffer_pos = 0;
341 audiobuffer_fillpos = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000342 crossfade_init = false;
343 crossfade_active = false;
Brandon Low6c0908b2006-04-23 22:54:34 +0000344 pcmbuf_flush = false;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000345
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000346 pcmbuf_boost(false);
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000347
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000348}
349
Brandon Low413da2a2006-02-07 20:38:55 +0000350int 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
360int pcmbuf_descs(void) {
Brandon Lowddaf5f02006-05-08 11:03:19 +0000361 return pcmbuf_size / PCMBUF_MINAVG_CHUNK;
Brandon Low413da2a2006-02-07 20:38:55 +0000362}
363
364size_t get_pcmbuf_descsize(void) {
365 return pcmbuf_descsize;
366}
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000367
Brandon Low413da2a2006-02-07 20:38:55 +0000368static 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 Roberts8bdd92b2006-03-30 05:56:19 +0000378
Brandon Low413da2a2006-02-07 20:38:55 +0000379/* Initialize the pcmbuffer the structure looks like this:
380 * ...CODECBUFFER|---------PCMBUF---------|GUARDBUF|DESCS| */
381void pcmbuf_init(size_t bufsize)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000382{
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000383 pcmbuf_size = bufsize;
Brandon Low413da2a2006-02-07 20:38:55 +0000384 pcmbuf_descsize = pcmbuf_descs()*sizeof(struct pcmbufdesc);
385 audiobuffer = (char *)&audiobuf[(audiobufend - audiobuf) -
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000386 (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 Low413da2a2006-02-07 20:38:55 +0000390 pcmbuf_init_pcmbuffers();
Brandon Lowee6a95a2006-01-22 00:03:20 +0000391 position_callback = NULL;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000392 pcmbuf_event_handler = NULL;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000393 pcmbuf_play_stop();
394}
395
Brandon Low413da2a2006-02-07 20:38:55 +0000396size_t pcmbuf_get_bufsize(void)
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000397{
398 return pcmbuf_size;
399}
400
Brandon Low413da2a2006-02-07 20:38:55 +0000401void pcmbuf_pause(bool pause) {
Brandon Low8307d0b2006-03-24 02:38:57 +0000402 if (pause)
403 pcm_mute(true);
Brandon Low413da2a2006-02-07 20:38:55 +0000404 pcm_play_pause(!pause);
Brandon Low8307d0b2006-03-24 02:38:57 +0000405 if (!pause)
406 pcm_mute(false);
Brandon Low86c7e1a2006-04-14 16:42:14 +0000407 pcmbuf_boost(!pause && pcm_is_playing());
Brandon Low413da2a2006-02-07 20:38:55 +0000408}
409
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000410/* Force playback. */
411void pcmbuf_play_start(void)
412{
413 if (!pcm_is_playing() && pcmbuf_unplayed_bytes)
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000414 {
415 /** Prevent a very tiny pop from happening by muting audio
416 * until dma has been initialized. */
417 pcm_mute(true);
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000418
Brandon Low413da2a2006-02-07 20:38:55 +0000419 last_chunksize = pcmbuf_read->size;
420 pcmbuf_unplayed_bytes -= last_chunksize;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000421 pcm_play_data(pcmbuf_callback,
Brandon Low413da2a2006-02-07 20:38:55 +0000422 (unsigned char *)pcmbuf_read->addr, last_chunksize);
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000423
424 /* Now unmute the audio. */
425 pcm_mute(false);
426 }
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000427}
428
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000429/**
430 * Commit samples waiting to the pcm buffer.
431 */
Brandon Low37faaab2006-04-20 02:30:59 +0000432static bool pcmbuf_flush_fillpos(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000433{
Brandon Low413da2a2006-02-07 20:38:55 +0000434 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 Pekkarinen20b38972005-07-13 12:48:22 +0000439 pcmbuf_boost(false);
Brandon Low86f1e2e2006-03-24 13:43:15 +0000440 /* If this happens, something is being stupid */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000441 if (!pcm_is_playing()) {
Brandon Low413da2a2006-02-07 20:38:55 +0000442 logf("pcmbuf_flush_fillpos error");
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000443 pcmbuf_play_start();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000444 }
Brandon Low86f1e2e2006-03-24 13:43:15 +0000445 /* Let approximately one chunk of data playback */
446 sleep(PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY * 4) / 5);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000447 }
Brandon Low413da2a2006-02-07 20:38:55 +0000448 pcmbuf_add_chunk();
Brandon Low37faaab2006-04-20 02:30:59 +0000449 return true;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000450 }
Brandon Low37faaab2006-04-20 02:30:59 +0000451 return false;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000452}
453
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000454/**
455 * Completely process the crossfade fade out effect with current pcm buffer.
456 */
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000457static void crossfade_process_buffer(size_t fade_in_delay,
458 size_t fade_out_delay, size_t fade_out_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000459{
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +0000460 if (!crossfade_mixmode)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000461 {
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000462 /* Fade out the specified amount of the already processed audio */
463 size_t total_fade_out = fade_out_rem;
Brandon Low6c0908b2006-04-23 22:54:34 +0000464 size_t fade_out_sample;
465 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000466
Brandon Low6c0908b2006-04-23 22:54:34 +0000467 /* Find the right chunk to start fading out */
Brandon Low9ca16a62006-04-24 03:43:43 +0000468 fade_out_delay += crossfade_sample * 2;
Brandon Low6c0908b2006-04-23 22:54:34 +0000469 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 Lowdbcc9c22006-03-30 00:47:05 +0000477 while (fade_out_rem > 0)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000478 {
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000479 /* Each 1/10 second of audio will have the same fade applied */
Brandon Low19247692006-04-25 10:39:56 +0000480 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
Brandon Low6c0908b2006-04-23 22:54:34 +0000481 int factor = (fade_out_rem << 8) / total_fade_out;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000482
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000483 fade_out_rem -= block_rem;
484
485 /* Fade this block */
Brandon Low6c0908b2006-04-23 22:54:34 +0000486 while (block_rem > 0)
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000487 {
488 /* Fade one sample */
Brandon Low6c0908b2006-04-23 22:54:34 +0000489 short *buf = (short *)(fade_out_chunk->addr);
490 int sample = buf[fade_out_sample];
491 buf[fade_out_sample++] = (sample * factor) >> 8;
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000492
Brandon Low19247692006-04-25 10:39:56 +0000493 block_rem -= 2;
Brandon Low6c0908b2006-04-23 22:54:34 +0000494 /* Move to the next chunk as needed */
495 if (fade_out_sample * 2 >= fade_out_chunk->size)
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000496 {
Brandon Low6c0908b2006-04-23 22:54:34 +0000497 fade_out_chunk = fade_out_chunk->link;
498 fade_out_sample = 0;
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000499 }
500 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000501 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000502 }
503
Brandon Low6c0908b2006-04-23 22:54:34 +0000504 /* Find the right chunk and sample to start fading in */
Brandon Low9ca16a62006-04-24 03:43:43 +0000505 fade_in_delay += crossfade_sample * 2;
Brandon Low6c0908b2006-04-23 22:54:34 +0000506 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 Pekkarinene7461b32005-11-06 16:40:20 +0000512 logf("process done!");
513}
514
Brandon Low6c0908b2006-04-23 22:54:34 +0000515/* Initializes crossfader, calculates all necessary parameters and
516 * performs fade-out with the pcm buffer. */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000517static void crossfade_start(void)
518{
Brandon Low6c0908b2006-04-23 22:54:34 +0000519 size_t crossfade_rem;
Brandon Low9aa49a42006-04-25 02:28:21 +0000520 size_t crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000521 size_t fade_out_rem;
522 size_t fade_out_delay;
523 size_t fade_in_delay;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000524
Brandon Low37faaab2006-04-20 02:30:59 +0000525 crossfade_init = false;
Brandon Low413da2a2006-02-07 20:38:55 +0000526 /* Reject crossfade if less than .5s of data */
527 if (LOW_DATA(2)) {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000528 logf("crossfade rejected");
529 pcmbuf_play_stop();
530 return ;
531 }
532
533 logf("crossfade_start");
Brandon Low413da2a2006-02-07 20:38:55 +0000534 pcmbuf_flush_fillpos();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000535 crossfade_active = true;
Brandon Low6c0908b2006-04-23 22:54:34 +0000536
Brandon Low413da2a2006-02-07 20:38:55 +0000537 /* Initialize the crossfade buffer size to all of the buffered data that
538 * has not yet been sent to the DMA */
Brandon Low6c0908b2006-04-23 22:54:34 +0000539 crossfade_rem = pcmbuf_unplayed_bytes;
540 crossfade_chunk = pcmbuf_read->link;
Brandon Low8ef18272006-04-24 12:41:30 +0000541 crossfade_sample = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000542
Brandon Low6c0908b2006-04-23 22:54:34 +0000543 /* Get fade out delay from settings. */
544 fade_out_delay =
545 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000546
Brandon Low6c0908b2006-04-23 22:54:34 +0000547 /* Get fade out duration from settings. */
548 fade_out_rem =
549 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000550
Brandon Low9aa49a42006-04-25 02:28:21 +0000551 crossfade_need = fade_out_delay + fade_out_rem;
Brandon Low6c0908b2006-04-23 22:54:34 +0000552 /* We want only to modify the last part of the buffer. */
Brandon Low9aa49a42006-04-25 02:28:21 +0000553 if (crossfade_rem > crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000554 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000555 size_t crossfade_extra = crossfade_rem - crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000556 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 Low9aa49a42006-04-25 02:28:21 +0000564 else if (crossfade_rem < crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000565 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000566 size_t crossfade_short = crossfade_need - crossfade_rem;
567 if (fade_out_rem >= crossfade_short)
Brandon Low6c0908b2006-04-23 22:54:34 +0000568 fade_out_rem -= crossfade_short;
569 else
570 {
571 fade_out_delay -= crossfade_short - fade_out_rem;
572 fade_out_rem = 0;
573 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000574 }
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000575
Brandon Low6c0908b2006-04-23 22:54:34 +0000576 /* 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 Roberts8bdd92b2006-03-30 05:56:19 +0000580
Brandon Low6c0908b2006-04-23 22:54:34 +0000581 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 Pekkarinen20b38972005-07-13 12:48:22 +0000585}
586
Brandon Low9602dd72006-04-25 12:34:28 +0000587/* Returns the number of bytes _NOT_ mixed */
588static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000589{
Brandon Low6c0908b2006-04-23 22:54:34 +0000590 const short *input_buf = (const short *)buf;
Brandon Low9aa49a42006-04-25 02:28:21 +0000591 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 Roberts8bdd92b2006-03-30 05:56:19 +0000594
Brandon Low9602dd72006-04-25 12:34:28 +0000595 while (fade_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000596 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000597 int sample = *input_buf++;
598 sample = ((sample * factor) >> 8) + *output_buf;
599 *output_buf++ = MIN(32767, MAX(-32768, sample));
Brandon Low9602dd72006-04-25 12:34:28 +0000600 fade_rem -= 2;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000601
Brandon Low9aa49a42006-04-25 02:28:21 +0000602 if (output_buf >= chunk_end)
Brandon Low413da2a2006-02-07 20:38:55 +0000603 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000604 crossfade_chunk = crossfade_chunk->link;
605 if (!crossfade_chunk)
Brandon Low9602dd72006-04-25 12:34:28 +0000606 return fade_rem;
Brandon Low9aa49a42006-04-25 02:28:21 +0000607 output_buf = (short *)(crossfade_chunk->addr);
608 chunk_end = (short *)((size_t)output_buf + crossfade_chunk->size);
Brandon Low413da2a2006-02-07 20:38:55 +0000609 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000610 }
Brandon Low9aa49a42006-04-25 02:28:21 +0000611 crossfade_sample = (size_t)(output_buf - (short *)(crossfade_chunk->addr));
Brandon Low9602dd72006-04-25 12:34:28 +0000612 return 0;
613}
614
615/* Returns the number of bytes _NOT_ mixed */
616static 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 Pekkarinen20b38972005-07-13 12:48:22 +0000640}
641
Brandon Low413da2a2006-02-07 20:38:55 +0000642static void pcmbuf_flush_buffer(const char *buf, size_t length)
643{
644 size_t copy_n;
Brandon Low413da2a2006-02-07 20:38:55 +0000645 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 Low9aa49a42006-04-25 02:28:21 +0000660static void flush_crossfade(char *buf, size_t length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000661{
Brandon Low9602dd72006-04-25 12:34:28 +0000662 if (length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000663 {
Brandon Low9602dd72006-04-25 12:34:28 +0000664 if (crossfade_fade_in_rem)
Brandon Low9aa49a42006-04-25 02:28:21 +0000665 {
666 size_t samples;
667 short *input_buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000668
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 Low08cdc432006-04-25 16:12:43 +0000686 if (!length)
687 return;
688 if (!fade_rem)
689 goto fade_done;
Brandon Low9602dd72006-04-25 12:34:28 +0000690 }
691
Brandon Low9aa49a42006-04-25 02:28:21 +0000692 samples = fade_rem / 2;
693 input_buf = (short *)buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000694 /* Fade remaining samples in place */
Brandon Low9aa49a42006-04-25 02:28:21 +0000695 while (samples)
696 {
697 int sample = *input_buf;
698 *input_buf++ = (sample * factor) >> 8;
699 samples--;
700 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000701 }
Brandon Low413da2a2006-02-07 20:38:55 +0000702
Brandon Low08cdc432006-04-25 16:12:43 +0000703fade_done:
Brandon Low9602dd72006-04-25 12:34:28 +0000704 if (crossfade_chunk)
Brandon Low6c0908b2006-04-23 22:54:34 +0000705 {
Brandon Low9602dd72006-04-25 12:34:28 +0000706 /* Mix the data */
707 size_t mix_total = length;
708 length = crossfade_mix(buf, length);
709 buf += mix_total - length;
Brandon Low08cdc432006-04-25 16:12:43 +0000710 if (!length)
711 return;
Brandon Low6c0908b2006-04-23 22:54:34 +0000712 }
Brandon Low9602dd72006-04-25 12:34:28 +0000713
Brandon Low08cdc432006-04-25 16:12:43 +0000714 /* Flush samples to the buffer */
715 while (!prepare_insert(length))
716 sleep(1);
717 pcmbuf_flush_buffer(buf, length);
Brandon Low9aa49a42006-04-25 02:28:21 +0000718 }
719
Brandon Low413da2a2006-02-07 20:38:55 +0000720}
721
722static bool prepare_insert(size_t length)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000723{
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000724 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 Roberts8bdd92b2006-03-30 05:56:19 +0000731
Brandon Low3bbd93b2006-02-13 17:08:53 +0000732 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
Brandon Low9602dd72006-04-25 12:34:28 +0000733 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
Brandon Low413da2a2006-02-07 20:38:55 +0000734 {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000735 pcmbuf_boost(false);
736 return false;
737 }
Miika Pekkarinend83b6592005-07-19 19:57:23 +0000738
Brandon Low413da2a2006-02-07 20:38:55 +0000739 if (!pcm_is_playing())
740 {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000741 pcmbuf_boost(true);
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000742 /* Pre-buffer 1s. */
Brandon Low413da2a2006-02-07 20:38:55 +0000743 if (!LOW_DATA(4))
744 {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000745 logf("pcm starting");
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000746 pcmbuf_play_start();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000747 }
Brandon Low3a37fae2006-02-13 16:55:25 +0000748 } else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
749 pcmbuf_under_watermark();
750
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000751 return true;
752}
753
Brandon Low413da2a2006-02-07 20:38:55 +0000754void* pcmbuf_request_buffer(size_t length, size_t *realsize)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000755{
Brandon Low37faaab2006-04-20 02:30:59 +0000756 if (crossfade_init)
757 crossfade_start();
758
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000759 if (crossfade_active) {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000760 *realsize = MIN(length, PCMBUF_MIX_CHUNK);
761 return fadebuf;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000762 }
Brandon Low413da2a2006-02-07 20:38:55 +0000763 else
764 {
765 if(prepare_insert(length))
766 {
767 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
Brandon Low3bbd93b2006-02-13 17:08:53 +0000768 *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 Low413da2a2006-02-07 20:38:55 +0000773 }
774 else
775 {
Brandon Low3bbd93b2006-02-13 17:08:53 +0000776 /* Flush and wrap the buffer */
777 pcmbuf_flush_fillpos();
778 audiobuffer_pos = 0;
779 return &audiobuffer[0];
Brandon Low413da2a2006-02-07 20:38:55 +0000780 }
781 }
782 else
783 {
784 *realsize = 0;
785 return NULL;
786 }
787 }
788}
789
790void* pcmbuf_request_voice_buffer(size_t length, size_t *realsize, bool mix)
791{
792 if (mix)
793 {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000794 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 Low413da2a2006-02-07 20:38:55 +0000804 }
805 else
806 return pcmbuf_request_buffer(length, realsize);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000807}
808
809bool pcmbuf_is_crossfade_active(void)
810{
811 return crossfade_active || crossfade_init;
812}
813
Brandon Low413da2a2006-02-07 20:38:55 +0000814void pcmbuf_write_complete(size_t length)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000815{
Brandon Low37faaab2006-04-20 02:30:59 +0000816 if (crossfade_active)
Brandon Low08cdc432006-04-25 16:12:43 +0000817 {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000818 flush_crossfade(fadebuf, length);
Brandon Low08cdc432006-04-25 16:12:43 +0000819 if (!(crossfade_fade_in_rem || crossfade_chunk))
820 crossfade_active = false;
821 }
Brandon Low413da2a2006-02-07 20:38:55 +0000822 else
823 {
Brandon Low413da2a2006-02-07 20:38:55 +0000824 audiobuffer_fillpos += length;
825
826 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000827 pcmbuf_flush_fillpos();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000828 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000829}
830
Brandon Low9aa49a42006-04-25 02:28:21 +0000831bool pcmbuf_insert_buffer(char *buf, size_t length)
Brandon Low413da2a2006-02-07 20:38:55 +0000832{
Brandon Low08cdc432006-04-25 16:12:43 +0000833 if (crossfade_active)
834 {
Brandon Low37faaab2006-04-20 02:30:59 +0000835 flush_crossfade(buf, length);
Brandon Low08cdc432006-04-25 16:12:43 +0000836 if (!(crossfade_fade_in_rem || crossfade_chunk))
837 crossfade_active = false;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000838 }
Brandon Low413da2a2006-02-07 20:38:55 +0000839 else
840 {
Brandon Low08cdc432006-04-25 16:12:43 +0000841 if (!prepare_insert(length))
842 return false;
Brandon Low413da2a2006-02-07 20:38:55 +0000843 pcmbuf_flush_buffer(buf, length);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000844 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000845 return true;
846}
847
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000848/* Generates a constant square wave sound with a given frequency
849 in Hertz for a duration in milliseconds. */
Brandon Low9535a9a2006-02-22 01:56:44 +0000850void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000851{
Brandon Low9535a9a2006-02-22 01:56:44 +0000852 unsigned int count = 0, i = 0;
Brandon Low413da2a2006-02-07 20:38:55 +0000853 unsigned int interval = NATIVE_FREQUENCY / frequency;
Brandon Low0fcd4112006-04-05 13:00:31 +0000854 long sample;
Brandon Low9535a9a2006-02-22 01:56:44 +0000855 short *buf;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000856 short *pcmbuf_end = (short *)fadebuf;
Brandon Low9535a9a2006-02-22 01:56:44 +0000857 size_t samples = NATIVE_FREQUENCY / 1000 * duration;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000858
Brandon Low0fcd4112006-04-05 13:00:31 +0000859 if (pcm_is_playing())
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000860 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000861 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 Low0fcd4112006-04-05 13:00:31 +0000874 while (i++ < samples)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000875 {
Brandon Low0fcd4112006-04-05 13:00:31 +0000876 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 Pekkarinen159c52d2005-08-20 11:13:19 +0000891 }
Brandon Low9535a9a2006-02-22 01:56:44 +0000892 }
Brandon Low0fcd4112006-04-05 13:00:31 +0000893 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 Lowa1315802006-04-07 20:03:26 +0000903 /* Toggle square wav side */
Brandon Low0fcd4112006-04-05 13:00:31 +0000904 if (++count >= interval)
905 {
906 count = 0;
907 amplitude = -amplitude;
908 }
909 if (buf > pcmbuf_end)
910 buf = (short *)audiobuffer;
911 }
Brandon Low9535a9a2006-02-22 01:56:44 +0000912 pcm_play_data(NULL, (unsigned char *)audiobuffer, samples * 4);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000913 }
914}
915
916/* Returns pcm buffer usage in percents (0 to 100). */
917int pcmbuf_usage(void)
918{
919 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
920}
921
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000922int pcmbuf_mix_free(void)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000923{
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000924 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 Pekkarinen159c52d2005-08-20 11:13:19 +0000934}
935
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000936void pcmbuf_mix_voice(size_t length)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000937{
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000938 short *ibuf = (short *)voicebuf;
939 short *obuf;
940 size_t chunk_samples;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000941
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000942 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 Pekkarinen159c52d2005-08-20 11:13:19 +0000950
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000951 obuf = (short *)pcmbuf_mix_chunk->addr;
952 chunk_samples = pcmbuf_mix_chunk->size / 2;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000953
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000954 length /= 2;
955
956 while (length-- > 0) {
Brandon Low6c0908b2006-04-23 22:54:34 +0000957 int sample = *ibuf++;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000958 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 Low920516c2006-04-23 05:30:52 +0000967 sample += obuf[pcmbuf_mix_sample] >> 2;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000968 obuf[pcmbuf_mix_sample++] = MIN(MAX(sample, -32768), 32767);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000969 }
970}
971
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000972void pcmbuf_crossfade_enable(bool on_off)
973{
974 crossfade_enabled = on_off;
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000975
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000976 if (crossfade_enabled) {
Miika Pekkarinenf3add922006-08-02 18:35:24 +0000977 /* If crossfading, try to keep the buffer full other than 1 second */
978 pcmbuf_set_watermark_bytes(pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1));
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000979 } else {
Miika Pekkarinenf3add922006-08-02 18:35:24 +0000980 /* Otherwise, just keep it above 2 second */
Brandon Lowa3868d32006-01-21 22:42:44 +0000981 pcmbuf_set_watermark_bytes(PCMBUF_WATERMARK);
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000982 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000983}
984
985bool pcmbuf_is_crossfade_enabled(void)
986{
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000987 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
988 return global_settings.playlist_shuffle;
989
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000990 return crossfade_enabled;
991}
992