Fix a few potential redraw problems with the custom statusbar and wps fighting for...
[kugel-rb.git] / apps / pcmbuf.c
blob319e3e80444e4f71bf4d12379183c446fbb2a82b
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 #ifdef HAVE_CROSSFADE
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;
100 #endif
102 static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
103 static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
104 static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
105 static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
106 static size_t last_chunksize IDATA_ATTR;
108 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
109 static size_t pcmbuf_watermark IDATA_ATTR;
111 static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
112 static size_t pcmbuf_mix_sample IDATA_ATTR;
114 static bool low_latency_mode = false;
115 static bool pcmbuf_flush;
117 #ifdef HAVE_PRIORITY_SCHEDULING
118 static int codec_thread_priority = PRIORITY_PLAYBACK;
119 #endif
121 extern unsigned int codec_thread_id;
123 /* Helpful macros for use in conditionals this assumes some of the above
124 * static variable names */
125 #define NEED_FLUSH(position) \
126 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
127 #define LOW_DATA(quarter_secs) \
128 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
130 static bool prepare_insert(size_t length);
131 static void pcmbuf_under_watermark(bool under);
132 static bool pcmbuf_flush_fillpos(void);
134 #define CALL_IF_EXISTS(function, args...) if (function) function(args)
135 /* This function has 2 major logical parts (separated by brackets both for
136 * readability and variable scoping). The first part performs the
137 * operastions related to finishing off the last buffer we fed to the DMA.
138 * The second part performs the operations involved in sending a new buffer
139 * to the DMA. Finally the function checks the status of the buffer and
140 * boosts if necessary */
141 static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
142 static void pcmbuf_callback(unsigned char** start, size_t* size)
145 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
146 /* Take the finished buffer out of circulation */
147 pcmbuf_read = pcmbuf_current->link;
149 /* The buffer is finished, call the callback functions */
150 CALL_IF_EXISTS(position_callback, last_chunksize);
151 CALL_IF_EXISTS(pcmbuf_current->callback);
153 /* Put the finished buffer back into circulation */
154 pcmbuf_write_end->link = pcmbuf_current;
155 pcmbuf_write_end = pcmbuf_current;
157 /* If we've read over the mix chunk while it's still mixing there */
158 if (pcmbuf_current == pcmbuf_mix_chunk)
159 pcmbuf_mix_chunk = NULL;
160 /* If we've read over the crossfade chunk while it's still fading */
161 if (pcmbuf_current == crossfade_chunk)
162 crossfade_chunk = pcmbuf_read;
166 /* Send the new buffer to the pcm */
167 struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
168 size_t *realsize = size;
169 unsigned char** realstart = start;
170 if(pcmbuf_new)
172 size_t current_size = pcmbuf_new->size;
174 pcmbuf_unplayed_bytes -= current_size;
175 last_chunksize = current_size;
176 *realsize = current_size;
177 *realstart = pcmbuf_new->addr;
179 else
181 /* No more buffers */
182 last_chunksize = 0;
183 *realsize = 0;
184 *realstart = NULL;
185 CALL_IF_EXISTS(pcmbuf_event_handler);
190 void pcmbuf_set_position_callback(void (*callback)(size_t size))
192 position_callback = callback;
195 static void pcmbuf_set_watermark_bytes(void)
197 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
198 /* If crossfading, try to keep the buffer full other than 1 second */
199 (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
200 /* Otherwise, just use the default */
201 PCMBUF_WATERMARK;
204 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
205 * in a separate function for the moment */
206 static inline void pcmbuf_add_chunk(void)
208 register size_t size = audiobuffer_fillpos;
209 /* Grab the next description to write, and change the write pointer */
210 register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
211 pcmbuf_write = pcmbuf_current->link;
212 /* Fill in the values in the new buffer chunk */
213 pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
214 pcmbuf_current->size = size;
215 pcmbuf_current->callback = pcmbuf_event_handler;
216 pcmbuf_current->link = NULL;
217 /* This is single use only */
218 pcmbuf_event_handler = NULL;
219 if (pcmbuf_read != NULL) {
220 if (pcmbuf_flush)
222 pcmbuf_write_end->link = pcmbuf_read->link;
223 pcmbuf_read->link = pcmbuf_current;
224 while (pcmbuf_write_end->link)
226 pcmbuf_write_end = pcmbuf_write_end->link;
227 pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
229 pcmbuf_flush = false;
231 /* If there is already a read buffer setup, add to it */
232 else
233 pcmbuf_read_end->link = pcmbuf_current;
234 } else {
235 /* Otherwise create the buffer */
236 pcmbuf_read = pcmbuf_current;
238 /* This is now the last buffer to read */
239 pcmbuf_read_end = pcmbuf_current;
241 /* Update bytes counters */
242 pcmbuf_unplayed_bytes += size;
244 audiobuffer_pos += size;
245 if (audiobuffer_pos >= pcmbuf_size)
246 audiobuffer_pos -= pcmbuf_size;
248 audiobuffer_fillpos = 0;
251 #ifdef HAVE_PRIORITY_SCHEDULING
252 static void boost_codec_thread(bool boost)
254 /* Keep voice and codec threads at the same priority or else voice
255 * will starve if the codec thread's priority is boosted. */
256 if (boost)
258 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
259 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
261 if (priority != codec_thread_priority)
263 codec_thread_priority = priority;
264 thread_set_priority(codec_thread_id, priority);
265 voice_thread_set_priority(priority);
268 else if (codec_thread_priority != PRIORITY_PLAYBACK)
270 thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
271 voice_thread_set_priority(PRIORITY_PLAYBACK);
272 codec_thread_priority = PRIORITY_PLAYBACK;
275 #endif /* HAVE_PRIORITY_SCHEDULING */
277 static void pcmbuf_under_watermark(bool under)
279 /* Only codec thread initiates boost - voice boosts the cpu when playing
280 a clip */
281 #ifndef SIMULATOR
282 if (thread_get_current() == codec_thread_id)
283 #endif /* SIMULATOR */
285 if (under)
287 /* Fill audio buffer by boosting cpu */
288 trigger_cpu_boost();
289 #ifdef HAVE_PRIORITY_SCHEDULING
290 /* If buffer is critically low, override UI priority, else
291 set back to the original priority. */
292 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
293 #endif
295 else
297 #ifdef HAVE_PRIORITY_SCHEDULING
298 boost_codec_thread(false);
299 #endif
303 /* Disable crossfade if < .5s of audio */
304 if (LOW_DATA(2))
306 crossfade_active = false;
310 void pcmbuf_set_event_handler(void (*event_handler)(void))
312 pcmbuf_event_handler = event_handler;
315 unsigned int pcmbuf_get_latency(void)
317 /* Be careful how this calculation is rearranged, it's easy to overflow */
318 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
319 return bytes / 4 / (NATIVE_FREQUENCY/1000);
322 void pcmbuf_set_low_latency(bool state)
324 low_latency_mode = state;
327 bool pcmbuf_is_lowdata(void)
329 if (!pcm_is_playing() || pcm_is_paused() ||
330 crossfade_init || crossfade_active)
331 return false;
333 #if MEMORYSIZE > 2
334 /* 1 seconds of buffer is low data */
335 return LOW_DATA(4);
336 #else
337 /* under watermark is low data */
338 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
339 #endif
342 /* Amount of bytes left in the buffer. */
343 inline size_t pcmbuf_free(void)
345 if (pcmbuf_read != NULL)
347 void *read = pcmbuf_read->addr;
348 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
349 if (read < write)
350 return (size_t)(read - write) + pcmbuf_size;
351 else
352 return (size_t) (read - write);
354 return pcmbuf_size;
357 bool pcmbuf_crossfade_init(bool manual_skip)
359 /* Can't do two crossfades at once and, no fade if pcm is off now */
360 if (crossfade_init || crossfade_active || !pcm_is_playing())
362 pcmbuf_play_stop();
363 return false;
366 trigger_cpu_boost();
368 /* Not enough data, or crossfade disabled, flush the old data instead */
369 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
371 pcmbuf_flush_fillpos();
372 pcmbuf_flush = true;
373 return false;
376 /* Don't enable mix mode when skipping tracks manually. */
377 if (manual_skip)
378 crossfade_mixmode = false;
379 else
380 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
382 crossfade_init = true;
384 return true;
388 void pcmbuf_play_stop(void)
390 pcm_play_stop();
392 pcmbuf_unplayed_bytes = 0;
393 pcmbuf_mix_chunk = NULL;
394 if (pcmbuf_read) {
395 pcmbuf_write_end->link = pcmbuf_read;
396 pcmbuf_write_end = pcmbuf_read_end;
397 pcmbuf_read = pcmbuf_read_end = NULL;
399 audiobuffer_pos = 0;
400 audiobuffer_fillpos = 0;
401 crossfade_init = false;
402 crossfade_active = false;
403 pcmbuf_flush = false;
405 #ifdef HAVE_PRIORITY_SCHEDULING
406 /* Can unboost the codec thread here no matter who's calling */
407 boost_codec_thread(false);
408 #endif
411 int pcmbuf_used_descs(void)
413 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
414 unsigned int i = 0;
415 while (pcmbuf_temp) {
416 pcmbuf_temp = pcmbuf_temp->link;
417 i++;
419 return i;
422 int pcmbuf_descs(void)
424 return PCMBUF_DESCS(pcmbuf_size);
427 static void pcmbuf_init_pcmbuffers(void)
429 struct pcmbufdesc *next = pcmbuf_write;
430 next++;
431 pcmbuf_write_end = pcmbuf_write;
432 while ((void *)next < (void *)pcmbuf_bufend) {
433 pcmbuf_write_end->link=next;
434 pcmbuf_write_end=next;
435 next++;
439 static size_t pcmbuf_get_next_required_pcmbuf_size(void)
441 size_t seconds = 1;
443 if (crossfade_enabled_pending)
444 seconds += global_settings.crossfade_fade_out_delay
445 + global_settings.crossfade_fade_out_duration;
447 #if MEMORYSIZE > 2
448 /* Buffer has to be at least 2s long. */
449 seconds += 2;
450 #endif
451 logf("pcmbuf len: %ld", (long)seconds);
452 return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
455 static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
457 return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
458 PCMBUF_DESCS_SIZE(bufsize));
461 bool pcmbuf_is_same_size(void)
463 if (audiobuffer == NULL)
464 return true; /* Not set up yet even once so always */
466 size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
467 return pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
470 /* Initialize the pcmbuffer the structure looks like this:
471 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
472 size_t pcmbuf_init(unsigned char *bufend)
474 pcmbuf_bufend = bufend;
475 pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
476 audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
477 fadebuf = &audiobuffer[pcmbuf_size];
478 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
479 pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
481 pcmbuf_init_pcmbuffers();
483 position_callback = NULL;
484 pcmbuf_event_handler = NULL;
486 pcmbuf_crossfade_enable_finished();
488 pcmbuf_play_stop();
490 return pcmbuf_bufend - audiobuffer;
493 size_t pcmbuf_get_bufsize(void)
495 return pcmbuf_size;
498 #ifdef ROCKBOX_HAS_LOGF
499 unsigned char * pcmbuf_get_meminfo(size_t *length)
501 *length = pcmbuf_bufend - audiobuffer;
502 return audiobuffer;
504 #endif
506 void pcmbuf_pause(bool pause)
508 if (pcm_is_playing())
509 pcm_play_pause(!pause);
510 else if (!pause)
511 pcmbuf_play_start();
514 /* Force playback. */
515 void pcmbuf_play_start(void)
517 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
519 last_chunksize = pcmbuf_read->size;
520 pcmbuf_unplayed_bytes -= last_chunksize;
521 pcm_play_data(pcmbuf_callback,
522 (unsigned char *)pcmbuf_read->addr, last_chunksize);
527 * Commit samples waiting to the pcm buffer.
529 static bool pcmbuf_flush_fillpos(void)
531 if (audiobuffer_fillpos) {
532 /* Never use the last buffer descriptor */
533 while (pcmbuf_write == pcmbuf_write_end) {
534 /* If this happens, something is being stupid */
535 if (!pcm_is_playing()) {
536 logf("pcmbuf_flush_fillpos error");
537 pcmbuf_play_start();
539 /* Let approximately one chunk of data playback */
540 sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
542 pcmbuf_add_chunk();
543 return true;
545 return false;
548 /**
549 * Low memory targets don't have crossfade, so don't compile crossfade
550 * specific code in order to save some memory. */
552 #ifdef HAVE_CROSSFADE
554 * Completely process the crossfade fade out effect with current pcm buffer.
556 static void crossfade_process_buffer(size_t fade_in_delay,
557 size_t fade_out_delay, size_t fade_out_rem)
559 if (!crossfade_mixmode)
561 /* Fade out the specified amount of the already processed audio */
562 size_t total_fade_out = fade_out_rem;
563 size_t fade_out_sample;
564 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
566 /* Find the right chunk to start fading out */
567 fade_out_delay += crossfade_sample * 2;
568 while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
570 fade_out_delay -= fade_out_chunk->size;
571 fade_out_chunk = fade_out_chunk->link;
573 /* The start sample within the chunk */
574 fade_out_sample = fade_out_delay / 2;
576 while (fade_out_rem > 0)
578 /* Each 1/10 second of audio will have the same fade applied */
579 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
580 int factor = (fade_out_rem << 8) / total_fade_out;
582 fade_out_rem -= block_rem;
584 /* Fade this block */
585 while (block_rem > 0 && fade_out_chunk != NULL)
587 /* Fade one sample */
588 int16_t *buf = (int16_t *)fade_out_chunk->addr;
589 int32_t sample = buf[fade_out_sample];
590 buf[fade_out_sample++] = (sample * factor) >> 8;
592 block_rem -= 2;
593 /* Move to the next chunk as needed */
594 if (fade_out_sample * 2 >= fade_out_chunk->size)
596 fade_out_chunk = fade_out_chunk->link;
597 fade_out_sample = 0;
603 /* Find the right chunk and sample to start fading in */
604 fade_in_delay += crossfade_sample * 2;
605 while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
607 fade_in_delay -= crossfade_chunk->size;
608 crossfade_chunk = crossfade_chunk->link;
610 crossfade_sample = fade_in_delay / 2;
611 logf("process done!");
614 /* Initializes crossfader, calculates all necessary parameters and
615 * performs fade-out with the pcm buffer. */
616 static void crossfade_start(void)
618 size_t crossfade_rem;
619 size_t crossfade_need;
620 size_t fade_out_rem;
621 size_t fade_out_delay;
622 size_t fade_in_delay;
624 crossfade_init = false;
625 /* Reject crossfade if less than .5s of data */
626 if (LOW_DATA(2)) {
627 logf("crossfade rejected");
628 pcmbuf_play_stop();
629 return ;
632 logf("crossfade_start");
633 pcmbuf_flush_fillpos();
634 crossfade_active = true;
636 /* Initialize the crossfade buffer size to all of the buffered data that
637 * has not yet been sent to the DMA */
638 crossfade_rem = pcmbuf_unplayed_bytes;
639 crossfade_chunk = pcmbuf_read->link;
640 crossfade_sample = 0;
642 /* Get fade out delay from settings. */
643 fade_out_delay =
644 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
646 /* Get fade out duration from settings. */
647 fade_out_rem =
648 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
650 crossfade_need = fade_out_delay + fade_out_rem;
651 /* We want only to modify the last part of the buffer. */
652 if (crossfade_rem > crossfade_need)
654 size_t crossfade_extra = crossfade_rem - crossfade_need;
655 while (crossfade_extra > crossfade_chunk->size)
657 crossfade_extra -= crossfade_chunk->size;
658 crossfade_chunk = crossfade_chunk->link;
660 crossfade_sample = crossfade_extra / 2;
662 /* Truncate fade out duration if necessary. */
663 else if (crossfade_rem < crossfade_need)
665 size_t crossfade_short = crossfade_need - crossfade_rem;
666 if (fade_out_rem >= crossfade_short)
667 fade_out_rem -= crossfade_short;
668 else
670 fade_out_delay -= crossfade_short - fade_out_rem;
671 fade_out_rem = 0;
675 /* Get also fade in duration and delays from settings. */
676 crossfade_fade_in_total =
677 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
678 crossfade_fade_in_rem = crossfade_fade_in_total;
680 fade_in_delay =
681 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
683 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
686 /* Returns the number of bytes _NOT_ mixed */
687 static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem)
689 const int16_t *input_buf = (const int16_t *)buf;
690 int16_t *output_buf = (int16_t *)(crossfade_chunk->addr);
691 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
692 output_buf = &output_buf[crossfade_sample];
693 int32_t sample;
695 while (fade_rem)
697 /* fade left and right channel at once to keep buffer alignment */
698 sample = *input_buf++;
699 sample = ((sample * factor) >> 8) + *output_buf;
700 *output_buf++ = clip_sample_16(sample);
702 sample = *input_buf++;
703 sample = ((sample * factor) >> 8) + *output_buf;
704 *output_buf++ = clip_sample_16(sample);
706 fade_rem -= 4; /* 2 samples, each 16 bit -> 4 bytes */
708 if (output_buf >= chunk_end)
710 crossfade_chunk = crossfade_chunk->link;
711 if (!crossfade_chunk)
712 return fade_rem;
713 output_buf = (int16_t *)crossfade_chunk->addr;
714 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
717 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
718 return 0;
721 /* Returns the number of bytes _NOT_ mixed */
722 static size_t crossfade_mix(const char *buf, size_t length)
724 const int16_t *input_buf = (const int16_t *)buf;
725 int16_t *output_buf = (int16_t *)crossfade_chunk->addr;
726 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
727 output_buf = &output_buf[crossfade_sample];
728 int32_t sample;
730 while (length)
732 /* fade left and right channel at once to keep buffer alignment */
733 sample = *input_buf++ + *output_buf;
734 *output_buf++ = clip_sample_16(sample);
736 sample = *input_buf++ + *output_buf;
737 *output_buf++ = clip_sample_16(sample);
739 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
741 if (output_buf >= chunk_end)
743 crossfade_chunk = crossfade_chunk->link;
744 if (!crossfade_chunk)
745 return length;
747 output_buf = (int16_t *)crossfade_chunk->addr;
748 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
751 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
752 return 0;
755 static void pcmbuf_flush_buffer(const char *buf, size_t length)
757 size_t copy_n;
758 while (length > 0) {
759 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
760 if (NEED_FLUSH(audiobuffer_index))
762 pcmbuf_flush_fillpos();
763 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
765 copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
766 memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
767 buf += copy_n;
768 audiobuffer_fillpos += copy_n;
769 length -= copy_n;
773 static void flush_crossfade(char *buf, size_t length)
775 if (length)
777 if (crossfade_fade_in_rem)
779 size_t samples;
780 int16_t *input_buf;
782 /* Fade factor for this packet */
783 int factor =
784 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
785 crossfade_fade_in_total;
786 /* Bytes to fade */
787 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
789 /* We _will_ fade this many bytes */
790 crossfade_fade_in_rem -= fade_rem;
792 if (crossfade_chunk)
794 /* Mix the data */
795 size_t fade_total = fade_rem;
796 fade_rem = crossfade_fade_mix(factor, buf, fade_rem);
797 length -= fade_total - fade_rem;
798 buf += fade_total - fade_rem;
799 if (!length)
800 return;
801 if (!fade_rem)
802 goto fade_done;
805 samples = fade_rem / 2;
806 input_buf = (int16_t *)buf;
807 /* Fade remaining samples in place */
808 while (samples)
810 int32_t sample = *input_buf;
811 *input_buf++ = (sample * factor) >> 8;
812 samples--;
816 fade_done:
817 if (crossfade_chunk)
819 /* Mix the data */
820 size_t mix_total = length;
821 length = crossfade_mix(buf, length);
822 buf += mix_total - length;
823 if (!length)
824 return;
827 /* Flush samples to the buffer */
828 while (!prepare_insert(length))
829 sleep(1);
830 pcmbuf_flush_buffer(buf, length);
834 #endif
836 static bool prepare_insert(size_t length)
838 if (low_latency_mode)
840 /* 1/4s latency. */
841 if (!LOW_DATA(1) && pcm_is_playing())
842 return false;
845 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
846 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
847 return false;
849 if (!pcm_is_playing())
851 trigger_cpu_boost();
853 /* Pre-buffer up to watermark */
854 #if MEMORYSIZE > 2
855 if (!LOW_DATA(4))
856 #else
857 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
858 #endif
860 logf("pcm starting");
861 if (!(audio_status() & AUDIO_STATUS_PAUSE))
862 pcmbuf_play_start();
865 else
866 pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
868 return true;
871 void* pcmbuf_request_buffer(int *count)
873 #ifdef HAVE_CROSSFADE
874 if (crossfade_init)
875 crossfade_start();
876 #endif
878 if (crossfade_active) {
879 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
880 return fadebuf;
882 else
884 if(prepare_insert(*count << 2))
886 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
887 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
889 /* Usual case, there's space here */
890 return &audiobuffer[audiobuffer_index];
892 else
894 /* Flush and wrap the buffer */
895 pcmbuf_flush_fillpos();
896 audiobuffer_pos = 0;
897 return &audiobuffer[0];
900 else
902 return NULL;
907 void * pcmbuf_request_voice_buffer(int *count)
909 /* A get-it-to-work-for-now hack (audio status could change by
910 completion) */
911 if (audio_status() & AUDIO_STATUS_PLAY)
913 if (pcmbuf_read == NULL)
915 return NULL;
917 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
918 (pcmbuf_mix_chunk || pcmbuf_read->link))
920 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
921 return voicebuf;
923 else
925 return NULL;
928 else
930 return pcmbuf_request_buffer(count);
934 bool pcmbuf_is_crossfade_active(void)
936 return crossfade_active || crossfade_init;
939 void pcmbuf_write_complete(int count)
941 size_t length = (size_t)(unsigned int)count << 2;
942 #ifdef HAVE_CROSSFADE
943 if (crossfade_active)
945 flush_crossfade(fadebuf, length);
946 if (!(crossfade_fade_in_rem || crossfade_chunk))
947 crossfade_active = false;
949 else
950 #endif
952 audiobuffer_fillpos += length;
954 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
955 pcmbuf_flush_fillpos();
959 #if 0
960 bool pcmbuf_insert_buffer(char *buf, int count)
962 size_t length = (size_t)(unsigned int)count << 2;
964 if (crossfade_active)
966 flush_crossfade(buf, length);
967 if (!(crossfade_fade_in_rem || crossfade_chunk))
968 crossfade_active = false;
970 else
972 if (!prepare_insert(length))
973 return false;
974 pcmbuf_flush_buffer(buf, length);
976 return true;
978 #endif
980 #ifndef HAVE_HARDWARE_BEEP
981 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
982 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
984 /* Generates a constant square wave sound with a given frequency
985 in Hertz for a duration in milliseconds. */
986 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
988 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
989 int32_t phase = 0;
990 int16_t *bufptr, *bufstart, *bufend;
991 int32_t sample;
992 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
993 bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
994 int i;
996 bufend = SKIPBYTES((int16_t *)audiobuffer, pcmbuf_size);
998 /* Find the insertion point and set bufstart to the start of it */
999 if (mix)
1001 /* Get the currently playing chunk at the current position. */
1002 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
1004 /* If above isn't implemented or pcm is stopped, no beepeth. */
1005 if (!bufstart || !pcm_is_playing())
1006 return;
1008 /* Give 5ms clearance. */
1009 bufstart += NATIVE_FREQUENCY * 4 / 200;
1011 #ifdef HAVE_PCM_DMA_ADDRESS
1012 /* Returned peak addresses are DMA addresses */
1013 bufend = pcm_dma_addr(bufend);
1014 #endif
1016 /* Wrapped above? */
1017 if (bufstart >= bufend)
1018 bufstart -= pcmbuf_size;
1020 /* NOTE: On some targets using hardware DMA, cache range flushing may
1021 * be required or the writes may not be picked up by the controller.
1022 * An incremental flush should be done periodically during the mixdown. */
1024 else if (nsamples <= MINIBUF_SAMPLES)
1026 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
1027 /* Use mini buffer */
1028 bufstart = minibuf;
1029 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1031 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED)
1033 /* Use audiobuffer */
1034 bufstart = (int16_t *)audiobuffer;
1036 else
1038 /* No place */
1039 return;
1042 bufptr = bufstart;
1044 /* Mix square wave into buffer */
1045 for (i = 0; i < nsamples; ++i)
1047 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
1048 sample = mix ? *bufptr : 0;
1049 *bufptr++ = clip_sample_16(sample + amp);
1050 if (bufptr >= bufend)
1051 bufptr = (int16_t *)audiobuffer;
1052 sample = mix ? *bufptr : 0;
1053 *bufptr++ = clip_sample_16(sample + amp);
1054 if (bufptr >= bufend)
1055 bufptr = (int16_t *)audiobuffer;
1057 phase += step;
1060 pcm_play_lock();
1061 #ifdef HAVE_RECORDING
1062 pcm_rec_lock();
1063 #endif
1065 /* Kick off playback if required and it won't interfere */
1066 if (!pcm_is_playing()
1067 #ifdef HAVE_RECORDING
1068 && !pcm_is_recording()
1069 #endif
1072 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
1075 pcm_play_unlock();
1076 #ifdef HAVE_RECORDING
1077 pcm_rec_unlock();
1078 #endif
1080 #endif /* HAVE_HARDWARE_BEEP */
1082 /* Returns pcm buffer usage in percents (0 to 100). */
1083 int pcmbuf_usage(void)
1085 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1088 int pcmbuf_mix_free(void)
1090 if (pcmbuf_mix_chunk)
1092 size_t my_mix_end =
1093 (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
1094 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
1095 if (my_write_pos < my_mix_end)
1096 my_write_pos += pcmbuf_size;
1097 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1099 return 100;
1102 void pcmbuf_write_voice_complete(int count)
1104 /* A get-it-to-work-for-now hack (audio status could have changed) */
1105 if (!(audio_status() & AUDIO_STATUS_PLAY))
1107 pcmbuf_write_complete(count);
1108 return;
1111 int16_t *ibuf = (int16_t *)voicebuf;
1112 int16_t *obuf;
1113 size_t chunk_samples;
1115 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
1117 pcmbuf_mix_chunk = pcmbuf_read->link;
1118 /* Start 1/8s into the next chunk */
1119 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
1122 if (!pcmbuf_mix_chunk)
1123 return;
1125 obuf = (int16_t *)pcmbuf_mix_chunk->addr;
1126 chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
1128 count <<= 1;
1130 while (count-- > 0)
1132 int32_t sample = *ibuf++;
1134 if (pcmbuf_mix_sample >= chunk_samples)
1136 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
1137 if (!pcmbuf_mix_chunk)
1138 return;
1139 pcmbuf_mix_sample = 0;
1140 obuf = pcmbuf_mix_chunk->addr;
1141 chunk_samples = pcmbuf_mix_chunk->size / 2;
1143 sample += obuf[pcmbuf_mix_sample] >> 2;
1144 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1148 void pcmbuf_crossfade_enable(bool on_off)
1150 /* Next setting to be used, not applied now */
1151 crossfade_enabled_pending = on_off;
1154 void pcmbuf_crossfade_enable_finished(void)
1156 /* Copy the pending setting over now */
1157 crossfade_enabled = crossfade_enabled_pending;
1158 pcmbuf_set_watermark_bytes();
1161 bool pcmbuf_is_crossfade_enabled(void)
1163 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
1164 return global_settings.playlist_shuffle;
1166 return crossfade_enabled;
1169 /** PLAY LAST REMAINING SAMPLES AT END OF PLAYBACK
1170 * Commit any remaining samples in the PCM buffer for playback. */
1171 void pcmbuf_play_remainder(void)
1173 if (audiobuffer_fillpos)
1174 pcmbuf_flush_fillpos();