Merge duplicating code to move cursor left/right.
[kugel-rb.git] / apps / pcmbuf.c
blobe431ccd3e09c3245bccbb63db05ec53a43a9f037
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include "config.h"
23 #include "system.h"
24 #include "debug.h"
25 #include <kernel.h>
26 #include "pcmbuf.h"
27 #include "pcm.h"
28 #include "playback.h"
30 /* Define LOGF_ENABLE to enable logf output in this file */
31 /*#define LOGF_ENABLE*/
32 #include "logf.h"
33 #ifndef SIMULATOR
34 #include "cpu.h"
35 #endif
36 #include <string.h>
37 #include "buffer.h"
38 #include "settings.h"
39 #include "audio.h"
40 #include "voice_thread.h"
41 #include "dsp.h"
42 #include "thread.h"
44 /* Clip sample to signed 16 bit range */
45 static inline int32_t clip_sample_16(int32_t sample)
47 if ((int16_t)sample != sample)
48 sample = 0x7fff ^ (sample >> 31);
49 return sample;
52 #define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
53 on the pcm buffer */
54 #define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
55 chunks on the pcm buffer (or we run out
56 of buffer descriptors, which is
57 non-fatal) */
58 #define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
59 this to the DMA */
60 #define PCMBUF_MIX_CHUNK 8192 /* This is the maximum size of one packet
61 for mixing (crossfade or voice) */
63 #if MEMORYSIZE > 2
64 /* Keep watermark high for iPods at least (2s) */
65 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
66 #else
67 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
68 #endif
70 /* Structure we can use to queue pcm chunks in memory to be played
71 * by the driver code. */
72 struct pcmbufdesc
74 void *addr;
75 size_t size;
76 struct pcmbufdesc* link;
77 /* true if last chunk in the track */
78 bool end_of_track;
81 #define PCMBUF_DESCS(bufsize) \
82 ((bufsize) / PCMBUF_MINAVG_CHUNK)
83 #define PCMBUF_DESCS_SIZE(bufsize) \
84 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
86 /* Size of the PCM buffer. */
87 static size_t pcmbuf_size IDATA_ATTR = 0;
88 static char *pcmbuf_bufend IDATA_ATTR;
89 static char *audiobuffer IDATA_ATTR;
90 /* Current audio buffer write index. */
91 static size_t audiobuffer_pos IDATA_ATTR;
92 /* Amount audiobuffer_pos will be increased.*/
93 static size_t audiobuffer_fillpos IDATA_ATTR;
94 static char *fadebuf IDATA_ATTR;
95 static char *voicebuf IDATA_ATTR;
97 static bool end_of_track IDATA_ATTR;
98 bool track_transition IDATA_ATTR;
100 /* Crossfade related state */
101 static bool crossfade_enabled;
102 static bool crossfade_enabled_pending;
103 static bool crossfade_mixmode;
104 static bool crossfade_active IDATA_ATTR;
105 static bool crossfade_init IDATA_ATTR;
107 /* Track the current location for processing crossfade */
108 static struct pcmbufdesc *crossfade_chunk IDATA_ATTR;
109 #ifdef HAVE_CROSSFADE
110 static size_t crossfade_sample IDATA_ATTR;
112 /* Counters for fading in new data */
113 static size_t crossfade_fade_in_total IDATA_ATTR;
114 static size_t crossfade_fade_in_rem IDATA_ATTR;
115 #endif
117 static struct pcmbufdesc *pcmbuf_read IDATA_ATTR;
118 static struct pcmbufdesc *pcmbuf_read_end IDATA_ATTR;
119 static struct pcmbufdesc *pcmbuf_write IDATA_ATTR;
120 static struct pcmbufdesc *pcmbuf_write_end IDATA_ATTR;
121 static size_t last_chunksize IDATA_ATTR;
123 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
124 static size_t pcmbuf_watermark IDATA_ATTR;
126 static struct pcmbufdesc *pcmbuf_mix_chunk IDATA_ATTR;
127 static size_t pcmbuf_mix_sample IDATA_ATTR;
129 static bool low_latency_mode = false;
130 static bool pcmbuf_flush;
132 #ifdef HAVE_PRIORITY_SCHEDULING
133 static int codec_thread_priority = PRIORITY_PLAYBACK;
134 #endif
136 extern unsigned int codec_thread_id;
138 /* Helpful macros for use in conditionals this assumes some of the above
139 * static variable names */
140 #define NEED_FLUSH(position) \
141 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
142 #define LOW_DATA(quarter_secs) \
143 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
145 static bool pcmbuf_flush_fillpos(void);
146 static void pcmbuf_finish_track_change(void);
147 #ifdef HAVE_CROSSFADE
148 static void crossfade_start(void);
149 static void flush_crossfade(char *buf, size_t length);
150 #endif
151 static bool pcmbuf_crossfade_init(bool manual_skip);
152 static void pcmbuf_finish_crossfade_enable(void);
153 static bool pcmbuf_is_crossfade_enabled(void);
155 /**************************************/
157 /* define this to show detailed pcmbufdesc usage information on the sim console */
158 /*#define DESC_DEBUG*/
160 #ifndef SIMULATOR
161 #undef DESC_DEBUG
162 #endif
163 #ifdef DESC_DEBUG
164 static struct pcmbufdesc *first_desc;
165 static bool show_desc_in_use = false;
166 #define DISPLAY_DESC(caller) while(!show_desc(caller))
167 #define DESC_IDX(desc) (desc ? desc - first_desc : -1)
168 #define DESCL_IDX(desc) (desc && desc->link ? desc->link - first_desc : -1)
169 #define SHOW_1ST(desc) if(DESC_IDX (desc)==-1) DEBUGF(" -- "); \
170 else DEBUGF(" %02d ", DESC_IDX(desc))
171 #define SHOW_2ND(desc) if(DESCL_IDX(desc)==-1) DEBUGF("l -- "); \
172 else DEBUGF("l %02d ", DESCL_IDX(desc))
173 #define DESC_SHOW(tag, desc) DEBUGF(tag);SHOW_1ST(desc); \
174 DEBUGF(tag);SHOW_2ND(desc)
176 static bool show_desc(char *caller)
178 if (show_desc_in_use) return false;
179 show_desc_in_use = true;
180 DEBUGF("%-14s\t", caller);
181 DESC_SHOW("r", pcmbuf_read);
182 DESC_SHOW("re", pcmbuf_read_end);
183 DEBUGF(" ");
184 DESC_SHOW("w", pcmbuf_write);
185 DESC_SHOW("we", pcmbuf_write_end);
186 DEBUGF("\n");
187 show_desc_in_use = false;
188 return true;
190 #else
191 #define DISPLAY_DESC(caller) do{}while(0)
192 #endif
195 /* Commit PCM data */
197 #ifdef HAVE_PRIORITY_SCHEDULING
198 static void boost_codec_thread(bool boost)
200 /* Keep voice and codec threads at the same priority or else voice
201 * will starve if the codec thread's priority is boosted. */
202 if (boost)
204 int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes
205 / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX;
207 if (priority != codec_thread_priority)
209 codec_thread_priority = priority;
210 thread_set_priority(codec_thread_id, priority);
211 voice_thread_set_priority(priority);
214 else if (codec_thread_priority != PRIORITY_PLAYBACK)
216 thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK);
217 voice_thread_set_priority(PRIORITY_PLAYBACK);
218 codec_thread_priority = PRIORITY_PLAYBACK;
221 #endif /* HAVE_PRIORITY_SCHEDULING */
223 static void pcmbuf_under_watermark(bool under)
225 /* Only codec thread initiates boost - voice boosts the cpu when playing
226 a clip */
227 #ifndef SIMULATOR
228 if (thread_get_current() == codec_thread_id)
229 #endif /* SIMULATOR */
231 if (under)
233 /* Fill audio buffer by boosting cpu */
234 trigger_cpu_boost();
235 #ifdef HAVE_PRIORITY_SCHEDULING
236 /* If buffer is critically low, override UI priority, else
237 set back to the original priority. */
238 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
239 #endif
241 else
243 #ifdef HAVE_PRIORITY_SCHEDULING
244 boost_codec_thread(false);
245 #endif
249 /* Disable crossfade if < .5s of audio */
250 if (LOW_DATA(2))
252 crossfade_active = false;
256 static bool prepare_insert(size_t length)
258 if (low_latency_mode)
260 /* 1/4s latency. */
261 if (!LOW_DATA(1) && pcm_is_playing())
262 return false;
265 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
266 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
267 return false;
269 if (!pcm_is_playing())
271 trigger_cpu_boost();
273 /* Pre-buffer up to watermark */
274 #if MEMORYSIZE > 2
275 if (!LOW_DATA(4))
276 #else
277 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
278 #endif
280 logf("pcm starting");
281 if (!(audio_status() & AUDIO_STATUS_PAUSE))
282 pcmbuf_play_start();
285 else
286 pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark);
288 return true;
291 void *pcmbuf_request_buffer(int *count)
293 #ifdef HAVE_CROSSFADE
294 if (crossfade_init)
295 crossfade_start();
296 #endif
298 if (crossfade_active) {
299 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
300 return fadebuf;
302 else
304 if(prepare_insert(*count << 2))
306 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
307 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
309 /* Usual case, there's space here */
310 return &audiobuffer[audiobuffer_index];
312 else
314 /* Flush and wrap the buffer */
315 pcmbuf_flush_fillpos();
316 audiobuffer_pos = 0;
317 return &audiobuffer[0];
320 else
322 return NULL;
327 void pcmbuf_write_complete(int count)
329 size_t length = (size_t)(unsigned int)count << 2;
330 #ifdef HAVE_CROSSFADE
331 if (crossfade_active)
333 flush_crossfade(fadebuf, length);
334 if (!(crossfade_fade_in_rem || crossfade_chunk))
335 crossfade_active = false;
337 else
338 #endif
340 audiobuffer_fillpos += length;
342 if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos))
343 pcmbuf_flush_fillpos();
347 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
348 * in a separate function for the moment */
349 static inline void pcmbuf_add_chunk(void)
351 register size_t size = audiobuffer_fillpos;
352 /* Grab the next description to write, and change the write pointer */
353 register struct pcmbufdesc *pcmbuf_current = pcmbuf_write;
354 pcmbuf_write = pcmbuf_current->link;
355 /* Fill in the values in the new buffer chunk */
356 pcmbuf_current->addr = &audiobuffer[audiobuffer_pos];
357 pcmbuf_current->size = size;
358 pcmbuf_current->end_of_track = end_of_track;
359 pcmbuf_current->link = NULL;
360 end_of_track = false; /* This is single use only */
361 if (pcmbuf_read != NULL) {
362 if (pcmbuf_flush)
364 pcmbuf_write_end->link = pcmbuf_read->link;
365 pcmbuf_read->link = pcmbuf_current;
366 while (pcmbuf_write_end->link)
368 pcmbuf_write_end = pcmbuf_write_end->link;
369 pcmbuf_unplayed_bytes -= pcmbuf_write_end->size;
371 pcmbuf_flush = false;
373 /* If there is already a read buffer setup, add to it */
374 else
375 pcmbuf_read_end->link = pcmbuf_current;
376 } else {
377 /* Otherwise create the buffer */
378 pcmbuf_read = pcmbuf_current;
380 /* This is now the last buffer to read */
381 pcmbuf_read_end = pcmbuf_current;
383 /* Update bytes counters */
384 pcmbuf_unplayed_bytes += size;
386 audiobuffer_pos += size;
387 if (audiobuffer_pos >= pcmbuf_size)
388 audiobuffer_pos -= pcmbuf_size;
390 audiobuffer_fillpos = 0;
391 DISPLAY_DESC("add_chunk");
395 * Commit samples waiting to the pcm buffer.
397 static bool pcmbuf_flush_fillpos(void)
399 if (audiobuffer_fillpos) {
400 /* Never use the last buffer descriptor */
401 while (pcmbuf_write == pcmbuf_write_end) {
402 /* If this happens, something is being stupid */
403 if (!pcm_is_playing()) {
404 logf("pcmbuf_flush_fillpos error");
405 pcmbuf_play_start();
407 /* Let approximately one chunk of data playback */
408 sleep(HZ*PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY*4));
410 pcmbuf_add_chunk();
411 return true;
413 return false;
417 /* Init */
419 static void pcmbuf_init_pcmbuffers(void)
421 #ifdef DESC_DEBUG
422 first_desc = pcmbuf_write;
423 #endif
424 struct pcmbufdesc *next = pcmbuf_write;
425 next++;
426 pcmbuf_write_end = pcmbuf_write;
427 while ((void *)next < (void *)pcmbuf_bufend) {
428 pcmbuf_write_end->link=next;
429 pcmbuf_write_end=next;
430 next++;
432 DISPLAY_DESC("init");
435 static size_t pcmbuf_get_next_required_pcmbuf_size(void)
437 size_t seconds = 1;
439 if (crossfade_enabled_pending)
440 seconds += global_settings.crossfade_fade_out_delay
441 + global_settings.crossfade_fade_out_duration;
443 #if MEMORYSIZE > 2
444 /* Buffer has to be at least 2s long. */
445 seconds += 2;
446 #endif
447 logf("pcmbuf len: %ld", (long)seconds);
448 return seconds * (NATIVE_FREQUENCY*4); /* 2 channels + 2 bytes/sample */
451 static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize)
453 return pcmbuf_bufend - (bufsize + PCMBUF_MIX_CHUNK * 2 +
454 PCMBUF_DESCS_SIZE(bufsize));
457 /* Initialize the pcmbuffer the structure looks like this:
458 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
459 size_t pcmbuf_init(unsigned char *bufend)
461 pcmbuf_bufend = bufend;
462 pcmbuf_size = pcmbuf_get_next_required_pcmbuf_size();
463 audiobuffer = pcmbuf_calc_audiobuffer_ptr(pcmbuf_size);
464 fadebuf = &audiobuffer[pcmbuf_size];
465 voicebuf = &fadebuf[PCMBUF_MIX_CHUNK];
466 pcmbuf_write = (struct pcmbufdesc *)&voicebuf[PCMBUF_MIX_CHUNK];
468 pcmbuf_init_pcmbuffers();
470 if(track_transition){logf("pcmbuf: (init) track transition false");}
471 end_of_track = false;
472 track_transition = false;
474 pcmbuf_finish_crossfade_enable();
476 pcmbuf_play_stop();
478 return pcmbuf_bufend - audiobuffer;
482 /* Playback */
484 /** PCM driver callback
485 * This function has 3 major logical parts (separated by brackets both for
486 * readability and variable scoping). The first part performs the
487 * operations related to finishing off the last buffer we fed to the DMA.
488 * The second part detects the end of playlist condition when the pcm
489 * buffer is empty except for uncommitted samples. Then they are committed.
490 * The third part performs the operations involved in sending a new buffer
491 * to the DMA. */
492 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR;
493 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
496 struct pcmbufdesc *pcmbuf_current = pcmbuf_read;
497 /* Take the finished buffer out of circulation */
498 pcmbuf_read = pcmbuf_current->link;
500 /* if during a track transition, update the elapsed time */
501 if (track_transition)
502 audio_pcmbuf_position_callback(last_chunksize);
504 /* if last buffer in the track, let the audio thread know */
505 if (pcmbuf_current->end_of_track)
506 pcmbuf_finish_track_change();
508 /* Put the finished buffer back into circulation */
509 pcmbuf_write_end->link = pcmbuf_current;
510 pcmbuf_write_end = pcmbuf_current;
512 /* If we've read over the mix chunk while it's still mixing there */
513 if (pcmbuf_current == pcmbuf_mix_chunk)
514 pcmbuf_mix_chunk = NULL;
515 /* If we've read over the crossfade chunk while it's still fading */
516 if (pcmbuf_current == crossfade_chunk)
517 crossfade_chunk = pcmbuf_read;
521 /* Commit last samples at end of playlist */
522 if (audiobuffer_fillpos && !pcmbuf_read)
524 logf("pcmbuf_pcm_callback: commit last samples");
525 pcmbuf_flush_fillpos();
530 /* Send the new buffer to the pcm */
531 struct pcmbufdesc *pcmbuf_new = pcmbuf_read;
532 size_t *realsize = size;
533 unsigned char** realstart = start;
534 if(pcmbuf_new)
536 size_t current_size = pcmbuf_new->size;
538 pcmbuf_unplayed_bytes -= current_size;
539 last_chunksize = current_size;
540 *realsize = current_size;
541 *realstart = pcmbuf_new->addr;
543 else
545 /* No more buffers */
546 last_chunksize = 0;
547 *realsize = 0;
548 *realstart = NULL;
549 if (end_of_track)
550 pcmbuf_finish_track_change();
553 DISPLAY_DESC("callback");
556 /* Force playback. */
557 void pcmbuf_play_start(void)
559 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL)
561 last_chunksize = pcmbuf_read->size;
562 pcmbuf_unplayed_bytes -= last_chunksize;
563 pcm_play_data(pcmbuf_pcm_callback,
564 (unsigned char *)pcmbuf_read->addr, last_chunksize);
568 void pcmbuf_play_stop(void)
570 pcm_play_stop();
572 pcmbuf_unplayed_bytes = 0;
573 pcmbuf_mix_chunk = NULL;
574 if (pcmbuf_read) {
575 pcmbuf_write_end->link = pcmbuf_read;
576 pcmbuf_write_end = pcmbuf_read_end;
577 pcmbuf_read = pcmbuf_read_end = NULL;
579 audiobuffer_pos = 0;
580 audiobuffer_fillpos = 0;
581 crossfade_init = false;
582 crossfade_active = false;
583 pcmbuf_flush = false;
584 DISPLAY_DESC("play_stop");
586 #ifdef HAVE_PRIORITY_SCHEDULING
587 /* Can unboost the codec thread here no matter who's calling */
588 boost_codec_thread(false);
589 #endif
592 void pcmbuf_pause(bool pause)
594 if (pcm_is_playing())
595 pcm_play_pause(!pause);
596 else if (!pause)
597 pcmbuf_play_start();
601 /* Track change */
603 /* The codec is moving on to the next track, but the current track is
604 * still playing. Set flags to make sure the elapsed time of the current
605 * track is updated properly, and mark the currently written chunk as the
606 * last one in the track. */
607 static void pcmbuf_gapless_track_change(void)
609 /* we're starting a track transition */
610 track_transition = true;
612 /* mark the last chunk in the track */
613 end_of_track = true;
616 static void pcmbuf_crossfade_track_change(void)
618 /* Initiate automatic crossfade mode */
619 pcmbuf_crossfade_init(false);
620 /* Notify the wps that the track change starts now */
621 audio_post_track_change(false);
624 void pcmbuf_start_track_change(bool manual_skip)
626 /* Manual track change (always crossfade or flush audio). */
627 if (manual_skip)
629 pcmbuf_crossfade_init(true);
630 audio_post_track_change(false);
632 /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */
633 else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active()
634 && global_settings.crossfade != CROSSFADE_ENABLE_TRACKSKIP)
636 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP)
638 if (global_settings.playlist_shuffle)
639 /* shuffle mode is on, so crossfade: */
640 pcmbuf_crossfade_track_change();
641 else
642 /* shuffle mode is off, so normal gapless playback */
643 pcmbuf_gapless_track_change();
645 else
646 /* normal crossfade: */
647 pcmbuf_crossfade_track_change();
649 else
650 /* normal gapless playback. */
651 pcmbuf_gapless_track_change();
654 /* Called when the last chunk in the track has been played */
655 static void pcmbuf_finish_track_change(void)
657 /* not in a track transition anymore */
658 if(track_transition){logf("pcmbuf: (finish change) track transition false");}
659 track_transition = false;
661 /* notify playback that the track has just finished */
662 audio_post_track_change(true);
666 /* Crossfade */
668 /**
669 * Low memory targets don't have crossfade, so don't compile crossfade
670 * specific code in order to save some memory. */
672 #ifdef HAVE_CROSSFADE
674 * Completely process the crossfade fade out effect with current pcm buffer.
676 static void crossfade_process_buffer(size_t fade_in_delay,
677 size_t fade_out_delay, size_t fade_out_rem)
679 if (!crossfade_mixmode)
681 /* Fade out the specified amount of the already processed audio */
682 size_t total_fade_out = fade_out_rem;
683 size_t fade_out_sample;
684 struct pcmbufdesc *fade_out_chunk = crossfade_chunk;
686 /* Find the right chunk to start fading out */
687 fade_out_delay += crossfade_sample * 2;
688 while (fade_out_delay != 0 && fade_out_delay >= fade_out_chunk->size)
690 fade_out_delay -= fade_out_chunk->size;
691 fade_out_chunk = fade_out_chunk->link;
693 /* The start sample within the chunk */
694 fade_out_sample = fade_out_delay / 2;
696 while (fade_out_rem > 0)
698 /* Each 1/10 second of audio will have the same fade applied */
699 size_t block_rem = MIN(NATIVE_FREQUENCY * 4 / 10, fade_out_rem);
700 int factor = (fade_out_rem << 8) / total_fade_out;
702 fade_out_rem -= block_rem;
704 /* Fade this block */
705 while (block_rem > 0 && fade_out_chunk != NULL)
707 /* Fade one sample */
708 int16_t *buf = (int16_t *)fade_out_chunk->addr;
709 int32_t sample = buf[fade_out_sample];
710 buf[fade_out_sample++] = (sample * factor) >> 8;
712 block_rem -= 2;
713 /* Move to the next chunk as needed */
714 if (fade_out_sample * 2 >= fade_out_chunk->size)
716 fade_out_chunk = fade_out_chunk->link;
717 fade_out_sample = 0;
723 /* Find the right chunk and sample to start fading in */
724 fade_in_delay += crossfade_sample * 2;
725 while (fade_in_delay != 0 && fade_in_delay >= crossfade_chunk->size)
727 fade_in_delay -= crossfade_chunk->size;
728 crossfade_chunk = crossfade_chunk->link;
730 crossfade_sample = fade_in_delay / 2;
731 logf("process done!");
734 /* Initializes crossfader, calculates all necessary parameters and
735 * performs fade-out with the pcm buffer. */
736 static void crossfade_start(void)
738 size_t crossfade_rem;
739 size_t crossfade_need;
740 size_t fade_out_rem;
741 size_t fade_out_delay;
742 size_t fade_in_delay;
744 crossfade_init = false;
745 /* Reject crossfade if less than .5s of data */
746 if (LOW_DATA(2)) {
747 logf("crossfade rejected");
748 pcmbuf_play_stop();
749 return ;
752 logf("crossfade_start");
753 pcmbuf_flush_fillpos();
754 crossfade_active = true;
756 /* Initialize the crossfade buffer size to all of the buffered data that
757 * has not yet been sent to the DMA */
758 crossfade_rem = pcmbuf_unplayed_bytes;
759 crossfade_chunk = pcmbuf_read->link;
760 crossfade_sample = 0;
762 /* Get fade out delay from settings. */
763 fade_out_delay =
764 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_delay * 4;
766 /* Get fade out duration from settings. */
767 fade_out_rem =
768 NATIVE_FREQUENCY * global_settings.crossfade_fade_out_duration * 4;
770 crossfade_need = fade_out_delay + fade_out_rem;
771 /* We want only to modify the last part of the buffer. */
772 if (crossfade_rem > crossfade_need)
774 size_t crossfade_extra = crossfade_rem - crossfade_need;
775 while (crossfade_extra > crossfade_chunk->size)
777 crossfade_extra -= crossfade_chunk->size;
778 crossfade_chunk = crossfade_chunk->link;
780 crossfade_sample = crossfade_extra / 2;
782 /* Truncate fade out duration if necessary. */
783 else if (crossfade_rem < crossfade_need)
785 size_t crossfade_short = crossfade_need - crossfade_rem;
786 if (fade_out_rem >= crossfade_short)
787 fade_out_rem -= crossfade_short;
788 else
790 fade_out_delay -= crossfade_short - fade_out_rem;
791 fade_out_rem = 0;
795 /* Get also fade in duration and delays from settings. */
796 crossfade_fade_in_total =
797 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_duration * 4;
798 crossfade_fade_in_rem = crossfade_fade_in_total;
800 fade_in_delay =
801 NATIVE_FREQUENCY * global_settings.crossfade_fade_in_delay * 4;
803 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
806 /* Returns the number of bytes _NOT_ mixed */
807 static size_t crossfade_mix(int factor, const char *buf, size_t length)
809 const int16_t *input_buf = (const int16_t *)buf;
810 int16_t *output_buf = (int16_t *)(crossfade_chunk->addr);
811 int16_t *chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
812 output_buf = &output_buf[crossfade_sample];
813 int32_t sample;
815 while (length)
817 /* fade left and right channel at once to keep buffer alignment */
818 int i;
819 for (i = 0; i < 2; i++)
821 sample = *input_buf++;
822 sample = ((sample * factor) >> 8) + *output_buf;
823 *output_buf++ = clip_sample_16(sample);
826 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
828 if (output_buf >= chunk_end)
830 crossfade_chunk = crossfade_chunk->link;
831 if (!crossfade_chunk)
832 return length;
833 output_buf = (int16_t *)crossfade_chunk->addr;
834 chunk_end = SKIPBYTES(output_buf, crossfade_chunk->size);
837 crossfade_sample = output_buf - (int16_t *)crossfade_chunk->addr;
838 return 0;
841 static void flush_crossfade(char *buf, size_t length)
843 if (length)
845 if (crossfade_fade_in_rem)
847 size_t samples;
848 int16_t *input_buf;
850 /* Fade factor for this packet */
851 int factor =
852 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
853 crossfade_fade_in_total;
854 /* Bytes to fade */
855 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
857 /* We _will_ fade this many bytes */
858 crossfade_fade_in_rem -= fade_rem;
860 if (crossfade_chunk)
862 /* Mix the data */
863 size_t fade_total = fade_rem;
864 fade_rem = crossfade_mix(factor, buf, fade_rem);
865 length -= fade_total - fade_rem;
866 buf += fade_total - fade_rem;
867 if (!length)
868 return;
871 samples = fade_rem / 2;
872 input_buf = (int16_t *)buf;
873 /* Fade remaining samples in place */
874 while (samples--)
876 int32_t sample = *input_buf;
877 *input_buf++ = (sample * factor) >> 8;
881 if (crossfade_chunk)
883 /* Mix the data */
884 size_t mix_total = length;
885 length = crossfade_mix(256, buf, length);
886 buf += mix_total - length;
887 if (!length)
888 return;
891 /* Flush samples to the buffer */
892 while (!prepare_insert(length))
893 sleep(1);
894 while (length > 0)
896 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
897 if (NEED_FLUSH(audiobuffer_index))
899 pcmbuf_flush_fillpos();
900 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
902 size_t copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
903 memcpy(&audiobuffer[audiobuffer_index], buf, copy_n);
904 buf += copy_n;
905 audiobuffer_fillpos += copy_n;
906 length -= copy_n;
910 #endif /* HAVE_CROSSFADE */
912 static bool pcmbuf_crossfade_init(bool manual_skip)
914 /* Can't do two crossfades at once and, no fade if pcm is off now */
915 if (crossfade_init || crossfade_active || !pcm_is_playing())
917 pcmbuf_play_stop();
918 return false;
921 trigger_cpu_boost();
923 /* Not enough data, or crossfade disabled, flush the old data instead */
924 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode)
926 pcmbuf_flush_fillpos();
927 pcmbuf_flush = true;
928 return false;
931 /* Don't enable mix mode when skipping tracks manually. */
932 if (manual_skip)
933 crossfade_mixmode = false;
934 else
935 crossfade_mixmode = global_settings.crossfade_fade_out_mixmode;
937 crossfade_init = true;
939 return true;
943 static void pcmbuf_finish_crossfade_enable(void)
945 /* Copy the pending setting over now */
946 crossfade_enabled = crossfade_enabled_pending;
948 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
949 /* If crossfading, try to keep the buffer full other than 1 second */
950 (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) :
951 /* Otherwise, just use the default */
952 PCMBUF_WATERMARK;
955 static bool pcmbuf_is_crossfade_enabled(void)
957 if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE)
958 return global_settings.playlist_shuffle;
960 return crossfade_enabled;
963 bool pcmbuf_is_crossfade_active(void)
965 return crossfade_active || crossfade_init;
968 void pcmbuf_request_crossfade_enable(bool on_off)
970 /* Next setting to be used, not applied now */
971 crossfade_enabled_pending = on_off;
974 bool pcmbuf_is_same_size(void)
976 bool same_size;
978 if (audiobuffer == NULL)
979 same_size = true; /* Not set up yet even once so always */
980 else
982 size_t bufsize = pcmbuf_get_next_required_pcmbuf_size();
983 same_size = pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer;
986 if (same_size)
987 pcmbuf_finish_crossfade_enable();
989 return same_size;
993 /* Voice */
995 /* Returns pcm buffer usage in percents (0 to 100). */
996 static int pcmbuf_usage(void)
998 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1001 static int pcmbuf_mix_free(void)
1003 if (pcmbuf_mix_chunk)
1005 size_t my_mix_end =
1006 (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample];
1007 size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos];
1008 if (my_write_pos < my_mix_end)
1009 my_write_pos += pcmbuf_size;
1010 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1012 return 100;
1015 void *pcmbuf_request_voice_buffer(int *count)
1017 /* A get-it-to-work-for-now hack (audio status could change by
1018 completion) */
1019 if (audio_status() & AUDIO_STATUS_PLAY)
1021 if (pcmbuf_read == NULL)
1023 return NULL;
1025 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
1026 (pcmbuf_mix_chunk || pcmbuf_read->link))
1028 *count = MIN(*count, PCMBUF_MIX_CHUNK/4);
1029 return voicebuf;
1031 else
1033 return NULL;
1036 else
1038 return pcmbuf_request_buffer(count);
1042 void pcmbuf_write_voice_complete(int count)
1044 /* A get-it-to-work-for-now hack (audio status could have changed) */
1045 if (!(audio_status() & AUDIO_STATUS_PLAY))
1047 pcmbuf_write_complete(count);
1048 return;
1051 int16_t *ibuf = (int16_t *)voicebuf;
1052 int16_t *obuf;
1053 size_t chunk_samples;
1055 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
1057 pcmbuf_mix_chunk = pcmbuf_read->link;
1058 /* Start 1/8s into the next chunk */
1059 pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16;
1062 if (!pcmbuf_mix_chunk)
1063 return;
1065 obuf = (int16_t *)pcmbuf_mix_chunk->addr;
1066 chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t);
1068 count <<= 1;
1070 while (count-- > 0)
1072 int32_t sample = *ibuf++;
1074 if (pcmbuf_mix_sample >= chunk_samples)
1076 pcmbuf_mix_chunk = pcmbuf_mix_chunk->link;
1077 if (!pcmbuf_mix_chunk)
1078 return;
1079 pcmbuf_mix_sample = 0;
1080 obuf = pcmbuf_mix_chunk->addr;
1081 chunk_samples = pcmbuf_mix_chunk->size / 2;
1083 sample += obuf[pcmbuf_mix_sample] >> 2;
1084 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1089 /* Debug menu, other metrics */
1091 /* Amount of bytes left in the buffer. */
1092 size_t pcmbuf_free(void)
1094 if (pcmbuf_read != NULL)
1096 void *read = pcmbuf_read->addr;
1097 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
1098 if (read < write)
1099 return (size_t)(read - write) + pcmbuf_size;
1100 else
1101 return (size_t) (read - write);
1103 return pcmbuf_size;
1106 size_t pcmbuf_get_bufsize(void)
1108 return pcmbuf_size;
1111 int pcmbuf_used_descs(void)
1113 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
1114 unsigned int i = 0;
1115 while (pcmbuf_temp) {
1116 pcmbuf_temp = pcmbuf_temp->link;
1117 i++;
1119 return i;
1122 int pcmbuf_descs(void)
1124 return PCMBUF_DESCS(pcmbuf_size);
1127 #ifdef ROCKBOX_HAS_LOGF
1128 unsigned char * pcmbuf_get_meminfo(size_t *length)
1130 *length = pcmbuf_bufend - audiobuffer;
1131 return audiobuffer;
1133 #endif
1136 /* Misc */
1138 bool pcmbuf_is_lowdata(void)
1140 if (!pcm_is_playing() || pcm_is_paused() ||
1141 crossfade_init || crossfade_active)
1142 return false;
1144 #if MEMORYSIZE > 2
1145 /* 1 seconds of buffer is low data */
1146 return LOW_DATA(4);
1147 #else
1148 /* under watermark is low data */
1149 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
1150 #endif
1153 void pcmbuf_set_low_latency(bool state)
1155 low_latency_mode = state;
1158 unsigned long pcmbuf_get_latency(void)
1160 /* Be careful how this calculation is rearranged, it's easy to overflow */
1161 size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting();
1162 return bytes / 4 * 1000 / NATIVE_FREQUENCY;
1165 #ifndef HAVE_HARDWARE_BEEP
1166 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
1167 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
1169 /* Generates a constant square wave sound with a given frequency
1170 in Hertz for a duration in milliseconds. */
1171 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
1173 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
1174 int32_t phase = 0;
1175 int16_t *bufptr, *bufstart, *bufend;
1176 int32_t sample;
1177 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
1178 bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
1179 int i;
1181 bufend = SKIPBYTES((int16_t *)audiobuffer, pcmbuf_size);
1183 /* Find the insertion point and set bufstart to the start of it */
1184 if (mix)
1186 /* Get the currently playing chunk at the current position. */
1187 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
1189 /* If above isn't implemented or pcm is stopped, no beepeth. */
1190 if (!bufstart || !pcm_is_playing())
1191 return;
1193 /* Give 5ms clearance. */
1194 bufstart += NATIVE_FREQUENCY * 4 / 200;
1196 #ifdef HAVE_PCM_DMA_ADDRESS
1197 /* Returned peak addresses are DMA addresses */
1198 bufend = pcm_dma_addr(bufend);
1199 #endif
1201 /* Wrapped above? */
1202 if (bufstart >= bufend)
1203 bufstart -= pcmbuf_size;
1205 /* NOTE: On some targets using hardware DMA, cache range flushing may
1206 * be required or the writes may not be picked up by the controller.
1207 * An incremental flush should be done periodically during the mixdown. */
1209 else if (nsamples <= MINIBUF_SAMPLES)
1211 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
1212 /* Use mini buffer */
1213 bufstart = minibuf;
1214 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1216 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED)
1218 /* Use audiobuffer */
1219 bufstart = (int16_t *)audiobuffer;
1221 else
1223 /* No place */
1224 return;
1227 bufptr = bufstart;
1229 /* Mix square wave into buffer */
1230 for (i = 0; i < nsamples; ++i)
1232 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
1233 sample = mix ? *bufptr : 0;
1234 *bufptr++ = clip_sample_16(sample + amp);
1235 if (bufptr >= bufend)
1236 bufptr = (int16_t *)audiobuffer;
1237 sample = mix ? *bufptr : 0;
1238 *bufptr++ = clip_sample_16(sample + amp);
1239 if (bufptr >= bufend)
1240 bufptr = (int16_t *)audiobuffer;
1242 phase += step;
1245 pcm_play_lock();
1246 #ifdef HAVE_RECORDING
1247 pcm_rec_lock();
1248 #endif
1250 /* Kick off playback if required and it won't interfere */
1251 if (!pcm_is_playing()
1252 #ifdef HAVE_RECORDING
1253 && !pcm_is_recording()
1254 #endif
1257 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
1260 pcm_play_unlock();
1261 #ifdef HAVE_RECORDING
1262 pcm_rec_unlock();
1263 #endif
1265 #endif /* HAVE_HARDWARE_BEEP */