Correct spelling of catalogue.
[kugel-rb.git] / apps / pcmbuf.c
blob946eb16021480d60f4818b95bc8f58acb47688aa
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 "pcm.h"
27 #include "pcm_mixer.h"
28 #include "pcmbuf.h"
29 #include "playback.h"
30 #include "codec_thread.h"
32 /* Define LOGF_ENABLE to enable logf output in this file */
33 /*#define LOGF_ENABLE*/
34 #include "logf.h"
35 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
36 #include "cpu.h"
37 #endif
38 #include <string.h>
39 #include "settings.h"
40 #include "audio.h"
41 #include "voice_thread.h"
42 #include "dsp.h"
44 #define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
45 on the pcm buffer */
46 #define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
47 chunks on the pcm buffer (or we run out
48 of buffer descriptors, which is
49 non-fatal) */
50 #define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
51 this to the DMA */
52 #define CROSSFADE_BUFSIZE 8192 /* Size of the crossfade buffer */
54 /* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */
55 #define BYTERATE (NATIVE_FREQUENCY * 4)
57 #if MEMORYSIZE > 2
58 /* Keep watermark high for iPods at least (2s) */
59 #define PCMBUF_WATERMARK (BYTERATE * 2)
60 #else
61 #define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */
62 #endif
64 /* Structure we can use to queue pcm chunks in memory to be played
65 * by the driver code. */
66 struct chunkdesc
68 unsigned char *addr;
69 size_t size;
70 struct chunkdesc* link;
71 /* true if last chunk in the track */
72 bool end_of_track;
75 #define NUM_CHUNK_DESCS(bufsize) \
76 ((bufsize) / PCMBUF_MINAVG_CHUNK)
78 /* Size of the PCM buffer. */
79 static size_t pcmbuf_size = 0;
80 static char *pcmbuf_bufend;
81 static char *pcmbuffer;
82 /* Current PCM buffer write index. */
83 static size_t pcmbuffer_pos;
84 /* Amount pcmbuffer_pos will be increased.*/
85 static size_t pcmbuffer_fillpos;
87 static struct chunkdesc *first_desc;
89 /* Gapless playback */
90 static bool track_transition;
92 /* Fade effect */
93 static unsigned int fade_vol = MIX_AMP_UNITY;
95 /* Voice */
96 static bool soft_mode = false;
98 #ifdef HAVE_CROSSFADE
99 /* Crossfade buffer */
100 static char *fadebuf;
102 /* Crossfade related state */
103 static bool crossfade_enabled;
104 static bool crossfade_enable_request;
105 static bool crossfade_mixmode;
106 static bool crossfade_auto_skip;
107 static bool crossfade_active;
108 static bool crossfade_track_change_started;
110 /* Track the current location for processing crossfade */
111 static struct chunkdesc *crossfade_chunk;
112 static size_t crossfade_sample;
114 /* Counters for fading in new data */
115 static size_t crossfade_fade_in_total;
116 static size_t crossfade_fade_in_rem;
117 #endif
119 static struct chunkdesc *read_chunk;
120 static struct chunkdesc *read_end_chunk;
121 static struct chunkdesc *write_chunk;
122 static struct chunkdesc *write_end_chunk;
123 static size_t last_chunksize;
125 static size_t pcmbuf_unplayed_bytes;
126 static size_t pcmbuf_watermark;
128 static bool low_latency_mode = false;
129 static bool flush_pcmbuf = false;
131 #ifdef HAVE_PRIORITY_SCHEDULING
132 static int codec_thread_priority = PRIORITY_PLAYBACK;
133 #endif
135 /* Helpful macros for use in conditionals this assumes some of the above
136 * static variable names */
137 #define COMMIT_IF_NEEDED if(pcmbuffer_fillpos > PCMBUF_TARGET_CHUNK || \
138 (pcmbuffer_pos + pcmbuffer_fillpos) >= pcmbuf_size) commit_chunk(false)
139 #define LOW_DATA(quarter_secs) \
140 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
142 #ifdef HAVE_CROSSFADE
143 static void crossfade_start(void);
144 static void write_to_crossfade(size_t length);
145 static void pcmbuf_finish_crossfade_enable(void);
146 #endif
148 /* Callbacks into playback.c */
149 extern void audio_pcmbuf_position_callback(unsigned int time);
150 extern void audio_pcmbuf_track_change(bool pcmbuf);
151 extern bool audio_pcmbuf_may_play(void);
154 /**************************************/
156 /* define this to show detailed chunkdesc usage information on the sim console */
157 /*#define DESC_DEBUG*/
159 #ifndef SIMULATOR
160 #undef DESC_DEBUG
161 #endif
163 #ifdef DESC_DEBUG
164 #define DISPLAY_DESC(caller) while(!show_desc(caller))
165 #define DESC_IDX(desc) (desc ? desc - first_desc : -1)
166 #define SHOW_DESC(desc) if(DESC_IDX(desc)==-1) DEBUGF("--"); \
167 else DEBUGF("%02d", DESC_IDX(desc))
168 #define SHOW_DESC_LINK(desc) if(desc){SHOW_DESC(desc->link);DEBUGF(" ");} \
169 else DEBUGF("-- ")
170 #define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \
171 SHOW_DESC_LINK(desc)
172 #define SHOW_POINT(tag,desc) DEBUGF("%s",tag);SHOW_DETAIL(desc)
173 #define SHOW_NUM(num,desc) DEBUGF("%02d>",num);SHOW_DESC_LINK(desc)
175 static bool show_desc(char *caller)
177 if (show_desc_in_use) return false;
178 show_desc_in_use = true;
179 DEBUGF("%-14s\t", caller);
180 SHOW_POINT("r", read_chunk);
181 SHOW_POINT("re", read_end_chunk);
182 DEBUGF(" ");
183 SHOW_POINT("w", write_chunk);
184 SHOW_POINT("we", write_end_chunk);
185 DEBUGF("\n");
186 int i;
187 for (i = 0; i < pcmbuf_descs(); i++)
189 SHOW_NUM(i, (first_desc + i));
190 if (i%10 == 9) DEBUGF("\n");
192 DEBUGF("\n\n");
193 show_desc_in_use = false;
194 return true;
196 #else
197 #define DISPLAY_DESC(caller) do{}while(0)
198 #endif
201 /** Accept new PCM data */
203 /* Commit PCM buffer samples as a new chunk for playback */
204 static void commit_chunk(bool flush_next_time)
206 if (!pcmbuffer_fillpos)
207 return;
209 /* Never use the last buffer descriptor */
210 while (write_chunk == write_end_chunk) {
211 /* If this happens, something is being stupid */
212 if (!pcm_is_playing()) {
213 logf("commit_chunk error");
214 pcmbuf_play_start();
216 /* Let approximately one chunk of data playback */
217 sleep(HZ * PCMBUF_TARGET_CHUNK / BYTERATE);
220 /* commit the chunk */
222 register size_t size = pcmbuffer_fillpos;
223 /* Grab the next description to write, and change the write pointer */
224 register struct chunkdesc *pcmbuf_current = write_chunk;
225 write_chunk = pcmbuf_current->link;
226 /* Fill in the values in the new buffer chunk */
227 pcmbuf_current->addr = &pcmbuffer[pcmbuffer_pos];
228 pcmbuf_current->size = size;
229 pcmbuf_current->end_of_track = false;
230 pcmbuf_current->link = NULL;
232 if (read_chunk != NULL)
234 if (flush_pcmbuf)
236 /* Flush! Discard all data after the currently playing chunk,
237 and make the current chunk play next */
238 logf("commit_chunk: flush");
239 pcm_play_lock();
240 write_end_chunk->link = read_chunk->link;
241 read_chunk->link = pcmbuf_current;
242 while (write_end_chunk->link)
244 write_end_chunk = write_end_chunk->link;
245 pcmbuf_unplayed_bytes -= write_end_chunk->size;
248 read_chunk->end_of_track = track_transition;
249 pcm_play_unlock();
251 /* If there is already a read buffer setup, add to it */
252 else
253 read_end_chunk->link = pcmbuf_current;
255 else
257 /* Otherwise create the buffer */
258 read_chunk = pcmbuf_current;
261 /* If flush_next_time is true, then the current chunk will be thrown out
262 * and the next chunk to be committed will be the next to be played.
263 * This is used to empty the PCM buffer for a track change. */
264 flush_pcmbuf = flush_next_time;
266 /* This is now the last buffer to read */
267 read_end_chunk = pcmbuf_current;
269 /* Update bytes counters */
270 pcmbuf_unplayed_bytes += size;
272 pcmbuffer_pos += size;
273 if (pcmbuffer_pos >= pcmbuf_size)
274 pcmbuffer_pos -= pcmbuf_size;
276 pcmbuffer_fillpos = 0;
277 DISPLAY_DESC("commit_chunk");
280 /* Set priority of the codec thread */
281 #ifdef HAVE_PRIORITY_SCHEDULING
283 * expects pcm_fill_state in tenth-% units (e.g. full pcm buffer is 10) */
284 static void boost_codec_thread(int pcm_fill_state)
286 static const int prios[11] = {
287 PRIORITY_PLAYBACK_MAX, /* 0 - 10% */
288 PRIORITY_PLAYBACK_MAX+1, /* 10 - 20% */
289 PRIORITY_PLAYBACK_MAX+3, /* 20 - 30% */
290 PRIORITY_PLAYBACK_MAX+5, /* 30 - 40% */
291 PRIORITY_PLAYBACK_MAX+7, /* 40 - 50% */
292 PRIORITY_PLAYBACK_MAX+8, /* 50 - 60% */
293 PRIORITY_PLAYBACK_MAX+9, /* 60 - 70% */
294 /* raiseing priority above 70% shouldn't be needed */
295 PRIORITY_PLAYBACK, /* 70 - 80% */
296 PRIORITY_PLAYBACK, /* 80 - 90% */
297 PRIORITY_PLAYBACK, /* 90 -100% */
298 PRIORITY_PLAYBACK, /* 100% */
300 int new_prio = prios[pcm_fill_state];
302 /* Keep voice and codec threads at the same priority or else voice
303 * will starve if the codec thread's priority is boosted. */
304 if (new_prio != codec_thread_priority)
306 codec_thread_set_priority(new_prio);
307 voice_thread_set_priority(new_prio);
308 codec_thread_priority = new_prio;
311 #else
312 #define boost_codec_thread(pcm_fill_state) do{}while(0)
313 #endif /* HAVE_PRIORITY_SCHEDULING */
315 /* Return true if the PCM buffer is able to receive new data.
316 * Also maintain buffer level above the watermark. */
317 static bool prepare_insert(size_t length)
319 bool playing = mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED;
321 if (low_latency_mode)
323 /* 1/4s latency. */
324 if (!LOW_DATA(1) && playing)
325 return false;
328 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
329 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
330 return false;
332 /* Maintain the buffer level above the watermark */
333 if (playing)
335 /* Only codec thread initiates boost - voice boosts the cpu when playing
336 a clip */
337 #ifndef SIMULATOR
338 if (is_codec_thread())
339 #endif /* SIMULATOR */
341 /* boost cpu if necessary */
342 if (pcmbuf_unplayed_bytes < pcmbuf_watermark)
343 trigger_cpu_boost();
344 boost_codec_thread(pcmbuf_unplayed_bytes*10/pcmbuf_size);
347 #ifdef HAVE_CROSSFADE
348 /* Disable crossfade if < .5s of audio */
349 if (LOW_DATA(2))
351 crossfade_active = false;
353 #endif
355 else /* !playing */
357 /* Boost CPU for pre-buffer */
358 trigger_cpu_boost();
360 /* If pre-buffered to the watermark, start playback */
361 #if MEMORYSIZE > 2
362 if (!LOW_DATA(4))
363 #else
364 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
365 #endif
367 logf("pcm starting");
368 if (audio_pcmbuf_may_play())
369 pcmbuf_play_start();
373 return true;
376 /* Request space in the buffer for writing output samples */
377 void *pcmbuf_request_buffer(int *count)
379 #ifdef HAVE_CROSSFADE
380 /* we're going to crossfade to a new track, which is now on its way */
381 if (crossfade_track_change_started)
382 crossfade_start();
384 /* crossfade has begun, put the new track samples in fadebuf */
385 if (crossfade_active)
387 int cnt = MIN(*count, CROSSFADE_BUFSIZE/4);
388 if (prepare_insert(cnt << 2))
390 *count = cnt;
391 return fadebuf;
394 else
395 #endif
396 /* if possible, reserve room in the PCM buffer for new samples */
398 if(prepare_insert(*count << 2))
400 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
401 if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK)
403 /* Usual case, there's space here */
404 return &pcmbuffer[pcmbuffer_index];
406 else
408 /* Wrap the buffer, the new samples go at the beginning */
409 commit_chunk(false);
410 pcmbuffer_pos = 0;
411 return &pcmbuffer[0];
415 /* PCM buffer not ready to receive new data yet */
416 return NULL;
419 /* Handle new samples to the buffer */
420 void pcmbuf_write_complete(int count)
422 size_t length = (size_t)(unsigned int)count << 2;
423 #ifdef HAVE_CROSSFADE
424 if (crossfade_active)
425 write_to_crossfade(length);
426 else
427 #endif
429 pcmbuffer_fillpos += length;
430 COMMIT_IF_NEEDED;
435 /** Init */
437 static inline void init_pcmbuffers(void)
439 first_desc = write_chunk;
440 struct chunkdesc *next = write_chunk;
441 next++;
442 write_end_chunk = write_chunk;
443 while ((void *)next < (void *)pcmbuf_bufend) {
444 write_end_chunk->link=next;
445 write_end_chunk=next;
446 next++;
448 DISPLAY_DESC("init");
451 static size_t get_next_required_pcmbuf_size(void)
453 size_t seconds = 1;
455 #ifdef HAVE_CROSSFADE
456 if (crossfade_enable_request)
457 seconds += global_settings.crossfade_fade_out_delay +
458 global_settings.crossfade_fade_out_duration;
459 #endif
461 #if MEMORYSIZE > 2
462 /* Buffer has to be at least 2s long. */
463 seconds += 2;
464 #endif
465 logf("pcmbuf len: %ld", (long)seconds);
466 return seconds * BYTERATE;
469 /* Initialize the pcmbuffer the structure looks like this:
470 * ...|---------PCMBUF---------[|FADEBUF]|DESCS|... */
471 size_t pcmbuf_init(unsigned char *bufend)
473 pcmbuf_bufend = bufend;
474 pcmbuf_size = get_next_required_pcmbuf_size();
475 write_chunk = (struct chunkdesc *)pcmbuf_bufend -
476 NUM_CHUNK_DESCS(pcmbuf_size);
478 #ifdef HAVE_CROSSFADE
479 fadebuf = (unsigned char *)write_chunk -
480 (crossfade_enable_request ? CROSSFADE_BUFSIZE : 0);
481 pcmbuffer = fadebuf - pcmbuf_size;
482 #else
483 pcmbuffer = (unsigned char *)write_chunk - pcmbuf_size;
484 #endif
486 init_pcmbuffers();
488 #ifdef HAVE_CROSSFADE
489 pcmbuf_finish_crossfade_enable();
490 #else
491 pcmbuf_watermark = PCMBUF_WATERMARK;
492 #endif
494 pcmbuf_play_stop();
496 pcmbuf_soft_mode(false);
498 return pcmbuf_bufend - pcmbuffer;
502 /** Track change */
503 void pcmbuf_monitor_track_change(bool monitor)
505 pcm_play_lock();
507 if (last_chunksize != 0)
509 /* If monitoring, wait until this track runs out. Place in
510 currently playing chunk. If not, cancel notification. */
511 track_transition = monitor;
512 read_end_chunk->end_of_track = monitor;
513 if (!monitor)
515 /* Clear all notifications */
516 struct chunkdesc *desc = first_desc;
517 struct chunkdesc *end = desc + pcmbuf_descs();
518 while (desc < end)
519 desc++->end_of_track = false;
522 else
524 /* Post now if PCM stopped and last buffer was sent. */
525 track_transition = false;
526 if (monitor)
527 audio_pcmbuf_track_change(false);
530 pcm_play_unlock();
533 bool pcmbuf_start_track_change(bool auto_skip)
535 bool crossfade = false;
536 #ifdef HAVE_CROSSFADE
537 /* Determine whether this track change needs to crossfade */
538 if(crossfade_enabled && !pcmbuf_is_crossfade_active())
540 switch(global_settings.crossfade)
542 case CROSSFADE_ENABLE_AUTOSKIP:
543 crossfade = auto_skip;
544 break;
545 case CROSSFADE_ENABLE_MANSKIP:
546 crossfade = !auto_skip;
547 break;
548 case CROSSFADE_ENABLE_SHUFFLE:
549 crossfade = global_settings.playlist_shuffle;
550 break;
551 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
552 crossfade = global_settings.playlist_shuffle || !auto_skip;
553 break;
554 case CROSSFADE_ENABLE_ALWAYS:
555 crossfade = true;
556 break;
559 #endif
561 if (!auto_skip || crossfade)
562 /* manual skip or crossfade */
564 if (crossfade)
565 { logf(" crossfade track change"); }
566 else
567 { logf(" manual track change"); }
569 pcm_play_lock();
571 /* Cancel any pending automatic gapless transition */
572 pcmbuf_monitor_track_change(false);
574 /* Can't do two crossfades at once and, no fade if pcm is off now */
575 if (
576 #ifdef HAVE_CROSSFADE
577 pcmbuf_is_crossfade_active() ||
578 #endif
579 mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED)
581 pcmbuf_play_stop();
582 pcm_play_unlock();
583 /* Notify playback that the track change starts now */
584 return true;
587 /* Not enough data, or not crossfading, flush the old data instead */
588 if (LOW_DATA(2) || !crossfade || low_latency_mode)
590 commit_chunk(true);
592 #ifdef HAVE_CROSSFADE
593 else
595 /* Don't enable mix mode when skipping tracks manually. */
596 crossfade_mixmode = auto_skip &&
597 global_settings.crossfade_fade_out_mixmode;
599 crossfade_auto_skip = auto_skip;
601 crossfade_track_change_started = crossfade;
603 #endif
604 pcm_play_unlock();
606 /* Keep trigger outside the play lock or HW FIFO underruns can happen
607 since frequency scaling is *not* always fast */
608 trigger_cpu_boost();
610 /* Notify playback that the track change starts now */
611 return true;
613 else /* automatic and not crossfading, so do gapless track change */
615 /* The codec is moving on to the next track, but the current track will
616 * continue to play. Set a flag to make sure the elapsed time of the
617 * current track will be updated properly, and mark the current chunk
618 * as the last one in the track. */
619 logf(" gapless track change");
620 pcmbuf_monitor_track_change(true);
621 return false;
626 /** Playback */
628 /* PCM driver callback
629 * This function has 3 major logical parts (separated by brackets both for
630 * readability and variable scoping). The first part performs the
631 * operations related to finishing off the last chunk we fed to the DMA.
632 * The second part detects the end of playlist condition when the PCM
633 * buffer is empty except for uncommitted samples. Then they are committed
634 * and sent to the PCM driver for playback. The third part performs the
635 * operations involved in sending a new chunk to the DMA. */
636 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
639 struct chunkdesc *pcmbuf_current = read_chunk;
640 /* Take the finished chunk out of circulation */
641 read_chunk = pcmbuf_current->link;
643 /* if during a track transition, update the elapsed time in ms */
644 if (track_transition)
645 audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE);
647 /* if last chunk in the track, stop updates and notify audio thread */
648 if (pcmbuf_current->end_of_track)
650 track_transition = false;
651 audio_pcmbuf_track_change(true);
654 /* Put the finished chunk back into circulation */
655 write_end_chunk->link = pcmbuf_current;
656 write_end_chunk = pcmbuf_current;
658 #ifdef HAVE_CROSSFADE
659 /* If we've read over the crossfade chunk while it's still fading */
660 if (pcmbuf_current == crossfade_chunk)
661 crossfade_chunk = read_chunk;
662 #endif
666 /* Commit last samples at end of playlist */
667 if (pcmbuffer_fillpos && !read_chunk)
669 logf("pcmbuf_pcm_callback: commit last samples");
670 commit_chunk(false);
675 /* Send the new chunk to the DMA */
676 if(read_chunk)
678 last_chunksize = read_chunk->size;
679 pcmbuf_unplayed_bytes -= last_chunksize;
680 *size = last_chunksize;
681 *start = read_chunk->addr;
683 else
685 /* No more chunks */
686 logf("pcmbuf_pcm_callback: no more chunks");
687 last_chunksize = 0;
688 *size = 0;
689 *start = NULL;
692 DISPLAY_DESC("callback");
695 /* Force playback */
696 void pcmbuf_play_start(void)
698 if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED &&
699 pcmbuf_unplayed_bytes && read_chunk != NULL)
701 logf("pcmbuf_play_start");
702 last_chunksize = read_chunk->size;
703 pcmbuf_unplayed_bytes -= last_chunksize;
704 mixer_channel_play_data(PCM_MIXER_CHAN_PLAYBACK,
705 pcmbuf_pcm_callback, NULL, 0);
709 void pcmbuf_play_stop(void)
711 logf("pcmbuf_play_stop");
712 mixer_channel_stop(PCM_MIXER_CHAN_PLAYBACK);
714 pcmbuf_unplayed_bytes = 0;
715 if (read_chunk) {
716 write_end_chunk->link = read_chunk;
717 write_end_chunk = read_end_chunk;
718 read_chunk = read_end_chunk = NULL;
720 last_chunksize = 0;
721 pcmbuffer_pos = 0;
722 pcmbuffer_fillpos = 0;
723 #ifdef HAVE_CROSSFADE
724 crossfade_track_change_started = false;
725 crossfade_active = false;
726 #endif
727 track_transition = false;
728 flush_pcmbuf = false;
729 DISPLAY_DESC("play_stop");
731 /* Can unboost the codec thread here no matter who's calling,
732 * pretend full pcm buffer to unboost */
733 boost_codec_thread(10);
736 void pcmbuf_pause(bool pause)
738 logf("pcmbuf_pause: %s", pause?"pause":"play");
740 if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED)
741 mixer_channel_play_pause(PCM_MIXER_CHAN_PLAYBACK, !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 /** Debug menu, other metrics */
1036 /* Amount of bytes left in the buffer. */
1037 size_t pcmbuf_free(void)
1039 if (read_chunk != NULL)
1041 void *read = (void *)read_chunk->addr;
1042 void *write = &pcmbuffer[pcmbuffer_pos + pcmbuffer_fillpos];
1043 if (read < write)
1044 return (size_t)(read - write) + pcmbuf_size;
1045 else
1046 return (size_t) (read - write);
1048 return pcmbuf_size - pcmbuffer_fillpos;
1051 size_t pcmbuf_get_bufsize(void)
1053 return pcmbuf_size;
1056 int pcmbuf_used_descs(void)
1058 struct chunkdesc *temp = read_chunk;
1059 unsigned int i = 0;
1060 while (temp) {
1061 temp = temp->link;
1062 i++;
1064 return i;
1067 int pcmbuf_descs(void)
1069 return NUM_CHUNK_DESCS(pcmbuf_size);
1072 #ifdef ROCKBOX_HAS_LOGF
1073 unsigned char *pcmbuf_get_meminfo(size_t *length)
1075 *length = pcmbuf_bufend - pcmbuffer;
1076 return pcmbuffer;
1078 #endif
1081 /** Fading and channel volume control */
1083 /* Sync the channel amplitude to all states */
1084 static void pcmbuf_update_volume(void)
1086 unsigned int vol = fade_vol;
1088 if (soft_mode)
1089 vol >>= 2;
1091 mixer_channel_set_amplitude(PCM_MIXER_CHAN_PLAYBACK, vol);
1094 /* Quiet-down the channel if 'shhh' is true or else play at normal level */
1095 void pcmbuf_soft_mode(bool shhh)
1097 soft_mode = shhh;
1098 pcmbuf_update_volume();
1101 /* Fade channel in or out */
1102 void pcmbuf_fade(bool fade, bool in)
1104 if (!fade)
1106 /* Simply set the level */
1107 fade_vol = in ? MIX_AMP_UNITY : MIX_AMP_MUTE;
1109 else
1111 /* Start from the opposing end */
1112 fade_vol = in ? MIX_AMP_MUTE : MIX_AMP_UNITY;
1115 pcmbuf_update_volume();
1117 if (fade)
1119 /* Do this on thread for now */
1120 #ifdef HAVE_PRIORITY_SCHEDULING
1121 int old_prio = thread_set_priority(thread_self(), PRIORITY_REALTIME);
1122 #endif
1124 while (1)
1126 /* Linear fade actually sounds better */
1127 if (in)
1128 fade_vol += MIN(MIX_AMP_UNITY/16, MIX_AMP_UNITY - fade_vol);
1129 else
1130 fade_vol -= MIN(MIX_AMP_UNITY/16, fade_vol - MIX_AMP_MUTE);
1132 pcmbuf_update_volume();
1134 if (fade_vol > MIX_AMP_MUTE && fade_vol < MIX_AMP_UNITY)
1136 sleep(0);
1137 continue;
1140 break;
1143 #ifdef HAVE_PRIORITY_SCHEDULING
1144 thread_set_priority(thread_self(), old_prio);
1145 #endif
1150 /** Misc */
1152 bool pcmbuf_is_lowdata(void)
1154 if (!pcm_is_playing() || pcm_is_paused()
1155 #ifdef HAVE_CROSSFADE
1156 || pcmbuf_is_crossfade_active()
1157 #endif
1159 return false;
1161 #if MEMORYSIZE > 2
1162 /* 1 seconds of buffer is low data */
1163 return LOW_DATA(4);
1164 #else
1165 /* under watermark is low data */
1166 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
1167 #endif
1170 void pcmbuf_set_low_latency(bool state)
1172 low_latency_mode = state;
1175 unsigned long pcmbuf_get_latency(void)
1177 return (pcmbuf_unplayed_bytes +
1178 mixer_channel_get_bytes_waiting(PCM_MIXER_CHAN_PLAYBACK)) * 1000 / BYTERATE;