Fix manual building for iPod 1G/2G/3G - scrollwheel is not defined for these in addit...
[maemo-rb.git] / apps / pcmbuf.c
blob7d2c579e4291d8d2fa205715e7424999abd20acf
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 ****************************************************************************/
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include "config.h"
25 #include "debug.h"
26 #include "panic.h"
27 #include <kernel.h>
28 #include "pcmbuf.h"
29 #include "pcm.h"
30 #include "logf.h"
31 #ifndef SIMULATOR
32 #include "cpu.h"
33 #endif
34 #include "system.h"
35 #include <string.h>
36 #include "buffer.h"
37 #include "settings.h"
38 #include "audio.h"
39 #include "voice_thread.h"
40 #include "dsp.h"
41 #include "thread.h"
43 /* Clip sample to signed 16 bit range */
44 static inline int32_t clip_sample_16(int32_t sample)
46 if ((int16_t)sample != sample)
47 sample = 0x7fff ^ (sample >> 31);
48 return sample;
51 #if MEMORYSIZE > 2
52 /* Keep watermark high for iPods at least (2s) */
53 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
54 #else
55 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
56 #endif
58 /* Structure we can use to queue pcm chunks in memory to be played
59 * by the driver code. */
60 struct pcmbufdesc
62 void *addr;
63 size_t size;
64 struct pcmbufdesc* link;
65 /* Call this when the buffer has been played */
66 void (*callback)(void);
69 #define PCMBUF_DESCS(bufsize) \
70 ((bufsize) / PCMBUF_MINAVG_CHUNK)
71 #define PCMBUF_DESCS_SIZE(bufsize) \
72 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
74 /* Size of the PCM buffer. */
75 static size_t pcmbuf_size IDATA_ATTR = 0;
76 static char *pcmbuf_bufend IDATA_ATTR;
77 static char *audiobuffer IDATA_ATTR;
78 /* Current audio buffer write index. */
79 static size_t audiobuffer_pos IDATA_ATTR;
80 /* Amount audiobuffer_pos will be increased.*/
81 static size_t audiobuffer_fillpos IDATA_ATTR;
82 static char *fadebuf IDATA_ATTR;
83 static char *voicebuf IDATA_ATTR;
85 static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
86 static void (*position_callback)(size_t size) IDATA_ATTR;
88 /* Crossfade related state */
89 static bool crossfade_enabled;
90 static bool crossfade_enabled_pending;
91 static bool crossfade_mixmode;
92 static bool crossfade_active IDATA_ATTR;
93 static bool crossfade_init IDATA_ATTR;
95 /* Track the current location for processing crossfade */
96 static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
97 static size_t crossfade_sample IDATA_ATTR;
99 /* Counters for fading in new data */
100 static size_t crossfade_fade_in_total IDATA_ATTR;
101 static size_t crossfade_fade_in_rem IDATA_ATTR;
103 static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
104 static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
105 static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
106 static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
107 static size_t last_chunksize IDATA_ATTR;
109 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
110 static size_t pcmbuf_watermark IDATA_ATTR;
112 static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
113 static size_t pcmbuf_mix_sample IDATA_ATTR;
115 static bool low_latency_mode = false;
116 static bool pcmbuf_flush;
118 #ifdef HAVE_PRIORITY_SCHEDULING
119 static int codec_thread_priority = PRIORITY_PLAYBACK;
120 #endif
122 extern unsigned int codec_thread_id;
124 /* Helpful macros for use in conditionals this assumes some of the above
125 * static variable names */
126 #define NEED_FLUSH(position) \
127 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
128 #define LOW_DATA(quarter_secs) \
129 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
131 static bool prepare_insert(size_t length);
132 static void pcmbuf_under_watermark(bool under);
133 static bool pcmbuf_flush_fillpos(void);
135 #define CALL_IF_EXISTS(function, args...) if (function) function(args)
136 /* This function has 2 major logical parts (separated by brackets both for
137 * readability and variable scoping). The first part performs the
138 * operastions related to finishing off the last buffer we fed to the DMA.
139 * The second part performs the operations involved in sending a new buffer
140 * to the DMA. Finally the function checks the status of the buffer and
141 * boosts if necessary */
142 static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
143 static void pcmbuf_callback(unsigned char** start, size_t* size)
146 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
147 /* Take the finished buffer out of circulation */
148 pcmbuf_read = pcmbuf_current->link;
150 /* The buffer is finished, call the callback functions */
151 CALL_IF_EXISTS(position_callback, last_chunksize);
152 CALL_IF_EXISTS(pcmbuf_current->callback);
154 /* Put the finished buffer back into circulation */
155 pcmbuf_write_end->link = pcmbuf_current;
156 pcmbuf_write_end = pcmbuf_current;
158 /* If we've read over the mix chunk while it's still mixing there */
159 if (pcmbuf_current == pcmbuf_mix_chunk)
160 pcmbuf_mix_chunk = NULL;
161 /* If we've read over the crossfade chunk while it's still fading */
162 if (pcmbuf_current == crossfade_chunk)
163 crossfade_chunk = pcmbuf_read;
167 /* Send the new buffer to the pcm */
168 struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
169 size_t *realsize = size;
170 unsigned char** realstart = start;
171 if(pcmbuf_new)
173 size_t current_size = pcmbuf_new->size;
175 pcmbuf_unplayed_bytes -= current_size;
176 last_chunksize = current_size;
177 *realsize = current_size;
178 *realstart = pcmbuf_new->addr;
180 else
182 /* No more buffers */
183 last_chunksize = 0;
184 *realsize = 0;
185 *realstart = NULL;
186 CALL_IF_EXISTS(pcmbuf_event_handler);
191 void pcmbuf_set_position_callback(void (*callback)(size_t size))
193 position_callback = callback;
196 static void pcmbuf_set_watermark_bytes(void)
198 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
199 /* If crossfading, try to keep the buffer full other than 1 second */
200 (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
201 /* Otherwise, just use the default */
202 PCMBUF_WATERMARK;
205 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
206 * in a separate function for the moment */
207 static inline void pcmbuf_add_chunk(void)
209 register size_t size = audiobuffer_fillpos;
210 /* Grab the next description to write, and change the write pointer */
211 register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
212 pcmbuf_write = pcmbuf_current->link;
213 /* Fill in the values in the new buffer chunk */
214 pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
215 pcmbuf_current->size = size;
216 pcmbuf_current->callback = pcmbuf_event_handler;
217 pcmbuf_current->link = NULL;
218 /* This is single use only */
219 pcmbuf_event_handler = NULL;
220 if (pcmbuf_read != NULL) {
221 if (pcmbuf_flush)
223 pcmbuf_write_end->link = pcmbuf_read->link;
224 pcmbuf_read->link = pcmbuf_current;
225 while (pcmbuf_write_end->link)
227 pcmbuf_write_end = pcmbuf_write_end->link;
228 pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
230 pcmbuf_flush = false;
232 /* If there is already a read buffer setup, add to it */
233 else
234 pcmbuf_read_end->link = pcmbuf_current;
235 } else {
236 /* Otherwise create the buffer */
237 pcmbuf_read = pcmbuf_current;
239 /* This is now the last buffer to read */
240 pcmbuf_read_end = pcmbuf_current;
242 /* Update bytes counters */
243 pcmbuf_unplayed_bytes += size;
245 audiobuffer_pos += size;
246 if (audiobuffer_pos >= pcmbuf_size)
247 audiobuffer_pos -= pcmbuf_size;
249 audiobuffer_fillpos = 0;
252 #ifdef HAVE_PRIORITY_SCHEDULING
253 static void boost_codec_thread(bool boost)
255 /* Keep voice and codec threads at the same priority or else voice
256 * will starve if the codec thread's priority is boosted. */
257 if (boost)
259 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
260 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
262 if (priority != codec_thread_priority)
264 codec_thread_priority = priority;
265 thread_set_priority(codec_thread_id, priority);
266 voice_thread_set_priority(priority);
269 else if (codec_thread_priority != PRIORITY_PLAYBACK)
271 thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
272 voice_thread_set_priority(PRIORITY_PLAYBACK);
273 codec_thread_priority = PRIORITY_PLAYBACK;
276 #endif /* HAVE_PRIORITY_SCHEDULING */
278 static void pcmbuf_under_watermark(bool under)
280 /* Only codec thread initiates boost - voice boosts the cpu when playing
281 a clip */
282 #ifndef SIMULATOR
283 if (thread_get_current() == codec_thread_id)
284 #endif /* SIMULATOR */
286 if (under)
288 /* Fill audio buffer by boosting cpu */
289 trigger_cpu_boost();
290 #ifdef HAVE_PRIORITY_SCHEDULING
291 /* If buffer is critically low, override UI priority, else
292 set back to the original priority. */
293 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
294 #endif
296 else
298 #ifdef HAVE_PRIORITY_SCHEDULING
299 boost_codec_thread(false);
300 #endif
304 /* Disable crossfade if < .5s of audio */
305 if (LOW_DATA(2))
307 crossfade_active = false;
311 void pcmbuf_set_event_handler(void (*event_handler)(void))
313 pcmbuf_event_handler = event_handler;
316 unsigned int pcmbuf_get_latency(void)
318 /* Be careful how this calculation is rearranged, it's easy to overflow */
319 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
320 return bytes / 4 / (NATIVE_FREQUENCY/1000);
323 void pcmbuf_set_low_latency(bool state)
325 low_latency_mode = state;
328 bool pcmbuf_is_lowdata(void)
330 if (!pcm_is_playing() || pcm_is_paused() ||
331 crossfade_init || crossfade_active)
332 return false;
334 #if MEMORYSIZE > 2
335 /* 1 seconds of buffer is low data */
336 return LOW_DATA(4);
337 #else
338 /* under watermark is low data */
339 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
340 #endif
343 /* Amount of bytes left in the buffer. */
344 inline size_t pcmbuf_free(void)
346 if (pcmbuf_read != NULL)
348 void *read = pcmbuf_read->addr;
349 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
350 if (read < write)
351 return (size_t)(read - write) + pcmbuf_size;
352 else
353 return (size_t) (read - write);
355 return pcmbuf_size;
358 bool pcmbuf_crossfade_init(bool manual_skip)
360 /* Can't do two crossfades at once and, no fade if pcm is off now */
361 if (crossfade_init || crossfade_active || !pcm_is_playing())
363 pcmbuf_play_stop();
364 return false;
367 trigger_cpu_boost();
369 /* Not enough data, or crossfade disabled, flush the old data instead */
370 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
372 pcmbuf_flush_fillpos();
373 pcmbuf_flush = true;
374 return false;
377 /* Don't enable mix mode when skipping tracks manually. */
378 if (manual_skip)
379 crossfade_mixmode = false;
380 else
381 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
383 crossfade_init = true;
385 return true;
389 void pcmbuf_play_stop(void)
391 pcm_play_stop();
393 pcmbuf_unplayed_bytes = 0;
394 pcmbuf_mix_chunk = NULL;
395 if (pcmbuf_read) {
396 pcmbuf_write_end->link = pcmbuf_read;
397 pcmbuf_write_end = pcmbuf_read_end;
398 pcmbuf_read = pcmbuf_read_end = NULL;
400 audiobuffer_pos = 0;
401 audiobuffer_fillpos = 0;
402 crossfade_init = false;
403 crossfade_active = false;
404 pcmbuf_flush = false;
406 #ifdef HAVE_PRIORITY_SCHEDULING
407 /* Can unboost the codec thread here no matter who's calling */
408 boost_codec_thread(false);
409 #endif
412 int pcmbuf_used_descs(void)
414 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
415 unsigned int i = 0;
416 while (pcmbuf_temp) {
417 pcmbuf_temp = pcmbuf_temp->link;
418 i++;
420 return i;
423 int pcmbuf_descs(void)
425 return PCMBUF_DESCS(pcmbuf_size);
428 static void pcmbuf_init_pcmbuffers(void)
430 struct pcmbufdesc *next = pcmbuf_write;
431 next++;
432 pcmbuf_write_end = pcmbuf_write;
433 while ((void *)next < (void *)pcmbuf_bufend) {
434 pcmbuf_write_end->link=next;
435 pcmbuf_write_end=next;
436 next++;
440 static size_t pcmbuf_get_next_required_pcmbuf_size(void)
442 size_t seconds = 1;
444 if (crossfade_enabled_pending)
445 seconds += global_settings.crossfade_fade_out_delay
446 + global_settings.crossfade_fade_out_duration;
448 #if MEMORYSIZE > 2
449 /* Buffer has to be at least 2s long. */
450 seconds += 2;
451 #endif
452 logf("pcmbuf len: %ld", seconds);
453 return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
456 static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
458 return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
459 PCMBUF_DESCS_SIZE(bufsize));
462 bool pcmbuf_is_same_size(void)
464 if (audiobuffer == NULL)
465 return true; /* Not set up yet even once so always */
467 size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
468 return pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
471 /* Initialize the pcmbuffer the structure looks like this:
472 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
473 size_t pcmbuf_init(unsigned char *bufend)
475 pcmbuf_bufend = bufend;
476 pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
477 audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
478 fadebuf = &audiobuffer[pcmbuf_size];
479 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
480 pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
482 pcmbuf_init_pcmbuffers();
484 position_callback = NULL;
485 pcmbuf_event_handler = NULL;
487 pcmbuf_crossfade_enable_finished();
489 pcmbuf_play_stop();
491 return pcmbuf_bufend - audiobuffer;
494 size_t pcmbuf_get_bufsize(void)
496 return pcmbuf_size;
499 #ifdef ROCKBOX_HAS_LOGF
500 unsigned char * pcmbuf_get_meminfo(size_t *length)
502 *length = pcmbuf_bufend - audiobuffer;
503 return audiobuffer;
505 #endif
507 void pcmbuf_pause(bool pause)
509 if (pcm_is_playing())
510 pcm_play_pause(!pause);
511 else if (!pause)
512 pcmbuf_play_start();
515 /* Force playback. */
516 void pcmbuf_play_start(void)
518 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
520 last_chunksize = pcmbuf_read->size;
521 pcmbuf_unplayed_bytes -= last_chunksize;
522 pcm_play_data(pcmbuf_callback,
523 (unsigned char *)pcmbuf_read->addr, last_chunksize);
528 * Commit samples waiting to the pcm buffer.
530 static bool pcmbuf_flush_fillpos(void)
532 if (audiobuffer_fillpos) {
533 /* Never use the last buffer descriptor */
534 while (pcmbuf_write == pcmbuf_write_end) {
535 /* If this happens, something is being stupid */
536 if (!pcm_is_playing()) {
537 logf("pcmbuf_flush_fillpos error");
538 pcmbuf_play_start();
540 /* Let approximately one chunk of data playback */
541 sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
543 pcmbuf_add_chunk();
544 return true;
546 return false;
550 * Completely process the crossfade fade out effect with current pcm buffer.
552 static void crossfade_process_buffer(size_t fade_in_delay,
553 size_t fade_out_delay, size_t fade_out_rem)
555 if (!crossfade_mixmode)
557 /* Fade out the specified amount of the already processed audio */
558 size_t total_fade_out = fade_out_rem;
559 size_t fade_out_sample;
560 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
562 /* Find the right chunk to start fading out */
563 fade_out_delay += crossfade_sample * 2;
564 while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
566 fade_out_delay -= fade_out_chunk->size;
567 fade_out_chunk = fade_out_chunk->link;
569 /* The start sample within the chunk */
570 fade_out_sample = fade_out_delay / 2;
572 while (fade_out_rem > 0)
574 /* Each 1/10 second of audio will have the same fade applied */
575 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
576 int factor = (fade_out_rem << 8) / total_fade_out;
578 fade_out_rem -= block_rem;
580 /* Fade this block */
581 while (block_rem > 0 && fade_out_chunk != NULL)
583 /* Fade one sample */
584 int16_t *buf = (int16_t *)fade_out_chunk->addr;
585 int32_t sample = buf[fade_out_sample];
586 buf[fade_out_sample++] = (sample * factor) >> 8;
588 block_rem -= 2;
589 /* Move to the next chunk as needed */
590 if (fade_out_sample * 2 >= fade_out_chunk->size)
592 fade_out_chunk = fade_out_chunk->link;
593 fade_out_sample = 0;
599 /* Find the right chunk and sample to start fading in */
600 fade_in_delay += crossfade_sample * 2;
601 while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
603 fade_in_delay -= crossfade_chunk->size;
604 crossfade_chunk = crossfade_chunk->link;
606 crossfade_sample = fade_in_delay / 2;
607 logf("process done!");
610 /* Initializes crossfader, calculates all necessary parameters and
611 * performs fade-out with the pcm buffer. */
612 static void crossfade_start(void)
614 size_t crossfade_rem;
615 size_t crossfade_need;
616 size_t fade_out_rem;
617 size_t fade_out_delay;
618 size_t fade_in_delay;
620 crossfade_init = false;
621 /* Reject crossfade if less than .5s of data */
622 if (LOW_DATA(2)) {
623 logf("crossfade rejected");
624 pcmbuf_play_stop();
625 return ;
628 logf("crossfade_start");
629 pcmbuf_flush_fillpos();
630 crossfade_active = true;
632 /* Initialize the crossfade buffer size to all of the buffered data that
633 * has not yet been sent to the DMA */
634 crossfade_rem = pcmbuf_unplayed_bytes;
635 crossfade_chunk = pcmbuf_read->link;
636 crossfade_sample = 0;
638 /* Get fade out delay from settings. */
639 fade_out_delay =
640 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
642 /* Get fade out duration from settings. */
643 fade_out_rem =
644 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
646 crossfade_need = fade_out_delay + fade_out_rem;
647 /* We want only to modify the last part of the buffer. */
648 if (crossfade_rem > crossfade_need)
650 size_t crossfade_extra = crossfade_rem - crossfade_need;
651 while (crossfade_extra > crossfade_chunk->size)
653 crossfade_extra -= crossfade_chunk->size;
654 crossfade_chunk = crossfade_chunk->link;
656 crossfade_sample = crossfade_extra / 2;
658 /* Truncate fade out duration if necessary. */
659 else if (crossfade_rem < crossfade_need)
661 size_t crossfade_short = crossfade_need - crossfade_rem;
662 if (fade_out_rem >= crossfade_short)
663 fade_out_rem -= crossfade_short;
664 else
666 fade_out_delay -= crossfade_short - fade_out_rem;
667 fade_out_rem = 0;
671 /* Get also fade in duration and delays from settings. */
672 crossfade_fade_in_total =
673 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
674 crossfade_fade_in_rem = crossfade_fade_in_total;
676 fade_in_delay =
677 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
679 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
682 /* Returns the number of bytes _NOT_ mixed */
683 static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem)
685 const int16_t *input_buf = (const int16_t *)buf;
686 int16_t *output_buf = (int16_t *)(crossfade_chunk->addr);
687 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
688 output_buf = &output_buf[crossfade_sample];
689 int32_t sample;
691 while (fade_rem)
693 /* fade left and right channel at once to keep buffer alignment */
694 sample = *input_buf++;
695 sample = ((sample * factor) >> 8) + *output_buf;
696 *output_buf++ = clip_sample_16(sample);
698 sample = *input_buf++;
699 sample = ((sample * factor) >> 8) + *output_buf;
700 *output_buf++ = clip_sample_16(sample);
702 fade_rem -= 4; /* 2 samples, each 16 bit -> 4 bytes */
704 if (output_buf >= chunk_end)
706 crossfade_chunk = crossfade_chunk->link;
707 if (!crossfade_chunk)
708 return fade_rem;
709 output_buf = (int16_t *)crossfade_chunk->addr;
710 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
713 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
714 return 0;
717 /* Returns the number of bytes _NOT_ mixed */
718 static size_t crossfade_mix(const char *buf, size_t length)
720 const int16_t *input_buf = (const int16_t *)buf;
721 int16_t *output_buf = (int16_t *)crossfade_chunk->addr;
722 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
723 output_buf = &output_buf[crossfade_sample];
724 int32_t sample;
726 while (length)
728 /* fade left and right channel at once to keep buffer alignment */
729 sample = *input_buf++ + *output_buf;
730 *output_buf++ = clip_sample_16(sample);
732 sample = *input_buf++ + *output_buf;
733 *output_buf++ = clip_sample_16(sample);
735 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
737 if (output_buf >= chunk_end)
739 crossfade_chunk = crossfade_chunk->link;
740 if (!crossfade_chunk)
741 return length;
743 output_buf = (int16_t *)crossfade_chunk->addr;
744 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
747 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
748 return 0;
751 static void pcmbuf_flush_buffer(const char *buf, size_t length)
753 size_t copy_n;
754 while (length > 0) {
755 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
756 if (NEED_FLUSH(audiobuffer_index))
758 pcmbuf_flush_fillpos();
759 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
761 copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
762 memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
763 buf += copy_n;
764 audiobuffer_fillpos += copy_n;
765 length -= copy_n;
769 static void flush_crossfade(char *buf, size_t length)
771 if (length)
773 if (crossfade_fade_in_rem)
775 size_t samples;
776 int16_t *input_buf;
778 /* Fade factor for this packet */
779 int factor =
780 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
781 crossfade_fade_in_total;
782 /* Bytes to fade */
783 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
785 /* We _will_ fade this many bytes */
786 crossfade_fade_in_rem -= fade_rem;
788 if (crossfade_chunk)
790 /* Mix the data */
791 size_t fade_total = fade_rem;
792 fade_rem = crossfade_fade_mix(factor, buf, fade_rem);
793 length -= fade_total - fade_rem;
794 buf += fade_total - fade_rem;
795 if (!length)
796 return;
797 if (!fade_rem)
798 goto fade_done;
801 samples = fade_rem / 2;
802 input_buf = (int16_t *)buf;
803 /* Fade remaining samples in place */
804 while (samples)
806 int32_t sample = *input_buf;
807 *input_buf++ = (sample * factor) >> 8;
808 samples--;
812 fade_done:
813 if (crossfade_chunk)
815 /* Mix the data */
816 size_t mix_total = length;
817 length = crossfade_mix(buf, length);
818 buf += mix_total - length;
819 if (!length)
820 return;
823 /* Flush samples to the buffer */
824 while (!prepare_insert(length))
825 sleep(1);
826 pcmbuf_flush_buffer(buf, length);
831 static bool prepare_insert(size_t length)
833 if (low_latency_mode)
835 /* 1/4s latency. */
836 if (!LOW_DATA(1) && pcm_is_playing())
837 return false;
840 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
841 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
842 return false;
844 if (!pcm_is_playing())
846 trigger_cpu_boost();
848 /* Pre-buffer up to watermark */
849 #if MEMORYSIZE > 2
850 if (!LOW_DATA(4))
851 #else
852 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
853 #endif
855 logf("pcm starting");
856 if (!(audio_status() & AUDIO_STATUS_PAUSE))
857 pcmbuf_play_start();
860 else
861 pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
863 return true;
866 void* pcmbuf_request_buffer(int *count)
868 if (crossfade_init)
869 crossfade_start();
871 if (crossfade_active) {
872 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
873 return fadebuf;
875 else
877 if(prepare_insert(*count << 2))
879 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
880 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
882 /* Usual case, there's space here */
883 return &audiobuffer[audiobuffer_index];
885 else
887 /* Flush and wrap the buffer */
888 pcmbuf_flush_fillpos();
889 audiobuffer_pos = 0;
890 return &audiobuffer[0];
893 else
895 return NULL;
900 void * pcmbuf_request_voice_buffer(int *count)
902 /* A get-it-to-work-for-now hack (audio status could change by
903 completion) */
904 if (audio_status() & AUDIO_STATUS_PLAY)
906 if (pcmbuf_read == NULL)
908 return NULL;
910 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
911 (pcmbuf_mix_chunk || pcmbuf_read->link))
913 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
914 return voicebuf;
916 else
918 return NULL;
921 else
923 return pcmbuf_request_buffer(count);
927 bool pcmbuf_is_crossfade_active(void)
929 return crossfade_active || crossfade_init;
932 void pcmbuf_write_complete(int count)
934 size_t length = (size_t)(unsigned int)count << 2;
936 if (crossfade_active)
938 flush_crossfade(fadebuf, length);
939 if (!(crossfade_fade_in_rem || crossfade_chunk))
940 crossfade_active = false;
942 else
944 audiobuffer_fillpos += length;
946 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
947 pcmbuf_flush_fillpos();
951 #if 0
952 bool pcmbuf_insert_buffer(char *buf, int count)
954 size_t length = (size_t)(unsigned int)count << 2;
956 if (crossfade_active)
958 flush_crossfade(buf, length);
959 if (!(crossfade_fade_in_rem || crossfade_chunk))
960 crossfade_active = false;
962 else
964 if (!prepare_insert(length))
965 return false;
966 pcmbuf_flush_buffer(buf, length);
968 return true;
970 #endif
972 #ifndef HAVE_HARDWARE_BEEP
973 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
974 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
976 /* Generates a constant square wave sound with a given frequency
977 in Hertz for a duration in milliseconds. */
978 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
980 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
981 int32_t phase = 0;
982 int16_t *bufptr, *bufstart, *bufend;
983 int32_t sample;
984 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
985 bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
986 int i;
988 bufend = SKIPBYTES((int16_t *)audiobuffer, pcmbuf_size);
990 /* Find the insertion point and set bufstart to the start of it */
991 if (mix)
993 /* Get the currently playing chunk at the current position. */
994 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
996 /* If above isn't implemented or pcm is stopped, no beepeth. */
997 if (!bufstart || !pcm_is_playing())
998 return;
1000 /* Give 5ms clearance. */
1001 bufstart += NATIVE_FREQUENCY * 4 / 200;
1003 #ifdef HAVE_PCM_DMA_ADDRESS
1004 /* Returned peak addresses are DMA addresses */
1005 bufend = pcm_dma_addr(bufend);
1006 #endif
1008 /* Wrapped above? */
1009 if (bufstart >= bufend)
1010 bufstart -= pcmbuf_size;
1012 /* NOTE: On some targets using hardware DMA, cache range flushing may
1013 * be required or the writes may not be picked up by the controller.
1014 * An incremental flush should be done periodically during the mixdown. */
1016 else if (nsamples <= MINIBUF_SAMPLES)
1018 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
1019 /* Use mini buffer */
1020 bufstart = minibuf;
1021 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1023 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED)
1025 /* Use audiobuffer */
1026 bufstart = (int16_t *)audiobuffer;
1028 else
1030 /* No place */
1031 return;
1034 bufptr = bufstart;
1036 /* Mix square wave into buffer */
1037 for (i = 0; i < nsamples; ++i)
1039 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
1040 sample = mix ? *bufptr : 0;
1041 *bufptr++ = clip_sample_16(sample + amp);
1042 if (bufptr >= bufend)
1043 bufptr = (int16_t *)audiobuffer;
1044 sample = mix ? *bufptr : 0;
1045 *bufptr++ = clip_sample_16(sample + amp);
1046 if (bufptr >= bufend)
1047 bufptr = (int16_t *)audiobuffer;
1049 phase += step;
1052 pcm_play_lock();
1053 #ifdef HAVE_RECORDING
1054 pcm_rec_lock();
1055 #endif
1057 /* Kick off playback if required and it won't interfere */
1058 if (!pcm_is_playing()
1059 #ifdef HAVE_RECORDING
1060 && !pcm_is_recording()
1061 #endif
1064 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
1067 pcm_play_unlock();
1068 #ifdef HAVE_RECORDING
1069 pcm_rec_unlock();
1070 #endif
1072 #endif /* HAVE_HARDWARE_BEEP */
1074 /* Returns pcm buffer usage in percents (0 to 100). */
1075 int pcmbuf_usage(void)
1077 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1080 int pcmbuf_mix_free(void)
1082 if (pcmbuf_mix_chunk)
1084 size_t my_mix_end =
1085 (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
1086 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
1087 if (my_write_pos < my_mix_end)
1088 my_write_pos += pcmbuf_size;
1089 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1091 return 100;
1094 void pcmbuf_write_voice_complete(int count)
1096 /* A get-it-to-work-for-now hack (audio status could have changed) */
1097 if (!(audio_status() & AUDIO_STATUS_PLAY))
1099 pcmbuf_write_complete(count);
1100 return;
1103 int16_t *ibuf = (int16_t *)voicebuf;
1104 int16_t *obuf;
1105 size_t chunk_samples;
1107 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
1109 pcmbuf_mix_chunk = pcmbuf_read->link;
1110 /* Start 1/8s into the next chunk */
1111 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
1114 if (!pcmbuf_mix_chunk)
1115 return;
1117 obuf = (int16_t *)pcmbuf_mix_chunk->addr;
1118 chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
1120 count <<= 1;
1122 while (count-- > 0)
1124 int32_t sample = *ibuf++;
1126 if (pcmbuf_mix_sample >= chunk_samples)
1128 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
1129 if (!pcmbuf_mix_chunk)
1130 return;
1131 pcmbuf_mix_sample = 0;
1132 obuf = pcmbuf_mix_chunk->addr;
1133 chunk_samples = pcmbuf_mix_chunk->size / 2;
1135 sample += obuf[pcmbuf_mix_sample] >> 2;
1136 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1140 void pcmbuf_crossfade_enable(bool on_off)
1142 /* Next setting to be used, not applied now */
1143 crossfade_enabled_pending = on_off;
1146 void pcmbuf_crossfade_enable_finished(void)
1148 /* Copy the pending setting over now */
1149 crossfade_enabled = crossfade_enabled_pending;
1150 pcmbuf_set_watermark_bytes();
1153 bool pcmbuf_is_crossfade_enabled(void)
1155 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
1156 return global_settings.playlist_shuffle;
1158 return crossfade_enabled;