blob: fda7543c0238258ab77266e1a6bba9ba0372369d [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 Pekkarinen4408b6b2006-02-07 19:17:51 +000039#define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 1)
Miika Pekkarinen20b38972005-07-13 12:48:22 +000040
Miika Pekkarinen20b38972005-07-13 12:48:22 +000041/* Structure we can use to queue pcm chunks in memory to be played
42 * by the driver code. */
43struct pcmbufdesc
44{
45 void *addr;
Brandon Low413da2a2006-02-07 20:38:55 +000046 size_t size;
47 struct pcmbufdesc* link;
Miika Pekkarinen20b38972005-07-13 12:48:22 +000048 /* Call this when the buffer has been played */
49 void (*callback)(void);
Brandon Low413da2a2006-02-07 20:38:55 +000050};
Miika Pekkarinen20b38972005-07-13 12:48:22 +000051
Brandon Low6c0908b2006-04-23 22:54:34 +000052/* Size of the PCM buffer. */
53static size_t pcmbuf_size IDATA_ATTR = 0;
54
55static char *audiobuffer IDATA_ATTR;
56/* Current audio buffer write index. */
57static size_t audiobuffer_pos IDATA_ATTR;
58/* Amount audiobuffer_pos will be increased.*/
59static size_t audiobuffer_fillpos IDATA_ATTR;
60static char *fadebuf IDATA_ATTR;
61static char *voicebuf IDATA_ATTR;
62
63static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
64static void (*position_callback)(size_t size) IDATA_ATTR;
65
66/* Crossfade related state */
67static bool crossfade_enabled;
Brandon Low9602dd72006-04-25 12:34:28 +000068static bool crossfade_mode;
Brandon Low6c0908b2006-04-23 22:54:34 +000069static bool crossfade_active IDATA_ATTR;
70static bool crossfade_init IDATA_ATTR;
71
72/* Track the current location for processing crossfade */
73static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
74static size_t crossfade_sample IDATA_ATTR;
75
76/* Counters for fading in new data */
77static size_t crossfade_fade_in_total IDATA_ATTR;
78static size_t crossfade_fade_in_rem IDATA_ATTR;
79
Brandon Low413da2a2006-02-07 20:38:55 +000080static size_t pcmbuf_descsize;
81static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
82static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
83static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
84static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
85static size_t last_chunksize IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +000086
Brandon Low413da2a2006-02-07 20:38:55 +000087static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
Brandon Low413da2a2006-02-07 20:38:55 +000088static size_t pcmbuf_watermark IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +000089
Brandon Lowf3bc1ef2006-04-22 14:40:13 +000090static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
91static size_t pcmbuf_mix_sample IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +000092
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +000093static bool low_latency_mode = false;
Brandon Low6c0908b2006-04-23 22:54:34 +000094static bool pcmbuf_flush;
Miika Pekkarinen20b38972005-07-13 12:48:22 +000095
Brandon Low413da2a2006-02-07 20:38:55 +000096/* Helpful macros for use in conditionals this assumes some of the above
97 * static variable names */
98#define NEED_FLUSH(position) \
99 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
100#define LOW_DATA(quarter_secs) \
101 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
102
Brandon Low9602dd72006-04-25 12:34:28 +0000103static bool prepare_insert(size_t length);
Brandon Low413da2a2006-02-07 20:38:55 +0000104static void pcmbuf_under_watermark(void);
Brandon Low37faaab2006-04-20 02:30:59 +0000105static bool pcmbuf_flush_fillpos(void);
Brandon Low413da2a2006-02-07 20:38:55 +0000106
107#if defined(HAVE_ADJUSTABLE_CPU_FREQ) && !defined(SIMULATOR)
Miika Pekkarinen29aad552005-08-28 14:16:03 +0000108void pcmbuf_boost(bool state)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000109{
110 static bool boost_state = false;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000111
Brandon Low0744e762006-04-13 17:30:54 +0000112 if (crossfade_init || crossfade_active)
Brandon Low413da2a2006-02-07 20:38:55 +0000113 return;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000114
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000115 if (state != boost_state) {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000116 cpu_boost(state);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000117 boost_state = state;
118 }
119}
Miika Pekkarinen65d43a22005-08-28 19:55:30 +0000120#endif
121
Brandon Low413da2a2006-02-07 20:38:55 +0000122#define CALL_IF_EXISTS(function, args...) if (function) function(args)
123/* This function has 2 major logical parts (separated by brackets both for
124 * readability and variable scoping). The first part performs the
125 * operastions related to finishing off the last buffer we fed to the DMA.
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000126 * The second part performs the operations involved in sending a new buffer
Brandon Low413da2a2006-02-07 20:38:55 +0000127 * to the DMA. Finally the function checks the status of the buffer and
128 * boosts if necessary */
129static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
130static void pcmbuf_callback(unsigned char** start, size_t* size)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000131{
Brandon Low413da2a2006-02-07 20:38:55 +0000132 {
133 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
134 /* Take the finished buffer out of circulation */
135 pcmbuf_read = pcmbuf_current->link;
136
Brandon Low6c0908b2006-04-23 22:54:34 +0000137 /* The buffer is finished, call the callback functions */
138 CALL_IF_EXISTS(position_callback, last_chunksize);
Brandon Low413da2a2006-02-07 20:38:55 +0000139 CALL_IF_EXISTS(pcmbuf_current->callback);
140
141 /* Put the finished buffer back into circulation */
142 pcmbuf_write_end->link = pcmbuf_current;
143 pcmbuf_write_end = pcmbuf_current;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000144
Brandon Low08cdc432006-04-25 16:12:43 +0000145 /* If we've read over the mix chunk while it's still mixing there */
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000146 if (pcmbuf_current == pcmbuf_mix_chunk)
147 pcmbuf_mix_chunk = NULL;
Brandon Low08cdc432006-04-25 16:12:43 +0000148 /* If we've read over the crossfade chunk while it's still fading */
Brandon Low9aa49a42006-04-25 02:28:21 +0000149 if (pcmbuf_current == crossfade_chunk)
Brandon Low23064332006-04-25 16:15:11 +0000150 crossfade_chunk = pcmbuf_read;
Brandon Low413da2a2006-02-07 20:38:55 +0000151 }
152
Brandon Low37faaab2006-04-20 02:30:59 +0000153process_new_buffer:
Brandon Low413da2a2006-02-07 20:38:55 +0000154 {
155 /* Send the new buffer to the pcm */
156 struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
157 size_t *realsize = size;
158 unsigned char** realstart = start;
159 if(pcmbuf_new)
160 {
161 size_t current_size = pcmbuf_new->size;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000162
Brandon Low413da2a2006-02-07 20:38:55 +0000163 pcmbuf_unplayed_bytes -= current_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000164 last_chunksize = current_size;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000165 *realsize = current_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000166 *realstart = pcmbuf_new->addr;
167 }
168 else
169 {
Brandon Low37faaab2006-04-20 02:30:59 +0000170 /* There may be more data waiting to flush, try to use it */
171 if (pcmbuf_flush_fillpos())
172 goto process_new_buffer;
173
Brandon Low413da2a2006-02-07 20:38:55 +0000174 /* No more buffers */
175 last_chunksize = 0;
176 *realsize = 0;
177 *realstart = NULL;
178 CALL_IF_EXISTS(pcmbuf_event_handler);
179 }
180 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000181}
182
Brandon Low413da2a2006-02-07 20:38:55 +0000183void pcmbuf_set_position_callback(void (*callback)(size_t size))
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000184{
Brandon Lowa3868d32006-01-21 22:42:44 +0000185 position_callback = callback;
186}
187
Brandon Low413da2a2006-02-07 20:38:55 +0000188static void pcmbuf_set_watermark_bytes(size_t numbytes)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000189{
190 pcmbuf_watermark = numbytes;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000191}
192
Brandon Low413da2a2006-02-07 20:38:55 +0000193/* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
194 * in a separate function for the moment */
195static inline void pcmbuf_add_chunk(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000196{
Brandon Low413da2a2006-02-07 20:38:55 +0000197 register size_t size = audiobuffer_fillpos;
198 /* Grab the next description to write, and change the write pointer */
199 register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
200 pcmbuf_write = pcmbuf_current->link;
201 /* Fill in the values in the new buffer chunk */
202 pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
203 pcmbuf_current->size = size;
204 pcmbuf_current->callback = pcmbuf_event_handler;
205 pcmbuf_current->link = NULL;
206 /* This is single use only */
207 pcmbuf_event_handler = NULL;
208 if (pcmbuf_read) {
Brandon Low6c0908b2006-04-23 22:54:34 +0000209 if (pcmbuf_flush)
210 {
211 pcmbuf_write_end->link = pcmbuf_read->link;
212 pcmbuf_read->link = pcmbuf_current;
213 while (pcmbuf_write_end->link)
214 {
215 pcmbuf_write_end = pcmbuf_write_end->link;
216 pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
217 }
218 pcmbuf_flush = false;
219 }
Brandon Low413da2a2006-02-07 20:38:55 +0000220 /* If there is already a read buffer setup, add to it */
Brandon Low6c0908b2006-04-23 22:54:34 +0000221 else
222 pcmbuf_read_end->link = pcmbuf_current;
Brandon Low413da2a2006-02-07 20:38:55 +0000223 } else {
224 /* Otherwise create the buffer */
225 pcmbuf_read = pcmbuf_current;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000226 }
Brandon Low413da2a2006-02-07 20:38:55 +0000227 /* This is now the last buffer to read */
228 pcmbuf_read_end = pcmbuf_current;
229
230 /* Update bytes counters */
231 pcmbuf_unplayed_bytes += size;
Brandon Low413da2a2006-02-07 20:38:55 +0000232
233 audiobuffer_pos += size;
234 if (audiobuffer_pos >= pcmbuf_size)
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000235 audiobuffer_pos -= pcmbuf_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000236
237 audiobuffer_fillpos = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000238}
239
Brandon Low413da2a2006-02-07 20:38:55 +0000240static void pcmbuf_under_watermark(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000241{
242 /* Fill audio buffer by boosting cpu */
243 pcmbuf_boost(true);
Brandon Low413da2a2006-02-07 20:38:55 +0000244 /* Disable crossfade if < .5s of audio */
Brandon Low6c0908b2006-04-23 22:54:34 +0000245 if (LOW_DATA(2))
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000246 crossfade_active = false;
247}
248
Brandon Low413da2a2006-02-07 20:38:55 +0000249void pcmbuf_set_event_handler(void (*event_handler)(void))
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000250{
251 pcmbuf_event_handler = event_handler;
252}
253
254unsigned int pcmbuf_get_latency(void)
255{
Brandon Low413da2a2006-02-07 20:38:55 +0000256 /* Be careful how this calculation is rearranted, it's easy to overflow */
257 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
258 return bytes / 4 / (NATIVE_FREQUENCY/1000);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000259}
260
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000261void pcmbuf_set_low_latency(bool state)
262{
263 low_latency_mode = state;
264}
265
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000266bool pcmbuf_is_lowdata(void)
267{
Brandon Low413da2a2006-02-07 20:38:55 +0000268 if (!pcm_is_playing() || pcm_is_paused() ||
269 crossfade_init || crossfade_active)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000270 return false;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000271
Brandon Low413da2a2006-02-07 20:38:55 +0000272 /* 0.5 seconds of buffer is low data */
273 return LOW_DATA(2);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000274}
275
Brandon Low6c0908b2006-04-23 22:54:34 +0000276/* Amount of bytes left in the buffer. */
277inline size_t pcmbuf_free(void)
278{
279 if (pcmbuf_read)
280 {
281 size_t read = (size_t)pcmbuf_read->addr;
282 size_t write =
283 (size_t)&audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
284 if (read < write)
285 read += pcmbuf_size;
286 return read - write;
287 }
288 return pcmbuf_size;
289}
290
Miika Pekkarinena4f8d1c2006-01-27 16:25:44 +0000291bool pcmbuf_crossfade_init(bool manual_skip)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000292{
Brandon Low6c0908b2006-04-23 22:54:34 +0000293 /* Can't do two crossfades at once and, no fade if pcm is off now */
294 if (crossfade_init || crossfade_active || !pcm_is_playing())
295 {
296 pcmbuf_play_stop();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000297 return false;
298 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000299
300 /* Not enough data, or crossfade disabled, flush the old data instead */
301 if (LOW_DATA(6) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
302 {
303 pcmbuf_boost(true);
Brandon Lowb9615512006-05-05 03:13:06 +0000304 pcmbuf_flush_fillpos();
Brandon Low6c0908b2006-04-23 22:54:34 +0000305 pcmbuf_flush = true;
306 return false;
307 }
308
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000309 logf("pcmbuf_crossfade_init");
310 pcmbuf_boost(true);
Miika Pekkarinen90161c92005-07-22 16:46:27 +0000311
Miika Pekkarinena4f8d1c2006-01-27 16:25:44 +0000312 /* Don't enable mix mode when skipping tracks manually. */
Brandon Low9602dd72006-04-25 12:34:28 +0000313 crossfade_mode = manual_skip && global_settings.crossfade_fade_out_mixmode;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000314 crossfade_init = true;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000315
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000316 return true;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000317
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000318}
319
320void pcmbuf_play_stop(void)
321{
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000322 /** Prevent a very tiny pop from happening by muting audio
323 * until dma has been initialized. */
324 pcm_mute(true);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000325 pcm_play_stop();
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000326 pcm_mute(false);
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000327
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000328 pcmbuf_unplayed_bytes = 0;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000329 pcmbuf_mix_chunk = NULL;
Brandon Low413da2a2006-02-07 20:38:55 +0000330 if (pcmbuf_read) {
331 pcmbuf_write_end->link = pcmbuf_read;
332 pcmbuf_write_end = pcmbuf_read_end;
333 pcmbuf_read = pcmbuf_read_end = NULL;
334 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000335 audiobuffer_pos = 0;
336 audiobuffer_fillpos = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000337 crossfade_init = false;
338 crossfade_active = false;
Brandon Low6c0908b2006-04-23 22:54:34 +0000339 pcmbuf_flush = false;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000340
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000341 pcmbuf_boost(false);
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000342
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000343}
344
Brandon Low413da2a2006-02-07 20:38:55 +0000345int pcmbuf_used_descs(void) {
346 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
347 unsigned int i = 0;
348 while (pcmbuf_temp) {
349 pcmbuf_temp = pcmbuf_temp->link;
350 i++;
351 }
352 return i;
353}
354
355int pcmbuf_descs(void) {
Brandon Lowddaf5f02006-05-08 11:03:19 +0000356 return pcmbuf_size / PCMBUF_MINAVG_CHUNK;
Brandon Low413da2a2006-02-07 20:38:55 +0000357}
358
359size_t get_pcmbuf_descsize(void) {
360 return pcmbuf_descsize;
361}
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000362
Brandon Low413da2a2006-02-07 20:38:55 +0000363static void pcmbuf_init_pcmbuffers(void) {
364 struct pcmbufdesc *next = pcmbuf_write;
365 next++;
366 pcmbuf_write_end = pcmbuf_write;
367 while ((void *)next < (void *)audiobufend) {
368 pcmbuf_write_end->link=next;
369 pcmbuf_write_end=next;
370 next++;
371 }
372}
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000373
Brandon Low413da2a2006-02-07 20:38:55 +0000374/* Initialize the pcmbuffer the structure looks like this:
375 * ...CODECBUFFER|---------PCMBUF---------|GUARDBUF|DESCS| */
376void pcmbuf_init(size_t bufsize)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000377{
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000378 pcmbuf_size = bufsize;
Brandon Low413da2a2006-02-07 20:38:55 +0000379 pcmbuf_descsize = pcmbuf_descs()*sizeof(struct pcmbufdesc);
380 audiobuffer = (char *)&audiobuf[(audiobufend - audiobuf) -
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000381 (pcmbuf_size + PCMBUF_MIX_CHUNK * 2 + pcmbuf_descsize)];
382 fadebuf = &audiobuffer[pcmbuf_size];
383 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
384 pcmbuf_write = (struct pcmbufdesc *)(&voicebuf[PCMBUF_MIX_CHUNK]);
Brandon Low413da2a2006-02-07 20:38:55 +0000385 pcmbuf_init_pcmbuffers();
Brandon Lowee6a95a2006-01-22 00:03:20 +0000386 position_callback = NULL;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000387 pcmbuf_event_handler = NULL;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000388 pcmbuf_play_stop();
389}
390
Brandon Low413da2a2006-02-07 20:38:55 +0000391size_t pcmbuf_get_bufsize(void)
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000392{
393 return pcmbuf_size;
394}
395
Brandon Low413da2a2006-02-07 20:38:55 +0000396void pcmbuf_pause(bool pause) {
Brandon Low8307d0b2006-03-24 02:38:57 +0000397 if (pause)
398 pcm_mute(true);
Brandon Low413da2a2006-02-07 20:38:55 +0000399 pcm_play_pause(!pause);
Brandon Low8307d0b2006-03-24 02:38:57 +0000400 if (!pause)
401 pcm_mute(false);
Brandon Low86c7e1a2006-04-14 16:42:14 +0000402 pcmbuf_boost(!pause && pcm_is_playing());
Brandon Low413da2a2006-02-07 20:38:55 +0000403}
404
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000405/* Force playback. */
406void pcmbuf_play_start(void)
407{
408 if (!pcm_is_playing() && pcmbuf_unplayed_bytes)
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000409 {
410 /** Prevent a very tiny pop from happening by muting audio
411 * until dma has been initialized. */
412 pcm_mute(true);
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000413
Brandon Low413da2a2006-02-07 20:38:55 +0000414 last_chunksize = pcmbuf_read->size;
415 pcmbuf_unplayed_bytes -= last_chunksize;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000416 pcm_play_data(pcmbuf_callback,
Brandon Low413da2a2006-02-07 20:38:55 +0000417 (unsigned char *)pcmbuf_read->addr, last_chunksize);
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000418
419 /* Now unmute the audio. */
420 pcm_mute(false);
421 }
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000422}
423
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000424/**
425 * Commit samples waiting to the pcm buffer.
426 */
Brandon Low37faaab2006-04-20 02:30:59 +0000427static bool pcmbuf_flush_fillpos(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000428{
Brandon Low413da2a2006-02-07 20:38:55 +0000429 if (audiobuffer_fillpos) {
430 /* Never use the last buffer descriptor */
431 while (pcmbuf_write == pcmbuf_write_end) {
432 logf("pcmbuf_flush_fillpos no descriptors");
433 /* Deboost to let the playback catchup */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000434 pcmbuf_boost(false);
Brandon Low86f1e2e2006-03-24 13:43:15 +0000435 /* If this happens, something is being stupid */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000436 if (!pcm_is_playing()) {
Brandon Low413da2a2006-02-07 20:38:55 +0000437 logf("pcmbuf_flush_fillpos error");
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000438 pcmbuf_play_start();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000439 }
Brandon Low86f1e2e2006-03-24 13:43:15 +0000440 /* Let approximately one chunk of data playback */
441 sleep(PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY * 4) / 5);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000442 }
Brandon Low413da2a2006-02-07 20:38:55 +0000443 pcmbuf_add_chunk();
Brandon Low37faaab2006-04-20 02:30:59 +0000444 return true;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000445 }
Brandon Low37faaab2006-04-20 02:30:59 +0000446 return false;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000447}
448
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000449/**
450 * Completely process the crossfade fade out effect with current pcm buffer.
451 */
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000452static void crossfade_process_buffer(size_t fade_in_delay,
453 size_t fade_out_delay, size_t fade_out_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000454{
Brandon Low9602dd72006-04-25 12:34:28 +0000455 if (!crossfade_mode)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000456 {
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000457 /* Fade out the specified amount of the already processed audio */
458 size_t total_fade_out = fade_out_rem;
Brandon Low6c0908b2006-04-23 22:54:34 +0000459 size_t fade_out_sample;
460 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000461
Brandon Low6c0908b2006-04-23 22:54:34 +0000462 /* Find the right chunk to start fading out */
Brandon Low9ca16a62006-04-24 03:43:43 +0000463 fade_out_delay += crossfade_sample * 2;
Brandon Low6c0908b2006-04-23 22:54:34 +0000464 while (fade_out_delay >= fade_out_chunk->size)
465 {
466 fade_out_delay -= fade_out_chunk->size;
467 fade_out_chunk = fade_out_chunk->link;
468 }
469 /* The start sample within the chunk */
470 fade_out_sample = fade_out_delay / 2;
471
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000472 while (fade_out_rem > 0)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000473 {
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000474 /* Each 1/10 second of audio will have the same fade applied */
Brandon Low19247692006-04-25 10:39:56 +0000475 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
Brandon Low6c0908b2006-04-23 22:54:34 +0000476 int factor = (fade_out_rem << 8) / total_fade_out;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000477
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000478 fade_out_rem -= block_rem;
479
480 /* Fade this block */
Brandon Low6c0908b2006-04-23 22:54:34 +0000481 while (block_rem > 0)
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000482 {
483 /* Fade one sample */
Brandon Low6c0908b2006-04-23 22:54:34 +0000484 short *buf = (short *)(fade_out_chunk->addr);
485 int sample = buf[fade_out_sample];
486 buf[fade_out_sample++] = (sample * factor) >> 8;
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000487
Brandon Low19247692006-04-25 10:39:56 +0000488 block_rem -= 2;
Brandon Low6c0908b2006-04-23 22:54:34 +0000489 /* Move to the next chunk as needed */
490 if (fade_out_sample * 2 >= fade_out_chunk->size)
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000491 {
Brandon Low6c0908b2006-04-23 22:54:34 +0000492 fade_out_chunk = fade_out_chunk->link;
493 fade_out_sample = 0;
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000494 }
495 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000496 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000497 }
498
Brandon Low6c0908b2006-04-23 22:54:34 +0000499 /* Find the right chunk and sample to start fading in */
Brandon Low9ca16a62006-04-24 03:43:43 +0000500 fade_in_delay += crossfade_sample * 2;
Brandon Low6c0908b2006-04-23 22:54:34 +0000501 while (fade_in_delay >= crossfade_chunk->size)
502 {
503 fade_in_delay -= crossfade_chunk->size;
504 crossfade_chunk = crossfade_chunk->link;
505 }
506 crossfade_sample = fade_in_delay / 2;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000507 logf("process done!");
508}
509
Brandon Low6c0908b2006-04-23 22:54:34 +0000510/* Initializes crossfader, calculates all necessary parameters and
511 * performs fade-out with the pcm buffer. */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000512static void crossfade_start(void)
513{
Brandon Low6c0908b2006-04-23 22:54:34 +0000514 size_t crossfade_rem;
Brandon Low9aa49a42006-04-25 02:28:21 +0000515 size_t crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000516 size_t fade_out_rem;
517 size_t fade_out_delay;
518 size_t fade_in_delay;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000519
Brandon Low37faaab2006-04-20 02:30:59 +0000520 crossfade_init = false;
Brandon Low413da2a2006-02-07 20:38:55 +0000521 /* Reject crossfade if less than .5s of data */
522 if (LOW_DATA(2)) {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000523 logf("crossfade rejected");
524 pcmbuf_play_stop();
525 return ;
526 }
527
528 logf("crossfade_start");
Brandon Low413da2a2006-02-07 20:38:55 +0000529 pcmbuf_flush_fillpos();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000530 crossfade_active = true;
Brandon Low6c0908b2006-04-23 22:54:34 +0000531
Brandon Low413da2a2006-02-07 20:38:55 +0000532 /* Initialize the crossfade buffer size to all of the buffered data that
533 * has not yet been sent to the DMA */
Brandon Low6c0908b2006-04-23 22:54:34 +0000534 crossfade_rem = pcmbuf_unplayed_bytes;
535 crossfade_chunk = pcmbuf_read->link;
Brandon Low8ef18272006-04-24 12:41:30 +0000536 crossfade_sample = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000537
Brandon Low6c0908b2006-04-23 22:54:34 +0000538 /* Get fade out delay from settings. */
539 fade_out_delay =
540 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000541
Brandon Low6c0908b2006-04-23 22:54:34 +0000542 /* Get fade out duration from settings. */
543 fade_out_rem =
544 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000545
Brandon Low9aa49a42006-04-25 02:28:21 +0000546 crossfade_need = fade_out_delay + fade_out_rem;
Brandon Low6c0908b2006-04-23 22:54:34 +0000547 /* We want only to modify the last part of the buffer. */
Brandon Low9aa49a42006-04-25 02:28:21 +0000548 if (crossfade_rem > crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000549 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000550 size_t crossfade_extra = crossfade_rem - crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000551 while (crossfade_extra > crossfade_chunk->size)
552 {
553 crossfade_extra -= crossfade_chunk->size;
554 crossfade_chunk = crossfade_chunk->link;
555 }
556 crossfade_sample = crossfade_extra / 2;
557 }
558 /* Truncate fade out duration if necessary. */
Brandon Low9aa49a42006-04-25 02:28:21 +0000559 else if (crossfade_rem < crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000560 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000561 size_t crossfade_short = crossfade_need - crossfade_rem;
562 if (fade_out_rem >= crossfade_short)
Brandon Low6c0908b2006-04-23 22:54:34 +0000563 fade_out_rem -= crossfade_short;
564 else
565 {
566 fade_out_delay -= crossfade_short - fade_out_rem;
567 fade_out_rem = 0;
568 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000569 }
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000570
Brandon Low6c0908b2006-04-23 22:54:34 +0000571 /* Get also fade in duration and delays from settings. */
572 crossfade_fade_in_total =
573 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
574 crossfade_fade_in_rem = crossfade_fade_in_total;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000575
Brandon Low6c0908b2006-04-23 22:54:34 +0000576 fade_in_delay =
577 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
578
579 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000580}
581
Brandon Low9602dd72006-04-25 12:34:28 +0000582/* Returns the number of bytes _NOT_ mixed */
583static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000584{
Brandon Low6c0908b2006-04-23 22:54:34 +0000585 const short *input_buf = (const short *)buf;
Brandon Low9aa49a42006-04-25 02:28:21 +0000586 short *output_buf = (short *)(crossfade_chunk->addr);
587 short *chunk_end = (short *)((size_t)output_buf + crossfade_chunk->size);
588 output_buf = &output_buf[crossfade_sample];
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000589
Brandon Low9602dd72006-04-25 12:34:28 +0000590 while (fade_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000591 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000592 int sample = *input_buf++;
593 sample = ((sample * factor) >> 8) + *output_buf;
594 *output_buf++ = MIN(32767, MAX(-32768, sample));
Brandon Low9602dd72006-04-25 12:34:28 +0000595 fade_rem -= 2;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000596
Brandon Low9aa49a42006-04-25 02:28:21 +0000597 if (output_buf >= chunk_end)
Brandon Low413da2a2006-02-07 20:38:55 +0000598 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000599 crossfade_chunk = crossfade_chunk->link;
600 if (!crossfade_chunk)
Brandon Low9602dd72006-04-25 12:34:28 +0000601 return fade_rem;
Brandon Low9aa49a42006-04-25 02:28:21 +0000602 output_buf = (short *)(crossfade_chunk->addr);
603 chunk_end = (short *)((size_t)output_buf + crossfade_chunk->size);
Brandon Low413da2a2006-02-07 20:38:55 +0000604 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000605 }
Brandon Low9aa49a42006-04-25 02:28:21 +0000606 crossfade_sample = (size_t)(output_buf - (short *)(crossfade_chunk->addr));
Brandon Low9602dd72006-04-25 12:34:28 +0000607 return 0;
608}
609
610/* Returns the number of bytes _NOT_ mixed */
611static size_t crossfade_mix(const char *buf, size_t length)
612{
613 const short *input_buf = (const short *)buf;
614 short *output_buf = (short *)(crossfade_chunk->addr);
615 short *chunk_end = (short *)((size_t)output_buf + crossfade_chunk->size);
616 output_buf = &output_buf[crossfade_sample];
617
618 while (length)
619 {
620 int sample = *input_buf++ + *output_buf;
621 *output_buf++ = MIN(32767, MAX(-32768, sample));
622 length -= 2;
623
624 if (output_buf >= chunk_end)
625 {
626 crossfade_chunk = crossfade_chunk->link;
627 if (!crossfade_chunk)
628 return length;
629 output_buf = (short *)(crossfade_chunk->addr);
630 chunk_end = (short *)((size_t)output_buf + crossfade_chunk->size);
631 }
632 }
633 crossfade_sample = (size_t)(output_buf - (short *)(crossfade_chunk->addr));
634 return 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000635}
636
Brandon Low413da2a2006-02-07 20:38:55 +0000637static void pcmbuf_flush_buffer(const char *buf, size_t length)
638{
639 size_t copy_n;
Brandon Low413da2a2006-02-07 20:38:55 +0000640 while (length > 0) {
641 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
642 if (NEED_FLUSH(audiobuffer_index))
643 {
644 pcmbuf_flush_fillpos();
645 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
646 }
647 copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
648 memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
649 buf += copy_n;
650 audiobuffer_fillpos += copy_n;
651 length -= copy_n;
652 }
653}
654
Brandon Low9aa49a42006-04-25 02:28:21 +0000655static void flush_crossfade(char *buf, size_t length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000656{
Brandon Low9602dd72006-04-25 12:34:28 +0000657 if (length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000658 {
Brandon Low9602dd72006-04-25 12:34:28 +0000659 if (crossfade_fade_in_rem)
Brandon Low9aa49a42006-04-25 02:28:21 +0000660 {
661 size_t samples;
662 short *input_buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000663
664 /* Fade factor for this packet */
665 int factor =
666 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
667 crossfade_fade_in_total;
668 /* Bytes to fade */
669 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
670
671 /* We _will_ fade this many bytes */
672 crossfade_fade_in_rem -= fade_rem;
673
674 if (crossfade_chunk)
675 {
676 /* Mix the data */
677 size_t fade_total = fade_rem;
678 fade_rem = crossfade_fade_mix(factor, buf, fade_rem);
679 length -= fade_total - fade_rem;
680 buf += fade_total - fade_rem;
Brandon Low08cdc432006-04-25 16:12:43 +0000681 if (!length)
682 return;
683 if (!fade_rem)
684 goto fade_done;
Brandon Low9602dd72006-04-25 12:34:28 +0000685 }
686
Brandon Low9aa49a42006-04-25 02:28:21 +0000687 samples = fade_rem / 2;
688 input_buf = (short *)buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000689 /* Fade remaining samples in place */
Brandon Low9aa49a42006-04-25 02:28:21 +0000690 while (samples)
691 {
692 int sample = *input_buf;
693 *input_buf++ = (sample * factor) >> 8;
694 samples--;
695 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000696 }
Brandon Low413da2a2006-02-07 20:38:55 +0000697
Brandon Low08cdc432006-04-25 16:12:43 +0000698fade_done:
Brandon Low9602dd72006-04-25 12:34:28 +0000699 if (crossfade_chunk)
Brandon Low6c0908b2006-04-23 22:54:34 +0000700 {
Brandon Low9602dd72006-04-25 12:34:28 +0000701 /* Mix the data */
702 size_t mix_total = length;
703 length = crossfade_mix(buf, length);
704 buf += mix_total - length;
Brandon Low08cdc432006-04-25 16:12:43 +0000705 if (!length)
706 return;
Brandon Low6c0908b2006-04-23 22:54:34 +0000707 }
Brandon Low9602dd72006-04-25 12:34:28 +0000708
Brandon Low08cdc432006-04-25 16:12:43 +0000709 /* Flush samples to the buffer */
710 while (!prepare_insert(length))
711 sleep(1);
712 pcmbuf_flush_buffer(buf, length);
Brandon Low9aa49a42006-04-25 02:28:21 +0000713 }
714
Brandon Low413da2a2006-02-07 20:38:55 +0000715}
716
717static bool prepare_insert(size_t length)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000718{
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000719 if (low_latency_mode)
720 {
721 /* 1/4s latency. */
722 if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 4
723 && pcm_is_playing())
724 return false;
725 }
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000726
Brandon Low3bbd93b2006-02-13 17:08:53 +0000727 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
Brandon Low9602dd72006-04-25 12:34:28 +0000728 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
Brandon Low413da2a2006-02-07 20:38:55 +0000729 {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000730 pcmbuf_boost(false);
731 return false;
732 }
Miika Pekkarinend83b6592005-07-19 19:57:23 +0000733
Brandon Low413da2a2006-02-07 20:38:55 +0000734 if (!pcm_is_playing())
735 {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000736 pcmbuf_boost(true);
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000737 /* Pre-buffer 1s. */
Brandon Low413da2a2006-02-07 20:38:55 +0000738 if (!LOW_DATA(4))
739 {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000740 logf("pcm starting");
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000741 pcmbuf_play_start();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000742 }
Brandon Low3a37fae2006-02-13 16:55:25 +0000743 } else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
744 pcmbuf_under_watermark();
745
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000746 return true;
747}
748
Brandon Low413da2a2006-02-07 20:38:55 +0000749void* pcmbuf_request_buffer(size_t length, size_t *realsize)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000750{
Brandon Low37faaab2006-04-20 02:30:59 +0000751 if (crossfade_init)
752 crossfade_start();
753
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000754 if (crossfade_active) {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000755 *realsize = MIN(length, PCMBUF_MIX_CHUNK);
756 return fadebuf;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000757 }
Brandon Low413da2a2006-02-07 20:38:55 +0000758 else
759 {
760 if(prepare_insert(length))
761 {
762 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
Brandon Low3bbd93b2006-02-13 17:08:53 +0000763 *realsize = length;
764 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
765 {
766 /* Usual case, there's space here */
767 return &audiobuffer[audiobuffer_index];
Brandon Low413da2a2006-02-07 20:38:55 +0000768 }
769 else
770 {
Brandon Low3bbd93b2006-02-13 17:08:53 +0000771 /* Flush and wrap the buffer */
772 pcmbuf_flush_fillpos();
773 audiobuffer_pos = 0;
774 return &audiobuffer[0];
Brandon Low413da2a2006-02-07 20:38:55 +0000775 }
776 }
777 else
778 {
779 *realsize = 0;
780 return NULL;
781 }
782 }
783}
784
785void* pcmbuf_request_voice_buffer(size_t length, size_t *realsize, bool mix)
786{
787 if (mix)
788 {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000789 if (pcmbuf_mix_chunk || pcmbuf_read->link)
790 {
791 *realsize = MIN(length, PCMBUF_MIX_CHUNK);
792 return voicebuf;
793 }
794 else
795 {
796 *realsize = 0;
797 return NULL;
798 }
Brandon Low413da2a2006-02-07 20:38:55 +0000799 }
800 else
801 return pcmbuf_request_buffer(length, realsize);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000802}
803
804bool pcmbuf_is_crossfade_active(void)
805{
806 return crossfade_active || crossfade_init;
807}
808
Brandon Low413da2a2006-02-07 20:38:55 +0000809void pcmbuf_write_complete(size_t length)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000810{
Brandon Low37faaab2006-04-20 02:30:59 +0000811 if (crossfade_active)
Brandon Low08cdc432006-04-25 16:12:43 +0000812 {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000813 flush_crossfade(fadebuf, length);
Brandon Low08cdc432006-04-25 16:12:43 +0000814 if (!(crossfade_fade_in_rem || crossfade_chunk))
815 crossfade_active = false;
816 }
Brandon Low413da2a2006-02-07 20:38:55 +0000817 else
818 {
Brandon Low413da2a2006-02-07 20:38:55 +0000819 audiobuffer_fillpos += length;
820
821 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000822 pcmbuf_flush_fillpos();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000823 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000824}
825
Brandon Low9aa49a42006-04-25 02:28:21 +0000826bool pcmbuf_insert_buffer(char *buf, size_t length)
Brandon Low413da2a2006-02-07 20:38:55 +0000827{
Brandon Low08cdc432006-04-25 16:12:43 +0000828 if (crossfade_active)
829 {
Brandon Low37faaab2006-04-20 02:30:59 +0000830 flush_crossfade(buf, length);
Brandon Low08cdc432006-04-25 16:12:43 +0000831 if (!(crossfade_fade_in_rem || crossfade_chunk))
832 crossfade_active = false;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000833 }
Brandon Low413da2a2006-02-07 20:38:55 +0000834 else
835 {
Brandon Low08cdc432006-04-25 16:12:43 +0000836 if (!prepare_insert(length))
837 return false;
Brandon Low413da2a2006-02-07 20:38:55 +0000838 pcmbuf_flush_buffer(buf, length);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000839 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000840 return true;
841}
842
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000843/* Generates a constant square wave sound with a given frequency
844 in Hertz for a duration in milliseconds. */
Brandon Low9535a9a2006-02-22 01:56:44 +0000845void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000846{
Brandon Low9535a9a2006-02-22 01:56:44 +0000847 unsigned int count = 0, i = 0;
Brandon Low413da2a2006-02-07 20:38:55 +0000848 unsigned int interval = NATIVE_FREQUENCY / frequency;
Brandon Low0fcd4112006-04-05 13:00:31 +0000849 long sample;
Brandon Low9535a9a2006-02-22 01:56:44 +0000850 short *buf;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000851 short *pcmbuf_end = (short *)fadebuf;
Brandon Low9535a9a2006-02-22 01:56:44 +0000852 size_t samples = NATIVE_FREQUENCY / 1000 * duration;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000853
Brandon Low0fcd4112006-04-05 13:00:31 +0000854 if (pcm_is_playing())
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000855 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000856 if (pcmbuf_read->link)
857 {
858 /* Get the next chunk */
859 char *pcmbuf_mix_buf = pcmbuf_read->link->addr;
860 /* Give at least 1/8s clearance. */
861 buf = (short *)&pcmbuf_mix_buf[NATIVE_FREQUENCY * 4 / 8];
862 }
863 else
864 {
865 logf("No place to beep");
866 return;
867 }
868
Brandon Low0fcd4112006-04-05 13:00:31 +0000869 while (i++ < samples)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000870 {
Brandon Low0fcd4112006-04-05 13:00:31 +0000871 sample = *buf;
872 *buf++ = MIN(MAX(sample + amplitude, -32768), 32767);
873 if (buf > pcmbuf_end)
874 buf = (short *)audiobuffer;
875 sample = *buf;
876 *buf++ = MIN(MAX(sample + amplitude, -32768), 32767);
877
878 /* Toggle square wav side */
879 if (++count >= interval)
880 {
881 count = 0;
882 amplitude = -amplitude;
883 }
884 if (buf > pcmbuf_end)
885 buf = (short *)audiobuffer;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000886 }
Brandon Low9535a9a2006-02-22 01:56:44 +0000887 }
Brandon Low0fcd4112006-04-05 13:00:31 +0000888 else
889 {
890 buf = (short *)audiobuffer;
891 while (i++ < samples)
892 {
893 *buf++ = amplitude;
894 if (buf > pcmbuf_end)
895 buf = (short *)audiobuffer;
896 *buf++ = amplitude;
897
Brandon Lowa1315802006-04-07 20:03:26 +0000898 /* Toggle square wav side */
Brandon Low0fcd4112006-04-05 13:00:31 +0000899 if (++count >= interval)
900 {
901 count = 0;
902 amplitude = -amplitude;
903 }
904 if (buf > pcmbuf_end)
905 buf = (short *)audiobuffer;
906 }
Brandon Low9535a9a2006-02-22 01:56:44 +0000907 pcm_play_data(NULL, (unsigned char *)audiobuffer, samples * 4);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000908 }
909}
910
911/* Returns pcm buffer usage in percents (0 to 100). */
912int pcmbuf_usage(void)
913{
914 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
915}
916
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000917int pcmbuf_mix_free(void)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000918{
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000919 if (pcmbuf_mix_chunk)
920 {
921 size_t my_mix_end =
922 (size_t)&((short *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
923 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
924 if (my_write_pos < my_mix_end)
925 my_write_pos += pcmbuf_size;
926 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
927 }
928 return 100;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000929}
930
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000931void pcmbuf_mix_voice(size_t length)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000932{
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000933 short *ibuf = (short *)voicebuf;
934 short *obuf;
935 size_t chunk_samples;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000936
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000937 if (!pcmbuf_mix_chunk && pcmbuf_read)
938 {
939 pcmbuf_mix_chunk = pcmbuf_read->link;
940 /* Start 1/8s into the next chunk */
941 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
942 }
943 if (!pcmbuf_mix_chunk)
944 return;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000945
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000946 obuf = (short *)pcmbuf_mix_chunk->addr;
947 chunk_samples = pcmbuf_mix_chunk->size / 2;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000948
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000949 length /= 2;
950
951 while (length-- > 0) {
Brandon Low6c0908b2006-04-23 22:54:34 +0000952 int sample = *ibuf++;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000953 if (pcmbuf_mix_sample >= chunk_samples)
954 {
955 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
956 if (!pcmbuf_mix_chunk)
957 return;
958 pcmbuf_mix_sample = 0;
959 obuf = pcmbuf_mix_chunk->addr;
960 chunk_samples = pcmbuf_mix_chunk->size / 2;
961 }
Brandon Low920516c2006-04-23 05:30:52 +0000962 sample += obuf[pcmbuf_mix_sample] >> 2;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000963 obuf[pcmbuf_mix_sample++] = MIN(MAX(sample, -32768), 32767);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000964 }
965}
966
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000967void pcmbuf_crossfade_enable(bool on_off)
968{
969 crossfade_enabled = on_off;
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000970
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000971 if (crossfade_enabled) {
Brandon Low413da2a2006-02-07 20:38:55 +0000972 /* If crossfading, try to keep the buffer full other than 2 second */
973 pcmbuf_set_watermark_bytes(pcmbuf_size - PCMBUF_WATERMARK * 2);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000974 } else {
Brandon Low413da2a2006-02-07 20:38:55 +0000975 /* Otherwise, just keep it above 1 second */
Brandon Lowa3868d32006-01-21 22:42:44 +0000976 pcmbuf_set_watermark_bytes(PCMBUF_WATERMARK);
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000977 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000978}
979
980bool pcmbuf_is_crossfade_enabled(void)
981{
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000982 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
983 return global_settings.playlist_shuffle;
984
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000985 return crossfade_enabled;
986}
987