1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
30 /* Define LOGF_ENABLE to enable logf output in this file */
31 /*#define LOGF_ENABLE*/
39 #include "voice_thread.h"
42 #define PCMBUF_TARGET_CHUNK 32768 /* This is the target fill size of chunks
44 #define PCMBUF_MINAVG_CHUNK 24576 /* This is the minimum average size of
45 chunks on the pcm buffer (or we run out
46 of buffer descriptors, which is
48 #define PCMBUF_MIN_CHUNK 4096 /* We try to never feed a chunk smaller than
50 #define PCMBUF_MIX_CHUNK 8192 /* This is the maximum size of one packet
51 for mixing (crossfade or voice) */
53 /* number of bytes played per second (sample rate * 2 channels * 2 bytes/sample) */
54 #define BYTERATE (NATIVE_FREQUENCY * 4)
57 /* Keep watermark high for iPods at least (2s) */
58 #define PCMBUF_WATERMARK (BYTERATE * 2)
60 #define PCMBUF_WATERMARK (BYTERATE / 4) /* 0.25 seconds */
63 /* Structure we can use to queue pcm chunks in memory to be played
64 * by the driver code. */
69 struct chunkdesc
* link
;
70 /* true if last chunk in the track */
74 #define NUM_CHUNK_DESCS(bufsize) \
75 ((bufsize) / PCMBUF_MINAVG_CHUNK)
77 /* Size of the PCM buffer. */
78 static size_t pcmbuf_size IDATA_ATTR
= 0;
79 static char *pcmbuf_bufend IDATA_ATTR
;
80 static char *pcmbuffer IDATA_ATTR
;
81 /* Current PCM buffer write index. */
82 static size_t pcmbuffer_pos IDATA_ATTR
;
83 /* Amount pcmbuffer_pos will be increased.*/
84 static size_t pcmbuffer_fillpos IDATA_ATTR
;
86 /* Gapless playback */
87 static bool end_of_track IDATA_ATTR
;
88 bool track_transition IDATA_ATTR
;
91 /* Crossfade buffer */
92 static char *fadebuf IDATA_ATTR
;
94 /* Crossfade related state */
95 static bool crossfade_enabled
;
96 static bool crossfade_enable_request
;
97 static bool crossfade_mixmode
;
98 static bool crossfade_auto_skip
;
99 static bool crossfade_active IDATA_ATTR
;
100 static bool crossfade_track_change_started IDATA_ATTR
;
102 /* Track the current location for processing crossfade */
103 static struct chunkdesc
*crossfade_chunk IDATA_ATTR
;
104 static size_t crossfade_sample IDATA_ATTR
;
106 /* Counters for fading in new data */
107 static size_t crossfade_fade_in_total IDATA_ATTR
;
108 static size_t crossfade_fade_in_rem IDATA_ATTR
;
111 static struct chunkdesc
*read_chunk IDATA_ATTR
;
112 static struct chunkdesc
*read_end_chunk IDATA_ATTR
;
113 static struct chunkdesc
*write_chunk IDATA_ATTR
;
114 static struct chunkdesc
*write_end_chunk IDATA_ATTR
;
115 static size_t last_chunksize IDATA_ATTR
;
117 static size_t pcmbuf_unplayed_bytes IDATA_ATTR
;
118 static size_t pcmbuf_watermark IDATA_ATTR
;
121 static char *voicebuf IDATA_ATTR
;
122 static struct chunkdesc
*mix_chunk IDATA_ATTR
;
123 static size_t pcmbuf_mix_sample IDATA_ATTR
;
125 static bool low_latency_mode
= false;
126 static bool flush_pcmbuf
= false;
128 #ifdef HAVE_PRIORITY_SCHEDULING
129 static int codec_thread_priority
= PRIORITY_PLAYBACK
;
132 extern unsigned int codec_thread_id
;
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);
148 /**************************************/
150 /* define this to show detailed chunkdesc usage information on the sim console */
151 /*#define 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 DESCL_IDX(desc) (desc && desc->link ? desc->link - first_desc : -1)
162 #define SHOW_1ST(desc) if(DESC_IDX (desc)==-1) DEBUGF(" -- "); \
163 else DEBUGF(" %02d ", DESC_IDX(desc))
164 #define SHOW_2ND(desc) if(DESCL_IDX(desc)==-1) DEBUGF("l -- "); \
165 else DEBUGF("l %02d ", DESCL_IDX(desc))
166 #define DESC_SHOW(tag, desc) DEBUGF(tag);SHOW_1ST(desc); \
167 DEBUGF(tag);SHOW_2ND(desc)
169 static bool show_desc(char *caller
)
171 if (show_desc_in_use
) return false;
172 show_desc_in_use
= true;
173 DEBUGF("%-14s\t", caller
);
174 DESC_SHOW("r", read_chunk
);
175 DESC_SHOW("re", read_end_chunk
);
177 DESC_SHOW("w", write_chunk
);
178 DESC_SHOW("we", write_end_chunk
);
180 show_desc_in_use
= false;
184 #define DISPLAY_DESC(caller) do{}while(0)
188 /** Accept new PCM data */
190 /* Commit PCM buffer samples as a new chunk for playback */
191 static void commit_chunk(bool flush_next_time
)
193 if (!pcmbuffer_fillpos
)
196 /* Never use the last buffer descriptor */
197 while (write_chunk
== write_end_chunk
) {
198 /* If this happens, something is being stupid */
199 if (!pcm_is_playing()) {
200 logf("commit_chunk error");
203 /* Let approximately one chunk of data playback */
204 sleep(HZ
* PCMBUF_TARGET_CHUNK
/ BYTERATE
);
207 /* commit the chunk */
209 register size_t size
= pcmbuffer_fillpos
;
210 /* Grab the next description to write, and change the write pointer */
211 register struct chunkdesc
*pcmbuf_current
= write_chunk
;
212 write_chunk
= pcmbuf_current
->link
;
213 /* Fill in the values in the new buffer chunk */
214 pcmbuf_current
->addr
= &pcmbuffer
[pcmbuffer_pos
];
215 pcmbuf_current
->size
= size
;
216 pcmbuf_current
->end_of_track
= end_of_track
;
217 pcmbuf_current
->link
= NULL
;
218 end_of_track
= false; /* This is single use only */
220 if (read_chunk
!= NULL
)
224 /* Flush! Discard all data after the currently playing chunk,
225 and make the current chunk play next */
226 write_end_chunk
->link
= read_chunk
->link
;
227 read_chunk
->link
= pcmbuf_current
;
228 while (write_end_chunk
->link
)
230 write_end_chunk
= write_end_chunk
->link
;
231 pcmbuf_unplayed_bytes
-= write_end_chunk
->size
;
234 /* If there is already a read buffer setup, add to it */
236 read_end_chunk
->link
= pcmbuf_current
;
240 /* Otherwise create the buffer */
241 read_chunk
= pcmbuf_current
;
244 /* If flush_next_time is true, then the current chunk will be thrown out
245 * and the next chunk to be committed will be the next to be played.
246 * This is used to empty the PCM buffer for a track change. */
247 flush_pcmbuf
= flush_next_time
;
249 /* This is now the last buffer to read */
250 read_end_chunk
= pcmbuf_current
;
252 /* Update bytes counters */
253 pcmbuf_unplayed_bytes
+= size
;
255 pcmbuffer_pos
+= size
;
256 if (pcmbuffer_pos
>= pcmbuf_size
)
257 pcmbuffer_pos
-= pcmbuf_size
;
259 pcmbuffer_fillpos
= 0;
260 DISPLAY_DESC("commit_chunk");
263 /* Set priority of the codec thread */
264 #ifdef HAVE_PRIORITY_SCHEDULING
265 static void boost_codec_thread(bool boost
)
267 /* Keep voice and codec threads at the same priority or else voice
268 * will starve if the codec thread's priority is boosted. */
271 int priority
= (PRIORITY_PLAYBACK
- PRIORITY_PLAYBACK_MAX
)*pcmbuf_unplayed_bytes
272 / (2*NATIVE_FREQUENCY
) + PRIORITY_PLAYBACK_MAX
;
274 if (priority
!= codec_thread_priority
)
276 codec_thread_priority
= priority
;
277 thread_set_priority(codec_thread_id
, priority
);
278 voice_thread_set_priority(priority
);
281 else if (codec_thread_priority
!= PRIORITY_PLAYBACK
)
283 thread_set_priority(codec_thread_id
, PRIORITY_PLAYBACK
);
284 voice_thread_set_priority(PRIORITY_PLAYBACK
);
285 codec_thread_priority
= PRIORITY_PLAYBACK
;
289 #define boost_codec_thread(boost) do{}while(0)
290 #endif /* HAVE_PRIORITY_SCHEDULING */
292 /* Return true if the PCM buffer is able to receive new data.
293 * Also maintain buffer level above the watermark. */
294 static bool prepare_insert(size_t length
)
296 if (low_latency_mode
)
299 if (!LOW_DATA(1) && pcm_is_playing())
303 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
304 if (pcmbuf_free() < length
+ PCMBUF_MIN_CHUNK
)
307 /* Maintain the buffer level above the watermark */
308 if (pcm_is_playing())
310 /* Only codec thread initiates boost - voice boosts the cpu when playing
313 if (thread_get_current() == codec_thread_id
)
314 #endif /* SIMULATOR */
316 if (pcmbuf_unplayed_bytes
<= pcmbuf_watermark
)
318 /* Fill PCM buffer by boosting cpu */
320 /* If buffer is critically low, override UI priority, else
321 set back to the original priority. */
322 boost_codec_thread(LOW_DATA(2));
326 boost_codec_thread(false);
330 #ifdef HAVE_CROSSFADE
331 /* Disable crossfade if < .5s of audio */
334 crossfade_active
= false;
338 else /* pcm_is_playing */
340 /* Boost CPU for pre-buffer */
343 /* If pre-buffered to the watermark, start playback */
347 if (pcmbuf_unplayed_bytes
> pcmbuf_watermark
)
350 logf("pcm starting");
351 if (!(audio_status() & AUDIO_STATUS_PAUSE
))
359 /* Request space in the buffer for writing output samples */
360 void *pcmbuf_request_buffer(int *count
)
362 #ifdef HAVE_CROSSFADE
363 /* we're going to crossfade to a new track, which is now on its way */
364 if (crossfade_track_change_started
)
367 /* crossfade has begun, put the new track samples in fadebuf */
368 if (crossfade_active
)
370 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
375 /* if possible, reserve room in the PCM buffer for new samples */
377 if(prepare_insert(*count
<< 2))
379 size_t pcmbuffer_index
= pcmbuffer_pos
+ pcmbuffer_fillpos
;
380 if (pcmbuf_size
- pcmbuffer_index
>= PCMBUF_MIN_CHUNK
)
382 /* Usual case, there's space here */
383 return &pcmbuffer
[pcmbuffer_index
];
387 /* Wrap the buffer, the new samples go at the beginning */
390 return &pcmbuffer
[0];
394 /* PCM buffer not ready to receive new data yet */
398 /* Handle new samples to the buffer */
399 void pcmbuf_write_complete(int count
)
401 size_t length
= (size_t)(unsigned int)count
<< 2;
402 #ifdef HAVE_CROSSFADE
403 if (crossfade_active
)
404 write_to_crossfade(length
);
408 pcmbuffer_fillpos
+= length
;
416 static inline void init_pcmbuffers(void)
419 first_desc
= write_chunk
;
421 struct chunkdesc
*next
= write_chunk
;
423 write_end_chunk
= write_chunk
;
424 while ((void *)next
< (void *)pcmbuf_bufend
) {
425 write_end_chunk
->link
=next
;
426 write_end_chunk
=next
;
429 DISPLAY_DESC("init");
432 static size_t get_next_required_pcmbuf_size(void)
436 #ifdef HAVE_CROSSFADE
437 if (crossfade_enable_request
)
438 seconds
+= global_settings
.crossfade_fade_out_delay
+
439 global_settings
.crossfade_fade_out_duration
;
443 /* Buffer has to be at least 2s long. */
446 logf("pcmbuf len: %ld", (long)seconds
);
447 return seconds
* BYTERATE
;
450 /* Initialize the pcmbuffer the structure looks like this:
451 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
452 size_t pcmbuf_init(unsigned char *bufend
)
454 pcmbuf_bufend
= bufend
;
455 pcmbuf_size
= get_next_required_pcmbuf_size();
456 write_chunk
= (struct chunkdesc
*)pcmbuf_bufend
-
457 NUM_CHUNK_DESCS(pcmbuf_size
);
458 voicebuf
= (char *)write_chunk
- PCMBUF_MIX_CHUNK
;
459 #ifdef HAVE_CROSSFADE
460 fadebuf
= voicebuf
- PCMBUF_MIX_CHUNK
;
461 pcmbuffer
= fadebuf
- pcmbuf_size
;
463 pcmbuffer
= voicebuf
- pcmbuf_size
;
468 #ifdef HAVE_CROSSFADE
469 pcmbuf_finish_crossfade_enable();
471 pcmbuf_watermark
= PCMBUF_WATERMARK
;
476 return pcmbuf_bufend
- pcmbuffer
;
482 void pcmbuf_start_track_change(bool auto_skip
)
484 bool crossfade
= false;
485 #ifdef HAVE_CROSSFADE
486 /* Determine whether this track change needs to crossfade */
487 if(crossfade_enabled
&& !pcmbuf_is_crossfade_active())
489 switch(global_settings
.crossfade
)
491 case CROSSFADE_ENABLE_AUTOSKIP
:
492 crossfade
= auto_skip
;
494 case CROSSFADE_ENABLE_MANSKIP
:
495 crossfade
= !auto_skip
;
497 case CROSSFADE_ENABLE_SHUFFLE
:
498 crossfade
= global_settings
.playlist_shuffle
;
500 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP
:
501 crossfade
= global_settings
.playlist_shuffle
|| !auto_skip
;
503 case CROSSFADE_ENABLE_ALWAYS
:
510 if (!auto_skip
|| crossfade
)
511 /* manual skip or crossfade */
514 { logf(" crossfade track change"); }
516 { logf(" manual track change"); }
518 /* Notify the wps that the track change starts now */
519 audio_post_track_change(false);
521 /* Can't do two crossfades at once and, no fade if pcm is off now */
523 #ifdef HAVE_CROSSFADE
524 pcmbuf_is_crossfade_active() ||
534 /* Not enough data, or not crossfading, flush the old data instead */
535 if (LOW_DATA(2) || !crossfade
|| low_latency_mode
)
541 #ifdef HAVE_CROSSFADE
542 /* Don't enable mix mode when skipping tracks manually. */
543 crossfade_mixmode
= auto_skip
&& global_settings
.crossfade_fade_out_mixmode
;
545 crossfade_auto_skip
= auto_skip
;
547 crossfade_track_change_started
= crossfade
;
550 else /* automatic and not crossfading, so do gapless track change */
552 /* The codec is moving on to the next track, but the current track will
553 * continue to play. Set a flag to make sure the elapsed time of the
554 * current track will be updated properly, and mark the current chunk
555 * as the last one in the track. */
556 logf(" gapless track change");
557 track_transition
= true;
565 /* PCM driver callback
566 * This function has 3 major logical parts (separated by brackets both for
567 * readability and variable scoping). The first part performs the
568 * operations related to finishing off the last chunk we fed to the DMA.
569 * The second part detects the end of playlist condition when the PCM
570 * buffer is empty except for uncommitted samples. Then they are committed
571 * and sent to the PCM driver for playback. The third part performs the
572 * operations involved in sending a new chunk to the DMA. */
573 static void pcmbuf_pcm_callback(unsigned char** start
, size_t* size
) ICODE_ATTR
;
574 static void pcmbuf_pcm_callback(unsigned char** start
, size_t* size
)
577 struct chunkdesc
*pcmbuf_current
= read_chunk
;
578 /* Take the finished chunk out of circulation */
579 read_chunk
= pcmbuf_current
->link
;
581 /* if during a track transition, update the elapsed time in ms */
582 if (track_transition
)
583 audio_pcmbuf_position_callback(last_chunksize
* 1000 / BYTERATE
);
585 /* if last chunk in the track, stop updates and notify audio thread */
586 if (pcmbuf_current
->end_of_track
)
588 track_transition
= false;
589 audio_post_track_change(true);
592 /* Put the finished chunk back into circulation */
593 write_end_chunk
->link
= pcmbuf_current
;
594 write_end_chunk
= pcmbuf_current
;
596 /* If we've read over the mix chunk while it's still mixing there */
597 if (pcmbuf_current
== mix_chunk
)
600 #ifdef HAVE_CROSSFADE
601 /* If we've read over the crossfade chunk while it's still fading */
602 if (pcmbuf_current
== crossfade_chunk
)
603 crossfade_chunk
= read_chunk
;
608 /* Commit last samples at end of playlist */
609 if (pcmbuffer_fillpos
&& !read_chunk
)
611 logf("pcmbuf_pcm_callback: commit last samples");
617 /* Send the new chunk to the PCM */
620 size_t current_size
= read_chunk
->size
;
622 pcmbuf_unplayed_bytes
-= current_size
;
623 last_chunksize
= current_size
;
624 *size
= current_size
;
625 *start
= read_chunk
->addr
;
630 logf("pcmbuf_pcm_callback: no more chunks");
636 DISPLAY_DESC("callback");
640 void pcmbuf_play_start(void)
642 if (!pcm_is_playing() && pcmbuf_unplayed_bytes
&& read_chunk
!= NULL
)
644 logf("pcmbuf_play_start");
645 last_chunksize
= read_chunk
->size
;
646 pcmbuf_unplayed_bytes
-= last_chunksize
;
647 pcm_play_data(pcmbuf_pcm_callback
,
648 (unsigned char *)read_chunk
->addr
, last_chunksize
);
652 void pcmbuf_play_stop(void)
654 logf("pcmbuf_play_stop");
657 pcmbuf_unplayed_bytes
= 0;
660 write_end_chunk
->link
= read_chunk
;
661 write_end_chunk
= read_end_chunk
;
662 read_chunk
= read_end_chunk
= NULL
;
665 pcmbuffer_fillpos
= 0;
666 #ifdef HAVE_CROSSFADE
667 crossfade_track_change_started
= false;
668 crossfade_active
= false;
670 end_of_track
= false;
671 track_transition
= false;
672 DISPLAY_DESC("play_stop");
674 /* Can unboost the codec thread here no matter who's calling */
675 boost_codec_thread(false);
678 void pcmbuf_pause(bool pause
)
680 logf("pcmbuf_pause: %s", pause
?"pause":"play");
681 if (pcm_is_playing())
682 pcm_play_pause(!pause
);
690 /* Clip sample to signed 16 bit range */
691 static inline int32_t clip_sample_16(int32_t sample
)
693 if ((int16_t)sample
!= sample
)
694 sample
= 0x7fff ^ (sample
>> 31);
698 #ifdef HAVE_CROSSFADE
699 /* Find the chunk that's (length) deep in the list. Return the position within
700 * the chunk, and leave the chunkdesc pointer pointing to the chunk. */
701 static size_t find_chunk(size_t length
, struct chunkdesc
**chunk
)
703 while (*chunk
&& length
>= (*chunk
)->size
)
705 length
-= (*chunk
)->size
;
706 *chunk
= (*chunk
)->link
;
711 /* Returns the number of bytes _NOT_ mixed/faded */
712 static size_t crossfade_mix_fade(int factor
, size_t length
, const char *buf
,
713 size_t *out_sample
, struct chunkdesc
**out_chunk
)
715 const int16_t *input_buf
= (const int16_t *)buf
;
716 int16_t *output_buf
= (int16_t *)((*out_chunk
)->addr
);
717 int16_t *chunk_end
= SKIPBYTES(output_buf
, (*out_chunk
)->size
);
718 output_buf
= &output_buf
[*out_sample
];
723 /* fade left and right channel at once to keep buffer alignment */
725 for (i
= 0; i
< 2; i
++)
728 /* fade the input buffer and mix into the chunk */
730 sample
= *input_buf
++;
731 sample
= ((sample
* factor
) >> 8) + *output_buf
;
732 *output_buf
++ = clip_sample_16(sample
);
735 /* fade the chunk only */
737 sample
= *output_buf
;
738 *output_buf
++ = (sample
* factor
) >> 8;
742 length
-= 4; /* 2 samples, each 16 bit -> 4 bytes */
744 /* move to next chunk as needed */
745 if (output_buf
>= chunk_end
)
747 *out_chunk
= (*out_chunk
)->link
;
750 output_buf
= (int16_t *)((*out_chunk
)->addr
);
751 chunk_end
= SKIPBYTES(output_buf
, (*out_chunk
)->size
);
754 *out_sample
= output_buf
- (int16_t *)((*out_chunk
)->addr
);
758 /* Initializes crossfader, calculates all necessary parameters and performs
759 * fade-out with the PCM buffer. */
760 static void crossfade_start(void)
762 size_t crossfade_rem
;
763 size_t crossfade_need
;
765 size_t fade_out_delay
;
766 size_t fade_in_delay
;
768 crossfade_track_change_started
= false;
769 /* Reject crossfade if less than .5s of data */
771 logf("crossfade rejected");
776 logf("crossfade_start");
778 crossfade_active
= true;
780 /* Initialize the crossfade buffer size to all of the buffered data that
781 * has not yet been sent to the DMA */
782 crossfade_rem
= pcmbuf_unplayed_bytes
;
783 crossfade_chunk
= read_chunk
->link
;
784 crossfade_sample
= 0;
786 /* Get fade out info from settings. */
787 fade_out_delay
= global_settings
.crossfade_fade_out_delay
* BYTERATE
;
788 fade_out_rem
= global_settings
.crossfade_fade_out_duration
* BYTERATE
;
790 crossfade_need
= fade_out_delay
+ fade_out_rem
;
791 if (crossfade_rem
> crossfade_need
)
793 if (crossfade_auto_skip
)
794 /* Automatic track changes only modify the last part of the buffer,
795 * so find the right chunk and sample to start the crossfade */
797 crossfade_sample
= find_chunk(crossfade_rem
- crossfade_need
,
799 crossfade_rem
= crossfade_need
;
802 /* Manual skips occur immediately, but give time to process */
804 crossfade_rem
-= crossfade_chunk
->size
;
805 crossfade_chunk
= crossfade_chunk
->link
;
808 /* Truncate fade out duration if necessary. */
809 if (crossfade_rem
< crossfade_need
)
811 size_t crossfade_short
= crossfade_need
- crossfade_rem
;
812 if (fade_out_rem
>= crossfade_short
)
813 fade_out_rem
-= crossfade_short
;
816 fade_out_delay
-= crossfade_short
- fade_out_rem
;
820 crossfade_rem
-= fade_out_delay
+ fade_out_rem
;
822 /* Completely process the crossfade fade-out effect with current PCM buffer */
823 if (!crossfade_mixmode
)
825 /* Fade out the specified amount of the already processed audio */
826 size_t total_fade_out
= fade_out_rem
;
827 size_t fade_out_sample
;
828 struct chunkdesc
*fade_out_chunk
= crossfade_chunk
;
830 /* Find the right chunk and sample to start fading out */
831 fade_out_delay
+= crossfade_sample
* 2;
832 fade_out_sample
= find_chunk(fade_out_delay
, &fade_out_chunk
);
834 while (fade_out_rem
> 0)
836 /* Each 1/10 second of audio will have the same fade applied */
837 size_t block_rem
= MIN(BYTERATE
/ 10, fade_out_rem
);
838 int factor
= (fade_out_rem
<< 8) / total_fade_out
;
840 fade_out_rem
-= block_rem
;
842 crossfade_mix_fade(factor
, block_rem
, NULL
,
843 &fade_out_sample
, &fade_out_chunk
);
846 /* zero out the rest of the buffer */
847 crossfade_mix_fade(0, crossfade_rem
, NULL
,
848 &fade_out_sample
, &fade_out_chunk
);
851 /* Initialize fade-in counters */
852 crossfade_fade_in_total
= global_settings
.crossfade_fade_in_duration
* BYTERATE
;
853 crossfade_fade_in_rem
= crossfade_fade_in_total
;
855 fade_in_delay
= global_settings
.crossfade_fade_in_delay
* BYTERATE
;
857 /* Find the right chunk and sample to start fading in */
858 fade_in_delay
+= crossfade_sample
* 2;
859 crossfade_sample
= find_chunk(fade_in_delay
, &crossfade_chunk
);
860 logf("crossfade_start done!");
863 /* Perform fade-in of new track */
864 static void write_to_crossfade(size_t length
)
869 if (crossfade_fade_in_rem
)
874 /* Fade factor for this packet */
876 ((crossfade_fade_in_total
- crossfade_fade_in_rem
) << 8) /
877 crossfade_fade_in_total
;
879 size_t fade_rem
= MIN(length
, crossfade_fade_in_rem
);
881 /* We _will_ fade this many bytes */
882 crossfade_fade_in_rem
-= fade_rem
;
887 size_t fade_total
= fade_rem
;
888 fade_rem
= crossfade_mix_fade(factor
, fade_rem
, buf
,
889 &crossfade_sample
, &crossfade_chunk
);
890 length
-= fade_total
- fade_rem
;
891 buf
+= fade_total
- fade_rem
;
896 samples
= fade_rem
/ 2;
897 input_buf
= (int16_t *)buf
;
898 /* Fade remaining samples in place */
901 int32_t sample
= *input_buf
;
902 *input_buf
++ = (sample
* factor
) >> 8;
909 size_t mix_total
= length
;
910 /* A factor of 256 means mix only, no fading */
911 length
= crossfade_mix_fade(256, length
, buf
,
912 &crossfade_sample
, &crossfade_chunk
);
913 buf
+= mix_total
- length
;
918 /* Commit samples to the buffer */
919 while (!prepare_insert(length
))
924 size_t pcmbuffer_index
= pcmbuffer_pos
+ pcmbuffer_fillpos
;
925 size_t copy_n
= MIN(length
, pcmbuf_size
- pcmbuffer_index
);
926 memcpy(&pcmbuffer
[pcmbuffer_index
], buf
, copy_n
);
928 pcmbuffer_fillpos
+= copy_n
;
932 /* if no more fading-in to do, stop the crossfade */
933 if (!(crossfade_fade_in_rem
|| crossfade_chunk
))
934 crossfade_active
= false;
937 static void pcmbuf_finish_crossfade_enable(void)
939 /* Copy the pending setting over now */
940 crossfade_enabled
= crossfade_enable_request
;
942 pcmbuf_watermark
= (crossfade_enabled
&& pcmbuf_size
) ?
943 /* If crossfading, try to keep the buffer full other than 1 second */
944 (pcmbuf_size
- BYTERATE
) :
945 /* Otherwise, just use the default */
949 bool pcmbuf_is_crossfade_active(void)
951 return crossfade_active
|| crossfade_track_change_started
;
954 void pcmbuf_request_crossfade_enable(bool on_off
)
956 /* Next setting to be used, not applied now */
957 crossfade_enable_request
= on_off
;
960 bool pcmbuf_is_same_size(void)
962 /* if pcmbuffer is NULL, then not set up yet even once so always */
963 bool same_size
= pcmbuffer
?
964 (get_next_required_pcmbuf_size() == pcmbuf_size
) : true;
966 /* no buffer change needed, so finish crossfade setup now */
968 pcmbuf_finish_crossfade_enable();
972 #endif /* HAVE_CROSSFADE */
977 /* Returns pcm buffer usage in percents (0 to 100). */
978 static int pcmbuf_usage(void)
980 return pcmbuf_unplayed_bytes
* 100 / pcmbuf_size
;
983 static int pcmbuf_mix_free(void)
988 (size_t)&((int16_t *)mix_chunk
->addr
)[pcmbuf_mix_sample
];
989 size_t my_write_pos
= (size_t)&pcmbuffer
[pcmbuffer_pos
];
990 if (my_write_pos
< my_mix_end
)
991 my_write_pos
+= pcmbuf_size
;
992 return (my_write_pos
- my_mix_end
) * 100 / pcmbuf_unplayed_bytes
;
997 void *pcmbuf_request_voice_buffer(int *count
)
999 /* A get-it-to-work-for-now hack (audio status could change by
1001 if (audio_status() & AUDIO_STATUS_PLAY
)
1003 if (read_chunk
== NULL
)
1007 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
1008 (mix_chunk
|| read_chunk
->link
))
1010 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
1020 return pcmbuf_request_buffer(count
);
1024 void pcmbuf_write_voice_complete(int count
)
1026 /* A get-it-to-work-for-now hack (audio status could have changed) */
1027 if (!(audio_status() & AUDIO_STATUS_PLAY
))
1029 pcmbuf_write_complete(count
);
1033 int16_t *ibuf
= (int16_t *)voicebuf
;
1035 size_t chunk_samples
;
1037 if (mix_chunk
== NULL
&& read_chunk
!= NULL
)
1039 mix_chunk
= read_chunk
->link
;
1040 /* Start 1/8s into the next chunk */
1041 pcmbuf_mix_sample
= BYTERATE
/ 16;
1047 obuf
= (int16_t *)mix_chunk
->addr
;
1048 chunk_samples
= mix_chunk
->size
/ sizeof (int16_t);
1054 int32_t sample
= *ibuf
++;
1056 if (pcmbuf_mix_sample
>= chunk_samples
)
1058 mix_chunk
= mix_chunk
->link
;
1061 pcmbuf_mix_sample
= 0;
1062 obuf
= mix_chunk
->addr
;
1063 chunk_samples
= mix_chunk
->size
/ 2;
1065 sample
+= obuf
[pcmbuf_mix_sample
] >> 2;
1066 obuf
[pcmbuf_mix_sample
++] = clip_sample_16(sample
);
1071 /** Debug menu, other metrics */
1073 /* Amount of bytes left in the buffer. */
1074 size_t pcmbuf_free(void)
1076 if (read_chunk
!= NULL
)
1078 void *read
= read_chunk
->addr
;
1079 void *write
= &pcmbuffer
[pcmbuffer_pos
+ pcmbuffer_fillpos
];
1081 return (size_t)(read
- write
) + pcmbuf_size
;
1083 return (size_t) (read
- write
);
1085 return pcmbuf_size
- pcmbuffer_fillpos
;
1088 size_t pcmbuf_get_bufsize(void)
1093 int pcmbuf_used_descs(void)
1095 struct chunkdesc
*temp
= read_chunk
;
1104 int pcmbuf_descs(void)
1106 return NUM_CHUNK_DESCS(pcmbuf_size
);
1109 #ifdef ROCKBOX_HAS_LOGF
1110 unsigned char *pcmbuf_get_meminfo(size_t *length
)
1112 *length
= pcmbuf_bufend
- pcmbuffer
;
1120 bool pcmbuf_is_lowdata(void)
1122 if (!pcm_is_playing() || pcm_is_paused()
1123 #ifdef HAVE_CROSSFADE
1124 || pcmbuf_is_crossfade_active()
1130 /* 1 seconds of buffer is low data */
1133 /* under watermark is low data */
1134 return (pcmbuf_unplayed_bytes
< pcmbuf_watermark
);
1138 void pcmbuf_set_low_latency(bool state
)
1140 low_latency_mode
= state
;
1143 unsigned long pcmbuf_get_latency(void)
1145 return (pcmbuf_unplayed_bytes
+ pcm_get_bytes_waiting()) * 1000 / BYTERATE
;
1148 #ifndef HAVE_HARDWARE_BEEP
1149 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
1150 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
1152 /* Generates a constant square wave sound with a given frequency
1153 in Hertz for a duration in milliseconds. */
1154 void pcmbuf_beep(unsigned int frequency
, size_t duration
, int amplitude
)
1156 unsigned int step
= 0xffffffffu
/ NATIVE_FREQUENCY
* frequency
;
1158 int16_t *bufptr
, *bufstart
, *bufend
;
1160 int nsamples
= NATIVE_FREQUENCY
/ 1000 * duration
;
1161 bool mix
= read_chunk
!= NULL
&& read_chunk
->link
!= NULL
;
1164 bufend
= SKIPBYTES((int16_t *)pcmbuffer
, pcmbuf_size
);
1166 /* Find the insertion point and set bufstart to the start of it */
1169 /* Get the currently playing chunk at the current position. */
1170 bufstart
= (int16_t *)pcm_play_dma_get_peak_buffer(&i
);
1172 /* If above isn't implemented or pcm is stopped, no beepeth. */
1173 if (!bufstart
|| !pcm_is_playing())
1176 /* Give 5ms clearance. */
1177 bufstart
+= BYTERATE
/ 200;
1179 #ifdef HAVE_PCM_DMA_ADDRESS
1180 /* Returned peak addresses are DMA addresses */
1181 bufend
= pcm_dma_addr(bufend
);
1184 /* Wrapped above? */
1185 if (bufstart
>= bufend
)
1186 bufstart
-= pcmbuf_size
;
1188 /* NOTE: On some targets using hardware DMA, cache range flushing may
1189 * be required or the writes may not be picked up by the controller.
1190 * An incremental flush should be done periodically during the mixdown. */
1192 else if (nsamples
<= MINIBUF_SAMPLES
)
1194 static int16_t minibuf
[MINIBUF_SAMPLES
*2] __attribute__((aligned(4)));
1195 /* Use mini buffer */
1197 bufend
= SKIPBYTES(bufstart
, MINIBUF_SIZE
);
1199 else if (!audio_buffer_state_trashed())
1202 bufstart
= (int16_t *)pcmbuffer
;
1212 /* Mix square wave into buffer */
1213 for (i
= 0; i
< nsamples
; ++i
)
1215 int32_t amp
= (phase
>> 31) ^ (int32_t)amplitude
;
1216 sample
= mix
? *bufptr
: 0;
1217 *bufptr
++ = clip_sample_16(sample
+ amp
);
1218 if (bufptr
>= bufend
)
1219 bufptr
= (int16_t *)pcmbuffer
;
1220 sample
= mix
? *bufptr
: 0;
1221 *bufptr
++ = clip_sample_16(sample
+ amp
);
1222 if (bufptr
>= bufend
)
1223 bufptr
= (int16_t *)pcmbuffer
;
1229 #ifdef HAVE_RECORDING
1233 /* Kick off playback if required and it won't interfere */
1234 if (!pcm_is_playing()
1235 #ifdef HAVE_RECORDING
1236 && !pcm_is_recording()
1240 pcm_play_data(NULL
, (unsigned char *)bufstart
, nsamples
* 4);
1244 #ifdef HAVE_RECORDING
1248 #endif /* HAVE_HARDWARE_BEEP */