blob: 7723b70876f42a8b2faee53d2fdf802ecf955ba5 [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 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Miika Pekkarinen20b38972005-07-13 12:48:22 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <stdbool.h>
23#include <stdio.h>
24#include "config.h"
25#include "debug.h"
26#include "panic.h"
27#include <kernel.h>
28#include "pcmbuf.h"
Michael Sevakis6077e5b2007-10-06 22:27:27 +000029#include "pcm.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000030#include "logf.h"
31#ifndef SIMULATOR
32#include "cpu.h"
33#endif
34#include "system.h"
35#include <string.h>
36#include "buffer.h"
Miika Pekkarinenf090dc32005-07-21 11:44:00 +000037#include "settings.h"
38#include "audio.h"
Michael Sevakis99617d72007-11-18 17:12:19 +000039#include "voice_thread.h"
Miika Pekkarinen159c52d2005-08-20 11:13:19 +000040#include "dsp.h"
Miika Pekkarinena85044b2006-09-16 16:18:11 +000041#include "thread.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000042
Michael Sevakis3bec38e2007-06-24 20:36:58 +000043/* Define PCMBUF_MUTING if the codec requires muting to prevent pops */
Michael Sevakis405e90e2008-05-05 14:52:07 +000044#if !defined(HAVE_UDA1380) && !defined(HAVE_TLV320) && !defined(HAVE_AS3514) \
45 && !defined(HAVE_WM8978)
Michael Sevakisbf656762006-11-16 05:33:42 +000046#define PCMBUF_MUTING
47#endif
48
Michael Sevakis53981252007-11-16 14:26:05 +000049/* Clip sample to signed 16 bit range */
50static inline int32_t clip_sample_16(int32_t sample)
51{
52 if ((int16_t)sample != sample)
53 sample = 0x7fff ^ (sample >> 31);
54 return sample;
55}
56
Miika Pekkarinena85044b2006-09-16 16:18:11 +000057/* Keep watermark high for iPods at least (2s) */
Brandon Low1f3360f2006-10-13 20:48:23 +000058#define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
Miika Pekkarinen20b38972005-07-13 12:48:22 +000059
Miika Pekkarinen20b38972005-07-13 12:48:22 +000060/* Structure we can use to queue pcm chunks in memory to be played
61 * by the driver code. */
62struct pcmbufdesc
63{
64 void *addr;
Brandon Low413da2a2006-02-07 20:38:55 +000065 size_t size;
66 struct pcmbufdesc* link;
Miika Pekkarinen20b38972005-07-13 12:48:22 +000067 /* Call this when the buffer has been played */
68 void (*callback)(void);
Brandon Low413da2a2006-02-07 20:38:55 +000069};
Miika Pekkarinen20b38972005-07-13 12:48:22 +000070
Michael Sevakise1dd10d2007-03-19 22:04:17 +000071#define PCMBUF_DESCS(bufsize) \
72 ((bufsize) / PCMBUF_MINAVG_CHUNK)
73#define PCMBUF_DESCS_SIZE(bufsize) \
74 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
Michael Sevakis0f5cb942006-11-06 18:07:30 +000075
Brandon Low6c0908b2006-04-23 22:54:34 +000076/* Size of the PCM buffer. */
77static size_t pcmbuf_size IDATA_ATTR = 0;
Michael Sevakis0f5cb942006-11-06 18:07:30 +000078static char *pcmbuf_bufend IDATA_ATTR;
Brandon Low6c0908b2006-04-23 22:54:34 +000079static char *audiobuffer IDATA_ATTR;
80/* Current audio buffer write index. */
81static size_t audiobuffer_pos IDATA_ATTR;
82/* Amount audiobuffer_pos will be increased.*/
83static size_t audiobuffer_fillpos IDATA_ATTR;
84static char *fadebuf IDATA_ATTR;
85static char *voicebuf IDATA_ATTR;
86
87static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
Steve Bavin135cc752008-03-28 12:51:33 +000088static void (*position_callback)(size_t size) IDATA_ATTR;
Brandon Low6c0908b2006-04-23 22:54:34 +000089
90/* Crossfade related state */
91static bool crossfade_enabled;
Michael Sevakise1dd10d2007-03-19 22:04:17 +000092static bool crossfade_enabled_pending;
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +000093static bool crossfade_mixmode;
Brandon Low6c0908b2006-04-23 22:54:34 +000094static bool crossfade_active IDATA_ATTR;
95static bool crossfade_init IDATA_ATTR;
96
97/* Track the current location for processing crossfade */
98static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
99static size_t crossfade_sample IDATA_ATTR;
100
101/* Counters for fading in new data */
102static size_t crossfade_fade_in_total IDATA_ATTR;
103static size_t crossfade_fade_in_rem IDATA_ATTR;
104
Brandon Low413da2a2006-02-07 20:38:55 +0000105static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
106static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
107static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
108static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
109static size_t last_chunksize IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +0000110
Brandon Low413da2a2006-02-07 20:38:55 +0000111static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
Brandon Low413da2a2006-02-07 20:38:55 +0000112static size_t pcmbuf_watermark IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +0000113
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000114static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
115static size_t pcmbuf_mix_sample IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +0000116
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000117static bool low_latency_mode = false;
Brandon Low6c0908b2006-04-23 22:54:34 +0000118static bool pcmbuf_flush;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000119
Michael Sevakis938593b2007-03-06 21:00:45 +0000120#ifdef HAVE_PRIORITY_SCHEDULING
Michael Sevakis27cf6772008-03-25 02:34:12 +0000121static int codec_thread_priority = PRIORITY_PLAYBACK;
Michael Sevakis938593b2007-03-06 21:00:45 +0000122#endif
Michael Sevakisb425de72007-03-06 20:32:13 +0000123
Miika Pekkarinena85044b2006-09-16 16:18:11 +0000124extern struct thread_entry *codec_thread_p;
125
Brandon Low413da2a2006-02-07 20:38:55 +0000126/* Helpful macros for use in conditionals this assumes some of the above
127 * static variable names */
128#define NEED_FLUSH(position) \
129 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
130#define LOW_DATA(quarter_secs) \
131 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
132
Brandon Low9602dd72006-04-25 12:34:28 +0000133static bool prepare_insert(size_t length);
Brandon Low413da2a2006-02-07 20:38:55 +0000134static void pcmbuf_under_watermark(void);
Brandon Low37faaab2006-04-20 02:30:59 +0000135static bool pcmbuf_flush_fillpos(void);
Brandon Low413da2a2006-02-07 20:38:55 +0000136
Brandon Low413da2a2006-02-07 20:38:55 +0000137#define CALL_IF_EXISTS(function, args...) if (function) function(args)
138/* This function has 2 major logical parts (separated by brackets both for
139 * readability and variable scoping). The first part performs the
140 * operastions related to finishing off the last buffer we fed to the DMA.
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000141 * The second part performs the operations involved in sending a new buffer
Brandon Low413da2a2006-02-07 20:38:55 +0000142 * to the DMA. Finally the function checks the status of the buffer and
143 * boosts if necessary */
144static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
145static void pcmbuf_callback(unsigned char** start, size_t* size)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000146{
Brandon Low413da2a2006-02-07 20:38:55 +0000147 {
148 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
149 /* Take the finished buffer out of circulation */
150 pcmbuf_read = pcmbuf_current->link;
151
Brandon Low6c0908b2006-04-23 22:54:34 +0000152 /* The buffer is finished, call the callback functions */
153 CALL_IF_EXISTS(position_callback, last_chunksize);
Brandon Low413da2a2006-02-07 20:38:55 +0000154 CALL_IF_EXISTS(pcmbuf_current->callback);
155
156 /* Put the finished buffer back into circulation */
157 pcmbuf_write_end->link = pcmbuf_current;
158 pcmbuf_write_end = pcmbuf_current;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000159
Brandon Low08cdc432006-04-25 16:12:43 +0000160 /* If we've read over the mix chunk while it's still mixing there */
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000161 if (pcmbuf_current == pcmbuf_mix_chunk)
162 pcmbuf_mix_chunk = NULL;
Brandon Low08cdc432006-04-25 16:12:43 +0000163 /* If we've read over the crossfade chunk while it's still fading */
Brandon Low9aa49a42006-04-25 02:28:21 +0000164 if (pcmbuf_current == crossfade_chunk)
Brandon Low23064332006-04-25 16:15:11 +0000165 crossfade_chunk = pcmbuf_read;
Brandon Low413da2a2006-02-07 20:38:55 +0000166 }
167
168 {
169 /* Send the new buffer to the pcm */
170 struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
171 size_t *realsize = size;
172 unsigned char** realstart = start;
173 if(pcmbuf_new)
174 {
175 size_t current_size = pcmbuf_new->size;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000176
Brandon Low413da2a2006-02-07 20:38:55 +0000177 pcmbuf_unplayed_bytes -= current_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000178 last_chunksize = current_size;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000179 *realsize = current_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000180 *realstart = pcmbuf_new->addr;
181 }
182 else
183 {
184 /* No more buffers */
185 last_chunksize = 0;
186 *realsize = 0;
187 *realstart = NULL;
188 CALL_IF_EXISTS(pcmbuf_event_handler);
189 }
190 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000191}
192
Steve Bavin135cc752008-03-28 12:51:33 +0000193void pcmbuf_set_position_callback(void (*callback)(size_t size))
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000194{
Brandon Lowa3868d32006-01-21 22:42:44 +0000195 position_callback = callback;
196}
197
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000198static void pcmbuf_set_watermark_bytes(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000199{
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000200 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
201 /* If crossfading, try to keep the buffer full other than 1 second */
202 (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
203 /* Otherwise, just keep it above 2 second */
204 PCMBUF_WATERMARK;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000205}
206
Brandon Low413da2a2006-02-07 20:38:55 +0000207/* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
208 * in a separate function for the moment */
209static inline void pcmbuf_add_chunk(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000210{
Brandon Low413da2a2006-02-07 20:38:55 +0000211 register size_t size = audiobuffer_fillpos;
212 /* Grab the next description to write, and change the write pointer */
213 register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
214 pcmbuf_write = pcmbuf_current->link;
215 /* Fill in the values in the new buffer chunk */
216 pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
217 pcmbuf_current->size = size;
218 pcmbuf_current->callback = pcmbuf_event_handler;
219 pcmbuf_current->link = NULL;
220 /* This is single use only */
221 pcmbuf_event_handler = NULL;
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000222 if (pcmbuf_read != NULL) {
Brandon Low6c0908b2006-04-23 22:54:34 +0000223 if (pcmbuf_flush)
224 {
225 pcmbuf_write_end->link = pcmbuf_read->link;
226 pcmbuf_read->link = pcmbuf_current;
227 while (pcmbuf_write_end->link)
228 {
229 pcmbuf_write_end = pcmbuf_write_end->link;
230 pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
231 }
232 pcmbuf_flush = false;
233 }
Brandon Low413da2a2006-02-07 20:38:55 +0000234 /* If there is already a read buffer setup, add to it */
Brandon Low6c0908b2006-04-23 22:54:34 +0000235 else
236 pcmbuf_read_end->link = pcmbuf_current;
Brandon Low413da2a2006-02-07 20:38:55 +0000237 } else {
238 /* Otherwise create the buffer */
239 pcmbuf_read = pcmbuf_current;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000240 }
Brandon Low413da2a2006-02-07 20:38:55 +0000241 /* This is now the last buffer to read */
242 pcmbuf_read_end = pcmbuf_current;
243
244 /* Update bytes counters */
245 pcmbuf_unplayed_bytes += size;
Brandon Low413da2a2006-02-07 20:38:55 +0000246
247 audiobuffer_pos += size;
248 if (audiobuffer_pos >= pcmbuf_size)
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000249 audiobuffer_pos -= pcmbuf_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000250
251 audiobuffer_fillpos = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000252}
253
Michael Sevakisb425de72007-03-06 20:32:13 +0000254#ifdef HAVE_PRIORITY_SCHEDULING
255static void boost_codec_thread(bool boost)
256{
Michael Sevakis99617d72007-11-18 17:12:19 +0000257 /* Keep voice and codec threads at the same priority or else voice
258 * will starve if the codec thread's priority is boosted. */
Michael Sevakisb425de72007-03-06 20:32:13 +0000259 if (boost)
260 {
Michael Sevakis27cf6772008-03-25 02:34:12 +0000261 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
262 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
263
264 if (priority != codec_thread_priority)
Michael Sevakis99617d72007-11-18 17:12:19 +0000265 {
Michael Sevakis27cf6772008-03-25 02:34:12 +0000266 codec_thread_priority = priority;
267 thread_set_priority(codec_thread_p, priority);
268 voice_thread_set_priority(priority);
Michael Sevakis99617d72007-11-18 17:12:19 +0000269 }
Michael Sevakisb425de72007-03-06 20:32:13 +0000270 }
Michael Sevakis27cf6772008-03-25 02:34:12 +0000271 else if (codec_thread_priority != PRIORITY_PLAYBACK)
Michael Sevakisb425de72007-03-06 20:32:13 +0000272 {
Michael Sevakis27cf6772008-03-25 02:34:12 +0000273 thread_set_priority(codec_thread_p, PRIORITY_PLAYBACK);
274 voice_thread_set_priority(PRIORITY_PLAYBACK);
275 codec_thread_priority = PRIORITY_PLAYBACK;
Michael Sevakisb425de72007-03-06 20:32:13 +0000276 }
277}
278#endif /* HAVE_PRIORITY_SCHEDULING */
279
Brandon Low413da2a2006-02-07 20:38:55 +0000280static void pcmbuf_under_watermark(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000281{
Michael Sevakisb425de72007-03-06 20:32:13 +0000282 /* Only codec thread initiates boost - voice boosts the cpu when playing
283 a clip */
Michael Sevakis938593b2007-03-06 21:00:45 +0000284#ifndef SIMULATOR
Michael Sevakisb425de72007-03-06 20:32:13 +0000285 if (thread_get_current() == codec_thread_p)
Michael Sevakis938593b2007-03-06 21:00:45 +0000286#endif /* SIMULATOR */
Michael Sevakisb425de72007-03-06 20:32:13 +0000287 {
Miika Pekkarinen70587522006-10-15 11:57:52 +0000288#ifdef HAVE_PRIORITY_SCHEDULING
Michael Sevakisb425de72007-03-06 20:32:13 +0000289 /* If buffer is critically low, override UI priority, else
290 set back to the original priority. */
291 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
Miika Pekkarinen70587522006-10-15 11:57:52 +0000292#endif
Michael Sevakisb425de72007-03-06 20:32:13 +0000293 /* Fill audio buffer by boosting cpu */
294 trigger_cpu_boost();
295 }
Miika Pekkarinen70587522006-10-15 11:57:52 +0000296
Brandon Low413da2a2006-02-07 20:38:55 +0000297 /* Disable crossfade if < .5s of audio */
Brandon Low6c0908b2006-04-23 22:54:34 +0000298 if (LOW_DATA(2))
Miika Pekkarinena85044b2006-09-16 16:18:11 +0000299 {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000300 crossfade_active = false;
Miika Pekkarinena85044b2006-09-16 16:18:11 +0000301 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000302}
303
Brandon Low413da2a2006-02-07 20:38:55 +0000304void pcmbuf_set_event_handler(void (*event_handler)(void))
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000305{
306 pcmbuf_event_handler = event_handler;
307}
308
309unsigned int pcmbuf_get_latency(void)
310{
Nicolas Pennequinca55f132008-01-08 14:33:17 +0000311 /* Be careful how this calculation is rearranged, it's easy to overflow */
Brandon Low413da2a2006-02-07 20:38:55 +0000312 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
313 return bytes / 4 / (NATIVE_FREQUENCY/1000);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000314}
315
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000316void pcmbuf_set_low_latency(bool state)
317{
318 low_latency_mode = state;
319}
320
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000321bool pcmbuf_is_lowdata(void)
322{
Brandon Low413da2a2006-02-07 20:38:55 +0000323 if (!pcm_is_playing() || pcm_is_paused() ||
324 crossfade_init || crossfade_active)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000325 return false;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000326
Miika Pekkarinena85044b2006-09-16 16:18:11 +0000327 /* 1 seconds of buffer is low data */
328 return LOW_DATA(4);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000329}
330
Brandon Low6c0908b2006-04-23 22:54:34 +0000331/* Amount of bytes left in the buffer. */
332inline size_t pcmbuf_free(void)
333{
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000334 if (pcmbuf_read != NULL)
Brandon Low6c0908b2006-04-23 22:54:34 +0000335 {
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000336 void *read = pcmbuf_read->addr;
337 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
Brandon Low6c0908b2006-04-23 22:54:34 +0000338 if (read < write)
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000339 return (size_t)(read - write) + pcmbuf_size;
340 else
341 return (size_t) (read - write);
Brandon Low6c0908b2006-04-23 22:54:34 +0000342 }
343 return pcmbuf_size;
344}
345
Miika Pekkarinena4f8d1c2006-01-27 16:25:44 +0000346bool pcmbuf_crossfade_init(bool manual_skip)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000347{
Brandon Low6c0908b2006-04-23 22:54:34 +0000348 /* Can't do two crossfades at once and, no fade if pcm is off now */
349 if (crossfade_init || crossfade_active || !pcm_is_playing())
350 {
351 pcmbuf_play_stop();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000352 return false;
353 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000354
Miika Pekkarinen70587522006-10-15 11:57:52 +0000355 trigger_cpu_boost();
Steve Bavinb24ed652008-04-03 14:24:37 +0000356
Brandon Low6c0908b2006-04-23 22:54:34 +0000357 /* Not enough data, or crossfade disabled, flush the old data instead */
Miika Pekkarinenf3add922006-08-02 18:35:24 +0000358 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
Brandon Low6c0908b2006-04-23 22:54:34 +0000359 {
Brandon Lowb9615512006-05-05 03:13:06 +0000360 pcmbuf_flush_fillpos();
Brandon Low6c0908b2006-04-23 22:54:34 +0000361 pcmbuf_flush = true;
362 return false;
363 }
364
Miika Pekkarinena4f8d1c2006-01-27 16:25:44 +0000365 /* Don't enable mix mode when skipping tracks manually. */
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +0000366 if (manual_skip)
367 crossfade_mixmode = false;
368 else
369 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
Steve Bavinb24ed652008-04-03 14:24:37 +0000370
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000371 crossfade_init = true;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000372
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000373 return true;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000374
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000375}
376
377void pcmbuf_play_stop(void)
378{
379 pcm_play_stop();
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000380
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000381 pcmbuf_unplayed_bytes = 0;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000382 pcmbuf_mix_chunk = NULL;
Brandon Low413da2a2006-02-07 20:38:55 +0000383 if (pcmbuf_read) {
384 pcmbuf_write_end->link = pcmbuf_read;
385 pcmbuf_write_end = pcmbuf_read_end;
386 pcmbuf_read = pcmbuf_read_end = NULL;
387 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000388 audiobuffer_pos = 0;
389 audiobuffer_fillpos = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000390 crossfade_init = false;
391 crossfade_active = false;
Brandon Low6c0908b2006-04-23 22:54:34 +0000392 pcmbuf_flush = false;
Michael Sevakisb425de72007-03-06 20:32:13 +0000393
394#ifdef HAVE_PRIORITY_SCHEDULING
395 /* Can unboost the codec thread here no matter who's calling */
396 boost_codec_thread(false);
397#endif
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000398}
399
Brandon Low413da2a2006-02-07 20:38:55 +0000400int pcmbuf_used_descs(void) {
401 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
402 unsigned int i = 0;
403 while (pcmbuf_temp) {
404 pcmbuf_temp = pcmbuf_temp->link;
405 i++;
406 }
407 return i;
408}
409
410int pcmbuf_descs(void) {
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000411 return PCMBUF_DESCS(pcmbuf_size);
Brandon Low413da2a2006-02-07 20:38:55 +0000412}
413
Brandon Low413da2a2006-02-07 20:38:55 +0000414static void pcmbuf_init_pcmbuffers(void) {
415 struct pcmbufdesc *next = pcmbuf_write;
416 next++;
417 pcmbuf_write_end = pcmbuf_write;
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000418 while ((void *)next < (void *)pcmbuf_bufend) {
Brandon Low413da2a2006-02-07 20:38:55 +0000419 pcmbuf_write_end->link=next;
420 pcmbuf_write_end=next;
421 next++;
422 }
423}
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000424
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000425static size_t pcmbuf_get_next_required_pcmbuf_size(void)
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000426{
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000427#if MEM > 1
428 size_t seconds = 1;
429
430 if (crossfade_enabled_pending)
431 seconds += global_settings.crossfade_fade_out_delay
432 + global_settings.crossfade_fade_out_duration;
433
434 /* Buffer has to be at least 2s long. */
435 seconds += 2;
436 logf("pcmbuf len: %ld", seconds);
437 return seconds * (NATIVE_FREQUENCY*4);
438#else
439 return NATIVE_FREQUENCY*2;
440#endif
441}
442
443static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
444{
445 return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
446 PCMBUF_DESCS_SIZE(bufsize));
447}
448
449bool pcmbuf_is_same_size(void)
450{
451 if (audiobuffer == NULL)
452 return true; /* Not set up yet even once so always */
453
454 size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
455 return pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000456}
457
Brandon Low413da2a2006-02-07 20:38:55 +0000458/* Initialize the pcmbuffer the structure looks like this:
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000459 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000460size_t pcmbuf_init(unsigned char *bufend)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000461{
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000462 pcmbuf_bufend = bufend;
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000463 pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
464 audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000465 fadebuf = &audiobuffer[pcmbuf_size];
466 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000467 pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000468
Brandon Low413da2a2006-02-07 20:38:55 +0000469 pcmbuf_init_pcmbuffers();
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000470
Brandon Lowee6a95a2006-01-22 00:03:20 +0000471 position_callback = NULL;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000472 pcmbuf_event_handler = NULL;
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000473
474 pcmbuf_crossfade_enable_finished();
475
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000476 pcmbuf_play_stop();
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000477
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000478 return pcmbuf_bufend - audiobuffer;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000479}
480
Brandon Low413da2a2006-02-07 20:38:55 +0000481size_t pcmbuf_get_bufsize(void)
Miika Pekkarinenf090dc32005-07-21 11:44:00 +0000482{
483 return pcmbuf_size;
484}
485
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000486#ifdef ROCKBOX_HAS_LOGF
487unsigned char * pcmbuf_get_meminfo(size_t *length)
488{
489 *length = pcmbuf_bufend - audiobuffer;
490 return audiobuffer;
491}
492#endif
493
Michael Sevakis3bec38e2007-06-24 20:36:58 +0000494void pcmbuf_pause(bool pause)
495{
Michael Sevakisbf656762006-11-16 05:33:42 +0000496#ifdef PCMBUF_MUTING
Brandon Low8307d0b2006-03-24 02:38:57 +0000497 if (pause)
Michael Sevakisbf656762006-11-16 05:33:42 +0000498 pcm_mute(true);
499#endif
Michael Sevakis6077e5b2007-10-06 22:27:27 +0000500
501 if (pcm_is_playing())
502 pcm_play_pause(!pause);
503 else if (!pause)
504 pcmbuf_play_start();
505
Michael Sevakisbf656762006-11-16 05:33:42 +0000506#ifdef PCMBUF_MUTING
Brandon Low8307d0b2006-03-24 02:38:57 +0000507 if (!pause)
508 pcm_mute(false);
Michael Sevakisbf656762006-11-16 05:33:42 +0000509#endif
Miika Pekkarinen70587522006-10-15 11:57:52 +0000510 trigger_cpu_boost();
Brandon Low413da2a2006-02-07 20:38:55 +0000511}
512
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000513/* Force playback. */
514void pcmbuf_play_start(void)
515{
Michael Sevakis3bec38e2007-06-24 20:36:58 +0000516 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000517 {
Michael Sevakis3bec38e2007-06-24 20:36:58 +0000518 last_chunksize = pcmbuf_read->size;
519 pcmbuf_unplayed_bytes -= last_chunksize;
520 pcm_play_data(pcmbuf_callback,
521 (unsigned char *)pcmbuf_read->addr, last_chunksize);
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000522 }
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000523}
524
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000525/**
526 * Commit samples waiting to the pcm buffer.
527 */
Brandon Low37faaab2006-04-20 02:30:59 +0000528static bool pcmbuf_flush_fillpos(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000529{
Brandon Low413da2a2006-02-07 20:38:55 +0000530 if (audiobuffer_fillpos) {
531 /* Never use the last buffer descriptor */
532 while (pcmbuf_write == pcmbuf_write_end) {
Brandon Low86f1e2e2006-03-24 13:43:15 +0000533 /* If this happens, something is being stupid */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000534 if (!pcm_is_playing()) {
Brandon Low413da2a2006-02-07 20:38:55 +0000535 logf("pcmbuf_flush_fillpos error");
Miika Pekkarinen6d6ca6b2006-01-22 10:25:07 +0000536 pcmbuf_play_start();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000537 }
Brandon Low86f1e2e2006-03-24 13:43:15 +0000538 /* Let approximately one chunk of data playback */
Miika Pekkarinena85044b2006-09-16 16:18:11 +0000539 sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000540 }
Brandon Low413da2a2006-02-07 20:38:55 +0000541 pcmbuf_add_chunk();
Brandon Low37faaab2006-04-20 02:30:59 +0000542 return true;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000543 }
Brandon Low37faaab2006-04-20 02:30:59 +0000544 return false;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000545}
546
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000547/**
548 * Completely process the crossfade fade out effect with current pcm buffer.
549 */
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000550static void crossfade_process_buffer(size_t fade_in_delay,
551 size_t fade_out_delay, size_t fade_out_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000552{
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +0000553 if (!crossfade_mixmode)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000554 {
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000555 /* Fade out the specified amount of the already processed audio */
556 size_t total_fade_out = fade_out_rem;
Brandon Low6c0908b2006-04-23 22:54:34 +0000557 size_t fade_out_sample;
558 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000559
Brandon Low6c0908b2006-04-23 22:54:34 +0000560 /* Find the right chunk to start fading out */
Brandon Low9ca16a62006-04-24 03:43:43 +0000561 fade_out_delay += crossfade_sample * 2;
Steve Bavind5dcb492006-09-27 15:23:10 +0000562 while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
Brandon Low6c0908b2006-04-23 22:54:34 +0000563 {
564 fade_out_delay -= fade_out_chunk->size;
565 fade_out_chunk = fade_out_chunk->link;
566 }
567 /* The start sample within the chunk */
568 fade_out_sample = fade_out_delay / 2;
Steve Bavinb24ed652008-04-03 14:24:37 +0000569
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000570 while (fade_out_rem > 0)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000571 {
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000572 /* Each 1/10 second of audio will have the same fade applied */
Brandon Low19247692006-04-25 10:39:56 +0000573 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
Brandon Low6c0908b2006-04-23 22:54:34 +0000574 int factor = (fade_out_rem << 8) / total_fade_out;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000575
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000576 fade_out_rem -= block_rem;
577
578 /* Fade this block */
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000579 while (block_rem > 0 && fade_out_chunk != NULL)
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000580 {
581 /* Fade one sample */
Michael Sevakis53981252007-11-16 14:26:05 +0000582 int16_t *buf = (int16_t *)fade_out_chunk->addr;
583 int32_t sample = buf[fade_out_sample];
Brandon Low6c0908b2006-04-23 22:54:34 +0000584 buf[fade_out_sample++] = (sample * factor) >> 8;
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000585
Brandon Low19247692006-04-25 10:39:56 +0000586 block_rem -= 2;
Brandon Low6c0908b2006-04-23 22:54:34 +0000587 /* Move to the next chunk as needed */
588 if (fade_out_sample * 2 >= fade_out_chunk->size)
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000589 {
Brandon Low6c0908b2006-04-23 22:54:34 +0000590 fade_out_chunk = fade_out_chunk->link;
591 fade_out_sample = 0;
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000592 }
593 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000594 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000595 }
596
Brandon Low6c0908b2006-04-23 22:54:34 +0000597 /* Find the right chunk and sample to start fading in */
Brandon Low9ca16a62006-04-24 03:43:43 +0000598 fade_in_delay += crossfade_sample * 2;
Steve Bavind5dcb492006-09-27 15:23:10 +0000599 while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
Brandon Low6c0908b2006-04-23 22:54:34 +0000600 {
601 fade_in_delay -= crossfade_chunk->size;
602 crossfade_chunk = crossfade_chunk->link;
603 }
604 crossfade_sample = fade_in_delay / 2;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000605 logf("process done!");
606}
607
Brandon Low6c0908b2006-04-23 22:54:34 +0000608/* Initializes crossfader, calculates all necessary parameters and
609 * performs fade-out with the pcm buffer. */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000610static void crossfade_start(void)
611{
Brandon Low6c0908b2006-04-23 22:54:34 +0000612 size_t crossfade_rem;
Brandon Low9aa49a42006-04-25 02:28:21 +0000613 size_t crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000614 size_t fade_out_rem;
615 size_t fade_out_delay;
616 size_t fade_in_delay;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000617
Brandon Low37faaab2006-04-20 02:30:59 +0000618 crossfade_init = false;
Brandon Low413da2a2006-02-07 20:38:55 +0000619 /* Reject crossfade if less than .5s of data */
620 if (LOW_DATA(2)) {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000621 logf("crossfade rejected");
622 pcmbuf_play_stop();
623 return ;
624 }
625
626 logf("crossfade_start");
Brandon Low413da2a2006-02-07 20:38:55 +0000627 pcmbuf_flush_fillpos();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000628 crossfade_active = true;
Brandon Low6c0908b2006-04-23 22:54:34 +0000629
Brandon Low413da2a2006-02-07 20:38:55 +0000630 /* Initialize the crossfade buffer size to all of the buffered data that
631 * has not yet been sent to the DMA */
Brandon Low6c0908b2006-04-23 22:54:34 +0000632 crossfade_rem = pcmbuf_unplayed_bytes;
633 crossfade_chunk = pcmbuf_read->link;
Brandon Low8ef18272006-04-24 12:41:30 +0000634 crossfade_sample = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000635
Brandon Low6c0908b2006-04-23 22:54:34 +0000636 /* Get fade out delay from settings. */
637 fade_out_delay =
638 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000639
Brandon Low6c0908b2006-04-23 22:54:34 +0000640 /* Get fade out duration from settings. */
641 fade_out_rem =
642 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000643
Brandon Low9aa49a42006-04-25 02:28:21 +0000644 crossfade_need = fade_out_delay + fade_out_rem;
Brandon Low6c0908b2006-04-23 22:54:34 +0000645 /* We want only to modify the last part of the buffer. */
Brandon Low9aa49a42006-04-25 02:28:21 +0000646 if (crossfade_rem > crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000647 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000648 size_t crossfade_extra = crossfade_rem - crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000649 while (crossfade_extra > crossfade_chunk->size)
650 {
651 crossfade_extra -= crossfade_chunk->size;
652 crossfade_chunk = crossfade_chunk->link;
653 }
654 crossfade_sample = crossfade_extra / 2;
655 }
656 /* Truncate fade out duration if necessary. */
Brandon Low9aa49a42006-04-25 02:28:21 +0000657 else if (crossfade_rem < crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000658 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000659 size_t crossfade_short = crossfade_need - crossfade_rem;
660 if (fade_out_rem >= crossfade_short)
Brandon Low6c0908b2006-04-23 22:54:34 +0000661 fade_out_rem -= crossfade_short;
662 else
663 {
664 fade_out_delay -= crossfade_short - fade_out_rem;
665 fade_out_rem = 0;
666 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000667 }
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000668
Brandon Low6c0908b2006-04-23 22:54:34 +0000669 /* Get also fade in duration and delays from settings. */
670 crossfade_fade_in_total =
671 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
672 crossfade_fade_in_rem = crossfade_fade_in_total;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000673
Brandon Low6c0908b2006-04-23 22:54:34 +0000674 fade_in_delay =
675 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
676
677 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000678}
679
Brandon Low9602dd72006-04-25 12:34:28 +0000680/* Returns the number of bytes _NOT_ mixed */
681static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000682{
Michael Sevakis53981252007-11-16 14:26:05 +0000683 const int16_t *input_buf = (const int16_t *)buf;
684 int16_t *output_buf = (int16_t *)(crossfade_chunk->addr);
685 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
Brandon Low9aa49a42006-04-25 02:28:21 +0000686 output_buf = &output_buf[crossfade_sample];
Andree Buschmann21870cf2008-03-30 10:11:31 +0000687 int32_t sample;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000688
Brandon Low9602dd72006-04-25 12:34:28 +0000689 while (fade_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000690 {
Andree Buschmann21870cf2008-03-30 10:11:31 +0000691 /* fade left and right channel at once to keep buffer alignment */
692 sample = *input_buf++;
Brandon Low9aa49a42006-04-25 02:28:21 +0000693 sample = ((sample * factor) >> 8) + *output_buf;
Michael Sevakis53981252007-11-16 14:26:05 +0000694 *output_buf++ = clip_sample_16(sample);
Steve Bavinb24ed652008-04-03 14:24:37 +0000695
Andree Buschmann21870cf2008-03-30 10:11:31 +0000696 sample = *input_buf++;
697 sample = ((sample * factor) >> 8) + *output_buf;
698 *output_buf++ = clip_sample_16(sample);
Steve Bavinb24ed652008-04-03 14:24:37 +0000699
Andree Buschmann21870cf2008-03-30 10:11:31 +0000700 fade_rem -= 4; /* 2 samples, each 16 bit -> 4 bytes */
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000701
Brandon Low9aa49a42006-04-25 02:28:21 +0000702 if (output_buf >= chunk_end)
Brandon Low413da2a2006-02-07 20:38:55 +0000703 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000704 crossfade_chunk = crossfade_chunk->link;
705 if (!crossfade_chunk)
Brandon Low9602dd72006-04-25 12:34:28 +0000706 return fade_rem;
Michael Sevakis53981252007-11-16 14:26:05 +0000707 output_buf = (int16_t *)crossfade_chunk->addr;
708 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
Brandon Low413da2a2006-02-07 20:38:55 +0000709 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000710 }
Michael Sevakis53981252007-11-16 14:26:05 +0000711 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
Brandon Low9602dd72006-04-25 12:34:28 +0000712 return 0;
713}
714
715/* Returns the number of bytes _NOT_ mixed */
716static size_t crossfade_mix(const char *buf, size_t length)
717{
Michael Sevakis53981252007-11-16 14:26:05 +0000718 const int16_t *input_buf = (const int16_t *)buf;
719 int16_t *output_buf = (int16_t *)crossfade_chunk->addr;
720 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
Brandon Low9602dd72006-04-25 12:34:28 +0000721 output_buf = &output_buf[crossfade_sample];
Andree Buschmann21870cf2008-03-30 10:11:31 +0000722 int32_t sample;
Brandon Low9602dd72006-04-25 12:34:28 +0000723
724 while (length)
725 {
Andree Buschmann21870cf2008-03-30 10:11:31 +0000726 /* fade left and right channel at once to keep buffer alignment */
727 sample = *input_buf++ + *output_buf;
Michael Sevakis53981252007-11-16 14:26:05 +0000728 *output_buf++ = clip_sample_16(sample);
Steve Bavinb24ed652008-04-03 14:24:37 +0000729
Andree Buschmann21870cf2008-03-30 10:11:31 +0000730 sample = *input_buf++ + *output_buf;
731 *output_buf++ = clip_sample_16(sample);
Steve Bavinb24ed652008-04-03 14:24:37 +0000732
Andree Buschmann21870cf2008-03-30 10:11:31 +0000733 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
Brandon Low9602dd72006-04-25 12:34:28 +0000734
735 if (output_buf >= chunk_end)
736 {
737 crossfade_chunk = crossfade_chunk->link;
738 if (!crossfade_chunk)
739 return length;
Michael Sevakis99617d72007-11-18 17:12:19 +0000740
Michael Sevakis18330a02007-11-16 14:34:15 +0000741 output_buf = (int16_t *)crossfade_chunk->addr;
Michael Sevakis53981252007-11-16 14:26:05 +0000742 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
Brandon Low9602dd72006-04-25 12:34:28 +0000743 }
744 }
Michael Sevakis53981252007-11-16 14:26:05 +0000745 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
Brandon Low9602dd72006-04-25 12:34:28 +0000746 return 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000747}
748
Brandon Low413da2a2006-02-07 20:38:55 +0000749static void pcmbuf_flush_buffer(const char *buf, size_t length)
750{
751 size_t copy_n;
Brandon Low413da2a2006-02-07 20:38:55 +0000752 while (length > 0) {
753 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
754 if (NEED_FLUSH(audiobuffer_index))
755 {
756 pcmbuf_flush_fillpos();
757 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
758 }
759 copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
760 memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
761 buf += copy_n;
762 audiobuffer_fillpos += copy_n;
763 length -= copy_n;
764 }
765}
766
Brandon Low9aa49a42006-04-25 02:28:21 +0000767static void flush_crossfade(char *buf, size_t length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000768{
Brandon Low9602dd72006-04-25 12:34:28 +0000769 if (length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000770 {
Brandon Low9602dd72006-04-25 12:34:28 +0000771 if (crossfade_fade_in_rem)
Brandon Low9aa49a42006-04-25 02:28:21 +0000772 {
773 size_t samples;
Michael Sevakis53981252007-11-16 14:26:05 +0000774 int16_t *input_buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000775
776 /* Fade factor for this packet */
777 int factor =
778 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
779 crossfade_fade_in_total;
780 /* Bytes to fade */
781 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
782
783 /* We _will_ fade this many bytes */
784 crossfade_fade_in_rem -= fade_rem;
785
786 if (crossfade_chunk)
787 {
788 /* Mix the data */
789 size_t fade_total = fade_rem;
790 fade_rem = crossfade_fade_mix(factor, buf, fade_rem);
791 length -= fade_total - fade_rem;
792 buf += fade_total - fade_rem;
Brandon Low08cdc432006-04-25 16:12:43 +0000793 if (!length)
794 return;
795 if (!fade_rem)
796 goto fade_done;
Brandon Low9602dd72006-04-25 12:34:28 +0000797 }
798
Brandon Low9aa49a42006-04-25 02:28:21 +0000799 samples = fade_rem / 2;
Michael Sevakis53981252007-11-16 14:26:05 +0000800 input_buf = (int16_t *)buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000801 /* Fade remaining samples in place */
Brandon Low9aa49a42006-04-25 02:28:21 +0000802 while (samples)
803 {
Michael Sevakis53981252007-11-16 14:26:05 +0000804 int32_t sample = *input_buf;
Brandon Low9aa49a42006-04-25 02:28:21 +0000805 *input_buf++ = (sample * factor) >> 8;
806 samples--;
807 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000808 }
Brandon Low413da2a2006-02-07 20:38:55 +0000809
Brandon Low08cdc432006-04-25 16:12:43 +0000810fade_done:
Brandon Low9602dd72006-04-25 12:34:28 +0000811 if (crossfade_chunk)
Brandon Low6c0908b2006-04-23 22:54:34 +0000812 {
Brandon Low9602dd72006-04-25 12:34:28 +0000813 /* Mix the data */
814 size_t mix_total = length;
815 length = crossfade_mix(buf, length);
816 buf += mix_total - length;
Brandon Low08cdc432006-04-25 16:12:43 +0000817 if (!length)
818 return;
Brandon Low6c0908b2006-04-23 22:54:34 +0000819 }
Brandon Low9602dd72006-04-25 12:34:28 +0000820
Brandon Low08cdc432006-04-25 16:12:43 +0000821 /* Flush samples to the buffer */
822 while (!prepare_insert(length))
823 sleep(1);
824 pcmbuf_flush_buffer(buf, length);
Brandon Low9aa49a42006-04-25 02:28:21 +0000825 }
826
Brandon Low413da2a2006-02-07 20:38:55 +0000827}
828
829static bool prepare_insert(size_t length)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000830{
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000831 if (low_latency_mode)
832 {
833 /* 1/4s latency. */
Michael Sevakis27cf6772008-03-25 02:34:12 +0000834 if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 2
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000835 && pcm_is_playing())
836 return false;
837 }
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000838
Brandon Low3bbd93b2006-02-13 17:08:53 +0000839 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
Brandon Low9602dd72006-04-25 12:34:28 +0000840 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000841 return false;
Miika Pekkarinend83b6592005-07-19 19:57:23 +0000842
Brandon Low413da2a2006-02-07 20:38:55 +0000843 if (!pcm_is_playing())
844 {
Miika Pekkarinen70587522006-10-15 11:57:52 +0000845 trigger_cpu_boost();
Steve Bavinb24ed652008-04-03 14:24:37 +0000846
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000847 /* Pre-buffer 1s. */
Dave Chapman0a7ded32006-08-13 09:19:24 +0000848#if MEM <= 1
Tomasz Malesinski28910e72006-08-12 23:01:52 +0000849 if (!LOW_DATA(1))
850#else
Brandon Low413da2a2006-02-07 20:38:55 +0000851 if (!LOW_DATA(4))
Tomasz Malesinski28910e72006-08-12 23:01:52 +0000852#endif
Brandon Low413da2a2006-02-07 20:38:55 +0000853 {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000854 logf("pcm starting");
Michael Sevakis6077e5b2007-10-06 22:27:27 +0000855 if (!(audio_status() & AUDIO_STATUS_PAUSE))
856 pcmbuf_play_start();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000857 }
Steve Bavinb24ed652008-04-03 14:24:37 +0000858 }
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000859 else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
Brandon Low3a37fae2006-02-13 16:55:25 +0000860 pcmbuf_under_watermark();
861
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000862 return true;
863}
864
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000865void* pcmbuf_request_buffer(int *count)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000866{
Brandon Low37faaab2006-04-20 02:30:59 +0000867 if (crossfade_init)
868 crossfade_start();
869
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000870 if (crossfade_active) {
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000871 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000872 return fadebuf;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000873 }
Brandon Low413da2a2006-02-07 20:38:55 +0000874 else
875 {
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000876 if(prepare_insert(*count << 2))
Brandon Low413da2a2006-02-07 20:38:55 +0000877 {
878 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
Brandon Low3bbd93b2006-02-13 17:08:53 +0000879 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
880 {
881 /* Usual case, there's space here */
882 return &audiobuffer[audiobuffer_index];
Brandon Low413da2a2006-02-07 20:38:55 +0000883 }
884 else
885 {
Brandon Low3bbd93b2006-02-13 17:08:53 +0000886 /* Flush and wrap the buffer */
887 pcmbuf_flush_fillpos();
888 audiobuffer_pos = 0;
889 return &audiobuffer[0];
Brandon Low413da2a2006-02-07 20:38:55 +0000890 }
891 }
892 else
893 {
Brandon Low413da2a2006-02-07 20:38:55 +0000894 return NULL;
895 }
896 }
897}
898
Michael Sevakis99617d72007-11-18 17:12:19 +0000899void * pcmbuf_request_voice_buffer(int *count)
Brandon Low413da2a2006-02-07 20:38:55 +0000900{
Michael Sevakis99617d72007-11-18 17:12:19 +0000901 /* A get-it-to-work-for-now hack (audio status could change by
902 completion) */
903 if (audio_status() & AUDIO_STATUS_PLAY)
Brandon Low413da2a2006-02-07 20:38:55 +0000904 {
Michael Sevakis99617d72007-11-18 17:12:19 +0000905 if (pcmbuf_read == NULL)
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000906 {
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000907 return NULL;
908 }
Michael Sevakis99617d72007-11-18 17:12:19 +0000909 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
910 (pcmbuf_mix_chunk || pcmbuf_read->link))
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000911 {
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000912 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000913 return voicebuf;
914 }
915 else
916 {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000917 return NULL;
918 }
Brandon Low413da2a2006-02-07 20:38:55 +0000919 }
920 else
Michael Sevakis99617d72007-11-18 17:12:19 +0000921 {
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000922 return pcmbuf_request_buffer(count);
Michael Sevakis99617d72007-11-18 17:12:19 +0000923 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000924}
925
926bool pcmbuf_is_crossfade_active(void)
927{
928 return crossfade_active || crossfade_init;
929}
930
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000931void pcmbuf_write_complete(int count)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000932{
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000933 size_t length = (size_t)(unsigned int)count << 2;
934
Brandon Low37faaab2006-04-20 02:30:59 +0000935 if (crossfade_active)
Brandon Low08cdc432006-04-25 16:12:43 +0000936 {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000937 flush_crossfade(fadebuf, length);
Brandon Low08cdc432006-04-25 16:12:43 +0000938 if (!(crossfade_fade_in_rem || crossfade_chunk))
939 crossfade_active = false;
940 }
Brandon Low413da2a2006-02-07 20:38:55 +0000941 else
942 {
Brandon Low413da2a2006-02-07 20:38:55 +0000943 audiobuffer_fillpos += length;
944
945 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000946 pcmbuf_flush_fillpos();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000947 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000948}
949
Jens Arnold2597a132006-12-25 14:01:47 +0000950#if 0
Steve Bavin135cc752008-03-28 12:51:33 +0000951bool pcmbuf_insert_buffer(char *buf, int count)
Brandon Low413da2a2006-02-07 20:38:55 +0000952{
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000953 size_t length = (size_t)(unsigned int)count << 2;
954
Brandon Low08cdc432006-04-25 16:12:43 +0000955 if (crossfade_active)
956 {
Brandon Low37faaab2006-04-20 02:30:59 +0000957 flush_crossfade(buf, length);
Brandon Low08cdc432006-04-25 16:12:43 +0000958 if (!(crossfade_fade_in_rem || crossfade_chunk))
959 crossfade_active = false;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000960 }
Brandon Low413da2a2006-02-07 20:38:55 +0000961 else
962 {
Brandon Low08cdc432006-04-25 16:12:43 +0000963 if (!prepare_insert(length))
964 return false;
Brandon Low413da2a2006-02-07 20:38:55 +0000965 pcmbuf_flush_buffer(buf, length);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000966 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000967 return true;
968}
Jens Arnold2597a132006-12-25 14:01:47 +0000969#endif
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000970
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000971/* Generates a constant square wave sound with a given frequency
972 in Hertz for a duration in milliseconds. */
Brandon Low9535a9a2006-02-22 01:56:44 +0000973void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000974{
Steve Bavinb24ed652008-04-03 14:24:37 +0000975 unsigned int count = 0;
976 unsigned int i;
Brandon Low413da2a2006-02-07 20:38:55 +0000977 unsigned int interval = NATIVE_FREQUENCY / frequency;
Steve Bavinb24ed652008-04-03 14:24:37 +0000978 unsigned int samples = NATIVE_FREQUENCY / 1000 * duration;
Michael Sevakis53981252007-11-16 14:26:05 +0000979 int32_t sample;
Steve Bavinb24ed652008-04-03 14:24:37 +0000980 int16_t *bufstart;
981 int16_t *bufptr;
Michael Sevakis53981252007-11-16 14:26:05 +0000982 int16_t *pcmbuf_end = (int16_t *)fadebuf;
Steve Bavinb24ed652008-04-03 14:24:37 +0000983 bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000984
Steve Bavinb24ed652008-04-03 14:24:37 +0000985 /* Find the insertion point and set bufstart to the start of it */
986 if (mix)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000987 {
Steve Bavinb24ed652008-04-03 14:24:37 +0000988 /* Get the next chunk */
989 char *pcmbuf_mix_buf = pcmbuf_read->link->addr;
990 /* Give 1/8s clearance. */
991 bufstart = (int16_t *)&pcmbuf_mix_buf[NATIVE_FREQUENCY * 4 / 8];
Brandon Low9535a9a2006-02-22 01:56:44 +0000992 }
Brandon Low0fcd4112006-04-05 13:00:31 +0000993 else
994 {
Steve Bavinb24ed652008-04-03 14:24:37 +0000995 /* Use audiobuffer */
996 bufstart = (int16_t *)audiobuffer;
997 }
Brandon Low0fcd4112006-04-05 13:00:31 +0000998
Steve Bavinb24ed652008-04-03 14:24:37 +0000999 /* Mix square wave into buffer */
1000 bufptr = bufstart;
1001 for (i = 0; i < samples; ++i)
1002 {
1003 sample = mix ? *bufptr : 0;
1004 *bufptr++ = clip_sample_16(sample + amplitude);
1005 if (bufptr > pcmbuf_end)
1006 bufptr = (int16_t *)audiobuffer;
1007 sample = mix ? *bufptr : 0;
1008 *bufptr++ = clip_sample_16(sample + amplitude);
1009 if (bufptr > pcmbuf_end)
1010 bufptr = (int16_t *)audiobuffer;
1011
1012 /* Toggle square wave edge */
1013 if (++count >= interval)
1014 {
1015 count = 0;
1016 amplitude = -amplitude;
Brandon Low0fcd4112006-04-05 13:00:31 +00001017 }
Steve Bavinb24ed652008-04-03 14:24:37 +00001018 }
1019
1020 /* Kick off playback if required */
1021 if (!pcm_is_playing())
1022 {
1023 pcm_play_data(NULL, (unsigned char *)bufstart, samples * 4);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001024 }
1025}
1026
Steve Bavinb24ed652008-04-03 14:24:37 +00001027
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001028/* Returns pcm buffer usage in percents (0 to 100). */
1029int pcmbuf_usage(void)
1030{
1031 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1032}
1033
Brandon Lowf3bc1ef2006-04-22 14:40:13 +00001034int pcmbuf_mix_free(void)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001035{
Brandon Lowf3bc1ef2006-04-22 14:40:13 +00001036 if (pcmbuf_mix_chunk)
1037 {
1038 size_t my_mix_end =
Michael Sevakis53981252007-11-16 14:26:05 +00001039 (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
Brandon Lowf3bc1ef2006-04-22 14:40:13 +00001040 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
1041 if (my_write_pos < my_mix_end)
1042 my_write_pos += pcmbuf_size;
1043 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1044 }
1045 return 100;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001046}
1047
Michael Sevakis99617d72007-11-18 17:12:19 +00001048void pcmbuf_write_voice_complete(int count)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001049{
Michael Sevakis99617d72007-11-18 17:12:19 +00001050 /* A get-it-to-work-for-now hack (audio status could have changed) */
1051 if (!(audio_status() & AUDIO_STATUS_PLAY))
1052 {
1053 pcmbuf_write_complete(count);
1054 return;
1055 }
1056
Michael Sevakis53981252007-11-16 14:26:05 +00001057 int16_t *ibuf = (int16_t *)voicebuf;
1058 int16_t *obuf;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +00001059 size_t chunk_samples;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001060
Miika Pekkarinen815684a2006-09-17 08:34:42 +00001061 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
Brandon Lowf3bc1ef2006-04-22 14:40:13 +00001062 {
1063 pcmbuf_mix_chunk = pcmbuf_read->link;
1064 /* Start 1/8s into the next chunk */
1065 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
1066 }
Michael Sevakis99617d72007-11-18 17:12:19 +00001067
Brandon Lowf3bc1ef2006-04-22 14:40:13 +00001068 if (!pcmbuf_mix_chunk)
1069 return;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001070
Michael Sevakis53981252007-11-16 14:26:05 +00001071 obuf = (int16_t *)pcmbuf_mix_chunk->addr;
Michael Sevakis18330a02007-11-16 14:34:15 +00001072 chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
Zakk Roberts8bdd92b2006-03-30 05:56:19 +00001073
Michael Sevakisaba6ca02007-02-07 00:51:50 +00001074 count <<= 1;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001075
Michael Sevakis88c47482007-11-16 14:49:17 +00001076 while (count-- > 0)
1077 {
1078 int32_t sample = *ibuf++;
Michael Sevakis99617d72007-11-18 17:12:19 +00001079
Brandon Lowf3bc1ef2006-04-22 14:40:13 +00001080 if (pcmbuf_mix_sample >= chunk_samples)
1081 {
1082 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
1083 if (!pcmbuf_mix_chunk)
1084 return;
1085 pcmbuf_mix_sample = 0;
1086 obuf = pcmbuf_mix_chunk->addr;
1087 chunk_samples = pcmbuf_mix_chunk->size / 2;
1088 }
Brandon Low920516c2006-04-23 05:30:52 +00001089 sample += obuf[pcmbuf_mix_sample] >> 2;
Michael Sevakis53981252007-11-16 14:26:05 +00001090 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001091 }
1092}
1093
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001094void pcmbuf_crossfade_enable(bool on_off)
1095{
Michael Sevakise1dd10d2007-03-19 22:04:17 +00001096#if MEM > 1
1097 /* Next setting to be used, not applied now */
1098 crossfade_enabled_pending = on_off;
1099#endif
1100 (void)on_off;
1101}
Miika Pekkarinenf090dc32005-07-21 11:44:00 +00001102
Michael Sevakise1dd10d2007-03-19 22:04:17 +00001103void pcmbuf_crossfade_enable_finished(void)
1104{
1105 /* Copy the pending setting over now */
1106 crossfade_enabled = crossfade_enabled_pending;
1107 pcmbuf_set_watermark_bytes();
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001108}
1109
1110bool pcmbuf_is_crossfade_enabled(void)
1111{
Miika Pekkarinene7461b32005-11-06 16:40:20 +00001112 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
1113 return global_settings.playlist_shuffle;
1114
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001115 return crossfade_enabled;
1116}