Add another delay in the AMSv2 sd controller driver as a work-around for SD initialis...
[maemo-rb.git] / apps / pcmbuf.c
blobf548b156b3dbe27033d3e16cec3f8cb5df5039a1
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"
29 #include "codec_thread.h"
31 /* Define LOGF_ENABLE to enable logf output in this file */
32 /*#define LOGF_ENABLE*/
33 #include "logf.h"
34 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
35 #include "cpu.h"
36 #endif
37 #include <string.h>
38 #include "settings.h"
39 #include "audio.h"
40 #include "voice_thread.h"
41 #include "dsp.h"
43 #define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
44 on the pcm buffer */
45 #define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
46 chunks on the pcm buffer (or we run out
47 of buffer descriptors, which is
48 non-fatal) */
49 #define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
50 this to the DMA */
51 #define CROSSFADE_BUFSIZE 8192 /* Size of the crossfade buffer */
52 #define AUX_BUFSIZE 512 /* Size of the aux buffer; can be 512 if no
53 resampling or timestretching is allowed in
54 the aux channel, must be 2048 otherwise */
56 /* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */
57 #define BYTERATE (NATIVE_FREQUENCY * 4)
59 #if MEMORYSIZE > 2
60 /* Keep watermark high for iPods at least (2s) */
61 #define PCMBUF_WATERMARK (BYTERATE * 2)
62 #else
63 #define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */
64 #endif
66 /* Structure we can use to queue pcm chunks in memory to be played
67 * by the driver code. */
68 struct chunkdesc
70 unsigned char *addr;
71 size_t size;
72 struct chunkdesc* link;
73 /* true if last chunk in the track */
74 bool end_of_track;
77 #define NUM_CHUNK_DESCS(bufsize) \
78 ((bufsize) / PCMBUF_MINAVG_CHUNK)
80 /* Size of the PCM buffer. */
81 static size_t pcmbuf_size IDATA_ATTR = 0;
82 static char *pcmbuf_bufend IDATA_ATTR;
83 static char *pcmbuffer IDATA_ATTR;
84 /* Current PCM buffer write index. */
85 static size_t pcmbuffer_pos IDATA_ATTR;
86 /* Amount pcmbuffer_pos will be increased.*/
87 static size_t pcmbuffer_fillpos IDATA_ATTR;
89 /* Gapless playback */
90 static bool track_transition IDATA_ATTR;
92 #ifdef HAVE_CROSSFADE
93 /* Crossfade buffer */
94 static char *fadebuf IDATA_ATTR;
96 /* Crossfade related state */
97 static bool crossfade_enabled;
98 static bool crossfade_enable_request;
99 static bool crossfade_mixmode;
100 static bool crossfade_auto_skip;
101 static bool crossfade_active IDATA_ATTR;
102 static bool crossfade_track_change_started IDATA_ATTR;
104 /* Track the current location for processing crossfade */
105 static struct chunkdesc *crossfade_chunk IDATA_ATTR;
106 static size_t crossfade_sample IDATA_ATTR;
108 /* Counters for fading in new data */
109 static size_t crossfade_fade_in_total IDATA_ATTR;
110 static size_t crossfade_fade_in_rem IDATA_ATTR;
111 #endif
113 static struct chunkdesc *read_chunk IDATA_ATTR;
114 static struct chunkdesc *read_end_chunk IDATA_ATTR;
115 static struct chunkdesc *write_chunk IDATA_ATTR;
116 static struct chunkdesc *write_end_chunk IDATA_ATTR;
117 static size_t last_chunksize IDATA_ATTR;
119 static size_t pcmbuf_unplayed_bytes IDATA_ATTR;
120 static size_t pcmbuf_watermark IDATA_ATTR;
122 /* Voice */
123 static char *voicebuf IDATA_ATTR;
124 static struct chunkdesc *mix_chunk IDATA_ATTR;
125 static size_t pcmbuf_mix_sample IDATA_ATTR;
127 static bool low_latency_mode = false;
128 static bool flush_pcmbuf = false;
130 #ifdef HAVE_PRIORITY_SCHEDULING
131 static int codec_thread_priority = PRIORITY_PLAYBACK;
132 #endif
134 /* Helpful macros for use in conditionals this assumes some of the above
135 * static variable names */
136 #define COMMIT_IF_NEEDED if(pcmbuffer_fillpos > PCMBUF_TARGET_CHUNK || \
137 (pcmbuffer_pos + pcmbuffer_fillpos) >= pcmbuf_size) commit_chunk(false)
138 #define LOW_DATA(quarter_secs) \
139 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
141 #ifdef HAVE_CROSSFADE
142 static void crossfade_start(void);
143 static void write_to_crossfade(size_t length);
144 static void pcmbuf_finish_crossfade_enable(void);
145 #endif
148 /**************************************/
150 /* define this to show detailed chunkdesc usage information on the sim console */
151 /*#define DESC_DEBUG*/
153 #ifndef SIMULATOR
154 #undef DESC_DEBUG
155 #endif
156 #ifdef DESC_DEBUG
157 static struct chunkdesc *first_desc;
158 static bool show_desc_in_use = false;
159 #define DISPLAY_DESC(caller) while(!show_desc(caller))
160 #define DESC_IDX(desc) (desc ? desc - first_desc : -1)
161 #define SHOW_DESC(desc) if(DESC_IDX(desc)==-1) DEBUGF("--"); \
162 else DEBUGF("%02d", DESC_IDX(desc))
163 #define SHOW_DESC_LINK(desc) if(desc){SHOW_DESC(desc->link);DEBUGF(" ");} \
164 else DEBUGF("-- ")
165 #define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \
166 SHOW_DESC_LINK(desc)
167 #define SHOW_POINT(tag,desc) DEBUGF("%s",tag);SHOW_DETAIL(desc)
168 #define SHOW_NUM(num,desc) DEBUGF("%02d>",num);SHOW_DESC_LINK(desc)
170 static bool show_desc(char *caller)
172 if (show_desc_in_use) return false;
173 show_desc_in_use = true;
174 DEBUGF("%-14s\t", caller);
175 SHOW_POINT("r", read_chunk);
176 SHOW_POINT("re", read_end_chunk);
177 DEBUGF(" ");
178 SHOW_POINT("w", write_chunk);
179 SHOW_POINT("we", write_end_chunk);
180 DEBUGF("\n");
181 int i;
182 for (i = 0; i < pcmbuf_descs(); i++)
184 SHOW_NUM(i, (first_desc + i));
185 if (i%10 == 9) DEBUGF("\n");
187 DEBUGF("\n\n");
188 show_desc_in_use = false;
189 return true;
191 #else
192 #define DISPLAY_DESC(caller) do{}while(0)
193 #endif
196 /** Accept new PCM data */
198 /* Commit PCM buffer samples as a new chunk for playback */
199 static void commit_chunk(bool flush_next_time)
201 if (!pcmbuffer_fillpos)
202 return;
204 /* Never use the last buffer descriptor */
205 while (write_chunk == write_end_chunk) {
206 /* If this happens, something is being stupid */
207 if (!pcm_is_playing()) {
208 logf("commit_chunk error");
209 pcmbuf_play_start();
211 /* Let approximately one chunk of data playback */
212 sleep(HZ * PCMBUF_TARGET_CHUNK / BYTERATE);
215 /* commit the chunk */
217 register size_t size = pcmbuffer_fillpos;
218 /* Grab the next description to write, and change the write pointer */
219 register struct chunkdesc *pcmbuf_current = write_chunk;
220 write_chunk = pcmbuf_current->link;
221 /* Fill in the values in the new buffer chunk */
222 pcmbuf_current->addr = &pcmbuffer[pcmbuffer_pos];
223 pcmbuf_current->size = size;
224 pcmbuf_current->end_of_track = false;
225 pcmbuf_current->link = NULL;
227 if (read_chunk != NULL)
229 if (flush_pcmbuf)
231 /* Flush! Discard all data after the currently playing chunk,
232 and make the current chunk play next */
233 logf("commit_chunk: flush");
234 write_end_chunk->link = read_chunk->link;
235 read_chunk->link = pcmbuf_current;
236 while (write_end_chunk->link)
238 write_end_chunk = write_end_chunk->link;
239 pcmbuf_unplayed_bytes -= write_end_chunk->size;
242 /* If there is already a read buffer setup, add to it */
243 else
244 read_end_chunk->link = pcmbuf_current;
246 else
248 /* Otherwise create the buffer */
249 read_chunk = pcmbuf_current;
252 /* If flush_next_time is true, then the current chunk will be thrown out
253 * and the next chunk to be committed will be the next to be played.
254 * This is used to empty the PCM buffer for a track change. */
255 flush_pcmbuf = flush_next_time;
257 /* This is now the last buffer to read */
258 read_end_chunk = pcmbuf_current;
260 /* Update bytes counters */
261 pcmbuf_unplayed_bytes += size;
263 pcmbuffer_pos += size;
264 if (pcmbuffer_pos >= pcmbuf_size)
265 pcmbuffer_pos -= pcmbuf_size;
267 pcmbuffer_fillpos = 0;
268 DISPLAY_DESC("commit_chunk");
271 /* Set priority of the codec thread */
272 #ifdef HAVE_PRIORITY_SCHEDULING
274 * expects pcm_fill_state in tenth-% units (e.g. full pcm buffer is 10) */
275 static void boost_codec_thread(int pcm_fill_state)
277 static const int prios[11] = {
278 PRIORITY_PLAYBACK_MAX, /* 0 - 10% */
279 PRIORITY_PLAYBACK_MAX+1, /* 10 - 20% */
280 PRIORITY_PLAYBACK_MAX+3, /* 20 - 30% */
281 PRIORITY_PLAYBACK_MAX+5, /* 30 - 40% */
282 PRIORITY_PLAYBACK_MAX+7, /* 40 - 50% */
283 PRIORITY_PLAYBACK_MAX+8, /* 50 - 60% */
284 PRIORITY_PLAYBACK_MAX+9, /* 60 - 70% */
285 /* raiseing priority above 70% shouldn't be needed */
286 PRIORITY_PLAYBACK, /* 70 - 80% */
287 PRIORITY_PLAYBACK, /* 80 - 90% */
288 PRIORITY_PLAYBACK, /* 90 -100% */
289 PRIORITY_PLAYBACK, /* 100% */
291 int new_prio = prios[pcm_fill_state];
293 /* Keep voice and codec threads at the same priority or else voice
294 * will starve if the codec thread's priority is boosted. */
295 if (new_prio != codec_thread_priority)
297 codec_thread_set_priority(new_prio);
298 voice_thread_set_priority(new_prio);
299 codec_thread_priority = new_prio;
302 #else
303 #define boost_codec_thread(pcm_fill_state) do{}while(0)
304 #endif /* HAVE_PRIORITY_SCHEDULING */
306 /* Return true if the PCM buffer is able to receive new data.
307 * Also maintain buffer level above the watermark. */
308 static bool prepare_insert(size_t length)
310 if (low_latency_mode)
312 /* 1/4s latency. */
313 if (!LOW_DATA(1) && pcm_is_playing())
314 return false;
317 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
318 if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK)
319 return false;
321 /* Maintain the buffer level above the watermark */
322 if (pcm_is_playing())
324 /* Only codec thread initiates boost - voice boosts the cpu when playing
325 a clip */
326 #ifndef SIMULATOR
327 if (is_codec_thread())
328 #endif /* SIMULATOR */
330 /* boost cpu if necessary */
331 if (pcmbuf_unplayed_bytes < pcmbuf_watermark)
332 trigger_cpu_boost();
333 boost_codec_thread(pcmbuf_unplayed_bytes*10/pcmbuf_size);
336 #ifdef HAVE_CROSSFADE
337 /* Disable crossfade if < .5s of audio */
338 if (LOW_DATA(2))
340 crossfade_active = false;
342 #endif
344 else /* pcm_is_playing */
346 /* Boost CPU for pre-buffer */
347 trigger_cpu_boost();
349 /* If pre-buffered to the watermark, start playback */
350 #if MEMORYSIZE > 2
351 if (!LOW_DATA(4))
352 #else
353 if (pcmbuf_unplayed_bytes > pcmbuf_watermark)
354 #endif
356 logf("pcm starting");
357 if (!(audio_status() & AUDIO_STATUS_PAUSE))
358 pcmbuf_play_start();
362 return true;
365 /* Request space in the buffer for writing output samples */
366 void *pcmbuf_request_buffer(int *count)
368 #ifdef HAVE_CROSSFADE
369 /* we're going to crossfade to a new track, which is now on its way */
370 if (crossfade_track_change_started)
371 crossfade_start();
373 /* crossfade has begun, put the new track samples in fadebuf */
374 if (crossfade_active)
376 *count = MIN(*count, CROSSFADE_BUFSIZE/4);
377 return fadebuf;
379 else
380 #endif
381 /* if possible, reserve room in the PCM buffer for new samples */
383 if(prepare_insert(*count << 2))
385 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
386 if (pcmbuf_size - pcmbuffer_index >= PCMBUF_MIN_CHUNK)
388 /* Usual case, there's space here */
389 return &pcmbuffer[pcmbuffer_index];
391 else
393 /* Wrap the buffer, the new samples go at the beginning */
394 commit_chunk(false);
395 pcmbuffer_pos = 0;
396 return &pcmbuffer[0];
400 /* PCM buffer not ready to receive new data yet */
401 return NULL;
404 /* Handle new samples to the buffer */
405 void pcmbuf_write_complete(int count)
407 size_t length = (size_t)(unsigned int)count << 2;
408 #ifdef HAVE_CROSSFADE
409 if (crossfade_active)
410 write_to_crossfade(length);
411 else
412 #endif
414 pcmbuffer_fillpos += length;
415 COMMIT_IF_NEEDED;
420 /** Init */
422 static inline void init_pcmbuffers(void)
424 #ifdef DESC_DEBUG
425 first_desc = write_chunk;
426 #endif
427 struct chunkdesc *next = write_chunk;
428 next++;
429 write_end_chunk = write_chunk;
430 while ((void *)next < (void *)pcmbuf_bufend) {
431 write_end_chunk->link=next;
432 write_end_chunk=next;
433 next++;
435 DISPLAY_DESC("init");
438 static size_t get_next_required_pcmbuf_size(void)
440 size_t seconds = 1;
442 #ifdef HAVE_CROSSFADE
443 if (crossfade_enable_request)
444 seconds += global_settings.crossfade_fade_out_delay +
445 global_settings.crossfade_fade_out_duration;
446 #endif
448 #if MEMORYSIZE > 2
449 /* Buffer has to be at least 2s long. */
450 seconds += 2;
451 #endif
452 logf("pcmbuf len: %ld", (long)seconds);
453 return seconds * BYTERATE;
456 /* Initialize the pcmbuffer the structure looks like this:
457 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
458 size_t pcmbuf_init(unsigned char *bufend)
460 pcmbuf_bufend = bufend;
461 pcmbuf_size = get_next_required_pcmbuf_size();
462 write_chunk = (struct chunkdesc *)pcmbuf_bufend -
463 NUM_CHUNK_DESCS(pcmbuf_size);
464 voicebuf = (char *)write_chunk - AUX_BUFSIZE;
465 #ifdef HAVE_CROSSFADE
466 fadebuf = voicebuf - CROSSFADE_BUFSIZE;
467 pcmbuffer = fadebuf - pcmbuf_size;
468 #else
469 pcmbuffer = voicebuf - pcmbuf_size;
470 #endif
472 init_pcmbuffers();
474 #ifdef HAVE_CROSSFADE
475 pcmbuf_finish_crossfade_enable();
476 #else
477 pcmbuf_watermark = PCMBUF_WATERMARK;
478 #endif
480 pcmbuf_play_stop();
482 return pcmbuf_bufend - pcmbuffer;
486 /** Track change */
487 void pcmbuf_monitor_track_change(bool monitor)
489 pcm_play_lock();
491 if (last_chunksize != 0)
493 /* If monitoring, wait until this track runs out. Place in
494 currently playing chunk. If not, cancel notification. */
495 track_transition = monitor;
496 read_end_chunk->end_of_track = monitor;
498 else
500 /* Post now if PCM stopped and last buffer was sent. */
501 track_transition = false;
502 if (monitor)
503 audio_post_track_change(false);
506 pcm_play_unlock();
509 void pcmbuf_start_track_change(bool auto_skip)
511 bool crossfade = false;
512 #ifdef HAVE_CROSSFADE
513 /* Determine whether this track change needs to crossfade */
514 if(crossfade_enabled && !pcmbuf_is_crossfade_active())
516 switch(global_settings.crossfade)
518 case CROSSFADE_ENABLE_AUTOSKIP:
519 crossfade = auto_skip;
520 break;
521 case CROSSFADE_ENABLE_MANSKIP:
522 crossfade = !auto_skip;
523 break;
524 case CROSSFADE_ENABLE_SHUFFLE:
525 crossfade = global_settings.playlist_shuffle;
526 break;
527 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP:
528 crossfade = global_settings.playlist_shuffle || !auto_skip;
529 break;
530 case CROSSFADE_ENABLE_ALWAYS:
531 crossfade = true;
532 break;
535 #endif
537 if (!auto_skip || crossfade)
538 /* manual skip or crossfade */
540 if (crossfade)
541 { logf(" crossfade track change"); }
542 else
543 { logf(" manual track change"); }
545 pcm_play_lock();
547 /* Cancel any pending automatic gapless transition */
548 pcmbuf_monitor_track_change(false);
550 /* Notify the wps that the track change starts now */
551 audio_post_track_change(false);
553 /* Can't do two crossfades at once and, no fade if pcm is off now */
554 if (
555 #ifdef HAVE_CROSSFADE
556 pcmbuf_is_crossfade_active() ||
557 #endif
558 !pcm_is_playing())
560 pcmbuf_play_stop();
561 pcm_play_unlock();
562 return;
565 /* Not enough data, or not crossfading, flush the old data instead */
566 if (LOW_DATA(2) || !crossfade || low_latency_mode)
568 commit_chunk(true);
570 #ifdef HAVE_CROSSFADE
571 else
573 /* Don't enable mix mode when skipping tracks manually. */
574 crossfade_mixmode = auto_skip &&
575 global_settings.crossfade_fade_out_mixmode;
577 crossfade_auto_skip = auto_skip;
579 crossfade_track_change_started = crossfade;
581 #endif
582 pcm_play_unlock();
584 /* Keep trigger outside the play lock or HW FIFO underruns can happen
585 since frequency scaling is *not* always fast */
586 trigger_cpu_boost();
588 else /* automatic and not crossfading, so do gapless track change */
590 /* The codec is moving on to the next track, but the current track will
591 * continue to play. Set a flag to make sure the elapsed time of the
592 * current track will be updated properly, and mark the current chunk
593 * as the last one in the track. */
594 logf(" gapless track change");
595 pcmbuf_monitor_track_change(true);
600 /** Playback */
602 /* PCM driver callback
603 * This function has 3 major logical parts (separated by brackets both for
604 * readability and variable scoping). The first part performs the
605 * operations related to finishing off the last chunk we fed to the DMA.
606 * The second part detects the end of playlist condition when the PCM
607 * buffer is empty except for uncommitted samples. Then they are committed
608 * and sent to the PCM driver for playback. The third part performs the
609 * operations involved in sending a new chunk to the DMA. */
610 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR;
611 static void pcmbuf_pcm_callback(unsigned char** start, size_t* size)
614 struct chunkdesc *pcmbuf_current = read_chunk;
615 /* Take the finished chunk out of circulation */
616 read_chunk = pcmbuf_current->link;
618 /* if during a track transition, update the elapsed time in ms */
619 if (track_transition)
620 audio_pcmbuf_position_callback(last_chunksize * 1000 / BYTERATE);
622 /* if last chunk in the track, stop updates and notify audio thread */
623 if (pcmbuf_current->end_of_track)
625 track_transition = false;
626 audio_post_track_change(true);
629 /* Put the finished chunk back into circulation */
630 write_end_chunk->link = pcmbuf_current;
631 write_end_chunk = pcmbuf_current;
633 /* If we've read over the mix chunk while it's still mixing there */
634 if (pcmbuf_current == mix_chunk)
635 mix_chunk = NULL;
637 #ifdef HAVE_CROSSFADE
638 /* If we've read over the crossfade chunk while it's still fading */
639 if (pcmbuf_current == crossfade_chunk)
640 crossfade_chunk = read_chunk;
641 #endif
645 /* Commit last samples at end of playlist */
646 if (pcmbuffer_fillpos && !read_chunk)
648 logf("pcmbuf_pcm_callback: commit last samples");
649 commit_chunk(false);
654 /* Send the new chunk to the DMA */
655 if(read_chunk)
657 last_chunksize = read_chunk->size;
658 pcmbuf_unplayed_bytes -= last_chunksize;
659 *size = last_chunksize;
660 *start = read_chunk->addr;
662 else
664 /* No more chunks */
665 logf("pcmbuf_pcm_callback: no more chunks");
666 last_chunksize = 0;
667 *size = 0;
668 *start = NULL;
671 DISPLAY_DESC("callback");
674 /* Force playback */
675 void pcmbuf_play_start(void)
677 if (!pcm_is_playing() && pcmbuf_unplayed_bytes && read_chunk != NULL)
679 logf("pcmbuf_play_start");
680 last_chunksize = read_chunk->size;
681 pcmbuf_unplayed_bytes -= last_chunksize;
682 pcm_play_data(pcmbuf_pcm_callback,
683 read_chunk->addr, last_chunksize);
687 void pcmbuf_play_stop(void)
689 logf("pcmbuf_play_stop");
690 pcm_play_stop();
692 pcmbuf_unplayed_bytes = 0;
693 mix_chunk = NULL;
694 if (read_chunk) {
695 write_end_chunk->link = read_chunk;
696 write_end_chunk = read_end_chunk;
697 read_chunk = read_end_chunk = NULL;
699 last_chunksize = 0;
700 pcmbuffer_pos = 0;
701 pcmbuffer_fillpos = 0;
702 #ifdef HAVE_CROSSFADE
703 crossfade_track_change_started = false;
704 crossfade_active = false;
705 #endif
706 track_transition = false;
707 flush_pcmbuf = false;
708 DISPLAY_DESC("play_stop");
710 /* Can unboost the codec thread here no matter who's calling,
711 * pretend full pcm buffer to unboost */
712 boost_codec_thread(10);
715 void pcmbuf_pause(bool pause)
717 logf("pcmbuf_pause: %s", pause?"pause":"play");
718 if (pcm_is_playing())
719 pcm_play_pause(!pause);
720 else if (!pause)
721 pcmbuf_play_start();
725 /** Crossfade */
727 /* Clip sample to signed 16 bit range */
728 static inline int32_t clip_sample_16(int32_t sample)
730 if ((int16_t)sample != sample)
731 sample = 0x7fff ^ (sample >> 31);
732 return sample;
735 #ifdef HAVE_CROSSFADE
736 /* Find the chunk that's (length) deep in the list. Return the position within
737 * the chunk, and leave the chunkdesc pointer pointing to the chunk. */
738 static size_t find_chunk(size_t length, struct chunkdesc **chunk)
740 while (*chunk && length >= (*chunk)->size)
742 length -= (*chunk)->size;
743 *chunk = (*chunk)->link;
745 return length;
748 /* Returns the number of bytes _NOT_ mixed/faded */
749 static size_t crossfade_mix_fade(int factor, size_t length, const char *buf,
750 size_t *out_sample, struct chunkdesc **out_chunk)
752 if (length == 0)
753 return 0;
755 const int16_t *input_buf = (const int16_t *)buf;
756 int16_t *output_buf = (int16_t *)((*out_chunk)->addr);
757 int16_t *chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
758 output_buf = &output_buf[*out_sample];
759 int32_t sample;
761 while (length)
763 /* fade left and right channel at once to keep buffer alignment */
764 int i;
765 for (i = 0; i < 2; i++)
767 if (input_buf)
768 /* fade the input buffer and mix into the chunk */
770 sample = *input_buf++;
771 sample = ((sample * factor) >> 8) + *output_buf;
772 *output_buf++ = clip_sample_16(sample);
774 else
775 /* fade the chunk only */
777 sample = *output_buf;
778 *output_buf++ = (sample * factor) >> 8;
782 length -= 4; /* 2 samples, each 16 bit -> 4 bytes */
784 /* move to next chunk as needed */
785 if (output_buf >= chunk_end)
787 *out_chunk = (*out_chunk)->link;
788 if (!(*out_chunk))
789 return length;
790 output_buf = (int16_t *)((*out_chunk)->addr);
791 chunk_end = SKIPBYTES(output_buf, (*out_chunk)->size);
794 *out_sample = output_buf - (int16_t *)((*out_chunk)->addr);
795 return 0;
798 /* Initializes crossfader, calculates all necessary parameters and performs
799 * fade-out with the PCM buffer. */
800 static void crossfade_start(void)
802 size_t crossfade_rem;
803 size_t crossfade_need;
804 size_t fade_out_rem;
805 size_t fade_out_delay;
806 size_t fade_in_delay;
808 crossfade_track_change_started = false;
809 /* Reject crossfade if less than .5s of data */
810 if (LOW_DATA(2)) {
811 logf("crossfade rejected");
812 pcmbuf_play_stop();
813 return ;
816 logf("crossfade_start");
817 commit_chunk(false);
818 crossfade_active = true;
820 /* Initialize the crossfade buffer size to all of the buffered data that
821 * has not yet been sent to the DMA */
822 crossfade_rem = pcmbuf_unplayed_bytes;
823 crossfade_chunk = read_chunk->link;
824 crossfade_sample = 0;
826 /* Get fade out info from settings. */
827 fade_out_delay = global_settings.crossfade_fade_out_delay * BYTERATE;
828 fade_out_rem = global_settings.crossfade_fade_out_duration * BYTERATE;
830 crossfade_need = fade_out_delay + fade_out_rem;
831 if (crossfade_rem > crossfade_need)
833 if (crossfade_auto_skip)
834 /* Automatic track changes only modify the last part of the buffer,
835 * so find the right chunk and sample to start the crossfade */
837 crossfade_sample = find_chunk(crossfade_rem - crossfade_need,
838 &crossfade_chunk) / 2;
839 crossfade_rem = crossfade_need;
841 else
842 /* Manual skips occur immediately, but give time to process */
844 crossfade_rem -= crossfade_chunk->size;
845 crossfade_chunk = crossfade_chunk->link;
848 /* Truncate fade out duration if necessary. */
849 if (crossfade_rem < crossfade_need)
851 size_t crossfade_short = crossfade_need - crossfade_rem;
852 if (fade_out_rem >= crossfade_short)
853 fade_out_rem -= crossfade_short;
854 else
856 fade_out_delay -= crossfade_short - fade_out_rem;
857 fade_out_rem = 0;
860 crossfade_rem -= fade_out_delay + fade_out_rem;
862 /* Completely process the crossfade fade-out effect with current PCM buffer */
863 if (!crossfade_mixmode)
865 /* Fade out the specified amount of the already processed audio */
866 size_t total_fade_out = fade_out_rem;
867 size_t fade_out_sample;
868 struct chunkdesc *fade_out_chunk = crossfade_chunk;
870 /* Find the right chunk and sample to start fading out */
871 fade_out_delay += crossfade_sample * 2;
872 fade_out_sample = find_chunk(fade_out_delay, &fade_out_chunk) / 2;
874 while (fade_out_rem > 0)
876 /* Each 1/10 second of audio will have the same fade applied */
877 size_t block_rem = MIN(BYTERATE / 10, fade_out_rem);
878 int factor = (fade_out_rem << 8) / total_fade_out;
880 fade_out_rem -= block_rem;
882 crossfade_mix_fade(factor, block_rem, NULL,
883 &fade_out_sample, &fade_out_chunk);
886 /* zero out the rest of the buffer */
887 crossfade_mix_fade(0, crossfade_rem, NULL,
888 &fade_out_sample, &fade_out_chunk);
891 /* Initialize fade-in counters */
892 crossfade_fade_in_total = global_settings.crossfade_fade_in_duration * BYTERATE;
893 crossfade_fade_in_rem = crossfade_fade_in_total;
895 fade_in_delay = global_settings.crossfade_fade_in_delay * BYTERATE;
897 /* Find the right chunk and sample to start fading in */
898 fade_in_delay += crossfade_sample * 2;
899 crossfade_sample = find_chunk(fade_in_delay, &crossfade_chunk) / 2;
900 logf("crossfade_start done!");
903 /* Perform fade-in of new track */
904 static void write_to_crossfade(size_t length)
906 if (length)
908 char *buf = fadebuf;
909 if (crossfade_fade_in_rem)
911 size_t samples;
912 int16_t *input_buf;
914 /* Fade factor for this packet */
915 int factor =
916 ((crossfade_fade_in_total - crossfade_fade_in_rem) << 8) /
917 crossfade_fade_in_total;
918 /* Bytes to fade */
919 size_t fade_rem = MIN(length, crossfade_fade_in_rem);
921 /* We _will_ fade this many bytes */
922 crossfade_fade_in_rem -= fade_rem;
924 if (crossfade_chunk)
926 /* Mix the data */
927 size_t fade_total = fade_rem;
928 fade_rem = crossfade_mix_fade(factor, fade_rem, buf,
929 &crossfade_sample, &crossfade_chunk);
930 length -= fade_total - fade_rem;
931 buf += fade_total - fade_rem;
932 if (!length)
933 return;
936 samples = fade_rem / 2;
937 input_buf = (int16_t *)buf;
938 /* Fade remaining samples in place */
939 while (samples--)
941 int32_t sample = *input_buf;
942 *input_buf++ = (sample * factor) >> 8;
946 if (crossfade_chunk)
948 /* Mix the data */
949 size_t mix_total = length;
950 /* A factor of 256 means mix only, no fading */
951 length = crossfade_mix_fade(256, length, buf,
952 &crossfade_sample, &crossfade_chunk);
953 buf += mix_total - length;
954 if (!length)
955 return;
958 /* Commit samples to the buffer */
959 while (!prepare_insert(length))
960 sleep(1);
961 while (length > 0)
963 COMMIT_IF_NEEDED;
964 size_t pcmbuffer_index = pcmbuffer_pos + pcmbuffer_fillpos;
965 size_t copy_n = MIN(length, pcmbuf_size - pcmbuffer_index);
966 memcpy(&pcmbuffer[pcmbuffer_index], buf, copy_n);
967 buf += copy_n;
968 pcmbuffer_fillpos += copy_n;
969 length -= copy_n;
972 /* if no more fading-in to do, stop the crossfade */
973 if (!(crossfade_fade_in_rem || crossfade_chunk))
974 crossfade_active = false;
977 static void pcmbuf_finish_crossfade_enable(void)
979 /* Copy the pending setting over now */
980 crossfade_enabled = crossfade_enable_request;
982 pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ?
983 /* If crossfading, try to keep the buffer full other than 1 second */
984 (pcmbuf_size - BYTERATE) :
985 /* Otherwise, just use the default */
986 PCMBUF_WATERMARK;
989 bool pcmbuf_is_crossfade_active(void)
991 return crossfade_active || crossfade_track_change_started;
994 void pcmbuf_request_crossfade_enable(bool on_off)
996 /* Next setting to be used, not applied now */
997 crossfade_enable_request = on_off;
1000 bool pcmbuf_is_same_size(void)
1002 /* if pcmbuffer is NULL, then not set up yet even once so always */
1003 bool same_size = pcmbuffer ?
1004 (get_next_required_pcmbuf_size() == pcmbuf_size) : true;
1006 /* no buffer change needed, so finish crossfade setup now */
1007 if (same_size)
1008 pcmbuf_finish_crossfade_enable();
1010 return same_size;
1012 #endif /* HAVE_CROSSFADE */
1015 /** Voice */
1017 /* Returns pcm buffer usage in percents (0 to 100). */
1018 static int pcmbuf_usage(void)
1020 return pcmbuf_unplayed_bytes * 100 / pcmbuf_size;
1023 static int pcmbuf_mix_free(void)
1025 if (mix_chunk)
1027 size_t my_mix_end =
1028 (size_t)&((int16_t *)mix_chunk->addr)[pcmbuf_mix_sample];
1029 size_t my_write_pos = (size_t)&pcmbuffer[pcmbuffer_pos];
1030 if (my_write_pos < my_mix_end)
1031 my_write_pos += pcmbuf_size;
1032 return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes;
1034 return 100;
1037 void *pcmbuf_request_voice_buffer(int *count)
1039 /* A get-it-to-work-for-now hack (audio status could change by
1040 completion) */
1041 if (audio_status() & AUDIO_STATUS_PLAY)
1043 if (read_chunk == NULL)
1045 return NULL;
1047 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
1048 (mix_chunk || read_chunk->link))
1050 *count = MIN(*count, AUX_BUFSIZE/4);
1051 return voicebuf;
1053 else
1055 return NULL;
1058 else
1060 return pcmbuf_request_buffer(count);
1064 void pcmbuf_write_voice_complete(int count)
1066 /* A get-it-to-work-for-now hack (audio status could have changed) */
1067 if (!(audio_status() & AUDIO_STATUS_PLAY))
1069 pcmbuf_write_complete(count);
1070 return;
1073 int16_t *ibuf = (int16_t *)voicebuf;
1074 int16_t *obuf;
1075 size_t chunk_samples;
1077 if (mix_chunk == NULL && read_chunk != NULL)
1079 mix_chunk = read_chunk->link;
1080 /* Start 1/8s into the next chunk */
1081 pcmbuf_mix_sample = BYTERATE / 16;
1084 if (!mix_chunk)
1085 return;
1087 obuf = (int16_t *)mix_chunk->addr;
1088 chunk_samples = mix_chunk->size / sizeof (int16_t);
1090 count <<= 1;
1092 while (count-- > 0)
1094 int32_t sample = *ibuf++;
1096 if (pcmbuf_mix_sample >= chunk_samples)
1098 mix_chunk = mix_chunk->link;
1099 if (!mix_chunk)
1100 return;
1101 pcmbuf_mix_sample = 0;
1102 obuf = (int16_t *)mix_chunk->addr;
1103 chunk_samples = mix_chunk->size / 2;
1105 sample += obuf[pcmbuf_mix_sample] >> 2;
1106 obuf[pcmbuf_mix_sample++] = clip_sample_16(sample);
1111 /** Debug menu, other metrics */
1113 /* Amount of bytes left in the buffer. */
1114 size_t pcmbuf_free(void)
1116 if (read_chunk != NULL)
1118 void *read = (void *)read_chunk->addr;
1119 void *write = &pcmbuffer[pcmbuffer_pos + pcmbuffer_fillpos];
1120 if (read < write)
1121 return (size_t)(read - write) + pcmbuf_size;
1122 else
1123 return (size_t) (read - write);
1125 return pcmbuf_size - pcmbuffer_fillpos;
1128 size_t pcmbuf_get_bufsize(void)
1130 return pcmbuf_size;
1133 int pcmbuf_used_descs(void)
1135 struct chunkdesc *temp = read_chunk;
1136 unsigned int i = 0;
1137 while (temp) {
1138 temp = temp->link;
1139 i++;
1141 return i;
1144 int pcmbuf_descs(void)
1146 return NUM_CHUNK_DESCS(pcmbuf_size);
1149 #ifdef ROCKBOX_HAS_LOGF
1150 unsigned char *pcmbuf_get_meminfo(size_t *length)
1152 *length = pcmbuf_bufend - pcmbuffer;
1153 return pcmbuffer;
1155 #endif
1158 /** Misc */
1160 bool pcmbuf_is_lowdata(void)
1162 if (!pcm_is_playing() || pcm_is_paused()
1163 #ifdef HAVE_CROSSFADE
1164 || pcmbuf_is_crossfade_active()
1165 #endif
1167 return false;
1169 #if MEMORYSIZE > 2
1170 /* 1 seconds of buffer is low data */
1171 return LOW_DATA(4);
1172 #else
1173 /* under watermark is low data */
1174 return (pcmbuf_unplayed_bytes < pcmbuf_watermark);
1175 #endif
1178 void pcmbuf_set_low_latency(bool state)
1180 low_latency_mode = state;
1183 unsigned long pcmbuf_get_latency(void)
1185 return (pcmbuf_unplayed_bytes + pcm_get_bytes_waiting()) * 1000 / BYTERATE;
1188 #ifndef HAVE_HARDWARE_BEEP
1189 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
1190 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
1192 /* Generates a constant square wave sound with a given frequency
1193 in Hertz for a duration in milliseconds. */
1194 void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
1196 unsigned int step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
1197 int32_t phase = 0;
1198 int16_t *bufptr, *bufstart, *bufend;
1199 int32_t sample;
1200 int nsamples = NATIVE_FREQUENCY / 1000 * duration;
1201 bool mix = read_chunk != NULL && read_chunk->link != NULL;
1202 int i;
1204 bufend = SKIPBYTES((int16_t *)pcmbuffer, pcmbuf_size);
1206 /* Find the insertion point and set bufstart to the start of it */
1207 if (mix)
1209 /* Get the currently playing chunk at the current position. */
1210 bufstart = (int16_t *)pcm_play_dma_get_peak_buffer(&i);
1212 /* If above isn't implemented or pcm is stopped, no beepeth. */
1213 if (!bufstart || !pcm_is_playing())
1214 return;
1216 /* Give 5ms clearance. */
1217 bufstart += BYTERATE / 200;
1219 #ifdef HAVE_PCM_DMA_ADDRESS
1220 /* Returned peak addresses are DMA addresses */
1221 bufend = pcm_dma_addr(bufend);
1222 #endif
1224 /* Wrapped above? */
1225 if (bufstart >= bufend)
1226 bufstart -= pcmbuf_size;
1228 /* NOTE: On some targets using hardware DMA, cache range flushing may
1229 * be required or the writes may not be picked up by the controller.
1230 * An incremental flush should be done periodically during the mixdown. */
1232 else if (nsamples <= MINIBUF_SAMPLES)
1234 static int16_t minibuf[MINIBUF_SAMPLES*2] __attribute__((aligned(4)));
1235 /* Use mini buffer */
1236 bufstart = minibuf;
1237 bufend = SKIPBYTES(bufstart, MINIBUF_SIZE);
1239 else if (!audio_buffer_state_trashed())
1241 /* Use pcmbuffer */
1242 bufstart = (int16_t *)pcmbuffer;
1244 else
1246 /* No place */
1247 return;
1250 bufptr = bufstart;
1252 /* Mix square wave into buffer */
1253 for (i = 0; i < nsamples; ++i)
1255 int32_t amp = (phase >> 31) ^ (int32_t)amplitude;
1256 sample = mix ? *bufptr : 0;
1257 *bufptr++ = clip_sample_16(sample + amp);
1258 if (bufptr >= bufend)
1259 bufptr = (int16_t *)pcmbuffer;
1260 sample = mix ? *bufptr : 0;
1261 *bufptr++ = clip_sample_16(sample + amp);
1262 if (bufptr >= bufend)
1263 bufptr = (int16_t *)pcmbuffer;
1265 phase += step;
1268 pcm_play_lock();
1269 #ifdef HAVE_RECORDING
1270 pcm_rec_lock();
1271 #endif
1273 /* Kick off playback if required and it won't interfere */
1274 if (!pcm_is_playing()
1275 #ifdef HAVE_RECORDING
1276 && !pcm_is_recording()
1277 #endif
1280 pcm_play_data(NULL, (unsigned char *)bufstart, nsamples * 4);
1283 pcm_play_unlock();
1284 #ifdef HAVE_RECORDING
1285 pcm_rec_unlock();
1286 #endif
1288 #endif /* HAVE_HARDWARE_BEEP */