Remove tabs.
[kugel-rb.git] / apps / pcmbuf.c
blobc7baad08e4e7bb5d448a350894f6857746de571d
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 "playback.h"
29 #include "codec_thread.h"
31 /* Define LOGF_ENABLE to enable logf output in this file */
32 /*#define LOGF_ENABLE*/
33 #include "logf.h"
34 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
35 #include "cpu.h"
36 #endif
37 #include <string.h>
38 #include "settings.h"
39 #include "audio.h"
40 #include "voice_thread.h"
41 #include "dsp.h"
43 #define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
44 on the pcm buffer */
45 #define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
46 chunks on the pcm buffer (or we run out
47 of buffer descriptors, which is
48 non-fatal) */
49 #define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
50 this to the DMA */
51 #define CROSSFADE_BUFSIZE 8192 /* Size of the crossfade buffer */
52 #define AUX_BUFSIZE 512 /* Size of the aux buffer; can be 512 if no
53 resampling or timestretching is allowed in
54 the aux channel, must be 2048 otherwise */
56 /* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */
57 #define BYTERATE (NATIVE_FREQUENCY * 4)
59 #if MEMORYSIZE > 2
60 /* Keep watermark high for iPods at least (2s) */
61 #define PCMBUF_WATERMARK (BYTERATE * 2)
62 #else
63 #define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */
64 #endif
66 /* Structure we can use to queue pcm chunks in memory to be played
67 * by the driver code. */
68 struct chunkdesc
70 unsigned char *addr;
71 size_t size;
72 struct chunkdesc* link;
73 /* true if last chunk in the track */
74 bool end_of_track;
77 #define NUM_CHUNK_DESCS(bufsize) \
78 ((bufsize) / PCMBUF_MINAVG_CHUNK)
80 /* Size of the PCM buffer. */
81 static size_t pcmbuf_size IDATA_ATTR = 0;
82 static char *pcmbuf_bufend IDATA_ATTR;
83 static char *pcmbuffer IDATA_ATTR;
84 /* Current PCM buffer write index. */
85 static size_t pcmbuffer_pos IDATA_ATTR;
86 /* Amount pcmbuffer_pos will be increased.*/
87 static size_t pcmbuffer_fillpos IDATA_ATTR;
89 static struct chunkdesc *first_desc;
91 /* Gapless playback */
92 static bool track_transition IDATA_ATTR;
94 #ifdef HAVE_CROSSFADE
95 /* Crossfade buffer */
96 static char *fadebuf IDATA_ATTR;
98 /* Crossfade related state */
99 static bool crossfade_enabled;
100 static bool crossfade_enable_request;
101 static bool crossfade_mixmode;
102 static bool crossfade_auto_skip;
103 static bool crossfade_active IDATA_ATTR;
104 static bool crossfade_track_change_started IDATA_ATTR;
106 /* Track the current location for processing crossfade */
107 static struct chunkdesc *crossfade_chunk IDATA_ATTR;
108 static size_t crossfade_sample IDATA_ATTR;
110 /* Counters for fading in new data */
111 static size_t crossfade_fade_in_total IDATA_ATTR;
112 static size_t crossfade_fade_in_rem IDATA_ATTR;
113 #endif
115 static struct chunkdesc *read_chunk IDATA_ATTR;
116 static struct chunkdesc *read_end_chunk IDATA_ATTR;
117 static struct chunkdesc *write_chunk IDATA_ATTR;
118 static struct chunkdesc *write_end_chunk IDATA_ATTR;
119 static size_t last_chunksize IDATA_ATTR;
121 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
122 static size_t pcmbuf_watermark IDATA_ATTR;
124 /* Voice */
125 static char *voicebuf IDATA_ATTR;
126 static struct chunkdesc *mix_chunk IDATA_ATTR;
127 static size_t pcmbuf_mix_sample IDATA_ATTR;
129 static bool low_latency_mode = false;
130 static bool flush_pcmbuf = false;
132 #ifdef HAVE_PRIORITY_SCHEDULING
133 static int codec_thread_priority = PRIORITY_PLAYBACK;
134 #endif
136 /* Helpful macros for use in conditionals this assumes some of the above
137 * static variable names */
138 #define COMMIT_IF_NEEDED if(pcmbuffer_fillpos > PCMBUF_TARGET_CHUNK || \
139 (pcmbuffer_pos + pcmbuffer_fillpos) >= pcmbuf_size) commit_chunk(false)
140 #define LOW_DATA(quarter_secs) \
141 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
143 #ifdef HAVE_CROSSFADE
144 static void crossfade_start(void);
145 static void write_to_crossfade(size_t length);
146 static void pcmbuf_finish_crossfade_enable(void);
147 #endif
149 /* Callbacks into playback.c */
150 extern void audio_pcmbuf_position_callback(unsigned int time);
151 extern void audio_pcmbuf_track_change(bool pcmbuf);
152 extern bool audio_pcmbuf_may_play(void);
155 /**************************************/
157 /* define this to show detailed chunkdesc usage information on the sim console */
158 /*#define DESC_DEBUG*/
160 #ifndef SIMULATOR
161 #undef DESC_DEBUG
162 #endif
164 #ifdef DESC_DEBUG
165 #define DISPLAY_DESC(caller) while(!show_desc(caller))
166 #define DESC_IDX(desc) (desc ? desc - first_desc : -1)
167 #define SHOW_DESC(desc) if(DESC_IDX(desc)==-1) DEBUGF("--"); \
168 else DEBUGF("%02d", DESC_IDX(desc))
169 #define SHOW_DESC_LINK(desc) if(desc){SHOW_DESC(desc->link);DEBUGF(" ");} \
170 else DEBUGF("-- ")
171 #define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \
172 SHOW_DESC_LINK(desc)
173 #define SHOW_POINT(tag,desc) DEBUGF("%s",tag);SHOW_DETAIL(desc)
174 #define SHOW_NUM(num,desc) DEBUGF("%02d>",num);SHOW_DESC_LINK(desc)
176 static bool show_desc(char *caller)
178 if (show_desc_in_use) return false;
179 show_desc_in_use = true;
180 DEBUGF("%-14s\t", caller);
181 SHOW_POINT("r", read_chunk);
182 SHOW_POINT("re", read_end_chunk);
183 DEBUGF(" ");
184 SHOW_POINT("w", write_chunk);
185 SHOW_POINT("we", write_end_chunk);
186 DEBUGF("\n");
187 int i;
188 for (i = 0; i < pcmbuf_descs(); i++)
190 SHOW_NUM(i, (first_desc + i));
191 if (i%10 == 9) DEBUGF("\n");
193 DEBUGF("\n\n");
194 show_desc_in_use = false;
195 return true;
197 #else
198 #define DISPLAY_DESC(caller) do{}while(0)
199 #endif
202 /** Accept new PCM data */
204 /* Commit PCM buffer samples as a new chunk for playback */
205 static void commit_chunk(bool flush_next_time)
207 if (!pcmbuffer_fillpos)
208 return;
210 /* Never use the last buffer descriptor */
211 while (write_chunk == write_end_chunk) {
212 /* If this happens, something is being stupid */
213 if (!pcm_is_playing()) {
214 logf("commit_chunk error");
215 pcmbuf_play_start();
217 /* Let approximately one chunk of data playback */
218 sleep(HZ * PCMBUF_TARGET_CHUNK / BYTERATE);
221 /* commit the chunk */
223 register size_t size = pcmbuffer_fillpos;
224 /* Grab the next description to write, and change the write pointer */
225 register struct chunkdesc *pcmbuf_current = write_chunk;
226 write_chunk = pcmbuf_current->link;
227 /* Fill in the values in the new buffer chunk */
228 pcmbuf_current->addr = &pcmbuffer[pcmbuffer_pos];
229 pcmbuf_current->size = size;
230 pcmbuf_current->end_of_track = false;
231 pcmbuf_current->link = NULL;
233 if (read_chunk != NULL)
235 if (flush_pcmbuf)
237 /* Flush! Discard all data after the currently playing chunk,
238 and make the current chunk play next */
239 logf("commit_chunk: flush");
240 pcm_play_lock();
241 write_end_chunk->link = read_chunk->link;
242 read_chunk->link = pcmbuf_current;
243 while (write_end_chunk->link)
245 write_end_chunk = write_end_chunk->link;
246 pcmbuf_unplayed_bytes -= write_end_chunk->size;
249 read_chunk->end_of_track = track_transition;
250 pcm_play_unlock();
252 /* If there is already a read buffer setup, add to it */
253 else
254 read_end_chunk->link = pcmbuf_current;
256 else
258 /* Otherwise create the buffer */
259 read_chunk = pcmbuf_current;
262 /* If flush_next_time is true, then the current chunk will be thrown out
263 * and the next chunk to be committed will be the next to be played.
264 * This is used to empty the PCM buffer for a track change. */
265 flush_pcmbuf = flush_next_time;
267 /* This is now the last buffer to read */
268 read_end_chunk = pcmbuf_current;
270 /* Update bytes counters */
271 pcmbuf_unplayed_bytes += size;
273 pcmbuffer_pos += size;
274 if (pcmbuffer_pos >= pcmbuf_size)
275 pcmbuffer_pos -= pcmbuf_size;
277 pcmbuffer_fillpos = 0;
278 DISPLAY_DESC("commit_chunk");
281 /* Set priority of the codec thread */
282 #ifdef HAVE_PRIORITY_SCHEDULING
284 * expects pcm_fill_state in tenth-% units (e.g. full pcm buffer is 10) */
285 static void boost_codec_thread(int pcm_fill_state)
287 static const int prios[11] = {
288 PRIORITY_PLAYBACK_MAX, /* 0 - 10% */
289 PRIORITY_PLAYBACK_MAX+1, /* 10 - 20% */
290 PRIORITY_PLAYBACK_MAX+3, /* 20 - 30% */
291 PRIORITY_PLAYBACK_MAX+5, /* 30 - 40% */
292 PRIORITY_PLAYBACK_MAX+7, /* 40 - 50% */
293 PRIORITY_PLAYBACK_MAX+8, /* 50 - 60% */
294 PRIORITY_PLAYBACK_MAX+9, /* 60 - 70% */
295 /* raiseing priority above 70% shouldn't be needed */
296 PRIORITY_PLAYBACK, /* 70 - 80% */
297 PRIORITY_PLAYBACK, /* 80 - 90% */
298 PRIORITY_PLAYBACK, /* 90 -100% */
299 PRIORITY_PLAYBACK, /* 100% */
301 int new_prio = prios[pcm_fill_state];
303 /* Keep voice and codec threads at the same priority or else voice
304 * will starve if the codec thread's priority is boosted. */
305 if (new_prio != codec_thread_priority)
307 codec_thread_set_priority(new_prio);
308 voice_thread_set_priority(new_prio);
309 codec_thread_priority = new_prio;
312 #else
313 #define boost_codec_thread(pcm_fill_state) do{}while(0)
314 #endif /* HAVE_PRIORITY_SCHEDULING */
316 /* Return true if the PCM buffer is able to receive new data.
317 * Also maintain buffer level above the watermark. */
318 static bool prepare_insert(size_t length)
320 if (low_latency_mode)
322 /* 1/4s latency. */
323 if (!LOW_DATA(1) && pcm_is_playing())
324 return false;
327 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
328 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
329 return false;
331 /* Maintain the buffer level above the watermark */
332 if (pcm_is_playing())
334 /* Only codec thread initiates boost - voice boosts the cpu when playing
335 a clip */
336 #ifndef SIMULATOR
337 if (is_codec_thread())
338 #endif /* SIMULATOR */
340 /* boost cpu if necessary */
341 if (pcmbuf_unplayed_bytes < pcmbuf_watermark)
342 trigger_cpu_boost();
343 boost_codec_thread(pcmbuf_unplayed_bytes*10/pcmbuf_size);
346 #ifdef HAVE_CROSSFADE
347 /* Disable crossfade if < .5s of audio */
348 if (LOW_DATA(2))
350 crossfade_active = false;
352 #endif
354 else /* pcm_is_playing */
356 /* Boost CPU for pre-buffer */
357 trigger_cpu_boost();
359 /* If pre-buffered to the watermark, start playback */
360 #if MEMORYSIZE > 2
361 if (!LOW_DATA(4))
362 #else
363 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
364 #endif
366 logf("pcm starting");
367 if (audio_pcmbuf_may_play())
368 pcmbuf_play_start();
372 return true;
375 /* Request space in the buffer for writing output samples */
376 void *pcmbuf_request_buffer(int *count)
378 #ifdef HAVE_CROSSFADE
379 /* we're going to crossfade to a new track, which is now on its way */
380 if (crossfade_track_change_started)
381 crossfade_start();
383 /* crossfade has begun, put the new track samples in fadebuf */
384 if (crossfade_active)
386 int cnt = MIN(*count, CROSSFADE_BUFSIZE/4);
387 if (prepare_insert(cnt << 2))
389 *count = cnt;
390 return fadebuf;
393 else
394 #endif
395 /* if possible, reserve room in the PCM buffer for new samples */
397 if(prepare_insert(*count << 2))
399 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
400 if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK)
402 /* Usual case, there's space here */
403 return &pcmbuffer[pcmbuffer_index];
405 else
407 /* Wrap the buffer, the new samples go at the beginning */
408 commit_chunk(false);
409 pcmbuffer_pos = 0;
410 return &pcmbuffer[0];
414 /* PCM buffer not ready to receive new data yet */
415 return NULL;
418 /* Handle new samples to the buffer */
419 void pcmbuf_write_complete(int count)
421 size_t length = (size_t)(unsigned int)count << 2;
422 #ifdef HAVE_CROSSFADE
423 if (crossfade_active)
424 write_to_crossfade(length);
425 else
426 #endif
428 pcmbuffer_fillpos += length;
429 COMMIT_IF_NEEDED;
434 /** Init */
436 static inline void init_pcmbuffers(void)
438 first_desc = write_chunk;
439 struct chunkdesc *next = write_chunk;
440 next++;
441 write_end_chunk = write_chunk;
442 while ((void *)next < (void *)pcmbuf_bufend) {
443 write_end_chunk->link=next;
444 write_end_chunk=next;
445 next++;
447 DISPLAY_DESC("init");
450 static size_t get_next_required_pcmbuf_size(void)
452 size_t seconds = 1;
454 #ifdef HAVE_CROSSFADE
455 if (crossfade_enable_request)
456 seconds += global_settings.crossfade_fade_out_delay +
457 global_settings.crossfade_fade_out_duration;
458 #endif
460 #if MEMORYSIZE > 2
461 /* Buffer has to be at least 2s long. */
462 seconds += 2;
463 #endif
464 logf("pcmbuf len: %ld", (long)seconds);
465 return seconds * BYTERATE;
468 /* Initialize the pcmbuffer the structure looks like this:
469 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
470 size_t pcmbuf_init(unsigned char *bufend)
472 pcmbuf_bufend = bufend;
473 pcmbuf_size = get_next_required_pcmbuf_size();
474 write_chunk = (struct chunkdesc *)pcmbuf_bufend -
475 NUM_CHUNK_DESCS(pcmbuf_size);
476 voicebuf = (char *)write_chunk - AUX_BUFSIZE;
477 #ifdef HAVE_CROSSFADE
478 fadebuf = voicebuf - CROSSFADE_BUFSIZE;
479 pcmbuffer = fadebuf - pcmbuf_size;
480 #else
481 pcmbuffer = voicebuf - pcmbuf_size;
482 #endif
484 init_pcmbuffers();
486 #ifdef HAVE_CROSSFADE
487 pcmbuf_finish_crossfade_enable();
488 #else
489 pcmbuf_watermark = PCMBUF_WATERMARK;
490 #endif
492 pcmbuf_play_stop();
494 return pcmbuf_bufend - pcmbuffer;
498 /** Track change */
499 void pcmbuf_monitor_track_change(bool monitor)
501 pcm_play_lock();
503 if (last_chunksize != 0)
505 /* If monitoring, wait until this track runs out. Place in
506 currently playing chunk. If not, cancel notification. */
507 track_transition = monitor;
508 read_end_chunk->end_of_track = monitor;
509 if (!monitor)
511 /* Clear all notifications */
512 struct chunkdesc *desc = first_desc;
513 struct chunkdesc *end = desc + pcmbuf_descs();
514 while (desc < end)
515 desc++->end_of_track = false;
518 else
520 /* Post now if PCM stopped and last buffer was sent. */
521 track_transition = false;
522 if (monitor)
523 audio_pcmbuf_track_change(false);
526 pcm_play_unlock();
529 bool pcmbuf_start_track_change(bool auto_skip)
531 bool crossfade = false;
532 #ifdef HAVE_CROSSFADE
533 /* Determine whether this track change needs to crossfade */
534 if(crossfade_enabled && !pcmbuf_is_crossfade_active())
536 switch(global_settings.crossfade)
538 case CROSSFADE_ENABLE_AUTOSKIP:
539 crossfade = auto_skip;
540 break;
541 case CROSSFADE_ENABLE_MANSKIP:
542 crossfade = !auto_skip;
543 break;
544 case CROSSFADE_ENABLE_SHUFFLE:
545 crossfade = global_settings.playlist_shuffle;
546 break;
547 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
548 crossfade = global_settings.playlist_shuffle || !auto_skip;
549 break;
550 case CROSSFADE_ENABLE_ALWAYS:
551 crossfade = true;
552 break;
555 #endif
557 if (!auto_skip || crossfade)
558 /* manual skip or crossfade */
560 if (crossfade)
561 { logf(" crossfade track change"); }
562 else
563 { logf(" manual track change"); }
565 pcm_play_lock();
567 /* Cancel any pending automatic gapless transition */
568 pcmbuf_monitor_track_change(false);
570 /* Can't do two crossfades at once and, no fade if pcm is off now */
571 if (
572 #ifdef HAVE_CROSSFADE
573 pcmbuf_is_crossfade_active() ||
574 #endif
575 !pcm_is_playing())
577 pcmbuf_play_stop();
578 pcm_play_unlock();
579 /* Notify playback that the track change starts now */
580 return true;
583 /* Not enough data, or not crossfading, flush the old data instead */
584 if (LOW_DATA(2) || !crossfade || low_latency_mode)
586 commit_chunk(true);
588 #ifdef HAVE_CROSSFADE
589 else
591 /* Don't enable mix mode when skipping tracks manually. */
592 crossfade_mixmode = auto_skip &&
593 global_settings.crossfade_fade_out_mixmode;
595 crossfade_auto_skip = auto_skip;
597 crossfade_track_change_started = crossfade;
599 #endif
600 pcm_play_unlock();
602 /* Keep trigger outside the play lock or HW FIFO underruns can happen
603 since frequency scaling is *not* always fast */
604 trigger_cpu_boost();
606 /* Notify playback that the track change starts now */
607 return true;
609 else /* automatic and not crossfading, so do gapless track change */
611 /* The codec is moving on to the next track, but the current track will
612 * continue to play. Set a flag to make sure the elapsed time of the
613 * current track will be updated properly, and mark the current chunk
614 * as the last one in the track. */
615 logf(" gapless track change");
616 pcmbuf_monitor_track_change(true);
617 return false;
622 /** Playback */
624 /* PCM driver callback
625 * This function has 3 major logical parts (separated by brackets both for
626 * readability and variable scoping). The first part performs the
627 * operations related to finishing off the last chunk we fed to the DMA.
628 * The second part detects the end of playlist condition when the PCM
629 * buffer is empty except for uncommitted samples. Then they are committed
630 * and sent to the PCM driver for playback. The third part performs the
631 * operations involved in sending a new chunk to the DMA. */
632 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR;
633 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
636 struct chunkdesc *pcmbuf_current = read_chunk;
637 /* Take the finished chunk out of circulation */
638 read_chunk = pcmbuf_current->link;
640 /* if during a track transition, update the elapsed time in ms */
641 if (track_transition)
642 audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE);
644 /* if last chunk in the track, stop updates and notify audio thread */
645 if (pcmbuf_current->end_of_track)
647 track_transition = false;
648 audio_pcmbuf_track_change(true);
651 /* Put the finished chunk back into circulation */
652 write_end_chunk->link = pcmbuf_current;
653 write_end_chunk = pcmbuf_current;
655 /* If we've read over the mix chunk while it's still mixing there */
656 if (pcmbuf_current == mix_chunk)
657 mix_chunk = NULL;
659 #ifdef HAVE_CROSSFADE
660 /* If we've read over the crossfade chunk while it's still fading */
661 if (pcmbuf_current == crossfade_chunk)
662 crossfade_chunk = read_chunk;
663 #endif
667 /* Commit last samples at end of playlist */
668 if (pcmbuffer_fillpos && !read_chunk)
670 logf("pcmbuf_pcm_callback: commit last samples");
671 commit_chunk(false);
676 /* Send the new chunk to the DMA */
677 if(read_chunk)
679 last_chunksize = read_chunk->size;
680 pcmbuf_unplayed_bytes -= last_chunksize;
681 *size = last_chunksize;
682 *start = read_chunk->addr;
684 else
686 /* No more chunks */
687 logf("pcmbuf_pcm_callback: no more chunks");
688 last_chunksize = 0;
689 *size = 0;
690 *start = NULL;
693 DISPLAY_DESC("callback");
696 /* Force playback */
697 void pcmbuf_play_start(void)
699 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && read_chunk != NULL)
701 logf("pcmbuf_play_start");
702 last_chunksize = read_chunk->size;
703 pcmbuf_unplayed_bytes -= last_chunksize;
704 pcm_play_data(pcmbuf_pcm_callback,
705 read_chunk->addr, last_chunksize);
709 void pcmbuf_play_stop(void)
711 logf("pcmbuf_play_stop");
712 pcm_play_stop();
714 pcmbuf_unplayed_bytes = 0;
715 mix_chunk = NULL;
716 if (read_chunk) {
717 write_end_chunk->link = read_chunk;
718 write_end_chunk = read_end_chunk;
719 read_chunk = read_end_chunk = NULL;
721 last_chunksize = 0;
722 pcmbuffer_pos = 0;
723 pcmbuffer_fillpos = 0;
724 #ifdef HAVE_CROSSFADE
725 crossfade_track_change_started = false;
726 crossfade_active = false;
727 #endif
728 track_transition = false;
729 flush_pcmbuf = false;
730 DISPLAY_DESC("play_stop");
732 /* Can unboost the codec thread here no matter who's calling,
733 * pretend full pcm buffer to unboost */
734 boost_codec_thread(10);
737 void pcmbuf_pause(bool pause)
739 logf("pcmbuf_pause: %s", pause?"pause":"play");
740 if (pcm_is_playing())
741 pcm_play_pause(!pause);
742 else if (!pause)
743 pcmbuf_play_start();
747 /** Crossfade */
749 /* Clip sample to signed 16 bit range */
750 static inline int32_t clip_sample_16(int32_t sample)
752 if ((int16_t)sample != sample)
753 sample = 0x7fff ^ (sample >> 31);
754 return sample;
757 #ifdef HAVE_CROSSFADE
758 /* Find the chunk that's (length) deep in the list. Return the position within
759 * the chunk, and leave the chunkdesc pointer pointing to the chunk. */
760 static size_t find_chunk(size_t length, struct chunkdesc **chunk)
762 while (*chunk && length >= (*chunk)->size)
764 length -= (*chunk)->size;
765 *chunk = (*chunk)->link;
767 return length;
770 /* Returns the number of bytes _NOT_ mixed/faded */
771 static size_t crossfade_mix_fade(int factor, size_t length, const char *buf,
772 size_t *out_sample, struct chunkdesc **out_chunk)
774 if (length == 0)
775 return 0;
777 const int16_t *input_buf = (const int16_t *)buf;
778 int16_t *output_buf = (int16_t *)((*out_chunk)->addr);
779 int16_t *chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
780 output_buf = &output_buf[*out_sample];
781 int32_t sample;
783 while (length)
785 /* fade left and right channel at once to keep buffer alignment */
786 int i;
787 for (i = 0; i < 2; i++)
789 if (input_buf)
790 /* fade the input buffer and mix into the chunk */
792 sample = *input_buf++;
793 sample = ((sample * factor) >> 8) + *output_buf;
794 *output_buf++ = clip_sample_16(sample);
796 else
797 /* fade the chunk only */
799 sample = *output_buf;
800 *output_buf++ = (sample * factor) >> 8;
804 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
806 /* move to next chunk as needed */
807 if (output_buf >= chunk_end)
809 *out_chunk = (*out_chunk)->link;
810 if (!(*out_chunk))
811 return length;
812 output_buf = (int16_t *)((*out_chunk)->addr);
813 chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
816 *out_sample = output_buf - (int16_t *)((*out_chunk)->addr);
817 return 0;
820 /* Initializes crossfader, calculates all necessary parameters and performs
821 * fade-out with the PCM buffer. */
822 static void crossfade_start(void)
824 size_t crossfade_rem;
825 size_t crossfade_need;
826 size_t fade_out_rem;
827 size_t fade_out_delay;
828 size_t fade_in_delay;
830 crossfade_track_change_started = false;
831 /* Reject crossfade if less than .5s of data */
832 if (LOW_DATA(2)) {
833 logf("crossfade rejected");
834 pcmbuf_play_stop();
835 return ;
838 logf("crossfade_start");
839 commit_chunk(false);
840 crossfade_active = true;
842 /* Initialize the crossfade buffer size to all of the buffered data that
843 * has not yet been sent to the DMA */
844 crossfade_rem = pcmbuf_unplayed_bytes;
845 crossfade_chunk = read_chunk->link;
846 crossfade_sample = 0;
848 /* Get fade out info from settings. */
849 fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE;
850 fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE;
852 crossfade_need = fade_out_delay + fade_out_rem;
853 if (crossfade_rem > crossfade_need)
855 if (crossfade_auto_skip)
856 /* Automatic track changes only modify the last part of the buffer,
857 * so find the right chunk and sample to start the crossfade */
859 crossfade_sample = find_chunk(crossfade_rem - crossfade_need,
860 &crossfade_chunk) / 2;
861 crossfade_rem = crossfade_need;
863 else
864 /* Manual skips occur immediately, but give time to process */
866 crossfade_rem -= crossfade_chunk->size;
867 crossfade_chunk = crossfade_chunk->link;
870 /* Truncate fade out duration if necessary. */
871 if (crossfade_rem < crossfade_need)
873 size_t crossfade_short = crossfade_need - crossfade_rem;
874 if (fade_out_rem >= crossfade_short)
875 fade_out_rem -= crossfade_short;
876 else
878 fade_out_delay -= crossfade_short - fade_out_rem;
879 fade_out_rem = 0;
882 crossfade_rem -= fade_out_delay + fade_out_rem;
884 /* Completely process the crossfade fade-out effect with current PCM buffer */
885 if (!crossfade_mixmode)
887 /* Fade out the specified amount of the already processed audio */
888 size_t total_fade_out = fade_out_rem;
889 size_t fade_out_sample;
890 struct chunkdesc *fade_out_chunk = crossfade_chunk;
892 /* Find the right chunk and sample to start fading out */
893 fade_out_delay += crossfade_sample * 2;
894 fade_out_sample = find_chunk(fade_out_delay, &fade_out_chunk) / 2;
896 while (fade_out_rem > 0)
898 /* Each 1/10 second of audio will have the same fade applied */
899 size_t block_rem = MIN(BYTERATE / 10, fade_out_rem);
900 int factor = (fade_out_rem << 8) / total_fade_out;
902 fade_out_rem -= block_rem;
904 crossfade_mix_fade(factor, block_rem, NULL,
905 &fade_out_sample, &fade_out_chunk);
908 /* zero out the rest of the buffer */
909 crossfade_mix_fade(0, crossfade_rem, NULL,
910 &fade_out_sample, &fade_out_chunk);
913 /* Initialize fade-in counters */
914 crossfade_fade_in_total = global_settings.crossfade_fade_in_duration * BYTERATE;
915 crossfade_fade_in_rem = crossfade_fade_in_total;
917 fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE;
919 /* Find the right chunk and sample to start fading in */
920 fade_in_delay += crossfade_sample * 2;
921 crossfade_sample = find_chunk(fade_in_delay, &crossfade_chunk) / 2;
922 logf("crossfade_start done!");
925 /* Perform fade-in of new track */
926 static void write_to_crossfade(size_t length)
928 if (length)
930 char *buf = fadebuf;
931 if (crossfade_fade_in_rem)
933 size_t samples;
934 int16_t *input_buf;
936 /* Fade factor for this packet */
937 int factor =
938 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
939 crossfade_fade_in_total;
940 /* Bytes to fade */
941 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
943 /* We _will_ fade this many bytes */
944 crossfade_fade_in_rem -= fade_rem;
946 if (crossfade_chunk)
948 /* Mix the data */
949 size_t fade_total = fade_rem;
950 fade_rem = crossfade_mix_fade(factor, fade_rem, buf,
951 &crossfade_sample, &crossfade_chunk);
952 length -= fade_total - fade_rem;
953 buf += fade_total - fade_rem;
954 if (!length)
955 return;
958 samples = fade_rem / 2;
959 input_buf = (int16_t *)buf;
960 /* Fade remaining samples in place */
961 while (samples--)
963 int32_t sample = *input_buf;
964 *input_buf++ = (sample * factor) >> 8;
968 if (crossfade_chunk)
970 /* Mix the data */
971 size_t mix_total = length;
972 /* A factor of 256 means mix only, no fading */
973 length = crossfade_mix_fade(256, length, buf,
974 &crossfade_sample, &crossfade_chunk);
975 buf += mix_total - length;
976 if (!length)
977 return;
980 while (length > 0)
982 COMMIT_IF_NEEDED;
983 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
984 size_t copy_n = MIN(length, pcmbuf_size - pcmbuffer_index);
985 memcpy(&pcmbuffer[pcmbuffer_index], buf, copy_n);
986 buf += copy_n;
987 pcmbuffer_fillpos += copy_n;
988 length -= copy_n;
991 /* if no more fading-in to do, stop the crossfade */
992 if (!(crossfade_fade_in_rem || crossfade_chunk))
993 crossfade_active = false;
996 static void pcmbuf_finish_crossfade_enable(void)
998 /* Copy the pending setting over now */
999 crossfade_enabled = crossfade_enable_request;
1001 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
1002 /* If crossfading, try to keep the buffer full other than 1 second */
1003 (pcmbuf_size - BYTERATE) :
1004 /* Otherwise, just use the default */
1005 PCMBUF_WATERMARK;
1008 bool pcmbuf_is_crossfade_active(void)
1010 return crossfade_active || crossfade_track_change_started;
1013 void pcmbuf_request_crossfade_enable(bool on_off)
1015 /* Next setting to be used, not applied now */
1016 crossfade_enable_request = on_off;
1019 bool pcmbuf_is_same_size(void)
1021 /* if pcmbuffer is NULL, then not set up yet even once so always */
1022 bool same_size = pcmbuffer ?
1023 (get_next_required_pcmbuf_size() == pcmbuf_size) : true;
1025 /* no buffer change needed, so finish crossfade setup now */
1026 if (same_size)
1027 pcmbuf_finish_crossfade_enable();
1029 return same_size;
1031 #endif /* HAVE_CROSSFADE */
1034 /** Voice */
1036 /* Returns pcm buffer usage in percents (0 to 100). */
1037 static int pcmbuf_usage(void)
1039 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1042 static int pcmbuf_mix_free(void)
1044 if (mix_chunk)
1046 size_t my_mix_end =
1047 (size_t)&((int16_t *)mix_chunk->addr)[pcmbuf_mix_sample];
1048 size_t my_write_pos = (size_t)&pcmbuffer[pcmbuffer_pos];
1049 if (my_write_pos < my_mix_end)
1050 my_write_pos += pcmbuf_size;
1051 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1053 return 100;
1056 void *pcmbuf_request_voice_buffer(int *count)
1058 /* A get-it-to-work-for-now hack (audio status could change by
1059 completion) */
1060 if (audio_status() & AUDIO_STATUS_PLAY)
1062 if (read_chunk == NULL)
1064 return NULL;
1066 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
1067 (mix_chunk || read_chunk->link))
1069 *count = MIN(*count, AUX_BUFSIZE/4);
1070 return voicebuf;
1072 else
1074 return NULL;
1077 else
1079 return pcmbuf_request_buffer(count);
1083 void pcmbuf_write_voice_complete(int count)
1085 /* A get-it-to-work-for-now hack (audio status could have changed) */
1086 if (!(audio_status() & AUDIO_STATUS_PLAY))
1088 pcmbuf_write_complete(count);
1089 return;
1092 int16_t *ibuf = (int16_t *)voicebuf;
1093 int16_t *obuf;
1094 size_t chunk_samples;
1096 if (mix_chunk == NULL && read_chunk != NULL)
1098 mix_chunk = read_chunk->link;
1099 /* Start 1/8s into the next chunk */
1100 pcmbuf_mix_sample = BYTERATE / 16;
1103 if (!mix_chunk)
1104 return;
1106 obuf = (int16_t *)mix_chunk->addr;
1107 chunk_samples = mix_chunk->size / sizeof (int16_t);
1109 count <<= 1;
1111 while (count-- > 0)
1113 int32_t sample = *ibuf++;
1115 if (pcmbuf_mix_sample >= chunk_samples)
1117 mix_chunk = mix_chunk->link;
1118 if (!mix_chunk)
1119 return;
1120 pcmbuf_mix_sample = 0;
1121 obuf = (int16_t *)mix_chunk->addr;
1122 chunk_samples = mix_chunk->size / 2;
1124 sample += obuf[pcmbuf_mix_sample] >> 2;
1125 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1130 /** Debug menu, other metrics */
1132 /* Amount of bytes left in the buffer. */
1133 size_t pcmbuf_free(void)
1135 if (read_chunk != NULL)
1137 void *read = (void *)read_chunk->addr;
1138 void *write = &pcmbuffer[pcmbuffer_pos + pcmbuffer_fillpos];
1139 if (read < write)
1140 return (size_t)(read - write) + pcmbuf_size;
1141 else
1142 return (size_t) (read - write);
1144 return pcmbuf_size - pcmbuffer_fillpos;
1147 size_t pcmbuf_get_bufsize(void)
1149 return pcmbuf_size;
1152 int pcmbuf_used_descs(void)
1154 struct chunkdesc *temp = read_chunk;
1155 unsigned int i = 0;
1156 while (temp) {
1157 temp = temp->link;
1158 i++;
1160 return i;
1163 int pcmbuf_descs(void)
1165 return NUM_CHUNK_DESCS(pcmbuf_size);
1168 #ifdef ROCKBOX_HAS_LOGF
1169 unsigned char *pcmbuf_get_meminfo(size_t *length)
1171 *length = pcmbuf_bufend - pcmbuffer;
1172 return pcmbuffer;
1174 #endif
1177 /** Misc */
1179 bool pcmbuf_is_lowdata(void)
1181 if (!pcm_is_playing() || pcm_is_paused()
1182 #ifdef HAVE_CROSSFADE
1183 || pcmbuf_is_crossfade_active()
1184 #endif
1186 return false;
1188 #if MEMORYSIZE > 2
1189 /* 1 seconds of buffer is low data */
1190 return LOW_DATA(4);
1191 #else
1192 /* under watermark is low data */
1193 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
1194 #endif
1197 void pcmbuf_set_low_latency(bool state)
1199 low_latency_mode = state;
1202 unsigned long pcmbuf_get_latency(void)
1204 return (pcmbuf_unplayed_bytes + pcm_get_bytes_waiting()) * 1000 / BYTERATE;
1207 #ifndef HAVE_HARDWARE_BEEP
1208 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
1209 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
1211 /* Generates a constant square wave sound with a given frequency
1212 in Hertz for a duration in milliseconds. */
1213 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
1215 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
1216 int32_t phase = 0;
1217 int16_t *bufptr, *bufstart, *bufend;
1218 int32_t sample;
1219 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
1220 bool mix = read_chunk != NULL && read_chunk->link != NULL;
1221 int i;
1223 bufend = SKIPBYTES((int16_t *)pcmbuffer, pcmbuf_size);
1225 /* Find the insertion point and set bufstart to the start of it */
1226 if (mix)
1228 /* Get the currently playing chunk at the current position. */
1229 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
1231 /* If above isn't implemented or pcm is stopped, no beepeth. */
1232 if (!bufstart || !pcm_is_playing())
1233 return;
1235 /* Give 5ms clearance. */
1236 bufstart += BYTERATE / 200;
1238 #ifdef HAVE_PCM_DMA_ADDRESS
1239 /* Returned peak addresses are DMA addresses */
1240 bufend = pcm_dma_addr(bufend);
1241 #endif
1243 /* Wrapped above? */
1244 if (bufstart >= bufend)
1245 bufstart -= pcmbuf_size;
1247 /* NOTE: On some targets using hardware DMA, cache range flushing may
1248 * be required or the writes may not be picked up by the controller.
1249 * An incremental flush should be done periodically during the mixdown. */
1251 else if (nsamples <= MINIBUF_SAMPLES)
1253 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
1254 /* Use mini buffer */
1255 bufstart = minibuf;
1256 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1258 else if (!audio_buffer_state_trashed())
1260 /* Use pcmbuffer */
1261 bufstart = (int16_t *)pcmbuffer;
1263 else
1265 /* No place */
1266 return;
1269 bufptr = bufstart;
1271 /* Mix square wave into buffer */
1272 for (i = 0; i < nsamples; ++i)
1274 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
1275 sample = mix ? *bufptr : 0;
1276 *bufptr++ = clip_sample_16(sample + amp);
1277 if (bufptr >= bufend)
1278 bufptr = (int16_t *)pcmbuffer;
1279 sample = mix ? *bufptr : 0;
1280 *bufptr++ = clip_sample_16(sample + amp);
1281 if (bufptr >= bufend)
1282 bufptr = (int16_t *)pcmbuffer;
1284 phase += step;
1287 pcm_play_lock();
1288 #ifdef HAVE_RECORDING
1289 pcm_rec_lock();
1290 #endif
1292 /* Kick off playback if required and it won't interfere */
1293 if (!pcm_is_playing()
1294 #ifdef HAVE_RECORDING
1295 && !pcm_is_recording()
1296 #endif
1299 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
1302 pcm_play_unlock();
1303 #ifdef HAVE_RECORDING
1304 pcm_rec_unlock();
1305 #endif
1307 #endif /* HAVE_HARDWARE_BEEP */