fix yellow
[maemo-rb.git] / apps / pcmbuf.c
blobebc31fbb2eb3c0c195be63bbf73fc8f1c70bdd6b
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 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
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 CROSSFADE_BUFSIZE 8192 /* Size of the crossfade buffer */
51 #define AUX_BUFSIZE 512 /* Size of the aux buffer; can be 512 if no
52 resampling or timestretching is allowed in
53 the aux channel, must be 2048 otherwise */
55 /* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */
56 #define BYTERATE (NATIVE_FREQUENCY * 4)
58 #if MEMORYSIZE > 2
59 /* Keep watermark high for iPods at least (2s) */
60 #define PCMBUF_WATERMARK (BYTERATE * 2)
61 #else
62 #define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */
63 #endif
65 /* Structure we can use to queue pcm chunks in memory to be played
66 * by the driver code. */
67 struct chunkdesc
69 unsigned char *addr;
70 size_t size;
71 struct chunkdesc* link;
72 /* true if last chunk in the track */
73 bool end_of_track;
76 #define NUM_CHUNK_DESCS(bufsize) \
77 ((bufsize) / PCMBUF_MINAVG_CHUNK)
79 /* Size of the PCM buffer. */
80 static size_t pcmbuf_size IDATA_ATTR = 0;
81 static char *pcmbuf_bufend IDATA_ATTR;
82 static char *pcmbuffer IDATA_ATTR;
83 /* Current PCM buffer write index. */
84 static size_t pcmbuffer_pos IDATA_ATTR;
85 /* Amount pcmbuffer_pos will be increased.*/
86 static size_t pcmbuffer_fillpos IDATA_ATTR;
88 /* Gapless playback */
89 static bool end_of_track IDATA_ATTR;
90 bool track_transition IDATA_ATTR;
92 #ifdef HAVE_CROSSFADE
93 /* Crossfade buffer */
94 static char *fadebuf IDATA_ATTR;
96 /* Crossfade related state */
97 static bool crossfade_enabled;
98 static bool crossfade_enable_request;
99 static bool crossfade_mixmode;
100 static bool crossfade_auto_skip;
101 static bool crossfade_active IDATA_ATTR;
102 static bool crossfade_track_change_started IDATA_ATTR;
104 /* Track the current location for processing crossfade */
105 static struct chunkdesc *crossfade_chunk IDATA_ATTR;
106 static size_t crossfade_sample IDATA_ATTR;
108 /* Counters for fading in new data */
109 static size_t crossfade_fade_in_total IDATA_ATTR;
110 static size_t crossfade_fade_in_rem IDATA_ATTR;
111 #endif
113 static struct chunkdesc *read_chunk IDATA_ATTR;
114 static struct chunkdesc *read_end_chunk IDATA_ATTR;
115 static struct chunkdesc *write_chunk IDATA_ATTR;
116 static struct chunkdesc *write_end_chunk IDATA_ATTR;
117 static size_t last_chunksize IDATA_ATTR;
119 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
120 static size_t pcmbuf_watermark IDATA_ATTR;
122 /* Voice */
123 static char *voicebuf IDATA_ATTR;
124 static struct chunkdesc *mix_chunk IDATA_ATTR;
125 static size_t pcmbuf_mix_sample IDATA_ATTR;
127 static bool low_latency_mode = false;
128 static bool flush_pcmbuf = false;
130 #ifdef HAVE_PRIORITY_SCHEDULING
131 static int codec_thread_priority = PRIORITY_PLAYBACK;
132 #endif
134 extern unsigned int codec_thread_id;
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
150 /**************************************/
152 /* define this to show detailed chunkdesc usage information on the sim console */
153 /*#define DESC_DEBUG*/
155 #ifndef SIMULATOR
156 #undef DESC_DEBUG
157 #endif
158 #ifdef DESC_DEBUG
159 static struct chunkdesc *first_desc;
160 static bool show_desc_in_use = false;
161 #define DISPLAY_DESC(caller) while(!show_desc(caller))
162 #define DESC_IDX(desc) (desc ? desc - first_desc : -1)
163 #define SHOW_DESC(desc) if(DESC_IDX(desc)==-1) DEBUGF("--"); \
164 else DEBUGF("%02d", DESC_IDX(desc))
165 #define SHOW_DESC_LINK(desc) if(desc){SHOW_DESC(desc->link);DEBUGF(" ");} \
166 else DEBUGF("-- ")
167 #define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \
168 SHOW_DESC_LINK(desc)
169 #define SHOW_POINT(tag,desc) DEBUGF("%s",tag);SHOW_DETAIL(desc)
170 #define SHOW_NUM(num,desc) DEBUGF("%02d>",num);SHOW_DESC_LINK(desc)
172 static bool show_desc(char *caller)
174 if (show_desc_in_use) return false;
175 show_desc_in_use = true;
176 DEBUGF("%-14s\t", caller);
177 SHOW_POINT("r", read_chunk);
178 SHOW_POINT("re", read_end_chunk);
179 DEBUGF(" ");
180 SHOW_POINT("w", write_chunk);
181 SHOW_POINT("we", write_end_chunk);
182 DEBUGF("\n");
183 int i;
184 for (i = 0; i < pcmbuf_descs(); i++)
186 SHOW_NUM(i, (first_desc + i));
187 if (i%10 == 9) DEBUGF("\n");
189 DEBUGF("\n\n");
190 show_desc_in_use = false;
191 return true;
193 #else
194 #define DISPLAY_DESC(caller) do{}while(0)
195 #endif
198 /** Accept new PCM data */
200 /* Commit PCM buffer samples as a new chunk for playback */
201 static void commit_chunk(bool flush_next_time)
203 if (!pcmbuffer_fillpos)
204 return;
206 /* Never use the last buffer descriptor */
207 while (write_chunk == write_end_chunk) {
208 /* If this happens, something is being stupid */
209 if (!pcm_is_playing()) {
210 logf("commit_chunk error");
211 pcmbuf_play_start();
213 /* Let approximately one chunk of data playback */
214 sleep(HZ * PCMBUF_TARGET_CHUNK / BYTERATE);
217 /* commit the chunk */
219 register size_t size = pcmbuffer_fillpos;
220 /* Grab the next description to write, and change the write pointer */
221 register struct chunkdesc *pcmbuf_current = write_chunk;
222 write_chunk = pcmbuf_current->link;
223 /* Fill in the values in the new buffer chunk */
224 pcmbuf_current->addr = &pcmbuffer[pcmbuffer_pos];
225 pcmbuf_current->size = size;
226 pcmbuf_current->end_of_track = end_of_track;
227 pcmbuf_current->link = NULL;
228 end_of_track = false; /* This is single use only */
230 if (read_chunk != NULL)
232 if (flush_pcmbuf)
234 /* Flush! Discard all data after the currently playing chunk,
235 and make the current chunk play next */
236 logf("commit_chunk: flush");
237 write_end_chunk->link = read_chunk->link;
238 read_chunk->link = pcmbuf_current;
239 while (write_end_chunk->link)
241 write_end_chunk = write_end_chunk->link;
242 pcmbuf_unplayed_bytes -= write_end_chunk->size;
245 /* If there is already a read buffer setup, add to it */
246 else
247 read_end_chunk->link = pcmbuf_current;
249 else
251 /* Otherwise create the buffer */
252 read_chunk = pcmbuf_current;
255 /* If flush_next_time is true, then the current chunk will be thrown out
256 * and the next chunk to be committed will be the next to be played.
257 * This is used to empty the PCM buffer for a track change. */
258 flush_pcmbuf = flush_next_time;
260 /* This is now the last buffer to read */
261 read_end_chunk = pcmbuf_current;
263 /* Update bytes counters */
264 pcmbuf_unplayed_bytes += size;
266 pcmbuffer_pos += size;
267 if (pcmbuffer_pos >= pcmbuf_size)
268 pcmbuffer_pos -= pcmbuf_size;
270 pcmbuffer_fillpos = 0;
271 DISPLAY_DESC("commit_chunk");
274 /* Set priority of the codec thread */
275 #ifdef HAVE_PRIORITY_SCHEDULING
276 static void boost_codec_thread(bool boost)
278 /* Keep voice and codec threads at the same priority or else voice
279 * will starve if the codec thread's priority is boosted. */
280 if (boost)
282 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
283 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
285 if (priority != codec_thread_priority)
287 codec_thread_priority = priority;
288 thread_set_priority(codec_thread_id, priority);
289 voice_thread_set_priority(priority);
292 else if (codec_thread_priority != PRIORITY_PLAYBACK)
294 thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
295 voice_thread_set_priority(PRIORITY_PLAYBACK);
296 codec_thread_priority = PRIORITY_PLAYBACK;
299 #else
300 #define boost_codec_thread(boost) do{}while(0)
301 #endif /* HAVE_PRIORITY_SCHEDULING */
303 /* Return true if the PCM buffer is able to receive new data.
304 * Also maintain buffer level above the watermark. */
305 static bool prepare_insert(size_t length)
307 if (low_latency_mode)
309 /* 1/4s latency. */
310 if (!LOW_DATA(1) && pcm_is_playing())
311 return false;
314 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
315 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
316 return false;
318 /* Maintain the buffer level above the watermark */
319 if (pcm_is_playing())
321 /* Only codec thread initiates boost - voice boosts the cpu when playing
322 a clip */
323 #ifndef SIMULATOR
324 if (thread_get_current() == codec_thread_id)
325 #endif /* SIMULATOR */
327 if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
329 /* Fill PCM buffer by boosting cpu */
330 trigger_cpu_boost();
331 /* If buffer is critically low, override UI priority, else
332 set back to the original priority. */
333 boost_codec_thread(LOW_DATA(2));
335 else
337 boost_codec_thread(false);
341 #ifdef HAVE_CROSSFADE
342 /* Disable crossfade if < .5s of audio */
343 if (LOW_DATA(2))
345 crossfade_active = false;
347 #endif
349 else /* pcm_is_playing */
351 /* Boost CPU for pre-buffer */
352 trigger_cpu_boost();
354 /* If pre-buffered to the watermark, start playback */
355 #if MEMORYSIZE > 2
356 if (!LOW_DATA(4))
357 #else
358 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
359 #endif
361 logf("pcm starting");
362 if (!(audio_status() & AUDIO_STATUS_PAUSE))
363 pcmbuf_play_start();
367 return true;
370 /* Request space in the buffer for writing output samples */
371 void *pcmbuf_request_buffer(int *count)
373 #ifdef HAVE_CROSSFADE
374 /* we're going to crossfade to a new track, which is now on its way */
375 if (crossfade_track_change_started)
376 crossfade_start();
378 /* crossfade has begun, put the new track samples in fadebuf */
379 if (crossfade_active)
381 *count = MIN(*count, CROSSFADE_BUFSIZE/4);
382 return fadebuf;
384 else
385 #endif
386 /* if possible, reserve room in the PCM buffer for new samples */
388 if(prepare_insert(*count << 2))
390 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
391 if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK)
393 /* Usual case, there's space here */
394 return &pcmbuffer[pcmbuffer_index];
396 else
398 /* Wrap the buffer, the new samples go at the beginning */
399 commit_chunk(false);
400 pcmbuffer_pos = 0;
401 return &pcmbuffer[0];
405 /* PCM buffer not ready to receive new data yet */
406 return NULL;
409 /* Handle new samples to the buffer */
410 void pcmbuf_write_complete(int count)
412 size_t length = (size_t)(unsigned int)count << 2;
413 #ifdef HAVE_CROSSFADE
414 if (crossfade_active)
415 write_to_crossfade(length);
416 else
417 #endif
419 pcmbuffer_fillpos += length;
420 COMMIT_IF_NEEDED;
425 /** Init */
427 static inline void init_pcmbuffers(void)
429 #ifdef DESC_DEBUG
430 first_desc = write_chunk;
431 #endif
432 struct chunkdesc *next = write_chunk;
433 next++;
434 write_end_chunk = write_chunk;
435 while ((void *)next < (void *)pcmbuf_bufend) {
436 write_end_chunk->link=next;
437 write_end_chunk=next;
438 next++;
440 DISPLAY_DESC("init");
443 static size_t get_next_required_pcmbuf_size(void)
445 size_t seconds = 1;
447 #ifdef HAVE_CROSSFADE
448 if (crossfade_enable_request)
449 seconds += global_settings.crossfade_fade_out_delay +
450 global_settings.crossfade_fade_out_duration;
451 #endif
453 #if MEMORYSIZE > 2
454 /* Buffer has to be at least 2s long. */
455 seconds += 2;
456 #endif
457 logf("pcmbuf len: %ld", (long)seconds);
458 return seconds * BYTERATE;
461 /* Initialize the pcmbuffer the structure looks like this:
462 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
463 size_t pcmbuf_init(unsigned char *bufend)
465 pcmbuf_bufend = bufend;
466 pcmbuf_size = get_next_required_pcmbuf_size();
467 write_chunk = (struct chunkdesc *)pcmbuf_bufend -
468 NUM_CHUNK_DESCS(pcmbuf_size);
469 voicebuf = (char *)write_chunk - AUX_BUFSIZE;
470 #ifdef HAVE_CROSSFADE
471 fadebuf = voicebuf - CROSSFADE_BUFSIZE;
472 pcmbuffer = fadebuf - pcmbuf_size;
473 #else
474 pcmbuffer = voicebuf - pcmbuf_size;
475 #endif
477 init_pcmbuffers();
479 #ifdef HAVE_CROSSFADE
480 pcmbuf_finish_crossfade_enable();
481 #else
482 pcmbuf_watermark = PCMBUF_WATERMARK;
483 #endif
485 pcmbuf_play_stop();
487 return pcmbuf_bufend - pcmbuffer;
491 /** Track change */
493 void pcmbuf_start_track_change(bool auto_skip)
495 bool crossfade = false;
496 #ifdef HAVE_CROSSFADE
497 /* Determine whether this track change needs to crossfade */
498 if(crossfade_enabled && !pcmbuf_is_crossfade_active())
500 switch(global_settings.crossfade)
502 case CROSSFADE_ENABLE_AUTOSKIP:
503 crossfade = auto_skip;
504 break;
505 case CROSSFADE_ENABLE_MANSKIP:
506 crossfade = !auto_skip;
507 break;
508 case CROSSFADE_ENABLE_SHUFFLE:
509 crossfade = global_settings.playlist_shuffle;
510 break;
511 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
512 crossfade = global_settings.playlist_shuffle || !auto_skip;
513 break;
514 case CROSSFADE_ENABLE_ALWAYS:
515 crossfade = true;
516 break;
519 #endif
521 if (!auto_skip || crossfade)
522 /* manual skip or crossfade */
524 if (crossfade)
525 { logf(" crossfade track change"); }
526 else
527 { logf(" manual track change"); }
529 /* Notify the wps that the track change starts now */
530 audio_post_track_change(false);
532 /* Can't do two crossfades at once and, no fade if pcm is off now */
533 if (
534 #ifdef HAVE_CROSSFADE
535 pcmbuf_is_crossfade_active() ||
536 #endif
537 !pcm_is_playing())
539 pcmbuf_play_stop();
540 return;
543 trigger_cpu_boost();
545 /* Not enough data, or not crossfading, flush the old data instead */
546 if (LOW_DATA(2) || !crossfade || low_latency_mode)
548 commit_chunk(true);
549 return;
552 #ifdef HAVE_CROSSFADE
553 /* Don't enable mix mode when skipping tracks manually. */
554 crossfade_mixmode = auto_skip && global_settings.crossfade_fade_out_mixmode;
556 crossfade_auto_skip = auto_skip;
558 crossfade_track_change_started = crossfade;
559 #endif
561 else /* automatic and not crossfading, so do gapless track change */
563 /* The codec is moving on to the next track, but the current track will
564 * continue to play. Set a flag to make sure the elapsed time of the
565 * current track will be updated properly, and mark the current chunk
566 * as the last one in the track. */
567 logf(" gapless track change");
568 track_transition = true;
569 end_of_track = true;
574 /** Playback */
576 /* PCM driver callback
577 * This function has 3 major logical parts (separated by brackets both for
578 * readability and variable scoping). The first part performs the
579 * operations related to finishing off the last chunk we fed to the DMA.
580 * The second part detects the end of playlist condition when the PCM
581 * buffer is empty except for uncommitted samples. Then they are committed
582 * and sent to the PCM driver for playback. The third part performs the
583 * operations involved in sending a new chunk to the DMA. */
584 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR;
585 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
588 struct chunkdesc *pcmbuf_current = read_chunk;
589 /* Take the finished chunk out of circulation */
590 read_chunk = pcmbuf_current->link;
592 /* if during a track transition, update the elapsed time in ms */
593 if (track_transition)
594 audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE);
596 /* if last chunk in the track, stop updates and notify audio thread */
597 if (pcmbuf_current->end_of_track)
599 track_transition = false;
600 audio_post_track_change(true);
603 /* Put the finished chunk back into circulation */
604 write_end_chunk->link = pcmbuf_current;
605 write_end_chunk = pcmbuf_current;
607 /* If we've read over the mix chunk while it's still mixing there */
608 if (pcmbuf_current == mix_chunk)
609 mix_chunk = NULL;
611 #ifdef HAVE_CROSSFADE
612 /* If we've read over the crossfade chunk while it's still fading */
613 if (pcmbuf_current == crossfade_chunk)
614 crossfade_chunk = read_chunk;
615 #endif
619 /* Commit last samples at end of playlist */
620 if (pcmbuffer_fillpos && !read_chunk)
622 logf("pcmbuf_pcm_callback: commit last samples");
623 commit_chunk(false);
628 /* Send the new chunk to the DMA */
629 if(read_chunk)
631 last_chunksize = read_chunk->size;
632 pcmbuf_unplayed_bytes -= last_chunksize;
633 *size = last_chunksize;
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 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;
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) / 2;
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) / 2;
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) / 2;
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, AUX_BUFSIZE/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 = (int16_t *)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 = (void *)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 */