Correct the BBX clause (no visual changes)
[kugel-rb/myfork.git] / apps / pcmbuf.c
blob9143e32660e6e24d096b1f2c3e2449c24eda0191
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 "panic.h"
26 #include <kernel.h>
27 #include "pcmbuf.h"
28 #include "pcm.h"
29 #include "logf.h"
30 #ifndef SIMULATOR
31 #include "cpu.h"
32 #endif
33 #include <string.h>
34 #include "buffer.h"
35 #include "settings.h"
36 #include "audio.h"
37 #include "voice_thread.h"
38 #include "dsp.h"
39 #include "thread.h"
41 /* Clip sample to signed 16 bit range */
42 static inline int32_t clip_sample_16(int32_t sample)
44 if ((int16_t)sample != sample)
45 sample = 0x7fff ^ (sample >> 31);
46 return sample;
49 #if MEMORYSIZE > 2
50 /* Keep watermark high for iPods at least (2s) */
51 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
52 #else
53 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
54 #endif
56 /* Structure we can use to queue pcm chunks in memory to be played
57 * by the driver code. */
58 struct pcmbufdesc
60 void *addr;
61 size_t size;
62 struct pcmbufdesc* link;
63 /* Call this when the buffer has been played */
64 void (*callback)(void);
67 #define PCMBUF_DESCS(bufsize) \
68 ((bufsize) / PCMBUF_MINAVG_CHUNK)
69 #define PCMBUF_DESCS_SIZE(bufsize) \
70 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
72 /* Size of the PCM buffer. */
73 static size_t pcmbuf_size IDATA_ATTR = 0;
74 static char *pcmbuf_bufend IDATA_ATTR;
75 static char *audiobuffer IDATA_ATTR;
76 /* Current audio buffer write index. */
77 static size_t audiobuffer_pos IDATA_ATTR;
78 /* Amount audiobuffer_pos will be increased.*/
79 static size_t audiobuffer_fillpos IDATA_ATTR;
80 static char *fadebuf IDATA_ATTR;
81 static char *voicebuf IDATA_ATTR;
83 static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
84 static void (*position_callback)(size_t size) IDATA_ATTR;
86 /* Crossfade related state */
87 static bool crossfade_enabled;
88 static bool crossfade_enabled_pending;
89 static bool crossfade_mixmode;
90 static bool crossfade_active IDATA_ATTR;
91 static bool crossfade_init IDATA_ATTR;
93 /* Track the current location for processing crossfade */
94 static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
95 static size_t crossfade_sample IDATA_ATTR;
97 /* Counters for fading in new data */
98 static size_t crossfade_fade_in_total IDATA_ATTR;
99 static size_t crossfade_fade_in_rem IDATA_ATTR;
101 static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
102 static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
103 static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
104 static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
105 static size_t last_chunksize IDATA_ATTR;
107 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
108 static size_t pcmbuf_watermark IDATA_ATTR;
110 static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
111 static size_t pcmbuf_mix_sample IDATA_ATTR;
113 static bool low_latency_mode = false;
114 static bool pcmbuf_flush;
116 #ifdef HAVE_PRIORITY_SCHEDULING
117 static int codec_thread_priority = PRIORITY_PLAYBACK;
118 #endif
120 extern unsigned int codec_thread_id;
122 /* Helpful macros for use in conditionals this assumes some of the above
123 * static variable names */
124 #define NEED_FLUSH(position) \
125 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
126 #define LOW_DATA(quarter_secs) \
127 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
129 static bool prepare_insert(size_t length);
130 static void pcmbuf_under_watermark(bool under);
131 static bool pcmbuf_flush_fillpos(void);
133 #define CALL_IF_EXISTS(function, args...) if (function) function(args)
134 /* This function has 2 major logical parts (separated by brackets both for
135 * readability and variable scoping). The first part performs the
136 * operastions related to finishing off the last buffer we fed to the DMA.
137 * The second part performs the operations involved in sending a new buffer
138 * to the DMA. Finally the function checks the status of the buffer and
139 * boosts if necessary */
140 static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
141 static void pcmbuf_callback(unsigned char** start, size_t* size)
144 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
145 /* Take the finished buffer out of circulation */
146 pcmbuf_read = pcmbuf_current->link;
148 /* The buffer is finished, call the callback functions */
149 CALL_IF_EXISTS(position_callback, last_chunksize);
150 CALL_IF_EXISTS(pcmbuf_current->callback);
152 /* Put the finished buffer back into circulation */
153 pcmbuf_write_end->link = pcmbuf_current;
154 pcmbuf_write_end = pcmbuf_current;
156 /* If we've read over the mix chunk while it's still mixing there */
157 if (pcmbuf_current == pcmbuf_mix_chunk)
158 pcmbuf_mix_chunk = NULL;
159 /* If we've read over the crossfade chunk while it's still fading */
160 if (pcmbuf_current == crossfade_chunk)
161 crossfade_chunk = pcmbuf_read;
165 /* Send the new buffer to the pcm */
166 struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
167 size_t *realsize = size;
168 unsigned char** realstart = start;
169 if(pcmbuf_new)
171 size_t current_size = pcmbuf_new->size;
173 pcmbuf_unplayed_bytes -= current_size;
174 last_chunksize = current_size;
175 *realsize = current_size;
176 *realstart = pcmbuf_new->addr;
178 else
180 /* No more buffers */
181 last_chunksize = 0;
182 *realsize = 0;
183 *realstart = NULL;
184 CALL_IF_EXISTS(pcmbuf_event_handler);
189 void pcmbuf_set_position_callback(void (*callback)(size_t size))
191 position_callback = callback;
194 static void pcmbuf_set_watermark_bytes(void)
196 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
197 /* If crossfading, try to keep the buffer full other than 1 second */
198 (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
199 /* Otherwise, just use the default */
200 PCMBUF_WATERMARK;
203 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
204 * in a separate function for the moment */
205 static inline void pcmbuf_add_chunk(void)
207 register size_t size = audiobuffer_fillpos;
208 /* Grab the next description to write, and change the write pointer */
209 register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
210 pcmbuf_write = pcmbuf_current->link;
211 /* Fill in the values in the new buffer chunk */
212 pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
213 pcmbuf_current->size = size;
214 pcmbuf_current->callback = pcmbuf_event_handler;
215 pcmbuf_current->link = NULL;
216 /* This is single use only */
217 pcmbuf_event_handler = NULL;
218 if (pcmbuf_read != NULL) {
219 if (pcmbuf_flush)
221 pcmbuf_write_end->link = pcmbuf_read->link;
222 pcmbuf_read->link = pcmbuf_current;
223 while (pcmbuf_write_end->link)
225 pcmbuf_write_end = pcmbuf_write_end->link;
226 pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
228 pcmbuf_flush = false;
230 /* If there is already a read buffer setup, add to it */
231 else
232 pcmbuf_read_end->link = pcmbuf_current;
233 } else {
234 /* Otherwise create the buffer */
235 pcmbuf_read = pcmbuf_current;
237 /* This is now the last buffer to read */
238 pcmbuf_read_end = pcmbuf_current;
240 /* Update bytes counters */
241 pcmbuf_unplayed_bytes += size;
243 audiobuffer_pos += size;
244 if (audiobuffer_pos >= pcmbuf_size)
245 audiobuffer_pos -= pcmbuf_size;
247 audiobuffer_fillpos = 0;
250 #ifdef HAVE_PRIORITY_SCHEDULING
251 static void boost_codec_thread(bool boost)
253 /* Keep voice and codec threads at the same priority or else voice
254 * will starve if the codec thread's priority is boosted. */
255 if (boost)
257 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
258 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
260 if (priority != codec_thread_priority)
262 codec_thread_priority = priority;
263 thread_set_priority(codec_thread_id, priority);
264 voice_thread_set_priority(priority);
267 else if (codec_thread_priority != PRIORITY_PLAYBACK)
269 thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
270 voice_thread_set_priority(PRIORITY_PLAYBACK);
271 codec_thread_priority = PRIORITY_PLAYBACK;
274 #endif /* HAVE_PRIORITY_SCHEDULING */
276 static void pcmbuf_under_watermark(bool under)
278 /* Only codec thread initiates boost - voice boosts the cpu when playing
279 a clip */
280 #ifndef SIMULATOR
281 if (thread_get_current() == codec_thread_id)
282 #endif /* SIMULATOR */
284 if (under)
286 /* Fill audio buffer by boosting cpu */
287 trigger_cpu_boost();
288 #ifdef HAVE_PRIORITY_SCHEDULING
289 /* 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());
292 #endif
294 else
296 #ifdef HAVE_PRIORITY_SCHEDULING
297 boost_codec_thread(false);
298 #endif
302 /* Disable crossfade if < .5s of audio */
303 if (LOW_DATA(2))
305 crossfade_active = false;
309 void pcmbuf_set_event_handler(void (*event_handler)(void))
311 pcmbuf_event_handler = event_handler;
314 unsigned int pcmbuf_get_latency(void)
316 /* Be careful how this calculation is rearranged, it's easy to overflow */
317 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
318 return bytes / 4 / (NATIVE_FREQUENCY/1000);
321 void pcmbuf_set_low_latency(bool state)
323 low_latency_mode = state;
326 bool pcmbuf_is_lowdata(void)
328 if (!pcm_is_playing() || pcm_is_paused() ||
329 crossfade_init || crossfade_active)
330 return false;
332 #if MEMORYSIZE > 2
333 /* 1 seconds of buffer is low data */
334 return LOW_DATA(4);
335 #else
336 /* under watermark is low data */
337 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
338 #endif
341 /* Amount of bytes left in the buffer. */
342 inline size_t pcmbuf_free(void)
344 if (pcmbuf_read != NULL)
346 void *read = pcmbuf_read->addr;
347 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
348 if (read < write)
349 return (size_t)(read - write) + pcmbuf_size;
350 else
351 return (size_t) (read - write);
353 return pcmbuf_size;
356 bool pcmbuf_crossfade_init(bool manual_skip)
358 /* Can't do two crossfades at once and, no fade if pcm is off now */
359 if (crossfade_init || crossfade_active || !pcm_is_playing())
361 pcmbuf_play_stop();
362 return false;
365 trigger_cpu_boost();
367 /* Not enough data, or crossfade disabled, flush the old data instead */
368 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
370 pcmbuf_flush_fillpos();
371 pcmbuf_flush = true;
372 return false;
375 /* Don't enable mix mode when skipping tracks manually. */
376 if (manual_skip)
377 crossfade_mixmode = false;
378 else
379 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
381 crossfade_init = true;
383 return true;
387 void pcmbuf_play_stop(void)
389 pcm_play_stop();
391 pcmbuf_unplayed_bytes = 0;
392 pcmbuf_mix_chunk = NULL;
393 if (pcmbuf_read) {
394 pcmbuf_write_end->link = pcmbuf_read;
395 pcmbuf_write_end = pcmbuf_read_end;
396 pcmbuf_read = pcmbuf_read_end = NULL;
398 audiobuffer_pos = 0;
399 audiobuffer_fillpos = 0;
400 crossfade_init = false;
401 crossfade_active = false;
402 pcmbuf_flush = false;
404 #ifdef HAVE_PRIORITY_SCHEDULING
405 /* Can unboost the codec thread here no matter who's calling */
406 boost_codec_thread(false);
407 #endif
410 int pcmbuf_used_descs(void)
412 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
413 unsigned int i = 0;
414 while (pcmbuf_temp) {
415 pcmbuf_temp = pcmbuf_temp->link;
416 i++;
418 return i;
421 int pcmbuf_descs(void)
423 return PCMBUF_DESCS(pcmbuf_size);
426 static void pcmbuf_init_pcmbuffers(void)
428 struct pcmbufdesc *next = pcmbuf_write;
429 next++;
430 pcmbuf_write_end = pcmbuf_write;
431 while ((void *)next < (void *)pcmbuf_bufend) {
432 pcmbuf_write_end->link=next;
433 pcmbuf_write_end=next;
434 next++;
438 static size_t pcmbuf_get_next_required_pcmbuf_size(void)
440 size_t seconds = 1;
442 if (crossfade_enabled_pending)
443 seconds += global_settings.crossfade_fade_out_delay
444 + global_settings.crossfade_fade_out_duration;
446 #if MEMORYSIZE > 2
447 /* Buffer has to be at least 2s long. */
448 seconds += 2;
449 #endif
450 logf("pcmbuf len: %ld", seconds);
451 return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
454 static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
456 return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
457 PCMBUF_DESCS_SIZE(bufsize));
460 bool pcmbuf_is_same_size(void)
462 if (audiobuffer == NULL)
463 return true; /* Not set up yet even once so always */
465 size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
466 return pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
469 /* Initialize the pcmbuffer the structure looks like this:
470 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
471 size_t pcmbuf_init(unsigned char *bufend)
473 pcmbuf_bufend = bufend;
474 pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
475 audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
476 fadebuf = &audiobuffer[pcmbuf_size];
477 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
478 pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
480 pcmbuf_init_pcmbuffers();
482 position_callback = NULL;
483 pcmbuf_event_handler = NULL;
485 pcmbuf_crossfade_enable_finished();
487 pcmbuf_play_stop();
489 return pcmbuf_bufend - audiobuffer;
492 size_t pcmbuf_get_bufsize(void)
494 return pcmbuf_size;
497 #ifdef ROCKBOX_HAS_LOGF
498 unsigned char * pcmbuf_get_meminfo(size_t *length)
500 *length = pcmbuf_bufend - audiobuffer;
501 return audiobuffer;
503 #endif
505 void pcmbuf_pause(bool pause)
507 if (pcm_is_playing())
508 pcm_play_pause(!pause);
509 else if (!pause)
510 pcmbuf_play_start();
513 /* Force playback. */
514 void pcmbuf_play_start(void)
516 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
518 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);
526 * Commit samples waiting to the pcm buffer.
528 static bool pcmbuf_flush_fillpos(void)
530 if (audiobuffer_fillpos) {
531 /* Never use the last buffer descriptor */
532 while (pcmbuf_write == pcmbuf_write_end) {
533 /* If this happens, something is being stupid */
534 if (!pcm_is_playing()) {
535 logf("pcmbuf_flush_fillpos error");
536 pcmbuf_play_start();
538 /* Let approximately one chunk of data playback */
539 sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
541 pcmbuf_add_chunk();
542 return true;
544 return false;
548 * Completely process the crossfade fade out effect with current pcm buffer.
550 static void crossfade_process_buffer(size_t fade_in_delay,
551 size_t fade_out_delay, size_t fade_out_rem)
553 if (!crossfade_mixmode)
555 /* Fade out the specified amount of the already processed audio */
556 size_t total_fade_out = fade_out_rem;
557 size_t fade_out_sample;
558 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
560 /* Find the right chunk to start fading out */
561 fade_out_delay += crossfade_sample * 2;
562 while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
564 fade_out_delay -= fade_out_chunk->size;
565 fade_out_chunk = fade_out_chunk->link;
567 /* The start sample within the chunk */
568 fade_out_sample = fade_out_delay / 2;
570 while (fade_out_rem > 0)
572 /* Each 1/10 second of audio will have the same fade applied */
573 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
574 int factor = (fade_out_rem << 8) / total_fade_out;
576 fade_out_rem -= block_rem;
578 /* Fade this block */
579 while (block_rem > 0 && fade_out_chunk != NULL)
581 /* Fade one sample */
582 int16_t *buf = (int16_t *)fade_out_chunk->addr;
583 int32_t sample = buf[fade_out_sample];
584 buf[fade_out_sample++] = (sample * factor) >> 8;
586 block_rem -= 2;
587 /* Move to the next chunk as needed */
588 if (fade_out_sample * 2 >= fade_out_chunk->size)
590 fade_out_chunk = fade_out_chunk->link;
591 fade_out_sample = 0;
597 /* Find the right chunk and sample to start fading in */
598 fade_in_delay += crossfade_sample * 2;
599 while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
601 fade_in_delay -= crossfade_chunk->size;
602 crossfade_chunk = crossfade_chunk->link;
604 crossfade_sample = fade_in_delay / 2;
605 logf("process done!");
608 /* Initializes crossfader, calculates all necessary parameters and
609 * performs fade-out with the pcm buffer. */
610 static void crossfade_start(void)
612 size_t crossfade_rem;
613 size_t crossfade_need;
614 size_t fade_out_rem;
615 size_t fade_out_delay;
616 size_t fade_in_delay;
618 crossfade_init = false;
619 /* Reject crossfade if less than .5s of data */
620 if (LOW_DATA(2)) {
621 logf("crossfade rejected");
622 pcmbuf_play_stop();
623 return ;
626 logf("crossfade_start");
627 pcmbuf_flush_fillpos();
628 crossfade_active = true;
630 /* Initialize the crossfade buffer size to all of the buffered data that
631 * has not yet been sent to the DMA */
632 crossfade_rem = pcmbuf_unplayed_bytes;
633 crossfade_chunk = pcmbuf_read->link;
634 crossfade_sample = 0;
636 /* Get fade out delay from settings. */
637 fade_out_delay =
638 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
640 /* Get fade out duration from settings. */
641 fade_out_rem =
642 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
644 crossfade_need = fade_out_delay + fade_out_rem;
645 /* We want only to modify the last part of the buffer. */
646 if (crossfade_rem > crossfade_need)
648 size_t crossfade_extra = crossfade_rem - crossfade_need;
649 while (crossfade_extra > crossfade_chunk->size)
651 crossfade_extra -= crossfade_chunk->size;
652 crossfade_chunk = crossfade_chunk->link;
654 crossfade_sample = crossfade_extra / 2;
656 /* Truncate fade out duration if necessary. */
657 else if (crossfade_rem < crossfade_need)
659 size_t crossfade_short = crossfade_need - crossfade_rem;
660 if (fade_out_rem >= crossfade_short)
661 fade_out_rem -= crossfade_short;
662 else
664 fade_out_delay -= crossfade_short - fade_out_rem;
665 fade_out_rem = 0;
669 /* 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;
674 fade_in_delay =
675 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
677 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
680 /* Returns the number of bytes _NOT_ mixed */
681 static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem)
683 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);
686 output_buf = &output_buf[crossfade_sample];
687 int32_t sample;
689 while (fade_rem)
691 /* fade left and right channel at once to keep buffer alignment */
692 sample = *input_buf++;
693 sample = ((sample * factor) >> 8) + *output_buf;
694 *output_buf++ = clip_sample_16(sample);
696 sample = *input_buf++;
697 sample = ((sample * factor) >> 8) + *output_buf;
698 *output_buf++ = clip_sample_16(sample);
700 fade_rem -= 4; /* 2 samples, each 16 bit -> 4 bytes */
702 if (output_buf >= chunk_end)
704 crossfade_chunk = crossfade_chunk->link;
705 if (!crossfade_chunk)
706 return fade_rem;
707 output_buf = (int16_t *)crossfade_chunk->addr;
708 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
711 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
712 return 0;
715 /* Returns the number of bytes _NOT_ mixed */
716 static size_t crossfade_mix(const char *buf, size_t length)
718 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);
721 output_buf = &output_buf[crossfade_sample];
722 int32_t sample;
724 while (length)
726 /* fade left and right channel at once to keep buffer alignment */
727 sample = *input_buf++ + *output_buf;
728 *output_buf++ = clip_sample_16(sample);
730 sample = *input_buf++ + *output_buf;
731 *output_buf++ = clip_sample_16(sample);
733 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
735 if (output_buf >= chunk_end)
737 crossfade_chunk = crossfade_chunk->link;
738 if (!crossfade_chunk)
739 return length;
741 output_buf = (int16_t *)crossfade_chunk->addr;
742 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
745 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
746 return 0;
749 static void pcmbuf_flush_buffer(const char *buf, size_t length)
751 size_t copy_n;
752 while (length > 0) {
753 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
754 if (NEED_FLUSH(audiobuffer_index))
756 pcmbuf_flush_fillpos();
757 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
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;
767 static void flush_crossfade(char *buf, size_t length)
769 if (length)
771 if (crossfade_fade_in_rem)
773 size_t samples;
774 int16_t *input_buf;
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);
783 /* We _will_ fade this many bytes */
784 crossfade_fade_in_rem -= fade_rem;
786 if (crossfade_chunk)
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;
793 if (!length)
794 return;
795 if (!fade_rem)
796 goto fade_done;
799 samples = fade_rem / 2;
800 input_buf = (int16_t *)buf;
801 /* Fade remaining samples in place */
802 while (samples)
804 int32_t sample = *input_buf;
805 *input_buf++ = (sample * factor) >> 8;
806 samples--;
810 fade_done:
811 if (crossfade_chunk)
813 /* Mix the data */
814 size_t mix_total = length;
815 length = crossfade_mix(buf, length);
816 buf += mix_total - length;
817 if (!length)
818 return;
821 /* Flush samples to the buffer */
822 while (!prepare_insert(length))
823 sleep(1);
824 pcmbuf_flush_buffer(buf, length);
829 static bool prepare_insert(size_t length)
831 if (low_latency_mode)
833 /* 1/4s latency. */
834 if (!LOW_DATA(1) && pcm_is_playing())
835 return false;
838 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
839 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
840 return false;
842 if (!pcm_is_playing())
844 trigger_cpu_boost();
846 /* Pre-buffer up to watermark */
847 #if MEMORYSIZE > 2
848 if (!LOW_DATA(4))
849 #else
850 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
851 #endif
853 logf("pcm starting");
854 if (!(audio_status() & AUDIO_STATUS_PAUSE))
855 pcmbuf_play_start();
858 else
859 pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
861 return true;
864 void* pcmbuf_request_buffer(int *count)
866 if (crossfade_init)
867 crossfade_start();
869 if (crossfade_active) {
870 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
871 return fadebuf;
873 else
875 if(prepare_insert(*count << 2))
877 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
878 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
880 /* Usual case, there's space here */
881 return &audiobuffer[audiobuffer_index];
883 else
885 /* Flush and wrap the buffer */
886 pcmbuf_flush_fillpos();
887 audiobuffer_pos = 0;
888 return &audiobuffer[0];
891 else
893 return NULL;
898 void * pcmbuf_request_voice_buffer(int *count)
900 /* A get-it-to-work-for-now hack (audio status could change by
901 completion) */
902 if (audio_status() & AUDIO_STATUS_PLAY)
904 if (pcmbuf_read == NULL)
906 return NULL;
908 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
909 (pcmbuf_mix_chunk || pcmbuf_read->link))
911 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
912 return voicebuf;
914 else
916 return NULL;
919 else
921 return pcmbuf_request_buffer(count);
925 bool pcmbuf_is_crossfade_active(void)
927 return crossfade_active || crossfade_init;
930 void pcmbuf_write_complete(int count)
932 size_t length = (size_t)(unsigned int)count << 2;
934 if (crossfade_active)
936 flush_crossfade(fadebuf, length);
937 if (!(crossfade_fade_in_rem || crossfade_chunk))
938 crossfade_active = false;
940 else
942 audiobuffer_fillpos += length;
944 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
945 pcmbuf_flush_fillpos();
949 #if 0
950 bool pcmbuf_insert_buffer(char *buf, int count)
952 size_t length = (size_t)(unsigned int)count << 2;
954 if (crossfade_active)
956 flush_crossfade(buf, length);
957 if (!(crossfade_fade_in_rem || crossfade_chunk))
958 crossfade_active = false;
960 else
962 if (!prepare_insert(length))
963 return false;
964 pcmbuf_flush_buffer(buf, length);
966 return true;
968 #endif
970 #ifndef HAVE_HARDWARE_BEEP
971 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
972 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
974 /* Generates a constant square wave sound with a given frequency
975 in Hertz for a duration in milliseconds. */
976 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
978 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
979 int32_t phase = 0;
980 int16_t *bufptr, *bufstart, *bufend;
981 int32_t sample;
982 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
983 bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
984 int i;
986 bufend = SKIPBYTES((int16_t *)audiobuffer, pcmbuf_size);
988 /* Find the insertion point and set bufstart to the start of it */
989 if (mix)
991 /* Get the currently playing chunk at the current position. */
992 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
994 /* If above isn't implemented or pcm is stopped, no beepeth. */
995 if (!bufstart || !pcm_is_playing())
996 return;
998 /* Give 5ms clearance. */
999 bufstart += NATIVE_FREQUENCY * 4 / 200;
1001 #ifdef HAVE_PCM_DMA_ADDRESS
1002 /* Returned peak addresses are DMA addresses */
1003 bufend = pcm_dma_addr(bufend);
1004 #endif
1006 /* Wrapped above? */
1007 if (bufstart >= bufend)
1008 bufstart -= pcmbuf_size;
1010 /* NOTE: On some targets using hardware DMA, cache range flushing may
1011 * be required or the writes may not be picked up by the controller.
1012 * An incremental flush should be done periodically during the mixdown. */
1014 else if (nsamples <= MINIBUF_SAMPLES)
1016 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
1017 /* Use mini buffer */
1018 bufstart = minibuf;
1019 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1021 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED)
1023 /* Use audiobuffer */
1024 bufstart = (int16_t *)audiobuffer;
1026 else
1028 /* No place */
1029 return;
1032 bufptr = bufstart;
1034 /* Mix square wave into buffer */
1035 for (i = 0; i < nsamples; ++i)
1037 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
1038 sample = mix ? *bufptr : 0;
1039 *bufptr++ = clip_sample_16(sample + amp);
1040 if (bufptr >= bufend)
1041 bufptr = (int16_t *)audiobuffer;
1042 sample = mix ? *bufptr : 0;
1043 *bufptr++ = clip_sample_16(sample + amp);
1044 if (bufptr >= bufend)
1045 bufptr = (int16_t *)audiobuffer;
1047 phase += step;
1050 pcm_play_lock();
1051 #ifdef HAVE_RECORDING
1052 pcm_rec_lock();
1053 #endif
1055 /* Kick off playback if required and it won't interfere */
1056 if (!pcm_is_playing()
1057 #ifdef HAVE_RECORDING
1058 && !pcm_is_recording()
1059 #endif
1062 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
1065 pcm_play_unlock();
1066 #ifdef HAVE_RECORDING
1067 pcm_rec_unlock();
1068 #endif
1070 #endif /* HAVE_HARDWARE_BEEP */
1072 /* Returns pcm buffer usage in percents (0 to 100). */
1073 int pcmbuf_usage(void)
1075 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1078 int pcmbuf_mix_free(void)
1080 if (pcmbuf_mix_chunk)
1082 size_t my_mix_end =
1083 (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
1084 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
1085 if (my_write_pos < my_mix_end)
1086 my_write_pos += pcmbuf_size;
1087 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1089 return 100;
1092 void pcmbuf_write_voice_complete(int count)
1094 /* A get-it-to-work-for-now hack (audio status could have changed) */
1095 if (!(audio_status() & AUDIO_STATUS_PLAY))
1097 pcmbuf_write_complete(count);
1098 return;
1101 int16_t *ibuf = (int16_t *)voicebuf;
1102 int16_t *obuf;
1103 size_t chunk_samples;
1105 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
1107 pcmbuf_mix_chunk = pcmbuf_read->link;
1108 /* Start 1/8s into the next chunk */
1109 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
1112 if (!pcmbuf_mix_chunk)
1113 return;
1115 obuf = (int16_t *)pcmbuf_mix_chunk->addr;
1116 chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
1118 count <<= 1;
1120 while (count-- > 0)
1122 int32_t sample = *ibuf++;
1124 if (pcmbuf_mix_sample >= chunk_samples)
1126 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
1127 if (!pcmbuf_mix_chunk)
1128 return;
1129 pcmbuf_mix_sample = 0;
1130 obuf = pcmbuf_mix_chunk->addr;
1131 chunk_samples = pcmbuf_mix_chunk->size / 2;
1133 sample += obuf[pcmbuf_mix_sample] >> 2;
1134 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1138 void pcmbuf_crossfade_enable(bool on_off)
1140 /* Next setting to be used, not applied now */
1141 crossfade_enabled_pending = on_off;
1144 void pcmbuf_crossfade_enable_finished(void)
1146 /* Copy the pending setting over now */
1147 crossfade_enabled = crossfade_enabled_pending;
1148 pcmbuf_set_watermark_bytes();
1151 bool pcmbuf_is_crossfade_enabled(void)
1153 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
1154 return global_settings.playlist_shuffle;
1156 return crossfade_enabled;