blob: ad1dfca34d98ff70378e3593d6d9166d514cf7ef [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 ****************************************************************************/
Miika Pekkarinen20b38972005-07-13 12:48:22 +000021#include <stdio.h>
22#include "config.h"
Michael Sevakis6c399b82009-02-19 20:40:03 +000023#include "system.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000024#include "debug.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000025#include <kernel.h>
26#include "pcmbuf.h"
Michael Sevakis6077e5b2007-10-06 22:27:27 +000027#include "pcm.h"
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +000028#include "playback.h"
Jeffrey Goodefaa47bf2009-10-22 00:59:42 +000029
30/* Define LOGF_ENABLE to enable logf output in this file */
31/*#define LOGF_ENABLE*/
Miika Pekkarinen20b38972005-07-13 12:48:22 +000032#include "logf.h"
33#ifndef SIMULATOR
34#include "cpu.h"
35#endif
Miika Pekkarinen20b38972005-07-13 12:48:22 +000036#include <string.h>
37#include "buffer.h"
Miika Pekkarinenf090dc32005-07-21 11:44:00 +000038#include "settings.h"
39#include "audio.h"
Michael Sevakis99617d72007-11-18 17:12:19 +000040#include "voice_thread.h"
Miika Pekkarinen159c52d2005-08-20 11:13:19 +000041#include "dsp.h"
Miika Pekkarinena85044b2006-09-16 16:18:11 +000042#include "thread.h"
Miika Pekkarinen20b38972005-07-13 12:48:22 +000043
Michael Sevakis53981252007-11-16 14:26:05 +000044/* Clip sample to signed 16 bit range */
45static inline int32_t clip_sample_16(int32_t sample)
46{
47 if ((int16_t)sample != sample)
48 sample = 0x7fff ^ (sample >> 31);
49 return sample;
50}
51
Jeffrey Goode04b01e12009-11-05 21:59:36 +000052#define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
53 on the pcm buffer */
54#define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
55 chunks on the pcm buffer (or we run out
56 of buffer descriptors, which is
57 non-fatal) */
58#define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
59 this to the DMA */
60#define PCMBUF_MIX_CHUNK 8192 /* This is the maximum size of one packet
61 for mixing (crossfade or voice) */
62
Björn Stenberg6427d122009-01-10 21:10:56 +000063#if MEMORYSIZE > 2
Miika Pekkarinena85044b2006-09-16 16:18:11 +000064/* Keep watermark high for iPods at least (2s) */
Brandon Low1f3360f2006-10-13 20:48:23 +000065#define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
Björn Stenberg6427d122009-01-10 21:10:56 +000066#else
67#define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
68#endif
Miika Pekkarinen20b38972005-07-13 12:48:22 +000069
Miika Pekkarinen20b38972005-07-13 12:48:22 +000070/* Structure we can use to queue pcm chunks in memory to be played
71 * by the driver code. */
72struct pcmbufdesc
73{
74 void *addr;
Brandon Low413da2a2006-02-07 20:38:55 +000075 size_t size;
76 struct pcmbufdesc* link;
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +000077 /* true if last chunk in the track */
78 bool end_of_track;
Brandon Low413da2a2006-02-07 20:38:55 +000079};
Miika Pekkarinen20b38972005-07-13 12:48:22 +000080
Michael Sevakise1dd10d2007-03-19 22:04:17 +000081#define PCMBUF_DESCS(bufsize) \
82 ((bufsize) / PCMBUF_MINAVG_CHUNK)
83#define PCMBUF_DESCS_SIZE(bufsize) \
84 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
Michael Sevakis0f5cb942006-11-06 18:07:30 +000085
Brandon Low6c0908b2006-04-23 22:54:34 +000086/* Size of the PCM buffer. */
87static size_t pcmbuf_size IDATA_ATTR = 0;
Michael Sevakis0f5cb942006-11-06 18:07:30 +000088static char *pcmbuf_bufend IDATA_ATTR;
Brandon Low6c0908b2006-04-23 22:54:34 +000089static char *audiobuffer IDATA_ATTR;
90/* Current audio buffer write index. */
91static size_t audiobuffer_pos IDATA_ATTR;
92/* Amount audiobuffer_pos will be increased.*/
93static size_t audiobuffer_fillpos IDATA_ATTR;
94static char *fadebuf IDATA_ATTR;
95static char *voicebuf IDATA_ATTR;
96
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +000097static bool end_of_track IDATA_ATTR;
98bool track_transition IDATA_ATTR;
Brandon Low6c0908b2006-04-23 22:54:34 +000099
100/* Crossfade related state */
101static bool crossfade_enabled;
Michael Sevakise1dd10d2007-03-19 22:04:17 +0000102static bool crossfade_enabled_pending;
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +0000103static bool crossfade_mixmode;
Brandon Low6c0908b2006-04-23 22:54:34 +0000104static bool crossfade_active IDATA_ATTR;
105static bool crossfade_init IDATA_ATTR;
106
107/* Track the current location for processing crossfade */
108static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
Michael Giacomelli64699262009-08-11 02:05:38 +0000109#ifdef HAVE_CROSSFADE
Brandon Low6c0908b2006-04-23 22:54:34 +0000110static size_t crossfade_sample IDATA_ATTR;
111
112/* Counters for fading in new data */
113static size_t crossfade_fade_in_total IDATA_ATTR;
114static size_t crossfade_fade_in_rem IDATA_ATTR;
Michael Giacomelli64699262009-08-11 02:05:38 +0000115#endif
Brandon Low6c0908b2006-04-23 22:54:34 +0000116
Brandon Low413da2a2006-02-07 20:38:55 +0000117static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
118static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
119static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
120static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
121static size_t last_chunksize IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +0000122
Brandon Low413da2a2006-02-07 20:38:55 +0000123static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
Brandon Low413da2a2006-02-07 20:38:55 +0000124static size_t pcmbuf_watermark IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +0000125
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000126static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
127static size_t pcmbuf_mix_sample IDATA_ATTR;
Brandon Low2da61ff2006-04-24 01:32:28 +0000128
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000129static bool low_latency_mode = false;
Brandon Low6c0908b2006-04-23 22:54:34 +0000130static bool pcmbuf_flush;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000131
Michael Sevakis938593b2007-03-06 21:00:45 +0000132#ifdef HAVE_PRIORITY_SCHEDULING
Michael Sevakis27cf6772008-03-25 02:34:12 +0000133static int codec_thread_priority = PRIORITY_PLAYBACK;
Michael Sevakis938593b2007-03-06 21:00:45 +0000134#endif
Michael Sevakisb425de72007-03-06 20:32:13 +0000135
Michael Sevakisf2f39472008-12-10 09:11:43 +0000136extern unsigned int codec_thread_id;
Miika Pekkarinena85044b2006-09-16 16:18:11 +0000137
Brandon Low413da2a2006-02-07 20:38:55 +0000138/* Helpful macros for use in conditionals this assumes some of the above
139 * static variable names */
140#define NEED_FLUSH(position) \
141 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
142#define LOW_DATA(quarter_secs) \
143 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
144
Brandon Low37faaab2006-04-20 02:30:59 +0000145static bool pcmbuf_flush_fillpos(void);
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000146static void pcmbuf_finish_track_change(void);
147static void crossfade_start(void);
148static void flush_crossfade(char *buf, size_t length);
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000149static bool pcmbuf_crossfade_init(bool manual_skip);
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000150static void pcmbuf_finish_crossfade_enable(void);
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000151static bool pcmbuf_is_crossfade_enabled(void);
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000152
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000153
Jeffrey Goode013fe352009-11-05 17:32:32 +0000154/**************************************/
155
156/* define this to show detailed pcmbufdesc usage information on the sim console */
157/*#define DESC_DEBUG*/
158
159#ifndef SIMULATOR
160#undef DESC_DEBUG
161#endif
162#ifdef DESC_DEBUG
163static struct pcmbufdesc *first_desc;
164static bool show_desc_in_use = false;
165#define DISPLAY_DESC(caller) while(!show_desc(caller))
166#define DESC_IDX(desc) (desc ? desc - first_desc : -1)
167#define DESCL_IDX(desc) (desc && desc->link ? desc->link - first_desc : -1)
168#define SHOW_1ST(desc) if(DESC_IDX (desc)==-1) DEBUGF(" -- "); \
169 else DEBUGF(" %02d ", DESC_IDX(desc))
170#define SHOW_2ND(desc) if(DESCL_IDX(desc)==-1) DEBUGF("l -- "); \
171 else DEBUGF("l %02d ", DESCL_IDX(desc))
172#define DESC_SHOW(tag, desc) DEBUGF(tag);SHOW_1ST(desc); \
173 DEBUGF(tag);SHOW_2ND(desc)
174
175static bool show_desc(char *caller)
176{
177 if (show_desc_in_use) return false;
178 show_desc_in_use = true;
179 DEBUGF("%-14s\t", caller);
180 DESC_SHOW("r", pcmbuf_read);
181 DESC_SHOW("re", pcmbuf_read_end);
182 DEBUGF(" ");
183 DESC_SHOW("w", pcmbuf_write);
184 DESC_SHOW("we", pcmbuf_write_end);
185 DEBUGF("\n");
186 show_desc_in_use = false;
187 return true;
188}
189#else
190#define DISPLAY_DESC(caller) do{}while(0)
191#endif
192
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000193
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000194/* Commit PCM data */
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000195
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000196#ifdef HAVE_PRIORITY_SCHEDULING
197static void boost_codec_thread(bool boost)
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000198{
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000199 /* Keep voice and codec threads at the same priority or else voice
200 * will starve if the codec thread's priority is boosted. */
201 if (boost)
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000202 {
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000203 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
204 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
205
206 if (priority != codec_thread_priority)
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000207 {
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000208 codec_thread_priority = priority;
209 thread_set_priority(codec_thread_id, priority);
210 voice_thread_set_priority(priority);
211 }
212 }
213 else if (codec_thread_priority != PRIORITY_PLAYBACK)
214 {
215 thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
216 voice_thread_set_priority(PRIORITY_PLAYBACK);
217 codec_thread_priority = PRIORITY_PLAYBACK;
218 }
219}
220#endif /* HAVE_PRIORITY_SCHEDULING */
221
222static void pcmbuf_under_watermark(bool under)
223{
224 /* Only codec thread initiates boost - voice boosts the cpu when playing
225 a clip */
226#ifndef SIMULATOR
227 if (thread_get_current() == codec_thread_id)
228#endif /* SIMULATOR */
229 {
230 if (under)
231 {
232 /* Fill audio buffer by boosting cpu */
233 trigger_cpu_boost();
234#ifdef HAVE_PRIORITY_SCHEDULING
235 /* If buffer is critically low, override UI priority, else
236 set back to the original priority. */
237 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
238#endif
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000239 }
240 else
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000241 {
242#ifdef HAVE_PRIORITY_SCHEDULING
243 boost_codec_thread(false);
244#endif
245 }
246 }
247
248 /* Disable crossfade if < .5s of audio */
249 if (LOW_DATA(2))
250 {
251 crossfade_active = false;
252 }
253}
254
255static bool prepare_insert(size_t length)
256{
257 if (low_latency_mode)
258 {
259 /* 1/4s latency. */
260 if (!LOW_DATA(1) && pcm_is_playing())
261 return false;
262 }
263
264 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
265 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
266 return false;
267
268 if (!pcm_is_playing())
269 {
270 trigger_cpu_boost();
271
272 /* Pre-buffer up to watermark */
273#if MEMORYSIZE > 2
274 if (!LOW_DATA(4))
275#else
276 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
277#endif
278 {
279 logf("pcm starting");
280 if (!(audio_status() & AUDIO_STATUS_PAUSE))
281 pcmbuf_play_start();
282 }
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000283 }
284 else
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000285 pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
286
287 return true;
Jeffrey Goode04b01e12009-11-05 21:59:36 +0000288}
289
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000290void *pcmbuf_request_buffer(int *count)
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000291{
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000292#ifdef HAVE_CROSSFADE
293 if (crossfade_init)
294 crossfade_start();
295#endif
296
297 if (crossfade_active) {
298 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
299 return fadebuf;
300 }
301 else
302 {
303 if(prepare_insert(*count << 2))
304 {
305 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
306 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
307 {
308 /* Usual case, there's space here */
309 return &audiobuffer[audiobuffer_index];
310 }
311 else
312 {
313 /* Flush and wrap the buffer */
314 pcmbuf_flush_fillpos();
315 audiobuffer_pos = 0;
316 return &audiobuffer[0];
317 }
318 }
319 else
320 {
321 return NULL;
322 }
323 }
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000324}
325
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000326void pcmbuf_write_complete(int count)
327{
328 size_t length = (size_t)(unsigned int)count << 2;
329#ifdef HAVE_CROSSFADE
330 if (crossfade_active)
331 {
332 flush_crossfade(fadebuf, length);
333 if (!(crossfade_fade_in_rem || crossfade_chunk))
334 crossfade_active = false;
335 }
336 else
337#endif
338 {
339 audiobuffer_fillpos += length;
340
341 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
342 pcmbuf_flush_fillpos();
343 }
344}
345
346/* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
347 * in a separate function for the moment */
348static inline void pcmbuf_add_chunk(void)
349{
350 register size_t size = audiobuffer_fillpos;
351 /* Grab the next description to write, and change the write pointer */
352 register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
353 pcmbuf_write = pcmbuf_current->link;
354 /* Fill in the values in the new buffer chunk */
355 pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
356 pcmbuf_current->size = size;
357 pcmbuf_current->end_of_track = end_of_track;
358 pcmbuf_current->link = NULL;
359 end_of_track = false; /* This is single use only */
360 if (pcmbuf_read != NULL) {
361 if (pcmbuf_flush)
362 {
363 pcmbuf_write_end->link = pcmbuf_read->link;
364 pcmbuf_read->link = pcmbuf_current;
365 while (pcmbuf_write_end->link)
366 {
367 pcmbuf_write_end = pcmbuf_write_end->link;
368 pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
369 }
370 pcmbuf_flush = false;
371 }
372 /* If there is already a read buffer setup, add to it */
373 else
374 pcmbuf_read_end->link = pcmbuf_current;
375 } else {
376 /* Otherwise create the buffer */
377 pcmbuf_read = pcmbuf_current;
378 }
379 /* This is now the last buffer to read */
380 pcmbuf_read_end = pcmbuf_current;
381
382 /* Update bytes counters */
383 pcmbuf_unplayed_bytes += size;
384
385 audiobuffer_pos += size;
386 if (audiobuffer_pos >= pcmbuf_size)
387 audiobuffer_pos -= pcmbuf_size;
388
389 audiobuffer_fillpos = 0;
390 DISPLAY_DESC("add_chunk");
391}
392
393/**
394 * Commit samples waiting to the pcm buffer.
395 */
396static bool pcmbuf_flush_fillpos(void)
397{
398 if (audiobuffer_fillpos) {
399 /* Never use the last buffer descriptor */
400 while (pcmbuf_write == pcmbuf_write_end) {
401 /* If this happens, something is being stupid */
402 if (!pcm_is_playing()) {
403 logf("pcmbuf_flush_fillpos error");
404 pcmbuf_play_start();
405 }
406 /* Let approximately one chunk of data playback */
407 sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
408 }
409 pcmbuf_add_chunk();
410 return true;
411 }
412 return false;
413}
414
415
416/* Init */
417
418static void pcmbuf_init_pcmbuffers(void)
419{
420#ifdef DESC_DEBUG
421 first_desc = pcmbuf_write;
422#endif
423 struct pcmbufdesc *next = pcmbuf_write;
424 next++;
425 pcmbuf_write_end = pcmbuf_write;
426 while ((void *)next < (void *)pcmbuf_bufend) {
427 pcmbuf_write_end->link=next;
428 pcmbuf_write_end=next;
429 next++;
430 }
431 DISPLAY_DESC("init");
432}
433
434static size_t pcmbuf_get_next_required_pcmbuf_size(void)
435{
436 size_t seconds = 1;
437
438 if (crossfade_enabled_pending)
439 seconds += global_settings.crossfade_fade_out_delay
440 + global_settings.crossfade_fade_out_duration;
441
442#if MEMORYSIZE > 2
443 /* Buffer has to be at least 2s long. */
444 seconds += 2;
445#endif
446 logf("pcmbuf len: %ld", (long)seconds);
447 return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
448}
449
450static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
451{
452 return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
453 PCMBUF_DESCS_SIZE(bufsize));
454}
455
456/* Initialize the pcmbuffer the structure looks like this:
457 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
458size_t pcmbuf_init(unsigned char *bufend)
459{
460 pcmbuf_bufend = bufend;
461 pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
462 audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
463 fadebuf = &audiobuffer[pcmbuf_size];
464 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
465 pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
466
467 pcmbuf_init_pcmbuffers();
468
469 if(track_transition){logf("pcmbuf: (init) track transition false");}
470 end_of_track = false;
471 track_transition = false;
472
473 pcmbuf_finish_crossfade_enable();
474
475 pcmbuf_play_stop();
476
477 return pcmbuf_bufend - audiobuffer;
478}
479
480
481/* Playback */
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000482
483/** PCM driver callback
484 * This function has 3 major logical parts (separated by brackets both for
Brandon Low413da2a2006-02-07 20:38:55 +0000485 * readability and variable scoping). The first part performs the
Jeffrey Goodee8eefe92009-11-01 19:39:23 +0000486 * operations related to finishing off the last buffer we fed to the DMA.
487 * The second part detects the end of playlist condition when the pcm
488 * buffer is empty except for uncommitted samples. Then they are committed.
489 * The third part performs the operations involved in sending a new buffer
490 * to the DMA. */
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000491static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR;
492static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000493{
Brandon Low413da2a2006-02-07 20:38:55 +0000494 {
495 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
496 /* Take the finished buffer out of circulation */
497 pcmbuf_read = pcmbuf_current->link;
498
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000499 /* if during a track transition, update the elapsed time */
500 if (track_transition)
501 audio_pcmbuf_position_callback(last_chunksize);
502
503 /* if last buffer in the track, let the audio thread know */
504 if (pcmbuf_current->end_of_track)
505 pcmbuf_finish_track_change();
Brandon Low413da2a2006-02-07 20:38:55 +0000506
507 /* Put the finished buffer back into circulation */
508 pcmbuf_write_end->link = pcmbuf_current;
509 pcmbuf_write_end = pcmbuf_current;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000510
Brandon Low08cdc432006-04-25 16:12:43 +0000511 /* If we've read over the mix chunk while it's still mixing there */
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000512 if (pcmbuf_current == pcmbuf_mix_chunk)
513 pcmbuf_mix_chunk = NULL;
Brandon Low08cdc432006-04-25 16:12:43 +0000514 /* If we've read over the crossfade chunk while it's still fading */
Brandon Low9aa49a42006-04-25 02:28:21 +0000515 if (pcmbuf_current == crossfade_chunk)
Brandon Low23064332006-04-25 16:15:11 +0000516 crossfade_chunk = pcmbuf_read;
Brandon Low413da2a2006-02-07 20:38:55 +0000517 }
Jeffrey Goodee8eefe92009-11-01 19:39:23 +0000518
519 {
520 /* Commit last samples at end of playlist */
521 if (audiobuffer_fillpos && !pcmbuf_read)
522 {
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000523 logf("pcmbuf_pcm_callback: commit last samples");
Jeffrey Goodee8eefe92009-11-01 19:39:23 +0000524 pcmbuf_flush_fillpos();
525 }
526 }
Brandon Low413da2a2006-02-07 20:38:55 +0000527
528 {
529 /* Send the new buffer to the pcm */
530 struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
531 size_t *realsize = size;
532 unsigned char** realstart = start;
533 if(pcmbuf_new)
534 {
535 size_t current_size = pcmbuf_new->size;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000536
Brandon Low413da2a2006-02-07 20:38:55 +0000537 pcmbuf_unplayed_bytes -= current_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000538 last_chunksize = current_size;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000539 *realsize = current_size;
Brandon Low413da2a2006-02-07 20:38:55 +0000540 *realstart = pcmbuf_new->addr;
541 }
542 else
543 {
544 /* No more buffers */
545 last_chunksize = 0;
546 *realsize = 0;
547 *realstart = NULL;
Jeffrey Goode5ce8e2c2009-11-04 03:58:33 +0000548 if (end_of_track)
549 pcmbuf_finish_track_change();
Brandon Low413da2a2006-02-07 20:38:55 +0000550 }
551 }
Jeffrey Goode013fe352009-11-05 17:32:32 +0000552 DISPLAY_DESC("callback");
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000553}
554
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000555/* Force playback. */
556void pcmbuf_play_start(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000557{
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000558 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
Michael Sevakisb425de72007-03-06 20:32:13 +0000559 {
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000560 last_chunksize = pcmbuf_read->size;
561 pcmbuf_unplayed_bytes -= last_chunksize;
562 pcm_play_data(pcmbuf_pcm_callback,
563 (unsigned char *)pcmbuf_read->addr, last_chunksize);
Michael Sevakisb425de72007-03-06 20:32:13 +0000564 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000565}
566
567void pcmbuf_play_stop(void)
568{
569 pcm_play_stop();
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000570
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000571 pcmbuf_unplayed_bytes = 0;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000572 pcmbuf_mix_chunk = NULL;
Brandon Low413da2a2006-02-07 20:38:55 +0000573 if (pcmbuf_read) {
574 pcmbuf_write_end->link = pcmbuf_read;
575 pcmbuf_write_end = pcmbuf_read_end;
576 pcmbuf_read = pcmbuf_read_end = NULL;
577 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000578 audiobuffer_pos = 0;
579 audiobuffer_fillpos = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000580 crossfade_init = false;
581 crossfade_active = false;
Brandon Low6c0908b2006-04-23 22:54:34 +0000582 pcmbuf_flush = false;
Jeffrey Goode013fe352009-11-05 17:32:32 +0000583 DISPLAY_DESC("play_stop");
Michael Sevakisb425de72007-03-06 20:32:13 +0000584
585#ifdef HAVE_PRIORITY_SCHEDULING
586 /* Can unboost the codec thread here no matter who's calling */
587 boost_codec_thread(false);
588#endif
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000589}
590
Michael Sevakis3bec38e2007-06-24 20:36:58 +0000591void pcmbuf_pause(bool pause)
592{
Michael Sevakis6077e5b2007-10-06 22:27:27 +0000593 if (pcm_is_playing())
594 pcm_play_pause(!pause);
595 else if (!pause)
596 pcmbuf_play_start();
Brandon Low413da2a2006-02-07 20:38:55 +0000597}
598
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000599
600/* Track change */
601
602/* The codec is moving on to the next track, but the current track is
603 * still playing. Set flags to make sure the elapsed time of the current
604 * track is updated properly, and mark the currently written chunk as the
605 * last one in the track. */
606static void pcmbuf_gapless_track_change(void)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000607{
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000608 /* we're starting a track transition */
609 track_transition = true;
610
611 /* mark the last chunk in the track */
612 end_of_track = true;
Miika Pekkarinen159c52d2005-08-20 11:13:19 +0000613}
614
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000615static void pcmbuf_crossfade_track_change(void)
616{
617 /* Initiate automatic crossfade mode */
618 pcmbuf_crossfade_init(false);
619 /* Notify the wps that the track change starts now */
620 audio_post_track_change(false);
621}
622
623void pcmbuf_start_track_change(bool manual_skip)
624{
625 /* Manual track change (always crossfade or flush audio). */
626 if (manual_skip)
627 {
628 pcmbuf_crossfade_init(true);
629 audio_post_track_change(false);
630 }
631 /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */
632 else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active()
633 && global_settings.crossfade != CROSSFADE_ENABLE_TRACKSKIP)
634 {
635 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP)
636 {
637 if (global_settings.playlist_shuffle)
638 /* shuffle mode is on, so crossfade: */
639 pcmbuf_crossfade_track_change();
640 else
641 /* shuffle mode is off, so normal gapless playback */
642 pcmbuf_gapless_track_change();
643 }
644 else
645 /* normal crossfade: */
646 pcmbuf_crossfade_track_change();
647 }
648 else
649 /* normal gapless playback. */
650 pcmbuf_gapless_track_change();
651}
652
653/* Called when the last chunk in the track has been played */
654static void pcmbuf_finish_track_change(void)
655{
656 /* not in a track transition anymore */
657 if(track_transition){logf("pcmbuf: (finish change) track transition false");}
658 track_transition = false;
659
660 /* notify playback that the track has just finished */
661 audio_post_track_change(true);
662}
663
664
665/* Crossfade */
666
Michael Giacomelli64699262009-08-11 02:05:38 +0000667/**
668 * Low memory targets don't have crossfade, so don't compile crossfade
669 * specific code in order to save some memory. */
670
671#ifdef HAVE_CROSSFADE
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000672/**
673 * Completely process the crossfade fade out effect with current pcm buffer.
674 */
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000675static void crossfade_process_buffer(size_t fade_in_delay,
676 size_t fade_out_delay, size_t fade_out_rem)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000677{
Miika Pekkarinenc9a1b4e2006-05-14 14:08:26 +0000678 if (!crossfade_mixmode)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000679 {
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000680 /* Fade out the specified amount of the already processed audio */
681 size_t total_fade_out = fade_out_rem;
Brandon Low6c0908b2006-04-23 22:54:34 +0000682 size_t fade_out_sample;
683 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000684
Brandon Low6c0908b2006-04-23 22:54:34 +0000685 /* Find the right chunk to start fading out */
Brandon Low9ca16a62006-04-24 03:43:43 +0000686 fade_out_delay += crossfade_sample * 2;
Steve Bavind5dcb492006-09-27 15:23:10 +0000687 while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
Brandon Low6c0908b2006-04-23 22:54:34 +0000688 {
689 fade_out_delay -= fade_out_chunk->size;
690 fade_out_chunk = fade_out_chunk->link;
691 }
692 /* The start sample within the chunk */
693 fade_out_sample = fade_out_delay / 2;
Steve Bavinb24ed652008-04-03 14:24:37 +0000694
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000695 while (fade_out_rem > 0)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000696 {
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000697 /* Each 1/10 second of audio will have the same fade applied */
Brandon Low19247692006-04-25 10:39:56 +0000698 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
Brandon Low6c0908b2006-04-23 22:54:34 +0000699 int factor = (fade_out_rem << 8) / total_fade_out;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000700
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000701 fade_out_rem -= block_rem;
702
703 /* Fade this block */
Miika Pekkarinen815684a2006-09-17 08:34:42 +0000704 while (block_rem > 0 && fade_out_chunk != NULL)
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000705 {
706 /* Fade one sample */
Michael Sevakis53981252007-11-16 14:26:05 +0000707 int16_t *buf = (int16_t *)fade_out_chunk->addr;
708 int32_t sample = buf[fade_out_sample];
Brandon Low6c0908b2006-04-23 22:54:34 +0000709 buf[fade_out_sample++] = (sample * factor) >> 8;
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000710
Brandon Low19247692006-04-25 10:39:56 +0000711 block_rem -= 2;
Brandon Low6c0908b2006-04-23 22:54:34 +0000712 /* Move to the next chunk as needed */
713 if (fade_out_sample * 2 >= fade_out_chunk->size)
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000714 {
Brandon Low6c0908b2006-04-23 22:54:34 +0000715 fade_out_chunk = fade_out_chunk->link;
716 fade_out_sample = 0;
Brandon Lowdbcc9c22006-03-30 00:47:05 +0000717 }
718 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000719 }
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000720 }
721
Brandon Low6c0908b2006-04-23 22:54:34 +0000722 /* Find the right chunk and sample to start fading in */
Brandon Low9ca16a62006-04-24 03:43:43 +0000723 fade_in_delay += crossfade_sample * 2;
Steve Bavind5dcb492006-09-27 15:23:10 +0000724 while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
Brandon Low6c0908b2006-04-23 22:54:34 +0000725 {
726 fade_in_delay -= crossfade_chunk->size;
727 crossfade_chunk = crossfade_chunk->link;
728 }
729 crossfade_sample = fade_in_delay / 2;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000730 logf("process done!");
731}
732
Brandon Low6c0908b2006-04-23 22:54:34 +0000733/* Initializes crossfader, calculates all necessary parameters and
734 * performs fade-out with the pcm buffer. */
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000735static void crossfade_start(void)
736{
Brandon Low6c0908b2006-04-23 22:54:34 +0000737 size_t crossfade_rem;
Brandon Low9aa49a42006-04-25 02:28:21 +0000738 size_t crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000739 size_t fade_out_rem;
740 size_t fade_out_delay;
741 size_t fade_in_delay;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000742
Brandon Low37faaab2006-04-20 02:30:59 +0000743 crossfade_init = false;
Brandon Low413da2a2006-02-07 20:38:55 +0000744 /* Reject crossfade if less than .5s of data */
745 if (LOW_DATA(2)) {
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000746 logf("crossfade rejected");
747 pcmbuf_play_stop();
748 return ;
749 }
750
751 logf("crossfade_start");
Brandon Low413da2a2006-02-07 20:38:55 +0000752 pcmbuf_flush_fillpos();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000753 crossfade_active = true;
Brandon Low6c0908b2006-04-23 22:54:34 +0000754
Brandon Low413da2a2006-02-07 20:38:55 +0000755 /* Initialize the crossfade buffer size to all of the buffered data that
756 * has not yet been sent to the DMA */
Brandon Low6c0908b2006-04-23 22:54:34 +0000757 crossfade_rem = pcmbuf_unplayed_bytes;
758 crossfade_chunk = pcmbuf_read->link;
Brandon Low8ef18272006-04-24 12:41:30 +0000759 crossfade_sample = 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000760
Brandon Low6c0908b2006-04-23 22:54:34 +0000761 /* Get fade out delay from settings. */
762 fade_out_delay =
763 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000764
Brandon Low6c0908b2006-04-23 22:54:34 +0000765 /* Get fade out duration from settings. */
766 fade_out_rem =
767 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000768
Brandon Low9aa49a42006-04-25 02:28:21 +0000769 crossfade_need = fade_out_delay + fade_out_rem;
Brandon Low6c0908b2006-04-23 22:54:34 +0000770 /* We want only to modify the last part of the buffer. */
Brandon Low9aa49a42006-04-25 02:28:21 +0000771 if (crossfade_rem > crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000772 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000773 size_t crossfade_extra = crossfade_rem - crossfade_need;
Brandon Low6c0908b2006-04-23 22:54:34 +0000774 while (crossfade_extra > crossfade_chunk->size)
775 {
776 crossfade_extra -= crossfade_chunk->size;
777 crossfade_chunk = crossfade_chunk->link;
778 }
779 crossfade_sample = crossfade_extra / 2;
780 }
781 /* Truncate fade out duration if necessary. */
Brandon Low9aa49a42006-04-25 02:28:21 +0000782 else if (crossfade_rem < crossfade_need)
Brandon Low6c0908b2006-04-23 22:54:34 +0000783 {
Brandon Low9aa49a42006-04-25 02:28:21 +0000784 size_t crossfade_short = crossfade_need - crossfade_rem;
785 if (fade_out_rem >= crossfade_short)
Brandon Low6c0908b2006-04-23 22:54:34 +0000786 fade_out_rem -= crossfade_short;
787 else
788 {
789 fade_out_delay -= crossfade_short - fade_out_rem;
790 fade_out_rem = 0;
791 }
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000792 }
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000793
Brandon Low6c0908b2006-04-23 22:54:34 +0000794 /* Get also fade in duration and delays from settings. */
795 crossfade_fade_in_total =
796 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
797 crossfade_fade_in_rem = crossfade_fade_in_total;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000798
Brandon Low6c0908b2006-04-23 22:54:34 +0000799 fade_in_delay =
800 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
801
802 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000803}
804
Brandon Low9602dd72006-04-25 12:34:28 +0000805/* Returns the number of bytes _NOT_ mixed */
Jeffrey Goode96bb9ef2009-10-23 01:01:00 +0000806static size_t crossfade_mix(int factor, const char *buf, size_t length)
Miika Pekkarinene7461b32005-11-06 16:40:20 +0000807{
Michael Sevakis53981252007-11-16 14:26:05 +0000808 const int16_t *input_buf = (const int16_t *)buf;
809 int16_t *output_buf = (int16_t *)(crossfade_chunk->addr);
810 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
Brandon Low9aa49a42006-04-25 02:28:21 +0000811 output_buf = &output_buf[crossfade_sample];
Andree Buschmann21870cf2008-03-30 10:11:31 +0000812 int32_t sample;
Zakk Roberts8bdd92b2006-03-30 05:56:19 +0000813
Brandon Low9602dd72006-04-25 12:34:28 +0000814 while (length)
815 {
Andree Buschmann21870cf2008-03-30 10:11:31 +0000816 /* fade left and right channel at once to keep buffer alignment */
Jeffrey Goode96bb9ef2009-10-23 01:01:00 +0000817 int i;
818 for (i = 0; i < 2; i++)
819 {
820 sample = *input_buf++;
821 sample = ((sample * factor) >> 8) + *output_buf;
822 *output_buf++ = clip_sample_16(sample);
823 }
Steve Bavinb24ed652008-04-03 14:24:37 +0000824
Andree Buschmann21870cf2008-03-30 10:11:31 +0000825 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
Brandon Low9602dd72006-04-25 12:34:28 +0000826
827 if (output_buf >= chunk_end)
828 {
829 crossfade_chunk = crossfade_chunk->link;
830 if (!crossfade_chunk)
831 return length;
Michael Sevakis18330a02007-11-16 14:34:15 +0000832 output_buf = (int16_t *)crossfade_chunk->addr;
Michael Sevakis53981252007-11-16 14:26:05 +0000833 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
Brandon Low9602dd72006-04-25 12:34:28 +0000834 }
835 }
Michael Sevakis53981252007-11-16 14:26:05 +0000836 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
Brandon Low9602dd72006-04-25 12:34:28 +0000837 return 0;
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000838}
839
Brandon Low9aa49a42006-04-25 02:28:21 +0000840static void flush_crossfade(char *buf, size_t length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000841{
Brandon Low9602dd72006-04-25 12:34:28 +0000842 if (length)
Brandon Low6c0908b2006-04-23 22:54:34 +0000843 {
Brandon Low9602dd72006-04-25 12:34:28 +0000844 if (crossfade_fade_in_rem)
Brandon Low9aa49a42006-04-25 02:28:21 +0000845 {
846 size_t samples;
Michael Sevakis53981252007-11-16 14:26:05 +0000847 int16_t *input_buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000848
849 /* Fade factor for this packet */
850 int factor =
851 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
852 crossfade_fade_in_total;
853 /* Bytes to fade */
854 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
855
856 /* We _will_ fade this many bytes */
857 crossfade_fade_in_rem -= fade_rem;
858
859 if (crossfade_chunk)
860 {
861 /* Mix the data */
862 size_t fade_total = fade_rem;
Jeffrey Goode96bb9ef2009-10-23 01:01:00 +0000863 fade_rem = crossfade_mix(factor, buf, fade_rem);
Brandon Low9602dd72006-04-25 12:34:28 +0000864 length -= fade_total - fade_rem;
865 buf += fade_total - fade_rem;
Brandon Low08cdc432006-04-25 16:12:43 +0000866 if (!length)
867 return;
Brandon Low9602dd72006-04-25 12:34:28 +0000868 }
869
Brandon Low9aa49a42006-04-25 02:28:21 +0000870 samples = fade_rem / 2;
Michael Sevakis53981252007-11-16 14:26:05 +0000871 input_buf = (int16_t *)buf;
Brandon Low9602dd72006-04-25 12:34:28 +0000872 /* Fade remaining samples in place */
Jeffrey Goode96bb9ef2009-10-23 01:01:00 +0000873 while (samples--)
Brandon Low9aa49a42006-04-25 02:28:21 +0000874 {
Michael Sevakis53981252007-11-16 14:26:05 +0000875 int32_t sample = *input_buf;
Brandon Low9aa49a42006-04-25 02:28:21 +0000876 *input_buf++ = (sample * factor) >> 8;
Brandon Low9aa49a42006-04-25 02:28:21 +0000877 }
Brandon Low6c0908b2006-04-23 22:54:34 +0000878 }
Brandon Low413da2a2006-02-07 20:38:55 +0000879
Brandon Low9602dd72006-04-25 12:34:28 +0000880 if (crossfade_chunk)
Brandon Low6c0908b2006-04-23 22:54:34 +0000881 {
Brandon Low9602dd72006-04-25 12:34:28 +0000882 /* Mix the data */
883 size_t mix_total = length;
Jeffrey Goode96bb9ef2009-10-23 01:01:00 +0000884 length = crossfade_mix(256, buf, length);
Brandon Low9602dd72006-04-25 12:34:28 +0000885 buf += mix_total - length;
Brandon Low08cdc432006-04-25 16:12:43 +0000886 if (!length)
887 return;
Brandon Low6c0908b2006-04-23 22:54:34 +0000888 }
Brandon Low9602dd72006-04-25 12:34:28 +0000889
Brandon Low08cdc432006-04-25 16:12:43 +0000890 /* Flush samples to the buffer */
891 while (!prepare_insert(length))
892 sleep(1);
Jeffrey Goode013fe352009-11-05 17:32:32 +0000893 while (length > 0)
894 {
895 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
896 if (NEED_FLUSH(audiobuffer_index))
897 {
898 pcmbuf_flush_fillpos();
899 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
900 }
901 size_t copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
902 memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
903 buf += copy_n;
904 audiobuffer_fillpos += copy_n;
905 length -= copy_n;
906 }
Brandon Low9aa49a42006-04-25 02:28:21 +0000907 }
Brandon Low413da2a2006-02-07 20:38:55 +0000908}
Jeffrey Goode013fe352009-11-05 17:32:32 +0000909#endif /* HAVE_CROSSFADE */
Brandon Low413da2a2006-02-07 20:38:55 +0000910
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000911static bool pcmbuf_crossfade_init(bool manual_skip)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000912{
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000913 /* Can't do two crossfades at once and, no fade if pcm is off now */
914 if (crossfade_init || crossfade_active || !pcm_is_playing())
Miika Pekkarinen4408b6b2006-02-07 19:17:51 +0000915 {
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000916 pcmbuf_play_stop();
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000917 return false;
Steve Bavinb24ed652008-04-03 14:24:37 +0000918 }
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000919
920 trigger_cpu_boost();
921
922 /* Not enough data, or crossfade disabled, flush the old data instead */
923 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
924 {
925 pcmbuf_flush_fillpos();
926 pcmbuf_flush = true;
927 return false;
928 }
929
930 /* Don't enable mix mode when skipping tracks manually. */
931 if (manual_skip)
932 crossfade_mixmode = false;
Björn Stenberg6427d122009-01-10 21:10:56 +0000933 else
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000934 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
935
936 crossfade_init = true;
Brandon Low3a37fae2006-02-13 16:55:25 +0000937
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000938 return true;
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000939
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000940}
941
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000942static void pcmbuf_finish_crossfade_enable(void)
Miika Pekkarinen20b38972005-07-13 12:48:22 +0000943{
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000944 /* Copy the pending setting over now */
945 crossfade_enabled = crossfade_enabled_pending;
946
947 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
948 /* If crossfading, try to keep the buffer full other than 1 second */
949 (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
950 /* Otherwise, just use the default */
951 PCMBUF_WATERMARK;
952}
Brandon Low37faaab2006-04-20 02:30:59 +0000953
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000954static bool pcmbuf_is_crossfade_enabled(void)
955{
956 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
957 return global_settings.playlist_shuffle;
958
959 return crossfade_enabled;
960}
961
962bool pcmbuf_is_crossfade_active(void)
963{
964 return crossfade_active || crossfade_init;
965}
966
967void pcmbuf_request_crossfade_enable(bool on_off)
968{
969 /* Next setting to be used, not applied now */
970 crossfade_enabled_pending = on_off;
971}
972
973bool pcmbuf_is_same_size(void)
974{
975 bool same_size;
976
977 if (audiobuffer == NULL)
978 same_size = true; /* Not set up yet even once so always */
Brandon Low413da2a2006-02-07 20:38:55 +0000979 else
980 {
Jeffrey Goode37adbee2009-11-06 04:13:36 +0000981 size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
982 same_size = pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
983 }
984
985 if (same_size)
986 pcmbuf_finish_crossfade_enable();
987
988 return same_size;
989}
990
991
992/* Voice */
993
994/* Returns pcm buffer usage in percents (0 to 100). */
995static int pcmbuf_usage(void)
996{
997 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
998}
999
1000static int pcmbuf_mix_free(void)
1001{
1002 if (pcmbuf_mix_chunk)
1003 {
1004 size_t my_mix_end =
1005 (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
1006 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
1007 if (my_write_pos < my_mix_end)
1008 my_write_pos += pcmbuf_size;
1009 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1010 }
1011 return 100;
1012}
1013
1014void *pcmbuf_request_voice_buffer(int *count)
1015{
1016 /* A get-it-to-work-for-now hack (audio status could change by
1017 completion) */
1018 if (audio_status() & AUDIO_STATUS_PLAY)
1019 {
1020 if (pcmbuf_read == NULL)
Brandon Low413da2a2006-02-07 20:38:55 +00001021 {
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001022 return NULL;
1023 }
1024 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
1025 (pcmbuf_mix_chunk || pcmbuf_read->link))
1026 {
1027 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
1028 return voicebuf;
Brandon Low413da2a2006-02-07 20:38:55 +00001029 }
1030 else
1031 {
Brandon Low413da2a2006-02-07 20:38:55 +00001032 return NULL;
1033 }
1034 }
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001035 else
1036 {
1037 return pcmbuf_request_buffer(count);
1038 }
Brandon Low413da2a2006-02-07 20:38:55 +00001039}
1040
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001041void pcmbuf_write_voice_complete(int count)
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001042{
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001043 /* A get-it-to-work-for-now hack (audio status could have changed) */
1044 if (!(audio_status() & AUDIO_STATUS_PLAY))
Brandon Low08cdc432006-04-25 16:12:43 +00001045 {
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001046 pcmbuf_write_complete(count);
1047 return;
Brandon Low08cdc432006-04-25 16:12:43 +00001048 }
Brandon Low413da2a2006-02-07 20:38:55 +00001049
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001050 int16_t *ibuf = (int16_t *)voicebuf;
1051 int16_t *obuf;
1052 size_t chunk_samples;
1053
1054 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
1055 {
1056 pcmbuf_mix_chunk = pcmbuf_read->link;
1057 /* Start 1/8s into the next chunk */
1058 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001059 }
Jeffrey Goode37adbee2009-11-06 04:13:36 +00001060
1061 if (!pcmbuf_mix_chunk)
1062 return;
1063
1064 obuf = (int16_t *)pcmbuf_mix_chunk->addr;
1065 chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
1066
1067 count <<= 1;
1068
1069 while (count-- > 0)
1070 {
1071 int32_t sample = *ibuf++;
1072
1073 if (pcmbuf_mix_sample >= chunk_samples)
1074 {
1075 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
1076 if (!pcmbuf_mix_chunk)
1077 return;
1078 pcmbuf_mix_sample = 0;
1079 obuf = pcmbuf_mix_chunk->addr;
1080 chunk_samples = pcmbuf_mix_chunk->size / 2;
1081 }
1082 sample += obuf[pcmbuf_mix_sample] >> 2;
1083 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1084 }
1085}
1086
1087
1088/* Debug menu, other metrics */
1089
1090/* Amount of bytes left in the buffer. */
1091size_t pcmbuf_free(void)
1092{
1093 if (pcmbuf_read != NULL)
1094 {
1095 void *read = pcmbuf_read->addr;
1096 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
1097 if (read < write)
1098 return (size_t)(read - write) + pcmbuf_size;
1099 else
1100 return (size_t) (read - write);
1101 }
1102 return pcmbuf_size;
1103}
1104
1105size_t pcmbuf_get_bufsize(void)
1106{
1107 return pcmbuf_size;
1108}
1109
1110int pcmbuf_used_descs(void)
1111{
1112 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
1113 unsigned int i = 0;
1114 while (pcmbuf_temp) {
1115 pcmbuf_temp = pcmbuf_temp->link;
1116 i++;
1117 }
1118 return i;
1119}
1120
1121int pcmbuf_descs(void)
1122{
1123 return PCMBUF_DESCS(pcmbuf_size);
1124}
1125
1126#ifdef ROCKBOX_HAS_LOGF
1127unsigned char * pcmbuf_get_meminfo(size_t *length)
1128{
1129 *length = pcmbuf_bufend - audiobuffer;
1130 return audiobuffer;
1131}
1132#endif
1133
1134
1135/* Misc */
1136
1137bool pcmbuf_is_lowdata(void)
1138{
1139 if (!pcm_is_playing() || pcm_is_paused() ||
1140 crossfade_init || crossfade_active)
1141 return false;
1142
1143#if MEMORYSIZE > 2
1144 /* 1 seconds of buffer is low data */
1145 return LOW_DATA(4);
1146#else
1147 /* under watermark is low data */
1148 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
1149#endif
1150}
1151
1152void pcmbuf_set_low_latency(bool state)
1153{
1154 low_latency_mode = state;
1155}
1156
1157unsigned long pcmbuf_get_latency(void)
1158{
1159 /* Be careful how this calculation is rearranged, it's easy to overflow */
1160 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
1161 return bytes / 4 * 1000 / NATIVE_FREQUENCY;
Miika Pekkarinen20b38972005-07-13 12:48:22 +00001162}
1163
Michael Sevakis89da4322008-12-07 00:07:47 +00001164#ifndef HAVE_HARDWARE_BEEP
Michael Sevakis93572e22008-12-13 06:01:08 +00001165#define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
1166#define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
1167
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001168/* Generates a constant square wave sound with a given frequency
1169 in Hertz for a duration in milliseconds. */
Brandon Low9535a9a2006-02-22 01:56:44 +00001170void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001171{
Michael Sevakis93572e22008-12-13 06:01:08 +00001172 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
1173 int32_t phase = 0;
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001174 int16_t *bufptr, *bufstart, *bufend;
Michael Sevakis53981252007-11-16 14:26:05 +00001175 int32_t sample;
Michael Sevakis93572e22008-12-13 06:01:08 +00001176 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
1177 bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001178 int i;
1179
Michael Sevakis93572e22008-12-13 06:01:08 +00001180 bufend = SKIPBYTES((int16_t *)audiobuffer, pcmbuf_size);
Zakk Roberts8bdd92b2006-03-30 05:56:19 +00001181
Steve Bavinb24ed652008-04-03 14:24:37 +00001182 /* Find the insertion point and set bufstart to the start of it */
1183 if (mix)
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001184 {
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001185 /* Get the currently playing chunk at the current position. */
1186 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
1187
Michael Sevakis26bf32c2008-12-14 11:42:47 +00001188 /* If above isn't implemented or pcm is stopped, no beepeth. */
1189 if (!bufstart || !pcm_is_playing())
1190 return;
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001191
1192 /* Give 5ms clearance. */
1193 bufstart += NATIVE_FREQUENCY * 4 / 200;
1194
Michael Sevakis94537f92009-02-08 22:32:41 +00001195#ifdef HAVE_PCM_DMA_ADDRESS
1196 /* Returned peak addresses are DMA addresses */
1197 bufend = pcm_dma_addr(bufend);
1198#endif
1199
Michael Sevakis93572e22008-12-13 06:01:08 +00001200 /* Wrapped above? */
1201 if (bufstart >= bufend)
1202 bufstart -= pcmbuf_size;
1203
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001204 /* NOTE: On some targets using hardware DMA, cache range flushing may
1205 * be required or the writes may not be picked up by the controller.
1206 * An incremental flush should be done periodically during the mixdown. */
Brandon Low9535a9a2006-02-22 01:56:44 +00001207 }
Michael Sevakis93572e22008-12-13 06:01:08 +00001208 else if (nsamples <= MINIBUF_SAMPLES)
1209 {
Michael Sevakise86ece42009-02-16 18:45:43 +00001210 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
Michael Sevakis93572e22008-12-13 06:01:08 +00001211 /* Use mini buffer */
1212 bufstart = minibuf;
1213 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1214 }
1215 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED)
Brandon Low0fcd4112006-04-05 13:00:31 +00001216 {
Steve Bavinb24ed652008-04-03 14:24:37 +00001217 /* Use audiobuffer */
1218 bufstart = (int16_t *)audiobuffer;
1219 }
Michael Sevakis93572e22008-12-13 06:01:08 +00001220 else
1221 {
1222 /* No place */
1223 return;
1224 }
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001225
Steve Bavinb24ed652008-04-03 14:24:37 +00001226 bufptr = bufstart;
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001227
1228 /* Mix square wave into buffer */
1229 for (i = 0; i < nsamples; ++i)
Steve Bavinb24ed652008-04-03 14:24:37 +00001230 {
Michael Sevakis89da4322008-12-07 00:07:47 +00001231 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
Steve Bavinb24ed652008-04-03 14:24:37 +00001232 sample = mix ? *bufptr : 0;
Michael Sevakis89da4322008-12-07 00:07:47 +00001233 *bufptr++ = clip_sample_16(sample + amp);
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001234 if (bufptr >= bufend)
Steve Bavinb24ed652008-04-03 14:24:37 +00001235 bufptr = (int16_t *)audiobuffer;
1236 sample = mix ? *bufptr : 0;
Michael Sevakis89da4322008-12-07 00:07:47 +00001237 *bufptr++ = clip_sample_16(sample + amp);
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001238 if (bufptr >= bufend)
Steve Bavinb24ed652008-04-03 14:24:37 +00001239 bufptr = (int16_t *)audiobuffer;
1240
Michael Sevakis89da4322008-12-07 00:07:47 +00001241 phase += step;
Steve Bavinb24ed652008-04-03 14:24:37 +00001242 }
1243
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001244 pcm_play_lock();
Michael Sevakis93572e22008-12-13 06:01:08 +00001245#ifdef HAVE_RECORDING
1246 pcm_rec_lock();
1247#endif
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001248
Michael Sevakis93572e22008-12-13 06:01:08 +00001249 /* Kick off playback if required and it won't interfere */
1250 if (!pcm_is_playing()
1251#ifdef HAVE_RECORDING
1252 && !pcm_is_recording()
1253#endif
1254 )
Steve Bavinb24ed652008-04-03 14:24:37 +00001255 {
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001256 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001257 }
Michael Sevakis5e73f9f2008-12-13 01:32:39 +00001258
1259 pcm_play_unlock();
Michael Sevakis93572e22008-12-13 06:01:08 +00001260#ifdef HAVE_RECORDING
1261 pcm_rec_unlock();
1262#endif
Miika Pekkarinen159c52d2005-08-20 11:13:19 +00001263}
Michael Sevakis89da4322008-12-07 00:07:47 +00001264#endif /* HAVE_HARDWARE_BEEP */