Add initial Packard Bell Vibe 500 port, by Szymon Dziok
[kugel-rb.git] / apps / pcmbuf.c
blobebb058cf87da2f78410cb4d55f1b2afbb24eb089
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"
30 /* Define LOGF_ENABLE to enable logf output in this file */
31 /*#define LOGF_ENABLE*/
32 #include "logf.h"
33 #ifndef SIMULATOR
34 #include "cpu.h"
35 #endif
36 #include <string.h>
37 #include "settings.h"
38 #include "audio.h"
39 #include "voice_thread.h"
40 #include "dsp.h"
42 #define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
43 on the pcm buffer */
44 #define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
45 chunks on the pcm buffer (or we run out
46 of buffer descriptors, which is
47 non-fatal) */
48 #define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
49 this to the DMA */
50 #define PCMBUF_MIX_CHUNK 8192 /* This is the maximum size of one packet
51 for mixing (crossfade or voice) */
53 /* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */
54 #define BYTERATE (NATIVE_FREQUENCY * 4)
56 #if MEMORYSIZE > 2
57 /* Keep watermark high for iPods at least (2s) */
58 #define PCMBUF_WATERMARK (BYTERATE * 2)
59 #else
60 #define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */
61 #endif
63 /* Structure we can use to queue pcm chunks in memory to be played
64 * by the driver code. */
65 struct chunkdesc
67 void *addr;
68 size_t size;
69 struct chunkdesc* link;
70 /* true if last chunk in the track */
71 bool end_of_track;
74 #define NUM_CHUNK_DESCS(bufsize) \
75 ((bufsize) / PCMBUF_MINAVG_CHUNK)
77 /* Size of the PCM buffer. */
78 static size_t pcmbuf_size IDATA_ATTR = 0;
79 static char *pcmbuf_bufend IDATA_ATTR;
80 static char *pcmbuffer IDATA_ATTR;
81 /* Current PCM buffer write index. */
82 static size_t pcmbuffer_pos IDATA_ATTR;
83 /* Amount pcmbuffer_pos will be increased.*/
84 static size_t pcmbuffer_fillpos IDATA_ATTR;
86 /* Gapless playback */
87 static bool end_of_track IDATA_ATTR;
88 bool track_transition IDATA_ATTR;
90 #ifdef HAVE_CROSSFADE
91 /* Crossfade buffer */
92 static char *fadebuf IDATA_ATTR;
94 /* Crossfade related state */
95 static bool crossfade_enabled;
96 static bool crossfade_enable_request;
97 static bool crossfade_mixmode;
98 static bool crossfade_auto_skip;
99 static bool crossfade_active IDATA_ATTR;
100 static bool crossfade_track_change_started IDATA_ATTR;
102 /* Track the current location for processing crossfade */
103 static struct chunkdesc *crossfade_chunk IDATA_ATTR;
104 static size_t crossfade_sample IDATA_ATTR;
106 /* Counters for fading in new data */
107 static size_t crossfade_fade_in_total IDATA_ATTR;
108 static size_t crossfade_fade_in_rem IDATA_ATTR;
109 #endif
111 static struct chunkdesc *read_chunk IDATA_ATTR;
112 static struct chunkdesc *read_end_chunk IDATA_ATTR;
113 static struct chunkdesc *write_chunk IDATA_ATTR;
114 static struct chunkdesc *write_end_chunk IDATA_ATTR;
115 static size_t last_chunksize IDATA_ATTR;
117 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
118 static size_t pcmbuf_watermark IDATA_ATTR;
120 /* Voice */
121 static char *voicebuf IDATA_ATTR;
122 static struct chunkdesc *mix_chunk IDATA_ATTR;
123 static size_t pcmbuf_mix_sample IDATA_ATTR;
125 static bool low_latency_mode = false;
126 static bool flush_pcmbuf = false;
128 #ifdef HAVE_PRIORITY_SCHEDULING
129 static int codec_thread_priority = PRIORITY_PLAYBACK;
130 #endif
132 extern unsigned int codec_thread_id;
134 /* Helpful macros for use in conditionals this assumes some of the above
135 * static variable names */
136 #define COMMIT_IF_NEEDED if(pcmbuffer_fillpos > PCMBUF_TARGET_CHUNK || \
137 (pcmbuffer_pos + pcmbuffer_fillpos) >= pcmbuf_size) commit_chunk(false)
138 #define LOW_DATA(quarter_secs) \
139 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
141 #ifdef HAVE_CROSSFADE
142 static void crossfade_start(void);
143 static void write_to_crossfade(size_t length);
144 static void pcmbuf_finish_crossfade_enable(void);
145 #endif
148 /**************************************/
150 /* define this to show detailed chunkdesc usage information on the sim console */
151 /*#define DESC_DEBUG*/
153 #ifndef SIMULATOR
154 #undef DESC_DEBUG
155 #endif
156 #ifdef DESC_DEBUG
157 static struct chunkdesc *first_desc;
158 static bool show_desc_in_use = false;
159 #define DISPLAY_DESC(caller) while(!show_desc(caller))
160 #define DESC_IDX(desc) (desc ? desc - first_desc : -1)
161 #define SHOW_DESC(desc) if(DESC_IDX(desc)==-1) DEBUGF("--"); \
162 else DEBUGF("%02d", DESC_IDX(desc))
163 #define SHOW_DESC_LINK(desc) if(desc){SHOW_DESC(desc->link);DEBUGF(" ");} \
164 else DEBUGF("-- ")
165 #define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \
166 SHOW_DESC_LINK(desc)
167 #define SHOW_POINT(tag,desc) DEBUGF("%s",tag);SHOW_DETAIL(desc)
168 #define SHOW_NUM(num,desc) DEBUGF("%02d>",num);SHOW_DESC_LINK(desc)
170 static bool show_desc(char *caller)
172 if (show_desc_in_use) return false;
173 show_desc_in_use = true;
174 DEBUGF("%-14s\t", caller);
175 SHOW_POINT("r", read_chunk);
176 SHOW_POINT("re", read_end_chunk);
177 DEBUGF(" ");
178 SHOW_POINT("w", write_chunk);
179 SHOW_POINT("we", write_end_chunk);
180 DEBUGF("\n");
181 int i;
182 for (i = 0; i < pcmbuf_descs(); i++)
184 SHOW_NUM(i, (first_desc + i));
185 if (i%10 == 9) DEBUGF("\n");
187 DEBUGF("\n\n");
188 show_desc_in_use = false;
189 return true;
191 #else
192 #define DISPLAY_DESC(caller) do{}while(0)
193 #endif
196 /** Accept new PCM data */
198 /* Commit PCM buffer samples as a new chunk for playback */
199 static void commit_chunk(bool flush_next_time)
201 if (!pcmbuffer_fillpos)
202 return;
204 /* Never use the last buffer descriptor */
205 while (write_chunk == write_end_chunk) {
206 /* If this happens, something is being stupid */
207 if (!pcm_is_playing()) {
208 logf("commit_chunk error");
209 pcmbuf_play_start();
211 /* Let approximately one chunk of data playback */
212 sleep(HZ * PCMBUF_TARGET_CHUNK / BYTERATE);
215 /* commit the chunk */
217 register size_t size = pcmbuffer_fillpos;
218 /* Grab the next description to write, and change the write pointer */
219 register struct chunkdesc *pcmbuf_current = write_chunk;
220 write_chunk = pcmbuf_current->link;
221 /* Fill in the values in the new buffer chunk */
222 pcmbuf_current->addr = &pcmbuffer[pcmbuffer_pos];
223 pcmbuf_current->size = size;
224 pcmbuf_current->end_of_track = end_of_track;
225 pcmbuf_current->link = NULL;
226 end_of_track = false; /* This is single use only */
228 if (read_chunk != NULL)
230 if (flush_pcmbuf)
232 /* Flush! Discard all data after the currently playing chunk,
233 and make the current chunk play next */
234 logf("commit_chunk: flush");
235 write_end_chunk->link = read_chunk->link;
236 read_chunk->link = pcmbuf_current;
237 while (write_end_chunk->link)
239 write_end_chunk = write_end_chunk->link;
240 pcmbuf_unplayed_bytes -= write_end_chunk->size;
243 /* If there is already a read buffer setup, add to it */
244 else
245 read_end_chunk->link = pcmbuf_current;
247 else
249 /* Otherwise create the buffer */
250 read_chunk = pcmbuf_current;
253 /* If flush_next_time is true, then the current chunk will be thrown out
254 * and the next chunk to be committed will be the next to be played.
255 * This is used to empty the PCM buffer for a track change. */
256 flush_pcmbuf = flush_next_time;
258 /* This is now the last buffer to read */
259 read_end_chunk = pcmbuf_current;
261 /* Update bytes counters */
262 pcmbuf_unplayed_bytes += size;
264 pcmbuffer_pos += size;
265 if (pcmbuffer_pos >= pcmbuf_size)
266 pcmbuffer_pos -= pcmbuf_size;
268 pcmbuffer_fillpos = 0;
269 DISPLAY_DESC("commit_chunk");
272 /* Set priority of the codec thread */
273 #ifdef HAVE_PRIORITY_SCHEDULING
274 static void boost_codec_thread(bool boost)
276 /* Keep voice and codec threads at the same priority or else voice
277 * will starve if the codec thread's priority is boosted. */
278 if (boost)
280 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
281 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
283 if (priority != codec_thread_priority)
285 codec_thread_priority = priority;
286 thread_set_priority(codec_thread_id, priority);
287 voice_thread_set_priority(priority);
290 else if (codec_thread_priority != PRIORITY_PLAYBACK)
292 thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
293 voice_thread_set_priority(PRIORITY_PLAYBACK);
294 codec_thread_priority = PRIORITY_PLAYBACK;
297 #else
298 #define boost_codec_thread(boost) do{}while(0)
299 #endif /* HAVE_PRIORITY_SCHEDULING */
301 /* Return true if the PCM buffer is able to receive new data.
302 * Also maintain buffer level above the watermark. */
303 static bool prepare_insert(size_t length)
305 if (low_latency_mode)
307 /* 1/4s latency. */
308 if (!LOW_DATA(1) && pcm_is_playing())
309 return false;
312 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
313 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
314 return false;
316 /* Maintain the buffer level above the watermark */
317 if (pcm_is_playing())
319 /* Only codec thread initiates boost - voice boosts the cpu when playing
320 a clip */
321 #ifndef SIMULATOR
322 if (thread_get_current() == codec_thread_id)
323 #endif /* SIMULATOR */
325 if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
327 /* Fill PCM buffer by boosting cpu */
328 trigger_cpu_boost();
329 /* If buffer is critically low, override UI priority, else
330 set back to the original priority. */
331 boost_codec_thread(LOW_DATA(2));
333 else
335 boost_codec_thread(false);
339 #ifdef HAVE_CROSSFADE
340 /* Disable crossfade if < .5s of audio */
341 if (LOW_DATA(2))
343 crossfade_active = false;
345 #endif
347 else /* pcm_is_playing */
349 /* Boost CPU for pre-buffer */
350 trigger_cpu_boost();
352 /* If pre-buffered to the watermark, start playback */
353 #if MEMORYSIZE > 2
354 if (!LOW_DATA(4))
355 #else
356 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
357 #endif
359 logf("pcm starting");
360 if (!(audio_status() & AUDIO_STATUS_PAUSE))
361 pcmbuf_play_start();
365 return true;
368 /* Request space in the buffer for writing output samples */
369 void *pcmbuf_request_buffer(int *count)
371 #ifdef HAVE_CROSSFADE
372 /* we're going to crossfade to a new track, which is now on its way */
373 if (crossfade_track_change_started)
374 crossfade_start();
376 /* crossfade has begun, put the new track samples in fadebuf */
377 if (crossfade_active)
379 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
380 return fadebuf;
382 else
383 #endif
384 /* if possible, reserve room in the PCM buffer for new samples */
386 if(prepare_insert(*count << 2))
388 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
389 if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK)
391 /* Usual case, there's space here */
392 return &pcmbuffer[pcmbuffer_index];
394 else
396 /* Wrap the buffer, the new samples go at the beginning */
397 commit_chunk(false);
398 pcmbuffer_pos = 0;
399 return &pcmbuffer[0];
403 /* PCM buffer not ready to receive new data yet */
404 return NULL;
407 /* Handle new samples to the buffer */
408 void pcmbuf_write_complete(int count)
410 size_t length = (size_t)(unsigned int)count << 2;
411 #ifdef HAVE_CROSSFADE
412 if (crossfade_active)
413 write_to_crossfade(length);
414 else
415 #endif
417 pcmbuffer_fillpos += length;
418 COMMIT_IF_NEEDED;
423 /** Init */
425 static inline void init_pcmbuffers(void)
427 #ifdef DESC_DEBUG
428 first_desc = write_chunk;
429 #endif
430 struct chunkdesc *next = write_chunk;
431 next++;
432 write_end_chunk = write_chunk;
433 while ((void *)next < (void *)pcmbuf_bufend) {
434 write_end_chunk->link=next;
435 write_end_chunk=next;
436 next++;
438 DISPLAY_DESC("init");
441 static size_t get_next_required_pcmbuf_size(void)
443 size_t seconds = 1;
445 #ifdef HAVE_CROSSFADE
446 if (crossfade_enable_request)
447 seconds += global_settings.crossfade_fade_out_delay +
448 global_settings.crossfade_fade_out_duration;
449 #endif
451 #if MEMORYSIZE > 2
452 /* Buffer has to be at least 2s long. */
453 seconds += 2;
454 #endif
455 logf("pcmbuf len: %ld", (long)seconds);
456 return seconds * BYTERATE;
459 /* Initialize the pcmbuffer the structure looks like this:
460 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
461 size_t pcmbuf_init(unsigned char *bufend)
463 pcmbuf_bufend = bufend;
464 pcmbuf_size = get_next_required_pcmbuf_size();
465 write_chunk = (struct chunkdesc *)pcmbuf_bufend -
466 NUM_CHUNK_DESCS(pcmbuf_size);
467 voicebuf = (char *)write_chunk - PCMBUF_MIX_CHUNK;
468 #ifdef HAVE_CROSSFADE
469 fadebuf = voicebuf - PCMBUF_MIX_CHUNK;
470 pcmbuffer = fadebuf - pcmbuf_size;
471 #else
472 pcmbuffer = voicebuf - pcmbuf_size;
473 #endif
475 init_pcmbuffers();
477 #ifdef HAVE_CROSSFADE
478 pcmbuf_finish_crossfade_enable();
479 #else
480 pcmbuf_watermark = PCMBUF_WATERMARK;
481 #endif
483 pcmbuf_play_stop();
485 return pcmbuf_bufend - pcmbuffer;
489 /** Track change */
491 void pcmbuf_start_track_change(bool auto_skip)
493 bool crossfade = false;
494 #ifdef HAVE_CROSSFADE
495 /* Determine whether this track change needs to crossfade */
496 if(crossfade_enabled && !pcmbuf_is_crossfade_active())
498 switch(global_settings.crossfade)
500 case CROSSFADE_ENABLE_AUTOSKIP:
501 crossfade = auto_skip;
502 break;
503 case CROSSFADE_ENABLE_MANSKIP:
504 crossfade = !auto_skip;
505 break;
506 case CROSSFADE_ENABLE_SHUFFLE:
507 crossfade = global_settings.playlist_shuffle;
508 break;
509 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
510 crossfade = global_settings.playlist_shuffle || !auto_skip;
511 break;
512 case CROSSFADE_ENABLE_ALWAYS:
513 crossfade = true;
514 break;
517 #endif
519 if (!auto_skip || crossfade)
520 /* manual skip or crossfade */
522 if (crossfade)
523 { logf(" crossfade track change"); }
524 else
525 { logf(" manual track change"); }
527 /* Notify the wps that the track change starts now */
528 audio_post_track_change(false);
530 /* Can't do two crossfades at once and, no fade if pcm is off now */
531 if (
532 #ifdef HAVE_CROSSFADE
533 pcmbuf_is_crossfade_active() ||
534 #endif
535 !pcm_is_playing())
537 pcmbuf_play_stop();
538 return;
541 trigger_cpu_boost();
543 /* Not enough data, or not crossfading, flush the old data instead */
544 if (LOW_DATA(2) || !crossfade || low_latency_mode)
546 commit_chunk(true);
547 return;
550 #ifdef HAVE_CROSSFADE
551 /* Don't enable mix mode when skipping tracks manually. */
552 crossfade_mixmode = auto_skip && global_settings.crossfade_fade_out_mixmode;
554 crossfade_auto_skip = auto_skip;
556 crossfade_track_change_started = crossfade;
557 #endif
559 else /* automatic and not crossfading, so do gapless track change */
561 /* The codec is moving on to the next track, but the current track will
562 * continue to play. Set a flag to make sure the elapsed time of the
563 * current track will be updated properly, and mark the current chunk
564 * as the last one in the track. */
565 logf(" gapless track change");
566 track_transition = true;
567 end_of_track = true;
572 /** Playback */
574 /* PCM driver callback
575 * This function has 3 major logical parts (separated by brackets both for
576 * readability and variable scoping). The first part performs the
577 * operations related to finishing off the last chunk we fed to the DMA.
578 * The second part detects the end of playlist condition when the PCM
579 * buffer is empty except for uncommitted samples. Then they are committed
580 * and sent to the PCM driver for playback. The third part performs the
581 * operations involved in sending a new chunk to the DMA. */
582 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR;
583 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
586 struct chunkdesc *pcmbuf_current = read_chunk;
587 /* Take the finished chunk out of circulation */
588 read_chunk = pcmbuf_current->link;
590 /* if during a track transition, update the elapsed time in ms */
591 if (track_transition)
592 audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE);
594 /* if last chunk in the track, stop updates and notify audio thread */
595 if (pcmbuf_current->end_of_track)
597 track_transition = false;
598 audio_post_track_change(true);
601 /* Put the finished chunk back into circulation */
602 write_end_chunk->link = pcmbuf_current;
603 write_end_chunk = pcmbuf_current;
605 /* If we've read over the mix chunk while it's still mixing there */
606 if (pcmbuf_current == mix_chunk)
607 mix_chunk = NULL;
609 #ifdef HAVE_CROSSFADE
610 /* If we've read over the crossfade chunk while it's still fading */
611 if (pcmbuf_current == crossfade_chunk)
612 crossfade_chunk = read_chunk;
613 #endif
617 /* Commit last samples at end of playlist */
618 if (pcmbuffer_fillpos && !read_chunk)
620 logf("pcmbuf_pcm_callback: commit last samples");
621 commit_chunk(false);
626 /* Send the new chunk to the PCM */
627 if(read_chunk)
629 size_t current_size = read_chunk->size;
631 pcmbuf_unplayed_bytes -= current_size;
632 last_chunksize = current_size;
633 *size = current_size;
634 *start = read_chunk->addr;
636 else
638 /* No more chunks */
639 logf("pcmbuf_pcm_callback: no more chunks");
640 last_chunksize = 0;
641 *size = 0;
642 *start = NULL;
645 DISPLAY_DESC("callback");
648 /* Force playback */
649 void pcmbuf_play_start(void)
651 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && read_chunk != NULL)
653 logf("pcmbuf_play_start");
654 last_chunksize = read_chunk->size;
655 pcmbuf_unplayed_bytes -= last_chunksize;
656 pcm_play_data(pcmbuf_pcm_callback,
657 (unsigned char *)read_chunk->addr, last_chunksize);
661 void pcmbuf_play_stop(void)
663 logf("pcmbuf_play_stop");
664 pcm_play_stop();
666 pcmbuf_unplayed_bytes = 0;
667 mix_chunk = NULL;
668 if (read_chunk) {
669 write_end_chunk->link = read_chunk;
670 write_end_chunk = read_end_chunk;
671 read_chunk = read_end_chunk = NULL;
673 pcmbuffer_pos = 0;
674 pcmbuffer_fillpos = 0;
675 #ifdef HAVE_CROSSFADE
676 crossfade_track_change_started = false;
677 crossfade_active = false;
678 #endif
679 end_of_track = false;
680 track_transition = false;
681 flush_pcmbuf = false;
682 DISPLAY_DESC("play_stop");
684 /* Can unboost the codec thread here no matter who's calling */
685 boost_codec_thread(false);
688 void pcmbuf_pause(bool pause)
690 logf("pcmbuf_pause: %s", pause?"pause":"play");
691 if (pcm_is_playing())
692 pcm_play_pause(!pause);
693 else if (!pause)
694 pcmbuf_play_start();
698 /** Crossfade */
700 /* Clip sample to signed 16 bit range */
701 static inline int32_t clip_sample_16(int32_t sample)
703 if ((int16_t)sample != sample)
704 sample = 0x7fff ^ (sample >> 31);
705 return sample;
708 #ifdef HAVE_CROSSFADE
709 /* Find the chunk that's (length) deep in the list. Return the position within
710 * the chunk, and leave the chunkdesc pointer pointing to the chunk. */
711 static size_t find_chunk(size_t length, struct chunkdesc **chunk)
713 while (*chunk && length >= (*chunk)->size)
715 length -= (*chunk)->size;
716 *chunk = (*chunk)->link;
718 return length / 2;
721 /* Returns the number of bytes _NOT_ mixed/faded */
722 static size_t crossfade_mix_fade(int factor, size_t length, const char *buf,
723 size_t *out_sample, struct chunkdesc **out_chunk)
725 if (length == 0)
726 return 0;
728 const int16_t *input_buf = (const int16_t *)buf;
729 int16_t *output_buf = (int16_t *)((*out_chunk)->addr);
730 int16_t *chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
731 output_buf = &output_buf[*out_sample];
732 int32_t sample;
734 while (length)
736 /* fade left and right channel at once to keep buffer alignment */
737 int i;
738 for (i = 0; i < 2; i++)
740 if (input_buf)
741 /* fade the input buffer and mix into the chunk */
743 sample = *input_buf++;
744 sample = ((sample * factor) >> 8) + *output_buf;
745 *output_buf++ = clip_sample_16(sample);
747 else
748 /* fade the chunk only */
750 sample = *output_buf;
751 *output_buf++ = (sample * factor) >> 8;
755 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
757 /* move to next chunk as needed */
758 if (output_buf >= chunk_end)
760 *out_chunk = (*out_chunk)->link;
761 if (!(*out_chunk))
762 return length;
763 output_buf = (int16_t *)((*out_chunk)->addr);
764 chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
767 *out_sample = output_buf - (int16_t *)((*out_chunk)->addr);
768 return 0;
771 /* Initializes crossfader, calculates all necessary parameters and performs
772 * fade-out with the PCM buffer. */
773 static void crossfade_start(void)
775 size_t crossfade_rem;
776 size_t crossfade_need;
777 size_t fade_out_rem;
778 size_t fade_out_delay;
779 size_t fade_in_delay;
781 crossfade_track_change_started = false;
782 /* Reject crossfade if less than .5s of data */
783 if (LOW_DATA(2)) {
784 logf("crossfade rejected");
785 pcmbuf_play_stop();
786 return ;
789 logf("crossfade_start");
790 commit_chunk(false);
791 crossfade_active = true;
793 /* Initialize the crossfade buffer size to all of the buffered data that
794 * has not yet been sent to the DMA */
795 crossfade_rem = pcmbuf_unplayed_bytes;
796 crossfade_chunk = read_chunk->link;
797 crossfade_sample = 0;
799 /* Get fade out info from settings. */
800 fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE;
801 fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE;
803 crossfade_need = fade_out_delay + fade_out_rem;
804 if (crossfade_rem > crossfade_need)
806 if (crossfade_auto_skip)
807 /* Automatic track changes only modify the last part of the buffer,
808 * so find the right chunk and sample to start the crossfade */
810 crossfade_sample = find_chunk(crossfade_rem - crossfade_need,
811 &crossfade_chunk);
812 crossfade_rem = crossfade_need;
814 else
815 /* Manual skips occur immediately, but give time to process */
817 crossfade_rem -= crossfade_chunk->size;
818 crossfade_chunk = crossfade_chunk->link;
821 /* Truncate fade out duration if necessary. */
822 if (crossfade_rem < crossfade_need)
824 size_t crossfade_short = crossfade_need - crossfade_rem;
825 if (fade_out_rem >= crossfade_short)
826 fade_out_rem -= crossfade_short;
827 else
829 fade_out_delay -= crossfade_short - fade_out_rem;
830 fade_out_rem = 0;
833 crossfade_rem -= fade_out_delay + fade_out_rem;
835 /* Completely process the crossfade fade-out effect with current PCM buffer */
836 if (!crossfade_mixmode)
838 /* Fade out the specified amount of the already processed audio */
839 size_t total_fade_out = fade_out_rem;
840 size_t fade_out_sample;
841 struct chunkdesc *fade_out_chunk = crossfade_chunk;
843 /* Find the right chunk and sample to start fading out */
844 fade_out_delay += crossfade_sample * 2;
845 fade_out_sample = find_chunk(fade_out_delay, &fade_out_chunk);
847 while (fade_out_rem > 0)
849 /* Each 1/10 second of audio will have the same fade applied */
850 size_t block_rem = MIN(BYTERATE / 10, fade_out_rem);
851 int factor = (fade_out_rem << 8) / total_fade_out;
853 fade_out_rem -= block_rem;
855 crossfade_mix_fade(factor, block_rem, NULL,
856 &fade_out_sample, &fade_out_chunk);
859 /* zero out the rest of the buffer */
860 crossfade_mix_fade(0, crossfade_rem, NULL,
861 &fade_out_sample, &fade_out_chunk);
864 /* Initialize fade-in counters */
865 crossfade_fade_in_total = global_settings.crossfade_fade_in_duration * BYTERATE;
866 crossfade_fade_in_rem = crossfade_fade_in_total;
868 fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE;
870 /* Find the right chunk and sample to start fading in */
871 fade_in_delay += crossfade_sample * 2;
872 crossfade_sample = find_chunk(fade_in_delay, &crossfade_chunk);
873 logf("crossfade_start done!");
876 /* Perform fade-in of new track */
877 static void write_to_crossfade(size_t length)
879 if (length)
881 char *buf = fadebuf;
882 if (crossfade_fade_in_rem)
884 size_t samples;
885 int16_t *input_buf;
887 /* Fade factor for this packet */
888 int factor =
889 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
890 crossfade_fade_in_total;
891 /* Bytes to fade */
892 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
894 /* We _will_ fade this many bytes */
895 crossfade_fade_in_rem -= fade_rem;
897 if (crossfade_chunk)
899 /* Mix the data */
900 size_t fade_total = fade_rem;
901 fade_rem = crossfade_mix_fade(factor, fade_rem, buf,
902 &crossfade_sample, &crossfade_chunk);
903 length -= fade_total - fade_rem;
904 buf += fade_total - fade_rem;
905 if (!length)
906 return;
909 samples = fade_rem / 2;
910 input_buf = (int16_t *)buf;
911 /* Fade remaining samples in place */
912 while (samples--)
914 int32_t sample = *input_buf;
915 *input_buf++ = (sample * factor) >> 8;
919 if (crossfade_chunk)
921 /* Mix the data */
922 size_t mix_total = length;
923 /* A factor of 256 means mix only, no fading */
924 length = crossfade_mix_fade(256, length, buf,
925 &crossfade_sample, &crossfade_chunk);
926 buf += mix_total - length;
927 if (!length)
928 return;
931 /* Commit samples to the buffer */
932 while (!prepare_insert(length))
933 sleep(1);
934 while (length > 0)
936 COMMIT_IF_NEEDED;
937 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
938 size_t copy_n = MIN(length, pcmbuf_size - pcmbuffer_index);
939 memcpy(&pcmbuffer[pcmbuffer_index], buf, copy_n);
940 buf += copy_n;
941 pcmbuffer_fillpos += copy_n;
942 length -= copy_n;
945 /* if no more fading-in to do, stop the crossfade */
946 if (!(crossfade_fade_in_rem || crossfade_chunk))
947 crossfade_active = false;
950 static void pcmbuf_finish_crossfade_enable(void)
952 /* Copy the pending setting over now */
953 crossfade_enabled = crossfade_enable_request;
955 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
956 /* If crossfading, try to keep the buffer full other than 1 second */
957 (pcmbuf_size - BYTERATE) :
958 /* Otherwise, just use the default */
959 PCMBUF_WATERMARK;
962 bool pcmbuf_is_crossfade_active(void)
964 return crossfade_active || crossfade_track_change_started;
967 void pcmbuf_request_crossfade_enable(bool on_off)
969 /* Next setting to be used, not applied now */
970 crossfade_enable_request = on_off;
973 bool pcmbuf_is_same_size(void)
975 /* if pcmbuffer is NULL, then not set up yet even once so always */
976 bool same_size = pcmbuffer ?
977 (get_next_required_pcmbuf_size() == pcmbuf_size) : true;
979 /* no buffer change needed, so finish crossfade setup now */
980 if (same_size)
981 pcmbuf_finish_crossfade_enable();
983 return same_size;
985 #endif /* HAVE_CROSSFADE */
988 /** Voice */
990 /* Returns pcm buffer usage in percents (0 to 100). */
991 static int pcmbuf_usage(void)
993 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
996 static int pcmbuf_mix_free(void)
998 if (mix_chunk)
1000 size_t my_mix_end =
1001 (size_t)&((int16_t *)mix_chunk->addr)[pcmbuf_mix_sample];
1002 size_t my_write_pos = (size_t)&pcmbuffer[pcmbuffer_pos];
1003 if (my_write_pos < my_mix_end)
1004 my_write_pos += pcmbuf_size;
1005 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1007 return 100;
1010 void *pcmbuf_request_voice_buffer(int *count)
1012 /* A get-it-to-work-for-now hack (audio status could change by
1013 completion) */
1014 if (audio_status() & AUDIO_STATUS_PLAY)
1016 if (read_chunk == NULL)
1018 return NULL;
1020 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
1021 (mix_chunk || read_chunk->link))
1023 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
1024 return voicebuf;
1026 else
1028 return NULL;
1031 else
1033 return pcmbuf_request_buffer(count);
1037 void pcmbuf_write_voice_complete(int count)
1039 /* A get-it-to-work-for-now hack (audio status could have changed) */
1040 if (!(audio_status() & AUDIO_STATUS_PLAY))
1042 pcmbuf_write_complete(count);
1043 return;
1046 int16_t *ibuf = (int16_t *)voicebuf;
1047 int16_t *obuf;
1048 size_t chunk_samples;
1050 if (mix_chunk == NULL && read_chunk != NULL)
1052 mix_chunk = read_chunk->link;
1053 /* Start 1/8s into the next chunk */
1054 pcmbuf_mix_sample = BYTERATE / 16;
1057 if (!mix_chunk)
1058 return;
1060 obuf = (int16_t *)mix_chunk->addr;
1061 chunk_samples = mix_chunk->size / sizeof (int16_t);
1063 count <<= 1;
1065 while (count-- > 0)
1067 int32_t sample = *ibuf++;
1069 if (pcmbuf_mix_sample >= chunk_samples)
1071 mix_chunk = mix_chunk->link;
1072 if (!mix_chunk)
1073 return;
1074 pcmbuf_mix_sample = 0;
1075 obuf = mix_chunk->addr;
1076 chunk_samples = mix_chunk->size / 2;
1078 sample += obuf[pcmbuf_mix_sample] >> 2;
1079 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1084 /** Debug menu, other metrics */
1086 /* Amount of bytes left in the buffer. */
1087 size_t pcmbuf_free(void)
1089 if (read_chunk != NULL)
1091 void *read = read_chunk->addr;
1092 void *write = &pcmbuffer[pcmbuffer_pos + pcmbuffer_fillpos];
1093 if (read < write)
1094 return (size_t)(read - write) + pcmbuf_size;
1095 else
1096 return (size_t) (read - write);
1098 return pcmbuf_size - pcmbuffer_fillpos;
1101 size_t pcmbuf_get_bufsize(void)
1103 return pcmbuf_size;
1106 int pcmbuf_used_descs(void)
1108 struct chunkdesc *temp = read_chunk;
1109 unsigned int i = 0;
1110 while (temp) {
1111 temp = temp->link;
1112 i++;
1114 return i;
1117 int pcmbuf_descs(void)
1119 return NUM_CHUNK_DESCS(pcmbuf_size);
1122 #ifdef ROCKBOX_HAS_LOGF
1123 unsigned char *pcmbuf_get_meminfo(size_t *length)
1125 *length = pcmbuf_bufend - pcmbuffer;
1126 return pcmbuffer;
1128 #endif
1131 /** Misc */
1133 bool pcmbuf_is_lowdata(void)
1135 if (!pcm_is_playing() || pcm_is_paused()
1136 #ifdef HAVE_CROSSFADE
1137 || pcmbuf_is_crossfade_active()
1138 #endif
1140 return false;
1142 #if MEMORYSIZE > 2
1143 /* 1 seconds of buffer is low data */
1144 return LOW_DATA(4);
1145 #else
1146 /* under watermark is low data */
1147 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
1148 #endif
1151 void pcmbuf_set_low_latency(bool state)
1153 low_latency_mode = state;
1156 unsigned long pcmbuf_get_latency(void)
1158 return (pcmbuf_unplayed_bytes + pcm_get_bytes_waiting()) * 1000 / BYTERATE;
1161 #ifndef HAVE_HARDWARE_BEEP
1162 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
1163 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
1165 /* Generates a constant square wave sound with a given frequency
1166 in Hertz for a duration in milliseconds. */
1167 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
1169 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
1170 int32_t phase = 0;
1171 int16_t *bufptr, *bufstart, *bufend;
1172 int32_t sample;
1173 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
1174 bool mix = read_chunk != NULL && read_chunk->link != NULL;
1175 int i;
1177 bufend = SKIPBYTES((int16_t *)pcmbuffer, pcmbuf_size);
1179 /* Find the insertion point and set bufstart to the start of it */
1180 if (mix)
1182 /* Get the currently playing chunk at the current position. */
1183 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
1185 /* If above isn't implemented or pcm is stopped, no beepeth. */
1186 if (!bufstart || !pcm_is_playing())
1187 return;
1189 /* Give 5ms clearance. */
1190 bufstart += BYTERATE / 200;
1192 #ifdef HAVE_PCM_DMA_ADDRESS
1193 /* Returned peak addresses are DMA addresses */
1194 bufend = pcm_dma_addr(bufend);
1195 #endif
1197 /* Wrapped above? */
1198 if (bufstart >= bufend)
1199 bufstart -= pcmbuf_size;
1201 /* NOTE: On some targets using hardware DMA, cache range flushing may
1202 * be required or the writes may not be picked up by the controller.
1203 * An incremental flush should be done periodically during the mixdown. */
1205 else if (nsamples <= MINIBUF_SAMPLES)
1207 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
1208 /* Use mini buffer */
1209 bufstart = minibuf;
1210 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1212 else if (!audio_buffer_state_trashed())
1214 /* Use pcmbuffer */
1215 bufstart = (int16_t *)pcmbuffer;
1217 else
1219 /* No place */
1220 return;
1223 bufptr = bufstart;
1225 /* Mix square wave into buffer */
1226 for (i = 0; i < nsamples; ++i)
1228 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
1229 sample = mix ? *bufptr : 0;
1230 *bufptr++ = clip_sample_16(sample + amp);
1231 if (bufptr >= bufend)
1232 bufptr = (int16_t *)pcmbuffer;
1233 sample = mix ? *bufptr : 0;
1234 *bufptr++ = clip_sample_16(sample + amp);
1235 if (bufptr >= bufend)
1236 bufptr = (int16_t *)pcmbuffer;
1238 phase += step;
1241 pcm_play_lock();
1242 #ifdef HAVE_RECORDING
1243 pcm_rec_lock();
1244 #endif
1246 /* Kick off playback if required and it won't interfere */
1247 if (!pcm_is_playing()
1248 #ifdef HAVE_RECORDING
1249 && !pcm_is_recording()
1250 #endif
1253 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
1256 pcm_play_unlock();
1257 #ifdef HAVE_RECORDING
1258 pcm_rec_unlock();
1259 #endif
1261 #endif /* HAVE_HARDWARE_BEEP */