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 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(" ");} \
165 #define SHOW_DETAIL(desc) DEBUGF(":");SHOW_DESC(desc); DEBUGF(">"); \
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
);
178 SHOW_POINT("w", write_chunk
);
179 SHOW_POINT("we", write_end_chunk
);
182 for (i
= 0; i
< pcmbuf_descs(); i
++)
184 SHOW_NUM(i
, (first_desc
+ i
));
185 if (i
%10 == 9) DEBUGF("\n");
188 show_desc_in_use
= false;
192 #define DISPLAY_DESC(caller) do{}while(0)
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
)
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");
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
= end_of_track
;
225 pcmbuf_current
->link
= NULL
;
226 end_of_track
= false; /* This is single use only */
228 if (read_chunk
!= NULL
)
232 /* Flush! Discard all data after the currently playing chunk,
233 and make the current chunk play next */
234 logf("commit_chunk: flush");
235 write_end_chunk
->link
= read_chunk
->link
;
236 read_chunk
->link
= pcmbuf_current
;
237 while (write_end_chunk
->link
)
239 write_end_chunk
= write_end_chunk
->link
;
240 pcmbuf_unplayed_bytes
-= write_end_chunk
->size
;
243 /* If there is already a read buffer setup, add to it */
245 read_end_chunk
->link
= pcmbuf_current
;
249 /* Otherwise create the buffer */
250 read_chunk
= pcmbuf_current
;
253 /* If flush_next_time is true, then the current chunk will be thrown out
254 * and the next chunk to be committed will be the next to be played.
255 * This is used to empty the PCM buffer for a track change. */
256 flush_pcmbuf
= flush_next_time
;
258 /* This is now the last buffer to read */
259 read_end_chunk
= pcmbuf_current
;
261 /* Update bytes counters */
262 pcmbuf_unplayed_bytes
+= size
;
264 pcmbuffer_pos
+= size
;
265 if (pcmbuffer_pos
>= pcmbuf_size
)
266 pcmbuffer_pos
-= pcmbuf_size
;
268 pcmbuffer_fillpos
= 0;
269 DISPLAY_DESC("commit_chunk");
272 /* Set priority of the codec thread */
273 #ifdef HAVE_PRIORITY_SCHEDULING
274 static void boost_codec_thread(bool boost
)
276 /* Keep voice and codec threads at the same priority or else voice
277 * will starve if the codec thread's priority is boosted. */
280 int priority
= (PRIORITY_PLAYBACK
- PRIORITY_PLAYBACK_MAX
)*pcmbuf_unplayed_bytes
281 / (2*NATIVE_FREQUENCY
) + PRIORITY_PLAYBACK_MAX
;
283 if (priority
!= codec_thread_priority
)
285 codec_thread_priority
= priority
;
286 thread_set_priority(codec_thread_id
, priority
);
287 voice_thread_set_priority(priority
);
290 else if (codec_thread_priority
!= PRIORITY_PLAYBACK
)
292 thread_set_priority(codec_thread_id
, PRIORITY_PLAYBACK
);
293 voice_thread_set_priority(PRIORITY_PLAYBACK
);
294 codec_thread_priority
= PRIORITY_PLAYBACK
;
298 #define boost_codec_thread(boost) do{}while(0)
299 #endif /* HAVE_PRIORITY_SCHEDULING */
301 /* Return true if the PCM buffer is able to receive new data.
302 * Also maintain buffer level above the watermark. */
303 static bool prepare_insert(size_t length
)
305 if (low_latency_mode
)
308 if (!LOW_DATA(1) && pcm_is_playing())
312 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
313 if (pcmbuf_free() < length
+ PCMBUF_MIN_CHUNK
)
316 /* Maintain the buffer level above the watermark */
317 if (pcm_is_playing())
319 /* Only codec thread initiates boost - voice boosts the cpu when playing
322 if (thread_get_current() == codec_thread_id
)
323 #endif /* SIMULATOR */
325 if (pcmbuf_unplayed_bytes
<= pcmbuf_watermark
)
327 /* Fill PCM buffer by boosting cpu */
329 /* If buffer is critically low, override UI priority, else
330 set back to the original priority. */
331 boost_codec_thread(LOW_DATA(2));
335 boost_codec_thread(false);
339 #ifdef HAVE_CROSSFADE
340 /* Disable crossfade if < .5s of audio */
343 crossfade_active
= false;
347 else /* pcm_is_playing */
349 /* Boost CPU for pre-buffer */
352 /* If pre-buffered to the watermark, start playback */
356 if (pcmbuf_unplayed_bytes
> pcmbuf_watermark
)
359 logf("pcm starting");
360 if (!(audio_status() & AUDIO_STATUS_PAUSE
))
368 /* Request space in the buffer for writing output samples */
369 void *pcmbuf_request_buffer(int *count
)
371 #ifdef HAVE_CROSSFADE
372 /* we're going to crossfade to a new track, which is now on its way */
373 if (crossfade_track_change_started
)
376 /* crossfade has begun, put the new track samples in fadebuf */
377 if (crossfade_active
)
379 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
384 /* if possible, reserve room in the PCM buffer for new samples */
386 if(prepare_insert(*count
<< 2))
388 size_t pcmbuffer_index
= pcmbuffer_pos
+ pcmbuffer_fillpos
;
389 if (pcmbuf_size
- pcmbuffer_index
>= PCMBUF_MIN_CHUNK
)
391 /* Usual case, there's space here */
392 return &pcmbuffer
[pcmbuffer_index
];
396 /* Wrap the buffer, the new samples go at the beginning */
399 return &pcmbuffer
[0];
403 /* PCM buffer not ready to receive new data yet */
407 /* Handle new samples to the buffer */
408 void pcmbuf_write_complete(int count
)
410 size_t length
= (size_t)(unsigned int)count
<< 2;
411 #ifdef HAVE_CROSSFADE
412 if (crossfade_active
)
413 write_to_crossfade(length
);
417 pcmbuffer_fillpos
+= length
;
425 static inline void init_pcmbuffers(void)
428 first_desc
= write_chunk
;
430 struct chunkdesc
*next
= write_chunk
;
432 write_end_chunk
= write_chunk
;
433 while ((void *)next
< (void *)pcmbuf_bufend
) {
434 write_end_chunk
->link
=next
;
435 write_end_chunk
=next
;
438 DISPLAY_DESC("init");
441 static size_t get_next_required_pcmbuf_size(void)
445 #ifdef HAVE_CROSSFADE
446 if (crossfade_enable_request
)
447 seconds
+= global_settings
.crossfade_fade_out_delay
+
448 global_settings
.crossfade_fade_out_duration
;
452 /* Buffer has to be at least 2s long. */
455 logf("pcmbuf len: %ld", (long)seconds
);
456 return seconds
* BYTERATE
;
459 /* Initialize the pcmbuffer the structure looks like this:
460 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
461 size_t pcmbuf_init(unsigned char *bufend
)
463 pcmbuf_bufend
= bufend
;
464 pcmbuf_size
= get_next_required_pcmbuf_size();
465 write_chunk
= (struct chunkdesc
*)pcmbuf_bufend
-
466 NUM_CHUNK_DESCS(pcmbuf_size
);
467 voicebuf
= (char *)write_chunk
- PCMBUF_MIX_CHUNK
;
468 #ifdef HAVE_CROSSFADE
469 fadebuf
= voicebuf
- PCMBUF_MIX_CHUNK
;
470 pcmbuffer
= fadebuf
- pcmbuf_size
;
472 pcmbuffer
= voicebuf
- pcmbuf_size
;
477 #ifdef HAVE_CROSSFADE
478 pcmbuf_finish_crossfade_enable();
480 pcmbuf_watermark
= PCMBUF_WATERMARK
;
485 return pcmbuf_bufend
- pcmbuffer
;
491 void pcmbuf_start_track_change(bool auto_skip
)
493 bool crossfade
= false;
494 #ifdef HAVE_CROSSFADE
495 /* Determine whether this track change needs to crossfade */
496 if(crossfade_enabled
&& !pcmbuf_is_crossfade_active())
498 switch(global_settings
.crossfade
)
500 case CROSSFADE_ENABLE_AUTOSKIP
:
501 crossfade
= auto_skip
;
503 case CROSSFADE_ENABLE_MANSKIP
:
504 crossfade
= !auto_skip
;
506 case CROSSFADE_ENABLE_SHUFFLE
:
507 crossfade
= global_settings
.playlist_shuffle
;
509 case CROSSFADE_ENABLE_SHUFFLE_OR_MANSKIP
:
510 crossfade
= global_settings
.playlist_shuffle
|| !auto_skip
;
512 case CROSSFADE_ENABLE_ALWAYS
:
519 if (!auto_skip
|| crossfade
)
520 /* manual skip or crossfade */
523 { logf(" crossfade track change"); }
525 { logf(" manual track change"); }
527 /* Notify the wps that the track change starts now */
528 audio_post_track_change(false);
530 /* Can't do two crossfades at once and, no fade if pcm is off now */
532 #ifdef HAVE_CROSSFADE
533 pcmbuf_is_crossfade_active() ||
543 /* Not enough data, or not crossfading, flush the old data instead */
544 if (LOW_DATA(2) || !crossfade
|| low_latency_mode
)
550 #ifdef HAVE_CROSSFADE
551 /* Don't enable mix mode when skipping tracks manually. */
552 crossfade_mixmode
= auto_skip
&& global_settings
.crossfade_fade_out_mixmode
;
554 crossfade_auto_skip
= auto_skip
;
556 crossfade_track_change_started
= crossfade
;
559 else /* automatic and not crossfading, so do gapless track change */
561 /* The codec is moving on to the next track, but the current track will
562 * continue to play. Set a flag to make sure the elapsed time of the
563 * current track will be updated properly, and mark the current chunk
564 * as the last one in the track. */
565 logf(" gapless track change");
566 track_transition
= true;
574 /* PCM driver callback
575 * This function has 3 major logical parts (separated by brackets both for
576 * readability and variable scoping). The first part performs the
577 * operations related to finishing off the last chunk we fed to the DMA.
578 * The second part detects the end of playlist condition when the PCM
579 * buffer is empty except for uncommitted samples. Then they are committed
580 * and sent to the PCM driver for playback. The third part performs the
581 * operations involved in sending a new chunk to the DMA. */
582 static void pcmbuf_pcm_callback(unsigned char** start
, size_t* size
) ICODE_ATTR
;
583 static void pcmbuf_pcm_callback(unsigned char** start
, size_t* size
)
586 struct chunkdesc
*pcmbuf_current
= read_chunk
;
587 /* Take the finished chunk out of circulation */
588 read_chunk
= pcmbuf_current
->link
;
590 /* if during a track transition, update the elapsed time in ms */
591 if (track_transition
)
592 audio_pcmbuf_position_callback(last_chunksize
* 1000 / BYTERATE
);
594 /* if last chunk in the track, stop updates and notify audio thread */
595 if (pcmbuf_current
->end_of_track
)
597 track_transition
= false;
598 audio_post_track_change(true);
601 /* Put the finished chunk back into circulation */
602 write_end_chunk
->link
= pcmbuf_current
;
603 write_end_chunk
= pcmbuf_current
;
605 /* If we've read over the mix chunk while it's still mixing there */
606 if (pcmbuf_current
== mix_chunk
)
609 #ifdef HAVE_CROSSFADE
610 /* If we've read over the crossfade chunk while it's still fading */
611 if (pcmbuf_current
== crossfade_chunk
)
612 crossfade_chunk
= read_chunk
;
617 /* Commit last samples at end of playlist */
618 if (pcmbuffer_fillpos
&& !read_chunk
)
620 logf("pcmbuf_pcm_callback: commit last samples");
626 /* Send the new chunk to the PCM */
629 size_t current_size
= read_chunk
->size
;
631 pcmbuf_unplayed_bytes
-= current_size
;
632 last_chunksize
= current_size
;
633 *size
= current_size
;
634 *start
= read_chunk
->addr
;
639 logf("pcmbuf_pcm_callback: no more chunks");
645 DISPLAY_DESC("callback");
649 void pcmbuf_play_start(void)
651 if (!pcm_is_playing() && pcmbuf_unplayed_bytes
&& read_chunk
!= NULL
)
653 logf("pcmbuf_play_start");
654 last_chunksize
= read_chunk
->size
;
655 pcmbuf_unplayed_bytes
-= last_chunksize
;
656 pcm_play_data(pcmbuf_pcm_callback
,
657 (unsigned char *)read_chunk
->addr
, last_chunksize
);
661 void pcmbuf_play_stop(void)
663 logf("pcmbuf_play_stop");
666 pcmbuf_unplayed_bytes
= 0;
669 write_end_chunk
->link
= read_chunk
;
670 write_end_chunk
= read_end_chunk
;
671 read_chunk
= read_end_chunk
= NULL
;
674 pcmbuffer_fillpos
= 0;
675 #ifdef HAVE_CROSSFADE
676 crossfade_track_change_started
= false;
677 crossfade_active
= false;
679 end_of_track
= false;
680 track_transition
= false;
681 flush_pcmbuf
= false;
682 DISPLAY_DESC("play_stop");
684 /* Can unboost the codec thread here no matter who's calling */
685 boost_codec_thread(false);
688 void pcmbuf_pause(bool pause
)
690 logf("pcmbuf_pause: %s", pause
?"pause":"play");
691 if (pcm_is_playing())
692 pcm_play_pause(!pause
);
700 /* Clip sample to signed 16 bit range */
701 static inline int32_t clip_sample_16(int32_t sample
)
703 if ((int16_t)sample
!= sample
)
704 sample
= 0x7fff ^ (sample
>> 31);
708 #ifdef HAVE_CROSSFADE
709 /* Find the chunk that's (length) deep in the list. Return the position within
710 * the chunk, and leave the chunkdesc pointer pointing to the chunk. */
711 static size_t find_chunk(size_t length
, struct chunkdesc
**chunk
)
713 while (*chunk
&& length
>= (*chunk
)->size
)
715 length
-= (*chunk
)->size
;
716 *chunk
= (*chunk
)->link
;
721 /* Returns the number of bytes _NOT_ mixed/faded */
722 static size_t crossfade_mix_fade(int factor
, size_t length
, const char *buf
,
723 size_t *out_sample
, struct chunkdesc
**out_chunk
)
725 const int16_t *input_buf
= (const int16_t *)buf
;
726 int16_t *output_buf
= (int16_t *)((*out_chunk
)->addr
);
727 int16_t *chunk_end
= SKIPBYTES(output_buf
, (*out_chunk
)->size
);
728 output_buf
= &output_buf
[*out_sample
];
733 /* fade left and right channel at once to keep buffer alignment */
735 for (i
= 0; i
< 2; i
++)
738 /* fade the input buffer and mix into the chunk */
740 sample
= *input_buf
++;
741 sample
= ((sample
* factor
) >> 8) + *output_buf
;
742 *output_buf
++ = clip_sample_16(sample
);
745 /* fade the chunk only */
747 sample
= *output_buf
;
748 *output_buf
++ = (sample
* factor
) >> 8;
752 length
-= 4; /* 2 samples, each 16 bit -> 4 bytes */
754 /* move to next chunk as needed */
755 if (output_buf
>= chunk_end
)
757 *out_chunk
= (*out_chunk
)->link
;
760 output_buf
= (int16_t *)((*out_chunk
)->addr
);
761 chunk_end
= SKIPBYTES(output_buf
, (*out_chunk
)->size
);
764 *out_sample
= output_buf
- (int16_t *)((*out_chunk
)->addr
);
768 /* Initializes crossfader, calculates all necessary parameters and performs
769 * fade-out with the PCM buffer. */
770 static void crossfade_start(void)
772 size_t crossfade_rem
;
773 size_t crossfade_need
;
775 size_t fade_out_delay
;
776 size_t fade_in_delay
;
778 crossfade_track_change_started
= false;
779 /* Reject crossfade if less than .5s of data */
781 logf("crossfade rejected");
786 logf("crossfade_start");
788 crossfade_active
= true;
790 /* Initialize the crossfade buffer size to all of the buffered data that
791 * has not yet been sent to the DMA */
792 crossfade_rem
= pcmbuf_unplayed_bytes
;
793 crossfade_chunk
= read_chunk
->link
;
794 crossfade_sample
= 0;
796 /* Get fade out info from settings. */
797 fade_out_delay
= global_settings
.crossfade_fade_out_delay
* BYTERATE
;
798 fade_out_rem
= global_settings
.crossfade_fade_out_duration
* BYTERATE
;
800 crossfade_need
= fade_out_delay
+ fade_out_rem
;
801 if (crossfade_rem
> crossfade_need
)
803 if (crossfade_auto_skip
)
804 /* Automatic track changes only modify the last part of the buffer,
805 * so find the right chunk and sample to start the crossfade */
807 crossfade_sample
= find_chunk(crossfade_rem
- crossfade_need
,
809 crossfade_rem
= crossfade_need
;
812 /* Manual skips occur immediately, but give time to process */
814 crossfade_rem
-= crossfade_chunk
->size
;
815 crossfade_chunk
= crossfade_chunk
->link
;
818 /* Truncate fade out duration if necessary. */
819 if (crossfade_rem
< crossfade_need
)
821 size_t crossfade_short
= crossfade_need
- crossfade_rem
;
822 if (fade_out_rem
>= crossfade_short
)
823 fade_out_rem
-= crossfade_short
;
826 fade_out_delay
-= crossfade_short
- fade_out_rem
;
830 crossfade_rem
-= fade_out_delay
+ fade_out_rem
;
832 /* Completely process the crossfade fade-out effect with current PCM buffer */
833 if (!crossfade_mixmode
)
835 /* Fade out the specified amount of the already processed audio */
836 size_t total_fade_out
= fade_out_rem
;
837 size_t fade_out_sample
;
838 struct chunkdesc
*fade_out_chunk
= crossfade_chunk
;
840 /* Find the right chunk and sample to start fading out */
841 fade_out_delay
+= crossfade_sample
* 2;
842 fade_out_sample
= find_chunk(fade_out_delay
, &fade_out_chunk
);
844 while (fade_out_rem
> 0)
846 /* Each 1/10 second of audio will have the same fade applied */
847 size_t block_rem
= MIN(BYTERATE
/ 10, fade_out_rem
);
848 int factor
= (fade_out_rem
<< 8) / total_fade_out
;
850 fade_out_rem
-= block_rem
;
852 crossfade_mix_fade(factor
, block_rem
, NULL
,
853 &fade_out_sample
, &fade_out_chunk
);
856 /* zero out the rest of the buffer */
857 crossfade_mix_fade(0, crossfade_rem
, NULL
,
858 &fade_out_sample
, &fade_out_chunk
);
861 /* Initialize fade-in counters */
862 crossfade_fade_in_total
= global_settings
.crossfade_fade_in_duration
* BYTERATE
;
863 crossfade_fade_in_rem
= crossfade_fade_in_total
;
865 fade_in_delay
= global_settings
.crossfade_fade_in_delay
* BYTERATE
;
867 /* Find the right chunk and sample to start fading in */
868 fade_in_delay
+= crossfade_sample
* 2;
869 crossfade_sample
= find_chunk(fade_in_delay
, &crossfade_chunk
);
870 logf("crossfade_start done!");
873 /* Perform fade-in of new track */
874 static void write_to_crossfade(size_t length
)
879 if (crossfade_fade_in_rem
)
884 /* Fade factor for this packet */
886 ((crossfade_fade_in_total
- crossfade_fade_in_rem
) << 8) /
887 crossfade_fade_in_total
;
889 size_t fade_rem
= MIN(length
, crossfade_fade_in_rem
);
891 /* We _will_ fade this many bytes */
892 crossfade_fade_in_rem
-= fade_rem
;
897 size_t fade_total
= fade_rem
;
898 fade_rem
= crossfade_mix_fade(factor
, fade_rem
, buf
,
899 &crossfade_sample
, &crossfade_chunk
);
900 length
-= fade_total
- fade_rem
;
901 buf
+= fade_total
- fade_rem
;
906 samples
= fade_rem
/ 2;
907 input_buf
= (int16_t *)buf
;
908 /* Fade remaining samples in place */
911 int32_t sample
= *input_buf
;
912 *input_buf
++ = (sample
* factor
) >> 8;
919 size_t mix_total
= length
;
920 /* A factor of 256 means mix only, no fading */
921 length
= crossfade_mix_fade(256, length
, buf
,
922 &crossfade_sample
, &crossfade_chunk
);
923 buf
+= mix_total
- length
;
928 /* Commit samples to the buffer */
929 while (!prepare_insert(length
))
934 size_t pcmbuffer_index
= pcmbuffer_pos
+ pcmbuffer_fillpos
;
935 size_t copy_n
= MIN(length
, pcmbuf_size
- pcmbuffer_index
);
936 memcpy(&pcmbuffer
[pcmbuffer_index
], buf
, copy_n
);
938 pcmbuffer_fillpos
+= copy_n
;
942 /* if no more fading-in to do, stop the crossfade */
943 if (!(crossfade_fade_in_rem
|| crossfade_chunk
))
944 crossfade_active
= false;
947 static void pcmbuf_finish_crossfade_enable(void)
949 /* Copy the pending setting over now */
950 crossfade_enabled
= crossfade_enable_request
;
952 pcmbuf_watermark
= (crossfade_enabled
&& pcmbuf_size
) ?
953 /* If crossfading, try to keep the buffer full other than 1 second */
954 (pcmbuf_size
- BYTERATE
) :
955 /* Otherwise, just use the default */
959 bool pcmbuf_is_crossfade_active(void)
961 return crossfade_active
|| crossfade_track_change_started
;
964 void pcmbuf_request_crossfade_enable(bool on_off
)
966 /* Next setting to be used, not applied now */
967 crossfade_enable_request
= on_off
;
970 bool pcmbuf_is_same_size(void)
972 /* if pcmbuffer is NULL, then not set up yet even once so always */
973 bool same_size
= pcmbuffer
?
974 (get_next_required_pcmbuf_size() == pcmbuf_size
) : true;
976 /* no buffer change needed, so finish crossfade setup now */
978 pcmbuf_finish_crossfade_enable();
982 #endif /* HAVE_CROSSFADE */
987 /* Returns pcm buffer usage in percents (0 to 100). */
988 static int pcmbuf_usage(void)
990 return pcmbuf_unplayed_bytes
* 100 / pcmbuf_size
;
993 static int pcmbuf_mix_free(void)
998 (size_t)&((int16_t *)mix_chunk
->addr
)[pcmbuf_mix_sample
];
999 size_t my_write_pos
= (size_t)&pcmbuffer
[pcmbuffer_pos
];
1000 if (my_write_pos
< my_mix_end
)
1001 my_write_pos
+= pcmbuf_size
;
1002 return (my_write_pos
- my_mix_end
) * 100 / pcmbuf_unplayed_bytes
;
1007 void *pcmbuf_request_voice_buffer(int *count
)
1009 /* A get-it-to-work-for-now hack (audio status could change by
1011 if (audio_status() & AUDIO_STATUS_PLAY
)
1013 if (read_chunk
== NULL
)
1017 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
1018 (mix_chunk
|| read_chunk
->link
))
1020 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
1030 return pcmbuf_request_buffer(count
);
1034 void pcmbuf_write_voice_complete(int count
)
1036 /* A get-it-to-work-for-now hack (audio status could have changed) */
1037 if (!(audio_status() & AUDIO_STATUS_PLAY
))
1039 pcmbuf_write_complete(count
);
1043 int16_t *ibuf
= (int16_t *)voicebuf
;
1045 size_t chunk_samples
;
1047 if (mix_chunk
== NULL
&& read_chunk
!= NULL
)
1049 mix_chunk
= read_chunk
->link
;
1050 /* Start 1/8s into the next chunk */
1051 pcmbuf_mix_sample
= BYTERATE
/ 16;
1057 obuf
= (int16_t *)mix_chunk
->addr
;
1058 chunk_samples
= mix_chunk
->size
/ sizeof (int16_t);
1064 int32_t sample
= *ibuf
++;
1066 if (pcmbuf_mix_sample
>= chunk_samples
)
1068 mix_chunk
= mix_chunk
->link
;
1071 pcmbuf_mix_sample
= 0;
1072 obuf
= mix_chunk
->addr
;
1073 chunk_samples
= mix_chunk
->size
/ 2;
1075 sample
+= obuf
[pcmbuf_mix_sample
] >> 2;
1076 obuf
[pcmbuf_mix_sample
++] = clip_sample_16(sample
);
1081 /** Debug menu, other metrics */
1083 /* Amount of bytes left in the buffer. */
1084 size_t pcmbuf_free(void)
1086 if (read_chunk
!= NULL
)
1088 void *read
= read_chunk
->addr
;
1089 void *write
= &pcmbuffer
[pcmbuffer_pos
+ pcmbuffer_fillpos
];
1091 return (size_t)(read
- write
) + pcmbuf_size
;
1093 return (size_t) (read
- write
);
1095 return pcmbuf_size
- pcmbuffer_fillpos
;
1098 size_t pcmbuf_get_bufsize(void)
1103 int pcmbuf_used_descs(void)
1105 struct chunkdesc
*temp
= read_chunk
;
1114 int pcmbuf_descs(void)
1116 return NUM_CHUNK_DESCS(pcmbuf_size
);
1119 #ifdef ROCKBOX_HAS_LOGF
1120 unsigned char *pcmbuf_get_meminfo(size_t *length
)
1122 *length
= pcmbuf_bufend
- pcmbuffer
;
1130 bool pcmbuf_is_lowdata(void)
1132 if (!pcm_is_playing() || pcm_is_paused()
1133 #ifdef HAVE_CROSSFADE
1134 || pcmbuf_is_crossfade_active()
1140 /* 1 seconds of buffer is low data */
1143 /* under watermark is low data */
1144 return (pcmbuf_unplayed_bytes
< pcmbuf_watermark
);
1148 void pcmbuf_set_low_latency(bool state
)
1150 low_latency_mode
= state
;
1153 unsigned long pcmbuf_get_latency(void)
1155 return (pcmbuf_unplayed_bytes
+ pcm_get_bytes_waiting()) * 1000 / BYTERATE
;
1158 #ifndef HAVE_HARDWARE_BEEP
1159 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
1160 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
1162 /* Generates a constant square wave sound with a given frequency
1163 in Hertz for a duration in milliseconds. */
1164 void pcmbuf_beep(unsigned int frequency
, size_t duration
, int amplitude
)
1166 unsigned int step
= 0xffffffffu
/ NATIVE_FREQUENCY
* frequency
;
1168 int16_t *bufptr
, *bufstart
, *bufend
;
1170 int nsamples
= NATIVE_FREQUENCY
/ 1000 * duration
;
1171 bool mix
= read_chunk
!= NULL
&& read_chunk
->link
!= NULL
;
1174 bufend
= SKIPBYTES((int16_t *)pcmbuffer
, pcmbuf_size
);
1176 /* Find the insertion point and set bufstart to the start of it */
1179 /* Get the currently playing chunk at the current position. */
1180 bufstart
= (int16_t *)pcm_play_dma_get_peak_buffer(&i
);
1182 /* If above isn't implemented or pcm is stopped, no beepeth. */
1183 if (!bufstart
|| !pcm_is_playing())
1186 /* Give 5ms clearance. */
1187 bufstart
+= BYTERATE
/ 200;
1189 #ifdef HAVE_PCM_DMA_ADDRESS
1190 /* Returned peak addresses are DMA addresses */
1191 bufend
= pcm_dma_addr(bufend
);
1194 /* Wrapped above? */
1195 if (bufstart
>= bufend
)
1196 bufstart
-= pcmbuf_size
;
1198 /* NOTE: On some targets using hardware DMA, cache range flushing may
1199 * be required or the writes may not be picked up by the controller.
1200 * An incremental flush should be done periodically during the mixdown. */
1202 else if (nsamples
<= MINIBUF_SAMPLES
)
1204 static int16_t minibuf
[MINIBUF_SAMPLES
*2] __attribute__((aligned(4)));
1205 /* Use mini buffer */
1207 bufend
= SKIPBYTES(bufstart
, MINIBUF_SIZE
);
1209 else if (!audio_buffer_state_trashed())
1212 bufstart
= (int16_t *)pcmbuffer
;
1222 /* Mix square wave into buffer */
1223 for (i
= 0; i
< nsamples
; ++i
)
1225 int32_t amp
= (phase
>> 31) ^ (int32_t)amplitude
;
1226 sample
= mix
? *bufptr
: 0;
1227 *bufptr
++ = clip_sample_16(sample
+ amp
);
1228 if (bufptr
>= bufend
)
1229 bufptr
= (int16_t *)pcmbuffer
;
1230 sample
= mix
? *bufptr
: 0;
1231 *bufptr
++ = clip_sample_16(sample
+ amp
);
1232 if (bufptr
>= bufend
)
1233 bufptr
= (int16_t *)pcmbuffer
;
1239 #ifdef HAVE_RECORDING
1243 /* Kick off playback if required and it won't interfere */
1244 if (!pcm_is_playing()
1245 #ifdef HAVE_RECORDING
1246 && !pcm_is_recording()
1250 pcm_play_data(NULL
, (unsigned char *)bufstart
, nsamples
* 4);
1254 #ifdef HAVE_RECORDING
1258 #endif /* HAVE_HARDWARE_BEEP */