set svn:keywords property
[kugel-rb.git] / apps / pcmbuf.c
blobb8d8a0c8d00313ebf19bf13dac027ede5720ade8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include "config.h"
23 #include "system.h"
24 #include "debug.h"
25 #include <kernel.h>
26 #include "pcmbuf.h"
27 #include "pcm.h"
28 #include "logf.h"
29 #ifndef SIMULATOR
30 #include "cpu.h"
31 #endif
32 #include <string.h>
33 #include "buffer.h"
34 #include "settings.h"
35 #include "audio.h"
36 #include "voice_thread.h"
37 #include "dsp.h"
38 #include "thread.h"
40 /* Clip sample to signed 16 bit range */
41 static inline int32_t clip_sample_16(int32_t sample)
43 if ((int16_t)sample != sample)
44 sample = 0x7fff ^ (sample >> 31);
45 return sample;
48 #if MEMORYSIZE > 2
49 /* Keep watermark high for iPods at least (2s) */
50 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
51 #else
52 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
53 #endif
55 /* Structure we can use to queue pcm chunks in memory to be played
56 * by the driver code. */
57 struct pcmbufdesc
59 void *addr;
60 size_t size;
61 struct pcmbufdesc* link;
62 /* Call this when the buffer has been played */
63 void (*callback)(void);
66 #define PCMBUF_DESCS(bufsize) \
67 ((bufsize) / PCMBUF_MINAVG_CHUNK)
68 #define PCMBUF_DESCS_SIZE(bufsize) \
69 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
71 /* Size of the PCM buffer. */
72 static size_t pcmbuf_size IDATA_ATTR = 0;
73 static char *pcmbuf_bufend IDATA_ATTR;
74 static char *audiobuffer IDATA_ATTR;
75 /* Current audio buffer write index. */
76 static size_t audiobuffer_pos IDATA_ATTR;
77 /* Amount audiobuffer_pos will be increased.*/
78 static size_t audiobuffer_fillpos IDATA_ATTR;
79 static char *fadebuf IDATA_ATTR;
80 static char *voicebuf IDATA_ATTR;
82 static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
83 static void (*position_callback)(size_t size) IDATA_ATTR;
85 /* Crossfade related state */
86 static bool crossfade_enabled;
87 static bool crossfade_enabled_pending;
88 static bool crossfade_mixmode;
89 static bool crossfade_active IDATA_ATTR;
90 static bool crossfade_init IDATA_ATTR;
92 /* Track the current location for processing crossfade */
93 static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
94 static size_t crossfade_sample IDATA_ATTR;
96 /* Counters for fading in new data */
97 static size_t crossfade_fade_in_total IDATA_ATTR;
98 static size_t crossfade_fade_in_rem IDATA_ATTR;
100 static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
101 static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
102 static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
103 static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
104 static size_t last_chunksize IDATA_ATTR;
106 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
107 static size_t pcmbuf_watermark IDATA_ATTR;
109 static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
110 static size_t pcmbuf_mix_sample IDATA_ATTR;
112 static bool low_latency_mode = false;
113 static bool pcmbuf_flush;
115 #ifdef HAVE_PRIORITY_SCHEDULING
116 static int codec_thread_priority = PRIORITY_PLAYBACK;
117 #endif
119 extern unsigned int codec_thread_id;
121 /* Helpful macros for use in conditionals this assumes some of the above
122 * static variable names */
123 #define NEED_FLUSH(position) \
124 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
125 #define LOW_DATA(quarter_secs) \
126 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
128 static bool prepare_insert(size_t length);
129 static void pcmbuf_under_watermark(bool under);
130 static bool pcmbuf_flush_fillpos(void);
132 #define CALL_IF_EXISTS(function, args...) if (function) function(args)
133 /* This function has 2 major logical parts (separated by brackets both for
134 * readability and variable scoping). The first part performs the
135 * operastions related to finishing off the last buffer we fed to the DMA.
136 * The second part performs the operations involved in sending a new buffer
137 * to the DMA. Finally the function checks the status of the buffer and
138 * boosts if necessary */
139 static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
140 static void pcmbuf_callback(unsigned char** start, size_t* size)
143 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
144 /* Take the finished buffer out of circulation */
145 pcmbuf_read = pcmbuf_current->link;
147 /* The buffer is finished, call the callback functions */
148 CALL_IF_EXISTS(position_callback, last_chunksize);
149 CALL_IF_EXISTS(pcmbuf_current->callback);
151 /* Put the finished buffer back into circulation */
152 pcmbuf_write_end->link = pcmbuf_current;
153 pcmbuf_write_end = pcmbuf_current;
155 /* If we've read over the mix chunk while it's still mixing there */
156 if (pcmbuf_current == pcmbuf_mix_chunk)
157 pcmbuf_mix_chunk = NULL;
158 /* If we've read over the crossfade chunk while it's still fading */
159 if (pcmbuf_current == crossfade_chunk)
160 crossfade_chunk = pcmbuf_read;
164 /* Send the new buffer to the pcm */
165 struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
166 size_t *realsize = size;
167 unsigned char** realstart = start;
168 if(pcmbuf_new)
170 size_t current_size = pcmbuf_new->size;
172 pcmbuf_unplayed_bytes -= current_size;
173 last_chunksize = current_size;
174 *realsize = current_size;
175 *realstart = pcmbuf_new->addr;
177 else
179 /* No more buffers */
180 last_chunksize = 0;
181 *realsize = 0;
182 *realstart = NULL;
183 CALL_IF_EXISTS(pcmbuf_event_handler);
188 void pcmbuf_set_position_callback(void (*callback)(size_t size))
190 position_callback = callback;
193 static void pcmbuf_set_watermark_bytes(void)
195 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
196 /* If crossfading, try to keep the buffer full other than 1 second */
197 (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
198 /* Otherwise, just use the default */
199 PCMBUF_WATERMARK;
202 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
203 * in a separate function for the moment */
204 static inline void pcmbuf_add_chunk(void)
206 register size_t size = audiobuffer_fillpos;
207 /* Grab the next description to write, and change the write pointer */
208 register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
209 pcmbuf_write = pcmbuf_current->link;
210 /* Fill in the values in the new buffer chunk */
211 pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
212 pcmbuf_current->size = size;
213 pcmbuf_current->callback = pcmbuf_event_handler;
214 pcmbuf_current->link = NULL;
215 /* This is single use only */
216 pcmbuf_event_handler = NULL;
217 if (pcmbuf_read != NULL) {
218 if (pcmbuf_flush)
220 pcmbuf_write_end->link = pcmbuf_read->link;
221 pcmbuf_read->link = pcmbuf_current;
222 while (pcmbuf_write_end->link)
224 pcmbuf_write_end = pcmbuf_write_end->link;
225 pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
227 pcmbuf_flush = false;
229 /* If there is already a read buffer setup, add to it */
230 else
231 pcmbuf_read_end->link = pcmbuf_current;
232 } else {
233 /* Otherwise create the buffer */
234 pcmbuf_read = pcmbuf_current;
236 /* This is now the last buffer to read */
237 pcmbuf_read_end = pcmbuf_current;
239 /* Update bytes counters */
240 pcmbuf_unplayed_bytes += size;
242 audiobuffer_pos += size;
243 if (audiobuffer_pos >= pcmbuf_size)
244 audiobuffer_pos -= pcmbuf_size;
246 audiobuffer_fillpos = 0;
249 #ifdef HAVE_PRIORITY_SCHEDULING
250 static void boost_codec_thread(bool boost)
252 /* Keep voice and codec threads at the same priority or else voice
253 * will starve if the codec thread's priority is boosted. */
254 if (boost)
256 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
257 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
259 if (priority != codec_thread_priority)
261 codec_thread_priority = priority;
262 thread_set_priority(codec_thread_id, priority);
263 voice_thread_set_priority(priority);
266 else if (codec_thread_priority != PRIORITY_PLAYBACK)
268 thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
269 voice_thread_set_priority(PRIORITY_PLAYBACK);
270 codec_thread_priority = PRIORITY_PLAYBACK;
273 #endif /* HAVE_PRIORITY_SCHEDULING */
275 static void pcmbuf_under_watermark(bool under)
277 /* Only codec thread initiates boost - voice boosts the cpu when playing
278 a clip */
279 #ifndef SIMULATOR
280 if (thread_get_current() == codec_thread_id)
281 #endif /* SIMULATOR */
283 if (under)
285 /* Fill audio buffer by boosting cpu */
286 trigger_cpu_boost();
287 #ifdef HAVE_PRIORITY_SCHEDULING
288 /* If buffer is critically low, override UI priority, else
289 set back to the original priority. */
290 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
291 #endif
293 else
295 #ifdef HAVE_PRIORITY_SCHEDULING
296 boost_codec_thread(false);
297 #endif
301 /* Disable crossfade if < .5s of audio */
302 if (LOW_DATA(2))
304 crossfade_active = false;
308 void pcmbuf_set_event_handler(void (*event_handler)(void))
310 pcmbuf_event_handler = event_handler;
313 unsigned int pcmbuf_get_latency(void)
315 /* Be careful how this calculation is rearranged, it's easy to overflow */
316 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
317 return bytes / 4 / (NATIVE_FREQUENCY/1000);
320 void pcmbuf_set_low_latency(bool state)
322 low_latency_mode = state;
325 bool pcmbuf_is_lowdata(void)
327 if (!pcm_is_playing() || pcm_is_paused() ||
328 crossfade_init || crossfade_active)
329 return false;
331 #if MEMORYSIZE > 2
332 /* 1 seconds of buffer is low data */
333 return LOW_DATA(4);
334 #else
335 /* under watermark is low data */
336 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
337 #endif
340 /* Amount of bytes left in the buffer. */
341 inline size_t pcmbuf_free(void)
343 if (pcmbuf_read != NULL)
345 void *read = pcmbuf_read->addr;
346 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
347 if (read < write)
348 return (size_t)(read - write) + pcmbuf_size;
349 else
350 return (size_t) (read - write);
352 return pcmbuf_size;
355 bool pcmbuf_crossfade_init(bool manual_skip)
357 /* Can't do two crossfades at once and, no fade if pcm is off now */
358 if (crossfade_init || crossfade_active || !pcm_is_playing())
360 pcmbuf_play_stop();
361 return false;
364 trigger_cpu_boost();
366 /* Not enough data, or crossfade disabled, flush the old data instead */
367 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
369 pcmbuf_flush_fillpos();
370 pcmbuf_flush = true;
371 return false;
374 /* Don't enable mix mode when skipping tracks manually. */
375 if (manual_skip)
376 crossfade_mixmode = false;
377 else
378 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
380 crossfade_init = true;
382 return true;
386 void pcmbuf_play_stop(void)
388 pcm_play_stop();
390 pcmbuf_unplayed_bytes = 0;
391 pcmbuf_mix_chunk = NULL;
392 if (pcmbuf_read) {
393 pcmbuf_write_end->link = pcmbuf_read;
394 pcmbuf_write_end = pcmbuf_read_end;
395 pcmbuf_read = pcmbuf_read_end = NULL;
397 audiobuffer_pos = 0;
398 audiobuffer_fillpos = 0;
399 crossfade_init = false;
400 crossfade_active = false;
401 pcmbuf_flush = false;
403 #ifdef HAVE_PRIORITY_SCHEDULING
404 /* Can unboost the codec thread here no matter who's calling */
405 boost_codec_thread(false);
406 #endif
409 int pcmbuf_used_descs(void)
411 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
412 unsigned int i = 0;
413 while (pcmbuf_temp) {
414 pcmbuf_temp = pcmbuf_temp->link;
415 i++;
417 return i;
420 int pcmbuf_descs(void)
422 return PCMBUF_DESCS(pcmbuf_size);
425 static void pcmbuf_init_pcmbuffers(void)
427 struct pcmbufdesc *next = pcmbuf_write;
428 next++;
429 pcmbuf_write_end = pcmbuf_write;
430 while ((void *)next < (void *)pcmbuf_bufend) {
431 pcmbuf_write_end->link=next;
432 pcmbuf_write_end=next;
433 next++;
437 static size_t pcmbuf_get_next_required_pcmbuf_size(void)
439 size_t seconds = 1;
441 if (crossfade_enabled_pending)
442 seconds += global_settings.crossfade_fade_out_delay
443 + global_settings.crossfade_fade_out_duration;
445 #if MEMORYSIZE > 2
446 /* Buffer has to be at least 2s long. */
447 seconds += 2;
448 #endif
449 logf("pcmbuf len: %ld", seconds);
450 return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
453 static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
455 return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
456 PCMBUF_DESCS_SIZE(bufsize));
459 bool pcmbuf_is_same_size(void)
461 if (audiobuffer == NULL)
462 return true; /* Not set up yet even once so always */
464 size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
465 return pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
468 /* Initialize the pcmbuffer the structure looks like this:
469 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
470 size_t pcmbuf_init(unsigned char *bufend)
472 pcmbuf_bufend = bufend;
473 pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
474 audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
475 fadebuf = &audiobuffer[pcmbuf_size];
476 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
477 pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
479 pcmbuf_init_pcmbuffers();
481 position_callback = NULL;
482 pcmbuf_event_handler = NULL;
484 pcmbuf_crossfade_enable_finished();
486 pcmbuf_play_stop();
488 return pcmbuf_bufend - audiobuffer;
491 size_t pcmbuf_get_bufsize(void)
493 return pcmbuf_size;
496 #ifdef ROCKBOX_HAS_LOGF
497 unsigned char * pcmbuf_get_meminfo(size_t *length)
499 *length = pcmbuf_bufend - audiobuffer;
500 return audiobuffer;
502 #endif
504 void pcmbuf_pause(bool pause)
506 if (pcm_is_playing())
507 pcm_play_pause(!pause);
508 else if (!pause)
509 pcmbuf_play_start();
512 /* Force playback. */
513 void pcmbuf_play_start(void)
515 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
517 last_chunksize = pcmbuf_read->size;
518 pcmbuf_unplayed_bytes -= last_chunksize;
519 pcm_play_data(pcmbuf_callback,
520 (unsigned char *)pcmbuf_read->addr, last_chunksize);
525 * Commit samples waiting to the pcm buffer.
527 static bool pcmbuf_flush_fillpos(void)
529 if (audiobuffer_fillpos) {
530 /* Never use the last buffer descriptor */
531 while (pcmbuf_write == pcmbuf_write_end) {
532 /* If this happens, something is being stupid */
533 if (!pcm_is_playing()) {
534 logf("pcmbuf_flush_fillpos error");
535 pcmbuf_play_start();
537 /* Let approximately one chunk of data playback */
538 sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
540 pcmbuf_add_chunk();
541 return true;
543 return false;
547 * Completely process the crossfade fade out effect with current pcm buffer.
549 static void crossfade_process_buffer(size_t fade_in_delay,
550 size_t fade_out_delay, size_t fade_out_rem)
552 if (!crossfade_mixmode)
554 /* Fade out the specified amount of the already processed audio */
555 size_t total_fade_out = fade_out_rem;
556 size_t fade_out_sample;
557 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
559 /* Find the right chunk to start fading out */
560 fade_out_delay += crossfade_sample * 2;
561 while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
563 fade_out_delay -= fade_out_chunk->size;
564 fade_out_chunk = fade_out_chunk->link;
566 /* The start sample within the chunk */
567 fade_out_sample = fade_out_delay / 2;
569 while (fade_out_rem > 0)
571 /* Each 1/10 second of audio will have the same fade applied */
572 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
573 int factor = (fade_out_rem << 8) / total_fade_out;
575 fade_out_rem -= block_rem;
577 /* Fade this block */
578 while (block_rem > 0 && fade_out_chunk != NULL)
580 /* Fade one sample */
581 int16_t *buf = (int16_t *)fade_out_chunk->addr;
582 int32_t sample = buf[fade_out_sample];
583 buf[fade_out_sample++] = (sample * factor) >> 8;
585 block_rem -= 2;
586 /* Move to the next chunk as needed */
587 if (fade_out_sample * 2 >= fade_out_chunk->size)
589 fade_out_chunk = fade_out_chunk->link;
590 fade_out_sample = 0;
596 /* Find the right chunk and sample to start fading in */
597 fade_in_delay += crossfade_sample * 2;
598 while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
600 fade_in_delay -= crossfade_chunk->size;
601 crossfade_chunk = crossfade_chunk->link;
603 crossfade_sample = fade_in_delay / 2;
604 logf("process done!");
607 /* Initializes crossfader, calculates all necessary parameters and
608 * performs fade-out with the pcm buffer. */
609 static void crossfade_start(void)
611 size_t crossfade_rem;
612 size_t crossfade_need;
613 size_t fade_out_rem;
614 size_t fade_out_delay;
615 size_t fade_in_delay;
617 crossfade_init = false;
618 /* Reject crossfade if less than .5s of data */
619 if (LOW_DATA(2)) {
620 logf("crossfade rejected");
621 pcmbuf_play_stop();
622 return ;
625 logf("crossfade_start");
626 pcmbuf_flush_fillpos();
627 crossfade_active = true;
629 /* Initialize the crossfade buffer size to all of the buffered data that
630 * has not yet been sent to the DMA */
631 crossfade_rem = pcmbuf_unplayed_bytes;
632 crossfade_chunk = pcmbuf_read->link;
633 crossfade_sample = 0;
635 /* Get fade out delay from settings. */
636 fade_out_delay =
637 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
639 /* Get fade out duration from settings. */
640 fade_out_rem =
641 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
643 crossfade_need = fade_out_delay + fade_out_rem;
644 /* We want only to modify the last part of the buffer. */
645 if (crossfade_rem > crossfade_need)
647 size_t crossfade_extra = crossfade_rem - crossfade_need;
648 while (crossfade_extra > crossfade_chunk->size)
650 crossfade_extra -= crossfade_chunk->size;
651 crossfade_chunk = crossfade_chunk->link;
653 crossfade_sample = crossfade_extra / 2;
655 /* Truncate fade out duration if necessary. */
656 else if (crossfade_rem < crossfade_need)
658 size_t crossfade_short = crossfade_need - crossfade_rem;
659 if (fade_out_rem >= crossfade_short)
660 fade_out_rem -= crossfade_short;
661 else
663 fade_out_delay -= crossfade_short - fade_out_rem;
664 fade_out_rem = 0;
668 /* Get also fade in duration and delays from settings. */
669 crossfade_fade_in_total =
670 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
671 crossfade_fade_in_rem = crossfade_fade_in_total;
673 fade_in_delay =
674 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
676 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
679 /* Returns the number of bytes _NOT_ mixed */
680 static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem)
682 const int16_t *input_buf = (const int16_t *)buf;
683 int16_t *output_buf = (int16_t *)(crossfade_chunk->addr);
684 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
685 output_buf = &output_buf[crossfade_sample];
686 int32_t sample;
688 while (fade_rem)
690 /* fade left and right channel at once to keep buffer alignment */
691 sample = *input_buf++;
692 sample = ((sample * factor) >> 8) + *output_buf;
693 *output_buf++ = clip_sample_16(sample);
695 sample = *input_buf++;
696 sample = ((sample * factor) >> 8) + *output_buf;
697 *output_buf++ = clip_sample_16(sample);
699 fade_rem -= 4; /* 2 samples, each 16 bit -> 4 bytes */
701 if (output_buf >= chunk_end)
703 crossfade_chunk = crossfade_chunk->link;
704 if (!crossfade_chunk)
705 return fade_rem;
706 output_buf = (int16_t *)crossfade_chunk->addr;
707 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
710 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
711 return 0;
714 /* Returns the number of bytes _NOT_ mixed */
715 static size_t crossfade_mix(const char *buf, size_t length)
717 const int16_t *input_buf = (const int16_t *)buf;
718 int16_t *output_buf = (int16_t *)crossfade_chunk->addr;
719 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
720 output_buf = &output_buf[crossfade_sample];
721 int32_t sample;
723 while (length)
725 /* fade left and right channel at once to keep buffer alignment */
726 sample = *input_buf++ + *output_buf;
727 *output_buf++ = clip_sample_16(sample);
729 sample = *input_buf++ + *output_buf;
730 *output_buf++ = clip_sample_16(sample);
732 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
734 if (output_buf >= chunk_end)
736 crossfade_chunk = crossfade_chunk->link;
737 if (!crossfade_chunk)
738 return length;
740 output_buf = (int16_t *)crossfade_chunk->addr;
741 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
744 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
745 return 0;
748 static void pcmbuf_flush_buffer(const char *buf, size_t length)
750 size_t copy_n;
751 while (length > 0) {
752 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
753 if (NEED_FLUSH(audiobuffer_index))
755 pcmbuf_flush_fillpos();
756 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
758 copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
759 memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
760 buf += copy_n;
761 audiobuffer_fillpos += copy_n;
762 length -= copy_n;
766 static void flush_crossfade(char *buf, size_t length)
768 if (length)
770 if (crossfade_fade_in_rem)
772 size_t samples;
773 int16_t *input_buf;
775 /* Fade factor for this packet */
776 int factor =
777 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
778 crossfade_fade_in_total;
779 /* Bytes to fade */
780 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
782 /* We _will_ fade this many bytes */
783 crossfade_fade_in_rem -= fade_rem;
785 if (crossfade_chunk)
787 /* Mix the data */
788 size_t fade_total = fade_rem;
789 fade_rem = crossfade_fade_mix(factor, buf, fade_rem);
790 length -= fade_total - fade_rem;
791 buf += fade_total - fade_rem;
792 if (!length)
793 return;
794 if (!fade_rem)
795 goto fade_done;
798 samples = fade_rem / 2;
799 input_buf = (int16_t *)buf;
800 /* Fade remaining samples in place */
801 while (samples)
803 int32_t sample = *input_buf;
804 *input_buf++ = (sample * factor) >> 8;
805 samples--;
809 fade_done:
810 if (crossfade_chunk)
812 /* Mix the data */
813 size_t mix_total = length;
814 length = crossfade_mix(buf, length);
815 buf += mix_total - length;
816 if (!length)
817 return;
820 /* Flush samples to the buffer */
821 while (!prepare_insert(length))
822 sleep(1);
823 pcmbuf_flush_buffer(buf, length);
828 static bool prepare_insert(size_t length)
830 if (low_latency_mode)
832 /* 1/4s latency. */
833 if (!LOW_DATA(1) && pcm_is_playing())
834 return false;
837 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
838 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
839 return false;
841 if (!pcm_is_playing())
843 trigger_cpu_boost();
845 /* Pre-buffer up to watermark */
846 #if MEMORYSIZE > 2
847 if (!LOW_DATA(4))
848 #else
849 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
850 #endif
852 logf("pcm starting");
853 if (!(audio_status() & AUDIO_STATUS_PAUSE))
854 pcmbuf_play_start();
857 else
858 pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
860 return true;
863 void* pcmbuf_request_buffer(int *count)
865 if (crossfade_init)
866 crossfade_start();
868 if (crossfade_active) {
869 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
870 return fadebuf;
872 else
874 if(prepare_insert(*count << 2))
876 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
877 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
879 /* Usual case, there's space here */
880 return &audiobuffer[audiobuffer_index];
882 else
884 /* Flush and wrap the buffer */
885 pcmbuf_flush_fillpos();
886 audiobuffer_pos = 0;
887 return &audiobuffer[0];
890 else
892 return NULL;
897 void * pcmbuf_request_voice_buffer(int *count)
899 /* A get-it-to-work-for-now hack (audio status could change by
900 completion) */
901 if (audio_status() & AUDIO_STATUS_PLAY)
903 if (pcmbuf_read == NULL)
905 return NULL;
907 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
908 (pcmbuf_mix_chunk || pcmbuf_read->link))
910 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
911 return voicebuf;
913 else
915 return NULL;
918 else
920 return pcmbuf_request_buffer(count);
924 bool pcmbuf_is_crossfade_active(void)
926 return crossfade_active || crossfade_init;
929 void pcmbuf_write_complete(int count)
931 size_t length = (size_t)(unsigned int)count << 2;
933 if (crossfade_active)
935 flush_crossfade(fadebuf, length);
936 if (!(crossfade_fade_in_rem || crossfade_chunk))
937 crossfade_active = false;
939 else
941 audiobuffer_fillpos += length;
943 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
944 pcmbuf_flush_fillpos();
948 #if 0
949 bool pcmbuf_insert_buffer(char *buf, int count)
951 size_t length = (size_t)(unsigned int)count << 2;
953 if (crossfade_active)
955 flush_crossfade(buf, length);
956 if (!(crossfade_fade_in_rem || crossfade_chunk))
957 crossfade_active = false;
959 else
961 if (!prepare_insert(length))
962 return false;
963 pcmbuf_flush_buffer(buf, length);
965 return true;
967 #endif
969 #ifndef HAVE_HARDWARE_BEEP
970 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
971 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
973 /* Generates a constant square wave sound with a given frequency
974 in Hertz for a duration in milliseconds. */
975 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
977 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
978 int32_t phase = 0;
979 int16_t *bufptr, *bufstart, *bufend;
980 int32_t sample;
981 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
982 bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
983 int i;
985 bufend = SKIPBYTES((int16_t *)audiobuffer, pcmbuf_size);
987 /* Find the insertion point and set bufstart to the start of it */
988 if (mix)
990 /* Get the currently playing chunk at the current position. */
991 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
993 /* If above isn't implemented or pcm is stopped, no beepeth. */
994 if (!bufstart || !pcm_is_playing())
995 return;
997 /* Give 5ms clearance. */
998 bufstart += NATIVE_FREQUENCY * 4 / 200;
1000 #ifdef HAVE_PCM_DMA_ADDRESS
1001 /* Returned peak addresses are DMA addresses */
1002 bufend = pcm_dma_addr(bufend);
1003 #endif
1005 /* Wrapped above? */
1006 if (bufstart >= bufend)
1007 bufstart -= pcmbuf_size;
1009 /* NOTE: On some targets using hardware DMA, cache range flushing may
1010 * be required or the writes may not be picked up by the controller.
1011 * An incremental flush should be done periodically during the mixdown. */
1013 else if (nsamples <= MINIBUF_SAMPLES)
1015 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
1016 /* Use mini buffer */
1017 bufstart = minibuf;
1018 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1020 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED)
1022 /* Use audiobuffer */
1023 bufstart = (int16_t *)audiobuffer;
1025 else
1027 /* No place */
1028 return;
1031 bufptr = bufstart;
1033 /* Mix square wave into buffer */
1034 for (i = 0; i < nsamples; ++i)
1036 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
1037 sample = mix ? *bufptr : 0;
1038 *bufptr++ = clip_sample_16(sample + amp);
1039 if (bufptr >= bufend)
1040 bufptr = (int16_t *)audiobuffer;
1041 sample = mix ? *bufptr : 0;
1042 *bufptr++ = clip_sample_16(sample + amp);
1043 if (bufptr >= bufend)
1044 bufptr = (int16_t *)audiobuffer;
1046 phase += step;
1049 pcm_play_lock();
1050 #ifdef HAVE_RECORDING
1051 pcm_rec_lock();
1052 #endif
1054 /* Kick off playback if required and it won't interfere */
1055 if (!pcm_is_playing()
1056 #ifdef HAVE_RECORDING
1057 && !pcm_is_recording()
1058 #endif
1061 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
1064 pcm_play_unlock();
1065 #ifdef HAVE_RECORDING
1066 pcm_rec_unlock();
1067 #endif
1069 #endif /* HAVE_HARDWARE_BEEP */
1071 /* Returns pcm buffer usage in percents (0 to 100). */
1072 int pcmbuf_usage(void)
1074 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1077 int pcmbuf_mix_free(void)
1079 if (pcmbuf_mix_chunk)
1081 size_t my_mix_end =
1082 (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
1083 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
1084 if (my_write_pos < my_mix_end)
1085 my_write_pos += pcmbuf_size;
1086 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1088 return 100;
1091 void pcmbuf_write_voice_complete(int count)
1093 /* A get-it-to-work-for-now hack (audio status could have changed) */
1094 if (!(audio_status() & AUDIO_STATUS_PLAY))
1096 pcmbuf_write_complete(count);
1097 return;
1100 int16_t *ibuf = (int16_t *)voicebuf;
1101 int16_t *obuf;
1102 size_t chunk_samples;
1104 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
1106 pcmbuf_mix_chunk = pcmbuf_read->link;
1107 /* Start 1/8s into the next chunk */
1108 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
1111 if (!pcmbuf_mix_chunk)
1112 return;
1114 obuf = (int16_t *)pcmbuf_mix_chunk->addr;
1115 chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
1117 count <<= 1;
1119 while (count-- > 0)
1121 int32_t sample = *ibuf++;
1123 if (pcmbuf_mix_sample >= chunk_samples)
1125 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
1126 if (!pcmbuf_mix_chunk)
1127 return;
1128 pcmbuf_mix_sample = 0;
1129 obuf = pcmbuf_mix_chunk->addr;
1130 chunk_samples = pcmbuf_mix_chunk->size / 2;
1132 sample += obuf[pcmbuf_mix_sample] >> 2;
1133 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1137 void pcmbuf_crossfade_enable(bool on_off)
1139 /* Next setting to be used, not applied now */
1140 crossfade_enabled_pending = on_off;
1143 void pcmbuf_crossfade_enable_finished(void)
1145 /* Copy the pending setting over now */
1146 crossfade_enabled = crossfade_enabled_pending;
1147 pcmbuf_set_watermark_bytes();
1150 bool pcmbuf_is_crossfade_enabled(void)
1152 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
1153 return global_settings.playlist_shuffle;
1155 return crossfade_enabled;
1158 void pcmbuf_play_remainder(void)
1160 if (audiobuffer_fillpos)
1161 pcmbuf_flush_fillpos();