Make the image tags examples nicer.
[Rockbox.git] / apps / pcmbuf.c
blob8f16c905234571a9b16d834f3a0a90052d4c6a64
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include "config.h"
23 #include "debug.h"
24 #include "panic.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 "system.h"
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 /* Define PCMBUF_MUTING if the codec requires muting to prevent pops */
42 #if !defined(HAVE_UDA1380) && !defined(HAVE_TLV320) && !defined(HAVE_AS3514)
43 #define PCMBUF_MUTING
44 #endif
46 /* Clip sample to signed 16 bit range */
47 static inline int32_t clip_sample_16(int32_t sample)
49 if ((int16_t)sample != sample)
50 sample = 0x7fff ^ (sample >> 31);
51 return sample;
54 /* Keep watermark high for iPods at least (2s) */
55 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
57 /* Structure we can use to queue pcm chunks in memory to be played
58 * by the driver code. */
59 struct pcmbufdesc
61 void *addr;
62 size_t size;
63 struct pcmbufdesc* link;
64 /* Call this when the buffer has been played */
65 void (*callback)(void);
68 #define PCMBUF_DESCS(bufsize) \
69 ((bufsize) / PCMBUF_MINAVG_CHUNK)
70 #define PCMBUF_DESCS_SIZE(bufsize) \
71 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
73 /* Size of the PCM buffer. */
74 static size_t pcmbuf_size IDATA_ATTR = 0;
75 static char *pcmbuf_bufend IDATA_ATTR;
76 static char *audiobuffer IDATA_ATTR;
77 /* Current audio buffer write index. */
78 static size_t audiobuffer_pos IDATA_ATTR;
79 /* Amount audiobuffer_pos will be increased.*/
80 static size_t audiobuffer_fillpos IDATA_ATTR;
81 static char *fadebuf IDATA_ATTR;
82 static char *voicebuf IDATA_ATTR;
84 static void (*pcmbuf_event_handler)(void) IDATA_ATTR;
85 static void (*position_callback)(size_t size) IDATA_ATTR;
87 /* Crossfade related state */
88 static bool crossfade_enabled;
89 static bool crossfade_enabled_pending;
90 static bool crossfade_mixmode;
91 static bool crossfade_active IDATA_ATTR;
92 static bool crossfade_init IDATA_ATTR;
94 /* Track the current location for processing crossfade */
95 static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
96 static size_t crossfade_sample IDATA_ATTR;
98 /* Counters for fading in new data */
99 static size_t crossfade_fade_in_total IDATA_ATTR;
100 static size_t crossfade_fade_in_rem IDATA_ATTR;
102 static size_t pcmbuf_descsize;
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 struct thread_entry *codec_thread_p;
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(void);
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 keep it above 2 second */
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_p, priority);
266 voice_thread_set_priority(priority);
269 else if (codec_thread_priority != PRIORITY_PLAYBACK)
271 thread_set_priority(codec_thread_p, 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(void)
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_p)
284 #endif /* SIMULATOR */
286 #ifdef HAVE_PRIORITY_SCHEDULING
287 /* If buffer is critically low, override UI priority, else
288 set back to the original priority. */
289 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
290 #endif
291 /* Fill audio buffer by boosting cpu */
292 trigger_cpu_boost();
295 /* Disable crossfade if < .5s of audio */
296 if (LOW_DATA(2))
298 crossfade_active = false;
302 void pcmbuf_set_event_handler(void (*event_handler)(void))
304 pcmbuf_event_handler = event_handler;
307 unsigned int pcmbuf_get_latency(void)
309 /* Be careful how this calculation is rearranged, it's easy to overflow */
310 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
311 return bytes / 4 / (NATIVE_FREQUENCY/1000);
314 void pcmbuf_set_low_latency(bool state)
316 low_latency_mode = state;
319 bool pcmbuf_is_lowdata(void)
321 if (!pcm_is_playing() || pcm_is_paused() ||
322 crossfade_init || crossfade_active)
323 return false;
325 /* 1 seconds of buffer is low data */
326 return LOW_DATA(4);
329 /* Amount of bytes left in the buffer. */
330 inline size_t pcmbuf_free(void)
332 if (pcmbuf_read != NULL)
334 void *read = pcmbuf_read->addr;
335 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
336 if (read < write)
337 return (size_t)(read - write) + pcmbuf_size;
338 else
339 return (size_t) (read - write);
341 return pcmbuf_size;
344 bool pcmbuf_crossfade_init(bool manual_skip)
346 /* Can't do two crossfades at once and, no fade if pcm is off now */
347 if (crossfade_init || crossfade_active || !pcm_is_playing())
349 pcmbuf_play_stop();
350 return false;
353 trigger_cpu_boost();
355 /* Not enough data, or crossfade disabled, flush the old data instead */
356 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
358 pcmbuf_flush_fillpos();
359 pcmbuf_flush = true;
360 return false;
363 /* Don't enable mix mode when skipping tracks manually. */
364 if (manual_skip)
365 crossfade_mixmode = false;
366 else
367 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
369 crossfade_init = true;
371 return true;
375 void pcmbuf_play_stop(void)
377 pcm_play_stop();
379 pcmbuf_unplayed_bytes = 0;
380 pcmbuf_mix_chunk = NULL;
381 if (pcmbuf_read) {
382 pcmbuf_write_end->link = pcmbuf_read;
383 pcmbuf_write_end = pcmbuf_read_end;
384 pcmbuf_read = pcmbuf_read_end = NULL;
386 audiobuffer_pos = 0;
387 audiobuffer_fillpos = 0;
388 crossfade_init = false;
389 crossfade_active = false;
390 pcmbuf_flush = false;
392 #ifdef HAVE_PRIORITY_SCHEDULING
393 /* Can unboost the codec thread here no matter who's calling */
394 boost_codec_thread(false);
395 #endif
398 int pcmbuf_used_descs(void) {
399 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
400 unsigned int i = 0;
401 while (pcmbuf_temp) {
402 pcmbuf_temp = pcmbuf_temp->link;
403 i++;
405 return i;
408 int pcmbuf_descs(void) {
409 return PCMBUF_DESCS(pcmbuf_size);
412 size_t get_pcmbuf_descsize(void) {
413 return pcmbuf_descsize;
416 static void pcmbuf_init_pcmbuffers(void) {
417 struct pcmbufdesc *next = pcmbuf_write;
418 next++;
419 pcmbuf_write_end = pcmbuf_write;
420 while ((void *)next < (void *)pcmbuf_bufend) {
421 pcmbuf_write_end->link=next;
422 pcmbuf_write_end=next;
423 next++;
427 static size_t pcmbuf_get_next_required_pcmbuf_size(void)
429 #if MEM > 1
430 size_t seconds = 1;
432 if (crossfade_enabled_pending)
433 seconds += global_settings.crossfade_fade_out_delay
434 + global_settings.crossfade_fade_out_duration;
436 /* Buffer has to be at least 2s long. */
437 seconds += 2;
438 logf("pcmbuf len: %ld", seconds);
439 return seconds * (NATIVE_FREQUENCY*4);
440 #else
441 return NATIVE_FREQUENCY*2;
442 #endif
445 static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
447 return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
448 PCMBUF_DESCS_SIZE(bufsize));
451 bool pcmbuf_is_same_size(void)
453 if (audiobuffer == NULL)
454 return true; /* Not set up yet even once so always */
456 size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
457 return pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
460 /* Initialize the pcmbuffer the structure looks like this:
461 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
462 size_t pcmbuf_init(unsigned char *bufend)
464 pcmbuf_bufend = bufend;
465 pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
466 audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
467 fadebuf = &audiobuffer[pcmbuf_size];
468 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
469 pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
471 pcmbuf_descsize = PCMBUF_DESCS_SIZE(pcmbuf_size);
472 pcmbuf_init_pcmbuffers();
474 position_callback = NULL;
475 pcmbuf_event_handler = NULL;
477 pcmbuf_crossfade_enable_finished();
479 pcmbuf_play_stop();
481 return pcmbuf_bufend - audiobuffer;
484 size_t pcmbuf_get_bufsize(void)
486 return pcmbuf_size;
489 #ifdef ROCKBOX_HAS_LOGF
490 unsigned char * pcmbuf_get_meminfo(size_t *length)
492 *length = pcmbuf_bufend - audiobuffer;
493 return audiobuffer;
495 #endif
497 void pcmbuf_pause(bool pause)
499 #ifdef PCMBUF_MUTING
500 if (pause)
501 pcm_mute(true);
502 #endif
504 if (pcm_is_playing())
505 pcm_play_pause(!pause);
506 else if (!pause)
507 pcmbuf_play_start();
509 #ifdef PCMBUF_MUTING
510 if (!pause)
511 pcm_mute(false);
512 #endif
513 trigger_cpu_boost();
516 /* Force playback. */
517 void pcmbuf_play_start(void)
519 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
521 last_chunksize = pcmbuf_read->size;
522 pcmbuf_unplayed_bytes -= last_chunksize;
523 pcm_play_data(pcmbuf_callback,
524 (unsigned char *)pcmbuf_read->addr, last_chunksize);
529 * Commit samples waiting to the pcm buffer.
531 static bool pcmbuf_flush_fillpos(void)
533 if (audiobuffer_fillpos) {
534 /* Never use the last buffer descriptor */
535 while (pcmbuf_write == pcmbuf_write_end) {
536 /* If this happens, something is being stupid */
537 if (!pcm_is_playing()) {
538 logf("pcmbuf_flush_fillpos error");
539 pcmbuf_play_start();
541 /* Let approximately one chunk of data playback */
542 sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
544 pcmbuf_add_chunk();
545 return true;
547 return false;
551 * Completely process the crossfade fade out effect with current pcm buffer.
553 static void crossfade_process_buffer(size_t fade_in_delay,
554 size_t fade_out_delay, size_t fade_out_rem)
556 if (!crossfade_mixmode)
558 /* Fade out the specified amount of the already processed audio */
559 size_t total_fade_out = fade_out_rem;
560 size_t fade_out_sample;
561 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
563 /* Find the right chunk to start fading out */
564 fade_out_delay += crossfade_sample * 2;
565 while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
567 fade_out_delay -= fade_out_chunk->size;
568 fade_out_chunk = fade_out_chunk->link;
570 /* The start sample within the chunk */
571 fade_out_sample = fade_out_delay / 2;
573 while (fade_out_rem > 0)
575 /* Each 1/10 second of audio will have the same fade applied */
576 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
577 int factor = (fade_out_rem << 8) / total_fade_out;
579 fade_out_rem -= block_rem;
581 /* Fade this block */
582 while (block_rem > 0 && fade_out_chunk != NULL)
584 /* Fade one sample */
585 int16_t *buf = (int16_t *)fade_out_chunk->addr;
586 int32_t sample = buf[fade_out_sample];
587 buf[fade_out_sample++] = (sample * factor) >> 8;
589 block_rem -= 2;
590 /* Move to the next chunk as needed */
591 if (fade_out_sample * 2 >= fade_out_chunk->size)
593 fade_out_chunk = fade_out_chunk->link;
594 fade_out_sample = 0;
600 /* Find the right chunk and sample to start fading in */
601 fade_in_delay += crossfade_sample * 2;
602 while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
604 fade_in_delay -= crossfade_chunk->size;
605 crossfade_chunk = crossfade_chunk->link;
607 crossfade_sample = fade_in_delay / 2;
608 logf("process done!");
611 /* Initializes crossfader, calculates all necessary parameters and
612 * performs fade-out with the pcm buffer. */
613 static void crossfade_start(void)
615 size_t crossfade_rem;
616 size_t crossfade_need;
617 size_t fade_out_rem;
618 size_t fade_out_delay;
619 size_t fade_in_delay;
621 crossfade_init = false;
622 /* Reject crossfade if less than .5s of data */
623 if (LOW_DATA(2)) {
624 logf("crossfade rejected");
625 pcmbuf_play_stop();
626 return ;
629 logf("crossfade_start");
630 pcmbuf_flush_fillpos();
631 crossfade_active = true;
633 /* Initialize the crossfade buffer size to all of the buffered data that
634 * has not yet been sent to the DMA */
635 crossfade_rem = pcmbuf_unplayed_bytes;
636 crossfade_chunk = pcmbuf_read->link;
637 crossfade_sample = 0;
639 /* Get fade out delay from settings. */
640 fade_out_delay =
641 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
643 /* Get fade out duration from settings. */
644 fade_out_rem =
645 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
647 crossfade_need = fade_out_delay + fade_out_rem;
648 /* We want only to modify the last part of the buffer. */
649 if (crossfade_rem > crossfade_need)
651 size_t crossfade_extra = crossfade_rem - crossfade_need;
652 while (crossfade_extra > crossfade_chunk->size)
654 crossfade_extra -= crossfade_chunk->size;
655 crossfade_chunk = crossfade_chunk->link;
657 crossfade_sample = crossfade_extra / 2;
659 /* Truncate fade out duration if necessary. */
660 else if (crossfade_rem < crossfade_need)
662 size_t crossfade_short = crossfade_need - crossfade_rem;
663 if (fade_out_rem >= crossfade_short)
664 fade_out_rem -= crossfade_short;
665 else
667 fade_out_delay -= crossfade_short - fade_out_rem;
668 fade_out_rem = 0;
672 /* Get also fade in duration and delays from settings. */
673 crossfade_fade_in_total =
674 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
675 crossfade_fade_in_rem = crossfade_fade_in_total;
677 fade_in_delay =
678 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
680 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
683 /* Returns the number of bytes _NOT_ mixed */
684 static size_t crossfade_fade_mix(int factor, const char *buf, size_t fade_rem)
686 const int16_t *input_buf = (const int16_t *)buf;
687 int16_t *output_buf = (int16_t *)(crossfade_chunk->addr);
688 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
689 output_buf = &output_buf[crossfade_sample];
691 while (fade_rem)
693 int32_t sample = *input_buf++;
694 sample = ((sample * factor) >> 8) + *output_buf;
695 *output_buf++ = clip_sample_16(sample);
696 fade_rem -= 2;
698 if (output_buf >= chunk_end)
700 crossfade_chunk = crossfade_chunk->link;
701 if (!crossfade_chunk)
702 return fade_rem;
703 output_buf = (int16_t *)crossfade_chunk->addr;
704 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
707 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
708 return 0;
711 /* Returns the number of bytes _NOT_ mixed */
712 static size_t crossfade_mix(const char *buf, size_t length)
714 const int16_t *input_buf = (const int16_t *)buf;
715 int16_t *output_buf = (int16_t *)crossfade_chunk->addr;
716 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
717 output_buf = &output_buf[crossfade_sample];
719 while (length)
721 int32_t sample = *input_buf++ + *output_buf;
722 *output_buf++ = clip_sample_16(sample);
723 length -= 2;
725 if (output_buf >= chunk_end)
727 crossfade_chunk = crossfade_chunk->link;
728 if (!crossfade_chunk)
729 return length;
731 output_buf = (int16_t *)crossfade_chunk->addr;
732 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
735 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
736 return 0;
739 static void pcmbuf_flush_buffer(const char *buf, size_t length)
741 size_t copy_n;
742 while (length > 0) {
743 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
744 if (NEED_FLUSH(audiobuffer_index))
746 pcmbuf_flush_fillpos();
747 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
749 copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
750 memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
751 buf += copy_n;
752 audiobuffer_fillpos += copy_n;
753 length -= copy_n;
757 static void flush_crossfade(char *buf, size_t length)
759 if (length)
761 if (crossfade_fade_in_rem)
763 size_t samples;
764 int16_t *input_buf;
766 /* Fade factor for this packet */
767 int factor =
768 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
769 crossfade_fade_in_total;
770 /* Bytes to fade */
771 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
773 /* We _will_ fade this many bytes */
774 crossfade_fade_in_rem -= fade_rem;
776 if (crossfade_chunk)
778 /* Mix the data */
779 size_t fade_total = fade_rem;
780 fade_rem = crossfade_fade_mix(factor, buf, fade_rem);
781 length -= fade_total - fade_rem;
782 buf += fade_total - fade_rem;
783 if (!length)
784 return;
785 if (!fade_rem)
786 goto fade_done;
789 samples = fade_rem / 2;
790 input_buf = (int16_t *)buf;
791 /* Fade remaining samples in place */
792 while (samples)
794 int32_t sample = *input_buf;
795 *input_buf++ = (sample * factor) >> 8;
796 samples--;
800 fade_done:
801 if (crossfade_chunk)
803 /* Mix the data */
804 size_t mix_total = length;
805 length = crossfade_mix(buf, length);
806 buf += mix_total - length;
807 if (!length)
808 return;
811 /* Flush samples to the buffer */
812 while (!prepare_insert(length))
813 sleep(1);
814 pcmbuf_flush_buffer(buf, length);
819 static bool prepare_insert(size_t length)
821 if (low_latency_mode)
823 /* 1/4s latency. */
824 if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 2
825 && pcm_is_playing())
826 return false;
829 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
830 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
831 return false;
833 if (!pcm_is_playing())
835 trigger_cpu_boost();
837 /* Pre-buffer 1s. */
838 #if MEM <= 1
839 if (!LOW_DATA(1))
840 #else
841 if (!LOW_DATA(4))
842 #endif
844 logf("pcm starting");
845 if (!(audio_status() & AUDIO_STATUS_PAUSE))
846 pcmbuf_play_start();
849 else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
850 pcmbuf_under_watermark();
852 return true;
855 void* pcmbuf_request_buffer(int *count)
857 if (crossfade_init)
858 crossfade_start();
860 if (crossfade_active) {
861 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
862 return fadebuf;
864 else
866 if(prepare_insert(*count << 2))
868 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
869 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
871 /* Usual case, there's space here */
872 return &audiobuffer[audiobuffer_index];
874 else
876 /* Flush and wrap the buffer */
877 pcmbuf_flush_fillpos();
878 audiobuffer_pos = 0;
879 return &audiobuffer[0];
882 else
884 return NULL;
889 void * pcmbuf_request_voice_buffer(int *count)
891 /* A get-it-to-work-for-now hack (audio status could change by
892 completion) */
893 if (audio_status() & AUDIO_STATUS_PLAY)
895 if (pcmbuf_read == NULL)
897 return NULL;
899 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
900 (pcmbuf_mix_chunk || pcmbuf_read->link))
902 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
903 return voicebuf;
905 else
907 return NULL;
910 else
912 return pcmbuf_request_buffer(count);
916 bool pcmbuf_is_crossfade_active(void)
918 return crossfade_active || crossfade_init;
921 void pcmbuf_write_complete(int count)
923 size_t length = (size_t)(unsigned int)count << 2;
925 if (crossfade_active)
927 flush_crossfade(fadebuf, length);
928 if (!(crossfade_fade_in_rem || crossfade_chunk))
929 crossfade_active = false;
931 else
933 audiobuffer_fillpos += length;
935 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
936 pcmbuf_flush_fillpos();
940 #if 0
941 bool pcmbuf_insert_buffer(char *buf, int count)
943 size_t length = (size_t)(unsigned int)count << 2;
945 if (crossfade_active)
947 flush_crossfade(buf, length);
948 if (!(crossfade_fade_in_rem || crossfade_chunk))
949 crossfade_active = false;
951 else
953 if (!prepare_insert(length))
954 return false;
955 pcmbuf_flush_buffer(buf, length);
957 return true;
959 #endif
961 /* Generates a constant square wave sound with a given frequency
962 in Hertz for a duration in milliseconds. */
963 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
965 unsigned int count = 0, i = 0;
966 unsigned int interval = NATIVE_FREQUENCY / frequency;
967 int32_t sample;
968 int16_t *buf;
969 int16_t *pcmbuf_end = (int16_t *)fadebuf;
970 size_t samples = NATIVE_FREQUENCY / 1000 * duration;
972 if (pcm_is_playing() && pcmbuf_read != NULL)
974 if (pcmbuf_read->link)
976 /* Get the next chunk */
977 char *pcmbuf_mix_buf = pcmbuf_read->link->addr;
978 /* Give at least 1/8s clearance. */
979 buf = (int16_t *)&pcmbuf_mix_buf[NATIVE_FREQUENCY * 4 / 8];
981 else
983 logf("No place to beep");
984 return;
987 while (i++ < samples)
989 sample = *buf;
990 *buf++ = clip_sample_16(sample + amplitude);
991 if (buf > pcmbuf_end)
992 buf = (int16_t *)audiobuffer;
993 sample = *buf;
994 *buf++ = clip_sample_16(sample + amplitude);
996 /* Toggle square wav side */
997 if (++count >= interval)
999 count = 0;
1000 amplitude = -amplitude;
1002 if (buf > pcmbuf_end)
1003 buf = (int16_t *)audiobuffer;
1006 else
1008 buf = (int16_t *)audiobuffer;
1009 while (i++ < samples)
1011 *buf++ = amplitude;
1012 if (buf > pcmbuf_end)
1013 buf = (int16_t *)audiobuffer;
1014 *buf++ = amplitude;
1016 /* Toggle square wav side */
1017 if (++count >= interval)
1019 count = 0;
1020 amplitude = -amplitude;
1022 if (buf > pcmbuf_end)
1023 buf = (int16_t *)audiobuffer;
1025 pcm_play_data(NULL, (unsigned char *)audiobuffer, samples * 4);
1029 /* Returns pcm buffer usage in percents (0 to 100). */
1030 int pcmbuf_usage(void)
1032 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1035 int pcmbuf_mix_free(void)
1037 if (pcmbuf_mix_chunk)
1039 size_t my_mix_end =
1040 (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
1041 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
1042 if (my_write_pos < my_mix_end)
1043 my_write_pos += pcmbuf_size;
1044 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1046 return 100;
1049 void pcmbuf_write_voice_complete(int count)
1051 /* A get-it-to-work-for-now hack (audio status could have changed) */
1052 if (!(audio_status() & AUDIO_STATUS_PLAY))
1054 pcmbuf_write_complete(count);
1055 return;
1058 int16_t *ibuf = (int16_t *)voicebuf;
1059 int16_t *obuf;
1060 size_t chunk_samples;
1062 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
1064 pcmbuf_mix_chunk = pcmbuf_read->link;
1065 /* Start 1/8s into the next chunk */
1066 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
1069 if (!pcmbuf_mix_chunk)
1070 return;
1072 obuf = (int16_t *)pcmbuf_mix_chunk->addr;
1073 chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
1075 count <<= 1;
1077 while (count-- > 0)
1079 int32_t sample = *ibuf++;
1081 if (pcmbuf_mix_sample >= chunk_samples)
1083 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
1084 if (!pcmbuf_mix_chunk)
1085 return;
1086 pcmbuf_mix_sample = 0;
1087 obuf = pcmbuf_mix_chunk->addr;
1088 chunk_samples = pcmbuf_mix_chunk->size / 2;
1090 sample += obuf[pcmbuf_mix_sample] >> 2;
1091 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1095 void pcmbuf_crossfade_enable(bool on_off)
1097 #if MEM > 1
1098 /* Next setting to be used, not applied now */
1099 crossfade_enabled_pending = on_off;
1100 #endif
1101 (void)on_off;
1104 void pcmbuf_crossfade_enable_finished(void)
1106 /* Copy the pending setting over now */
1107 crossfade_enabled = crossfade_enabled_pending;
1108 pcmbuf_set_watermark_bytes();
1111 bool pcmbuf_is_crossfade_enabled(void)
1113 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
1114 return global_settings.playlist_shuffle;
1116 return crossfade_enabled;