GSoC/Buflib: Enable compaction in buflib.
[kugel-rb.git] / apps / pcmbuf.c
blob2e8bc3f47c496aa9bda994f3a30f415ad52dd0ed
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 /* boost cpu if necessary */
336 if (pcmbuf_unplayed_bytes < pcmbuf_watermark)
337 trigger_cpu_boost();
338 boost_codec_thread(pcmbuf_unplayed_bytes*10/pcmbuf_size);
340 #ifdef HAVE_CROSSFADE
341 /* Disable crossfade if < .5s of audio */
342 if (LOW_DATA(2))
344 crossfade_active = false;
346 #endif
348 else /* !playing */
350 /* Boost CPU for pre-buffer */
351 trigger_cpu_boost();
353 /* If pre-buffered to the watermark, start playback */
354 #if MEMORYSIZE > 2
355 if (!LOW_DATA(4))
356 #else
357 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
358 #endif
360 logf("pcm starting");
361 if (audio_pcmbuf_may_play())
362 pcmbuf_play_start();
366 return true;
369 /* Request space in the buffer for writing output samples */
370 void *pcmbuf_request_buffer(int *count)
372 #ifdef HAVE_CROSSFADE
373 /* we're going to crossfade to a new track, which is now on its way */
374 if (crossfade_track_change_started)
375 crossfade_start();
377 /* crossfade has begun, put the new track samples in fadebuf */
378 if (crossfade_active)
380 int cnt = MIN(*count, CROSSFADE_BUFSIZE/4);
381 if (prepare_insert(cnt << 2))
383 *count = cnt;
384 return fadebuf;
387 else
388 #endif
389 /* if possible, reserve room in the PCM buffer for new samples */
391 if(prepare_insert(*count << 2))
393 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
394 if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK)
396 /* Usual case, there's space here */
397 return &pcmbuffer[pcmbuffer_index];
399 else
401 /* Wrap the buffer, the new samples go at the beginning */
402 commit_chunk(false);
403 pcmbuffer_pos = 0;
404 return &pcmbuffer[0];
408 /* PCM buffer not ready to receive new data yet */
409 return NULL;
412 /* Handle new samples to the buffer */
413 void pcmbuf_write_complete(int count)
415 size_t length = (size_t)(unsigned int)count << 2;
416 #ifdef HAVE_CROSSFADE
417 if (crossfade_active)
418 write_to_crossfade(length);
419 else
420 #endif
422 pcmbuffer_fillpos += length;
423 COMMIT_IF_NEEDED;
428 /** Init */
430 static inline void init_pcmbuffers(void)
432 first_desc = write_chunk;
433 struct chunkdesc *next = write_chunk;
434 next++;
435 write_end_chunk = write_chunk;
436 while ((void *)next < (void *)pcmbuf_bufend) {
437 write_end_chunk->link=next;
438 write_end_chunk=next;
439 next++;
441 DISPLAY_DESC("init");
444 static size_t get_next_required_pcmbuf_size(void)
446 size_t seconds = 1;
448 #ifdef HAVE_CROSSFADE
449 if (crossfade_enable_request)
450 seconds += global_settings.crossfade_fade_out_delay +
451 global_settings.crossfade_fade_out_duration;
452 #endif
454 #if MEMORYSIZE > 2
455 /* Buffer has to be at least 2s long. */
456 seconds += 2;
457 #endif
458 logf("pcmbuf len: %ld", (long)seconds);
459 return seconds * BYTERATE;
462 /* Initialize the pcmbuffer the structure looks like this:
463 * ...|---------PCMBUF---------[|FADEBUF]|DESCS|... */
464 size_t pcmbuf_init(unsigned char *bufend)
466 pcmbuf_bufend = bufend;
467 pcmbuf_size = get_next_required_pcmbuf_size();
468 write_chunk = (struct chunkdesc *)pcmbuf_bufend -
469 NUM_CHUNK_DESCS(pcmbuf_size);
471 #ifdef HAVE_CROSSFADE
472 fadebuf = (unsigned char *)write_chunk -
473 (crossfade_enable_request ? CROSSFADE_BUFSIZE : 0);
474 pcmbuffer = fadebuf - pcmbuf_size;
475 #else
476 pcmbuffer = (unsigned char *)write_chunk - pcmbuf_size;
477 #endif
479 init_pcmbuffers();
481 #ifdef HAVE_CROSSFADE
482 pcmbuf_finish_crossfade_enable();
483 #else
484 pcmbuf_watermark = PCMBUF_WATERMARK;
485 #endif
487 pcmbuf_play_stop();
489 pcmbuf_soft_mode(false);
491 return pcmbuf_bufend - pcmbuffer;
495 /** Track change */
496 void pcmbuf_monitor_track_change(bool monitor)
498 pcm_play_lock();
500 if (last_chunksize != 0)
502 /* If monitoring, wait until this track runs out. Place in
503 currently playing chunk. If not, cancel notification. */
504 track_transition = monitor;
505 read_end_chunk->end_of_track = monitor;
506 if (!monitor)
508 /* Clear all notifications */
509 struct chunkdesc *desc = first_desc;
510 struct chunkdesc *end = desc + pcmbuf_descs();
511 while (desc < end)
512 desc++->end_of_track = false;
515 else
517 /* Post now if PCM stopped and last buffer was sent. */
518 track_transition = false;
519 if (monitor)
520 audio_pcmbuf_track_change(false);
523 pcm_play_unlock();
526 bool pcmbuf_start_track_change(bool auto_skip)
528 bool crossfade = false;
529 #ifdef HAVE_CROSSFADE
530 /* Determine whether this track change needs to crossfade */
531 if(crossfade_enabled && !pcmbuf_is_crossfade_active())
533 switch(global_settings.crossfade)
535 case CROSSFADE_ENABLE_AUTOSKIP:
536 crossfade = auto_skip;
537 break;
538 case CROSSFADE_ENABLE_MANSKIP:
539 crossfade = !auto_skip;
540 break;
541 case CROSSFADE_ENABLE_SHUFFLE:
542 crossfade = global_settings.playlist_shuffle;
543 break;
544 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
545 crossfade = global_settings.playlist_shuffle || !auto_skip;
546 break;
547 case CROSSFADE_ENABLE_ALWAYS:
548 crossfade = true;
549 break;
552 #endif
554 if (!auto_skip || crossfade)
555 /* manual skip or crossfade */
557 if (crossfade)
558 { logf(" crossfade track change"); }
559 else
560 { logf(" manual track change"); }
562 pcm_play_lock();
564 /* Cancel any pending automatic gapless transition */
565 pcmbuf_monitor_track_change(false);
567 /* Can't do two crossfades at once and, no fade if pcm is off now */
568 if (
569 #ifdef HAVE_CROSSFADE
570 pcmbuf_is_crossfade_active() ||
571 #endif
572 mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED)
574 pcmbuf_play_stop();
575 pcm_play_unlock();
576 /* Notify playback that the track change starts now */
577 return true;
580 /* Not enough data, or not crossfading, flush the old data instead */
581 if (LOW_DATA(2) || !crossfade || low_latency_mode)
583 commit_chunk(true);
585 #ifdef HAVE_CROSSFADE
586 else
588 /* Don't enable mix mode when skipping tracks manually. */
589 crossfade_mixmode = auto_skip &&
590 global_settings.crossfade_fade_out_mixmode;
592 crossfade_auto_skip = auto_skip;
594 crossfade_track_change_started = crossfade;
596 #endif
597 pcm_play_unlock();
599 /* Keep trigger outside the play lock or HW FIFO underruns can happen
600 since frequency scaling is *not* always fast */
601 trigger_cpu_boost();
603 /* Notify playback that the track change starts now */
604 return true;
606 else /* automatic and not crossfading, so do gapless track change */
608 /* The codec is moving on to the next track, but the current track will
609 * continue to play. Set a flag to make sure the elapsed time of the
610 * current track will be updated properly, and mark the current chunk
611 * as the last one in the track. */
612 logf(" gapless track change");
613 pcmbuf_monitor_track_change(true);
614 return false;
619 /** Playback */
621 /* PCM driver callback
622 * This function has 3 major logical parts (separated by brackets both for
623 * readability and variable scoping). The first part performs the
624 * operations related to finishing off the last chunk we fed to the DMA.
625 * The second part detects the end of playlist condition when the PCM
626 * buffer is empty except for uncommitted samples. Then they are committed
627 * and sent to the PCM driver for playback. The third part performs the
628 * operations involved in sending a new chunk to the DMA. */
629 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
632 struct chunkdesc *pcmbuf_current = read_chunk;
633 /* Take the finished chunk out of circulation */
634 read_chunk = pcmbuf_current->link;
636 /* if during a track transition, update the elapsed time in ms */
637 if (track_transition)
638 audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE);
640 /* if last chunk in the track, stop updates and notify audio thread */
641 if (pcmbuf_current->end_of_track)
643 track_transition = false;
644 audio_pcmbuf_track_change(true);
647 /* Put the finished chunk back into circulation */
648 write_end_chunk->link = pcmbuf_current;
649 write_end_chunk = pcmbuf_current;
651 #ifdef HAVE_CROSSFADE
652 /* If we've read over the crossfade chunk while it's still fading */
653 if (pcmbuf_current == crossfade_chunk)
654 crossfade_chunk = read_chunk;
655 #endif
659 /* Commit last samples at end of playlist */
660 if (pcmbuffer_fillpos && !read_chunk)
662 logf("pcmbuf_pcm_callback: commit last samples");
663 commit_chunk(false);
668 /* Send the new chunk to the DMA */
669 if(read_chunk)
671 last_chunksize = read_chunk->size;
672 pcmbuf_unplayed_bytes -= last_chunksize;
673 *size = last_chunksize;
674 *start = read_chunk->addr;
676 else
678 /* No more chunks */
679 logf("pcmbuf_pcm_callback: no more chunks");
680 last_chunksize = 0;
681 *size = 0;
682 *start = NULL;
685 DISPLAY_DESC("callback");
688 /* Force playback */
689 void pcmbuf_play_start(void)
691 if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) == CHANNEL_STOPPED &&
692 pcmbuf_unplayed_bytes && read_chunk != NULL)
694 logf("pcmbuf_play_start");
695 last_chunksize = read_chunk->size;
696 pcmbuf_unplayed_bytes -= last_chunksize;
697 mixer_channel_play_data(PCM_MIXER_CHAN_PLAYBACK,
698 pcmbuf_pcm_callback, NULL, 0);
702 void pcmbuf_play_stop(void)
704 logf("pcmbuf_play_stop");
705 mixer_channel_stop(PCM_MIXER_CHAN_PLAYBACK);
707 pcmbuf_unplayed_bytes = 0;
708 if (read_chunk) {
709 write_end_chunk->link = read_chunk;
710 write_end_chunk = read_end_chunk;
711 read_chunk = read_end_chunk = NULL;
713 last_chunksize = 0;
714 pcmbuffer_pos = 0;
715 pcmbuffer_fillpos = 0;
716 #ifdef HAVE_CROSSFADE
717 crossfade_track_change_started = false;
718 crossfade_active = false;
719 #endif
720 track_transition = false;
721 flush_pcmbuf = false;
722 DISPLAY_DESC("play_stop");
724 /* Can unboost the codec thread here no matter who's calling,
725 * pretend full pcm buffer to unboost */
726 boost_codec_thread(10);
729 void pcmbuf_pause(bool pause)
731 logf("pcmbuf_pause: %s", pause?"pause":"play");
733 if (mixer_channel_status(PCM_MIXER_CHAN_PLAYBACK) != CHANNEL_STOPPED)
734 mixer_channel_play_pause(PCM_MIXER_CHAN_PLAYBACK, !pause);
735 else if (!pause)
736 pcmbuf_play_start();
740 /** Crossfade */
742 /* Clip sample to signed 16 bit range */
743 static inline int32_t clip_sample_16(int32_t sample)
745 if ((int16_t)sample != sample)
746 sample = 0x7fff ^ (sample >> 31);
747 return sample;
750 #ifdef HAVE_CROSSFADE
751 /* Find the chunk that's (length) deep in the list. Return the position within
752 * the chunk, and leave the chunkdesc pointer pointing to the chunk. */
753 static size_t find_chunk(size_t length, struct chunkdesc **chunk)
755 while (*chunk && length >= (*chunk)->size)
757 length -= (*chunk)->size;
758 *chunk = (*chunk)->link;
760 return length;
763 /* Returns the number of bytes _NOT_ mixed/faded */
764 static size_t crossfade_mix_fade(int factor, size_t length, const char *buf,
765 size_t *out_sample, struct chunkdesc **out_chunk)
767 if (length == 0)
768 return 0;
770 const int16_t *input_buf = (const int16_t *)buf;
771 int16_t *output_buf = (int16_t *)((*out_chunk)->addr);
772 int16_t *chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
773 output_buf = &output_buf[*out_sample];
774 int32_t sample;
776 while (length)
778 /* fade left and right channel at once to keep buffer alignment */
779 int i;
780 for (i = 0; i < 2; i++)
782 if (input_buf)
783 /* fade the input buffer and mix into the chunk */
785 sample = *input_buf++;
786 sample = ((sample * factor) >> 8) + *output_buf;
787 *output_buf++ = clip_sample_16(sample);
789 else
790 /* fade the chunk only */
792 sample = *output_buf;
793 *output_buf++ = (sample * factor) >> 8;
797 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
799 /* move to next chunk as needed */
800 if (output_buf >= chunk_end)
802 *out_chunk = (*out_chunk)->link;
803 if (!(*out_chunk))
804 return length;
805 output_buf = (int16_t *)((*out_chunk)->addr);
806 chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
809 *out_sample = output_buf - (int16_t *)((*out_chunk)->addr);
810 return 0;
813 /* Initializes crossfader, calculates all necessary parameters and performs
814 * fade-out with the PCM buffer. */
815 static void crossfade_start(void)
817 size_t crossfade_rem;
818 size_t crossfade_need;
819 size_t fade_out_rem;
820 size_t fade_out_delay;
821 size_t fade_in_delay;
823 crossfade_track_change_started = false;
824 /* Reject crossfade if less than .5s of data */
825 if (LOW_DATA(2)) {
826 logf("crossfade rejected");
827 pcmbuf_play_stop();
828 return ;
831 logf("crossfade_start");
832 commit_chunk(false);
833 crossfade_active = true;
835 /* Initialize the crossfade buffer size to all of the buffered data that
836 * has not yet been sent to the DMA */
837 crossfade_rem = pcmbuf_unplayed_bytes;
838 crossfade_chunk = read_chunk->link;
839 crossfade_sample = 0;
841 /* Get fade out info from settings. */
842 fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE;
843 fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE;
845 crossfade_need = fade_out_delay + fade_out_rem;
846 if (crossfade_rem > crossfade_need)
848 if (crossfade_auto_skip)
849 /* Automatic track changes only modify the last part of the buffer,
850 * so find the right chunk and sample to start the crossfade */
852 crossfade_sample = find_chunk(crossfade_rem - crossfade_need,
853 &crossfade_chunk) / 2;
854 crossfade_rem = crossfade_need;
856 else
857 /* Manual skips occur immediately, but give time to process */
859 crossfade_rem -= crossfade_chunk->size;
860 crossfade_chunk = crossfade_chunk->link;
863 /* Truncate fade out duration if necessary. */
864 if (crossfade_rem < crossfade_need)
866 size_t crossfade_short = crossfade_need - crossfade_rem;
867 if (fade_out_rem >= crossfade_short)
868 fade_out_rem -= crossfade_short;
869 else
871 fade_out_delay -= crossfade_short - fade_out_rem;
872 fade_out_rem = 0;
875 crossfade_rem -= fade_out_delay + fade_out_rem;
877 /* Completely process the crossfade fade-out effect with current PCM buffer */
878 if (!crossfade_mixmode)
880 /* Fade out the specified amount of the already processed audio */
881 size_t total_fade_out = fade_out_rem;
882 size_t fade_out_sample;
883 struct chunkdesc *fade_out_chunk = crossfade_chunk;
885 /* Find the right chunk and sample to start fading out */
886 fade_out_delay += crossfade_sample * 2;
887 fade_out_sample = find_chunk(fade_out_delay, &fade_out_chunk) / 2;
889 while (fade_out_rem > 0)
891 /* Each 1/10 second of audio will have the same fade applied */
892 size_t block_rem = MIN(BYTERATE / 10, fade_out_rem);
893 int factor = (fade_out_rem << 8) / total_fade_out;
895 fade_out_rem -= block_rem;
897 crossfade_mix_fade(factor, block_rem, NULL,
898 &fade_out_sample, &fade_out_chunk);
901 /* zero out the rest of the buffer */
902 crossfade_mix_fade(0, crossfade_rem, NULL,
903 &fade_out_sample, &fade_out_chunk);
906 /* Initialize fade-in counters */
907 crossfade_fade_in_total = global_settings.crossfade_fade_in_duration * BYTERATE;
908 crossfade_fade_in_rem = crossfade_fade_in_total;
910 fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE;
912 /* Find the right chunk and sample to start fading in */
913 fade_in_delay += crossfade_sample * 2;
914 crossfade_sample = find_chunk(fade_in_delay, &crossfade_chunk) / 2;
915 logf("crossfade_start done!");
918 /* Perform fade-in of new track */
919 static void write_to_crossfade(size_t length)
921 if (length)
923 char *buf = fadebuf;
924 if (crossfade_fade_in_rem)
926 size_t samples;
927 int16_t *input_buf;
929 /* Fade factor for this packet */
930 int factor =
931 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
932 crossfade_fade_in_total;
933 /* Bytes to fade */
934 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
936 /* We _will_ fade this many bytes */
937 crossfade_fade_in_rem -= fade_rem;
939 if (crossfade_chunk)
941 /* Mix the data */
942 size_t fade_total = fade_rem;
943 fade_rem = crossfade_mix_fade(factor, fade_rem, buf,
944 &crossfade_sample, &crossfade_chunk);
945 length -= fade_total - fade_rem;
946 buf += fade_total - fade_rem;
947 if (!length)
948 return;
951 samples = fade_rem / 2;
952 input_buf = (int16_t *)buf;
953 /* Fade remaining samples in place */
954 while (samples--)
956 int32_t sample = *input_buf;
957 *input_buf++ = (sample * factor) >> 8;
961 if (crossfade_chunk)
963 /* Mix the data */
964 size_t mix_total = length;
965 /* A factor of 256 means mix only, no fading */
966 length = crossfade_mix_fade(256, length, buf,
967 &crossfade_sample, &crossfade_chunk);
968 buf += mix_total - length;
969 if (!length)
970 return;
973 while (length > 0)
975 COMMIT_IF_NEEDED;
976 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
977 size_t copy_n = MIN(length, pcmbuf_size - pcmbuffer_index);
978 memcpy(&pcmbuffer[pcmbuffer_index], buf, copy_n);
979 buf += copy_n;
980 pcmbuffer_fillpos += copy_n;
981 length -= copy_n;
984 /* if no more fading-in to do, stop the crossfade */
985 if (!(crossfade_fade_in_rem || crossfade_chunk))
986 crossfade_active = false;
989 static void pcmbuf_finish_crossfade_enable(void)
991 /* Copy the pending setting over now */
992 crossfade_enabled = crossfade_enable_request;
994 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
995 /* If crossfading, try to keep the buffer full other than 1 second */
996 (pcmbuf_size - BYTERATE) :
997 /* Otherwise, just use the default */
998 PCMBUF_WATERMARK;
1001 bool pcmbuf_is_crossfade_active(void)
1003 return crossfade_active || crossfade_track_change_started;
1006 void pcmbuf_request_crossfade_enable(bool on_off)
1008 /* Next setting to be used, not applied now */
1009 crossfade_enable_request = on_off;
1012 bool pcmbuf_is_same_size(void)
1014 /* if pcmbuffer is NULL, then not set up yet even once so always */
1015 bool same_size = pcmbuffer ?
1016 (get_next_required_pcmbuf_size() == pcmbuf_size) : true;
1018 /* no buffer change needed, so finish crossfade setup now */
1019 if (same_size)
1020 pcmbuf_finish_crossfade_enable();
1022 return same_size;
1024 #endif /* HAVE_CROSSFADE */
1027 /** Debug menu, other metrics */
1029 /* Amount of bytes left in the buffer. */
1030 size_t pcmbuf_free(void)
1032 if (read_chunk != NULL)
1034 void *read = (void *)read_chunk->addr;
1035 void *write = &pcmbuffer[pcmbuffer_pos + pcmbuffer_fillpos];
1036 if (read < write)
1037 return (size_t)(read - write) + pcmbuf_size;
1038 else
1039 return (size_t) (read - write);
1041 return pcmbuf_size - pcmbuffer_fillpos;
1044 size_t pcmbuf_get_bufsize(void)
1046 return pcmbuf_size;
1049 int pcmbuf_used_descs(void)
1051 struct chunkdesc *temp = read_chunk;
1052 unsigned int i = 0;
1053 while (temp) {
1054 temp = temp->link;
1055 i++;
1057 return i;
1060 int pcmbuf_descs(void)
1062 return NUM_CHUNK_DESCS(pcmbuf_size);
1065 #ifdef ROCKBOX_HAS_LOGF
1066 unsigned char *pcmbuf_get_meminfo(size_t *length)
1068 *length = pcmbuf_bufend - pcmbuffer;
1069 return pcmbuffer;
1071 #endif
1074 /** Fading and channel volume control */
1076 /* Sync the channel amplitude to all states */
1077 static void pcmbuf_update_volume(void)
1079 unsigned int vol = fade_vol;
1081 if (soft_mode)
1082 vol >>= 2;
1084 mixer_channel_set_amplitude(PCM_MIXER_CHAN_PLAYBACK, vol);
1087 /* Quiet-down the channel if 'shhh' is true or else play at normal level */
1088 void pcmbuf_soft_mode(bool shhh)
1090 soft_mode = shhh;
1091 pcmbuf_update_volume();
1094 /* Fade channel in or out */
1095 void pcmbuf_fade(bool fade, bool in)
1097 if (!fade)
1099 /* Simply set the level */
1100 fade_vol = in ? MIX_AMP_UNITY : MIX_AMP_MUTE;
1102 else
1104 /* Start from the opposing end */
1105 fade_vol = in ? MIX_AMP_MUTE : MIX_AMP_UNITY;
1108 pcmbuf_update_volume();
1110 if (fade)
1112 /* Do this on thread for now */
1113 #ifdef HAVE_PRIORITY_SCHEDULING
1114 int old_prio = thread_set_priority(thread_self(), PRIORITY_REALTIME);
1115 #endif
1117 while (1)
1119 /* Linear fade actually sounds better */
1120 if (in)
1121 fade_vol += MIN(MIX_AMP_UNITY/16, MIX_AMP_UNITY - fade_vol);
1122 else
1123 fade_vol -= MIN(MIX_AMP_UNITY/16, fade_vol - MIX_AMP_MUTE);
1125 pcmbuf_update_volume();
1127 if (fade_vol > MIX_AMP_MUTE && fade_vol < MIX_AMP_UNITY)
1129 sleep(0);
1130 continue;
1133 break;
1136 #ifdef HAVE_PRIORITY_SCHEDULING
1137 thread_set_priority(thread_self(), old_prio);
1138 #endif
1143 /** Misc */
1145 bool pcmbuf_is_lowdata(void)
1147 if (!pcm_is_playing() || pcm_is_paused()
1148 #ifdef HAVE_CROSSFADE
1149 || pcmbuf_is_crossfade_active()
1150 #endif
1152 return false;
1154 #if MEMORYSIZE > 2
1155 /* 1 seconds of buffer is low data */
1156 return LOW_DATA(4);
1157 #else
1158 /* under watermark is low data */
1159 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
1160 #endif
1163 void pcmbuf_set_low_latency(bool state)
1165 low_latency_mode = state;
1168 unsigned long pcmbuf_get_latency(void)
1170 return (pcmbuf_unplayed_bytes +
1171 mixer_channel_get_bytes_waiting(PCM_MIXER_CHAN_PLAYBACK)) * 1000 / BYTERATE;