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) */
54 /* Keep watermark high for iPods at least (2s) */
55 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
57 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
60 /* Structure we can use to queue pcm chunks in memory to be played
61 * by the driver code. */
66 struct chunkdesc
* link
;
67 /* true if last chunk in the track */
71 #define CHUNK_DESCS(bufsize) \
72 ((bufsize) / PCMBUF_MINAVG_CHUNK)
73 #define CHUNK_DESCS_SIZE(bufsize) \
74 (CHUNK_DESCS(bufsize)*sizeof(struct chunkdesc))
76 /* Size of the PCM buffer. */
77 static size_t pcmbuf_size IDATA_ATTR
= 0;
78 static char *pcmbuf_bufend IDATA_ATTR
;
79 static char *pcmbuffer IDATA_ATTR
;
80 /* Current PCM buffer write index. */
81 static size_t pcmbuffer_pos IDATA_ATTR
;
82 /* Amount pcmbuffer_pos will be increased.*/
83 static size_t pcmbuffer_fillpos IDATA_ATTR
;
84 static char *fadebuf IDATA_ATTR
;
85 static char *voicebuf IDATA_ATTR
;
87 static bool end_of_track IDATA_ATTR
;
88 bool track_transition IDATA_ATTR
;
90 /* Crossfade related state */
91 static bool crossfade_enabled
;
92 static bool crossfade_enable_request
;
93 static bool crossfade_mixmode
;
94 static bool crossfade_active IDATA_ATTR
;
95 static bool crossfade_track_change_started IDATA_ATTR
;
97 /* Track the current location for processing crossfade */
98 static struct chunkdesc
*crossfade_chunk IDATA_ATTR
;
100 static size_t crossfade_sample IDATA_ATTR
;
102 /* Counters for fading in new data */
103 static size_t crossfade_fade_in_total IDATA_ATTR
;
104 static size_t crossfade_fade_in_rem IDATA_ATTR
;
107 static struct chunkdesc
*read_chunk IDATA_ATTR
;
108 static struct chunkdesc
*read_end_chunk IDATA_ATTR
;
109 static struct chunkdesc
*write_chunk IDATA_ATTR
;
110 static struct chunkdesc
*write_end_chunk IDATA_ATTR
;
111 static size_t last_chunksize IDATA_ATTR
;
113 static size_t pcmbuf_unplayed_bytes IDATA_ATTR
;
114 static size_t pcmbuf_watermark IDATA_ATTR
;
116 static struct chunkdesc
*mix_chunk IDATA_ATTR
;
117 static size_t pcmbuf_mix_sample IDATA_ATTR
;
119 static bool low_latency_mode
= false;
120 static bool flush_pcmbuf
;
122 #ifdef HAVE_PRIORITY_SCHEDULING
123 static int codec_thread_priority
= PRIORITY_PLAYBACK
;
126 extern unsigned int codec_thread_id
;
128 /* Helpful macros for use in conditionals this assumes some of the above
129 * static variable names */
130 #define NEED_FLUSH(position) \
131 (pcmbuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
132 #define LOW_DATA(quarter_secs) \
133 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
135 static void finish_gapless_track_change(void);
136 #ifdef HAVE_CROSSFADE
137 static void crossfade_start(void);
138 static void flush_crossfade(char *buf
, size_t length
);
140 static void pcmbuf_finish_crossfade_enable(void);
141 static bool pcmbuf_is_crossfade_enabled(void);
144 /**************************************/
146 /* define this to show detailed chunkdesc usage information on the sim console */
147 /*#define DESC_DEBUG*/
153 static struct chunkdesc
*first_desc
;
154 static bool show_desc_in_use
= false;
155 #define DISPLAY_DESC(caller) while(!show_desc(caller))
156 #define DESC_IDX(desc) (desc ? desc - first_desc : -1)
157 #define DESCL_IDX(desc) (desc && desc->link ? desc->link - first_desc : -1)
158 #define SHOW_1ST(desc) if(DESC_IDX (desc)==-1) DEBUGF(" -- "); \
159 else DEBUGF(" %02d ", DESC_IDX(desc))
160 #define SHOW_2ND(desc) if(DESCL_IDX(desc)==-1) DEBUGF("l -- "); \
161 else DEBUGF("l %02d ", DESCL_IDX(desc))
162 #define DESC_SHOW(tag, desc) DEBUGF(tag);SHOW_1ST(desc); \
163 DEBUGF(tag);SHOW_2ND(desc)
165 static bool show_desc(char *caller
)
167 if (show_desc_in_use
) return false;
168 show_desc_in_use
= true;
169 DEBUGF("%-14s\t", caller
);
170 DESC_SHOW("r", read_chunk
);
171 DESC_SHOW("re", read_end_chunk
);
173 DESC_SHOW("w", write_chunk
);
174 DESC_SHOW("we", write_end_chunk
);
176 show_desc_in_use
= false;
180 #define DISPLAY_DESC(caller) do{}while(0)
184 /* Commit PCM data */
187 * Commit samples waiting to the pcm buffer.
189 static void commit_chunk(void)
191 if (!pcmbuffer_fillpos
)
194 /* Never use the last buffer descriptor */
195 while (write_chunk
== write_end_chunk
) {
196 /* If this happens, something is being stupid */
197 if (!pcm_is_playing()) {
198 logf("commit_chunk error");
201 /* Let approximately one chunk of data playback */
202 sleep(HZ
*PCMBUF_TARGET_CHUNK
/(NATIVE_FREQUENCY
*4));
205 /* commit the chunk */
207 register size_t size
= pcmbuffer_fillpos
;
208 /* Grab the next description to write, and change the write pointer */
209 register struct chunkdesc
*pcmbuf_current
= write_chunk
;
210 write_chunk
= pcmbuf_current
->link
;
211 /* Fill in the values in the new buffer chunk */
212 pcmbuf_current
->addr
= &pcmbuffer
[pcmbuffer_pos
];
213 pcmbuf_current
->size
= size
;
214 pcmbuf_current
->end_of_track
= end_of_track
;
215 pcmbuf_current
->link
= NULL
;
216 end_of_track
= false; /* This is single use only */
218 if (read_chunk
!= NULL
)
222 /* flush! discard all data after the currently playing chunk,
223 and make the current chunk play next */
224 write_end_chunk
->link
= read_chunk
->link
;
225 read_chunk
->link
= pcmbuf_current
;
226 while (write_end_chunk
->link
)
228 write_end_chunk
= write_end_chunk
->link
;
229 pcmbuf_unplayed_bytes
-= write_end_chunk
->size
;
231 flush_pcmbuf
= false;
233 /* If there is already a read buffer setup, add to it */
235 read_end_chunk
->link
= pcmbuf_current
;
239 /* Otherwise create the buffer */
240 read_chunk
= pcmbuf_current
;
243 /* This is now the last buffer to read */
244 read_end_chunk
= pcmbuf_current
;
246 /* Update bytes counters */
247 pcmbuf_unplayed_bytes
+= size
;
249 pcmbuffer_pos
+= size
;
250 if (pcmbuffer_pos
>= pcmbuf_size
)
251 pcmbuffer_pos
-= pcmbuf_size
;
253 pcmbuffer_fillpos
= 0;
254 DISPLAY_DESC("commit_chunk");
257 #ifdef HAVE_PRIORITY_SCHEDULING
258 static void boost_codec_thread(bool boost
)
260 /* Keep voice and codec threads at the same priority or else voice
261 * will starve if the codec thread's priority is boosted. */
264 int priority
= (PRIORITY_PLAYBACK
- PRIORITY_PLAYBACK_MAX
)*pcmbuf_unplayed_bytes
265 / (2*NATIVE_FREQUENCY
) + PRIORITY_PLAYBACK_MAX
;
267 if (priority
!= codec_thread_priority
)
269 codec_thread_priority
= priority
;
270 thread_set_priority(codec_thread_id
, priority
);
271 voice_thread_set_priority(priority
);
274 else if (codec_thread_priority
!= PRIORITY_PLAYBACK
)
276 thread_set_priority(codec_thread_id
, PRIORITY_PLAYBACK
);
277 voice_thread_set_priority(PRIORITY_PLAYBACK
);
278 codec_thread_priority
= PRIORITY_PLAYBACK
;
282 #define boost_codec_thread(boost) do{}while(0)
283 #endif /* HAVE_PRIORITY_SCHEDULING */
285 static bool prepare_insert(size_t length
)
287 if (low_latency_mode
)
290 if (!LOW_DATA(1) && pcm_is_playing())
294 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
295 if (pcmbuf_free() < length
+ PCMBUF_MIN_CHUNK
)
298 /* boost CPU if needed to either fill to watermark or for pre-buffer */
299 if (pcm_is_playing())
301 /* Only codec thread initiates boost - voice boosts the cpu when playing
304 if (thread_get_current() == codec_thread_id
)
305 #endif /* SIMULATOR */
307 if (pcmbuf_unplayed_bytes
<= pcmbuf_watermark
)
309 /* Fill PCM buffer by boosting cpu */
311 /* If buffer is critically low, override UI priority, else
312 set back to the original priority. */
313 boost_codec_thread(LOW_DATA(2));
317 boost_codec_thread(false);
321 /* Disable crossfade if < .5s of audio */
324 crossfade_active
= false;
327 else /* pcm_is_playing */
331 /* Pre-buffer up to watermark */
335 if (pcmbuf_unplayed_bytes
> pcmbuf_watermark
)
338 logf("pcm starting");
339 if (!(audio_status() & AUDIO_STATUS_PAUSE
))
347 void *pcmbuf_request_buffer(int *count
)
349 #ifdef HAVE_CROSSFADE
350 if (crossfade_track_change_started
)
354 if (crossfade_active
) {
355 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
360 if(prepare_insert(*count
<< 2))
362 size_t pcmbuffer_index
= pcmbuffer_pos
+ pcmbuffer_fillpos
;
363 if (pcmbuf_size
- pcmbuffer_index
>= PCMBUF_MIN_CHUNK
)
365 /* Usual case, there's space here */
366 return &pcmbuffer
[pcmbuffer_index
];
370 /* Flush and wrap the buffer */
373 return &pcmbuffer
[0];
380 void pcmbuf_write_complete(int count
)
382 size_t length
= (size_t)(unsigned int)count
<< 2;
383 #ifdef HAVE_CROSSFADE
384 if (crossfade_active
)
386 flush_crossfade(fadebuf
, length
);
387 if (!(crossfade_fade_in_rem
|| crossfade_chunk
))
388 crossfade_active
= false;
393 pcmbuffer_fillpos
+= length
;
395 if (NEED_FLUSH(pcmbuffer_pos
+ pcmbuffer_fillpos
))
403 static inline void init_pcmbuffers(void)
406 first_desc
= write_chunk
;
408 struct chunkdesc
*next
= write_chunk
;
410 write_end_chunk
= write_chunk
;
411 while ((void *)next
< (void *)pcmbuf_bufend
) {
412 write_end_chunk
->link
=next
;
413 write_end_chunk
=next
;
416 DISPLAY_DESC("init");
419 static size_t get_next_required_pcmbuf_size(void)
423 if (crossfade_enable_request
)
424 seconds
+= global_settings
.crossfade_fade_out_delay
425 + global_settings
.crossfade_fade_out_duration
;
428 /* Buffer has to be at least 2s long. */
431 logf("pcmbuf len: %ld", (long)seconds
);
432 return seconds
* (NATIVE_FREQUENCY
*4); /* 2 channels + 2 bytes/sample */
435 static char *pcmbuf_calc_pcmbuffer_ptr(size_t bufsize
)
437 return pcmbuf_bufend
- (bufsize
+ PCMBUF_MIX_CHUNK
* 2 +
438 CHUNK_DESCS_SIZE(bufsize
));
441 /* Initialize the pcmbuffer the structure looks like this:
442 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
443 size_t pcmbuf_init(unsigned char *bufend
)
445 pcmbuf_bufend
= bufend
;
446 pcmbuf_size
= get_next_required_pcmbuf_size();
447 pcmbuffer
= pcmbuf_calc_pcmbuffer_ptr(pcmbuf_size
);
448 fadebuf
= &pcmbuffer
[pcmbuf_size
];
449 voicebuf
= &fadebuf
[PCMBUF_MIX_CHUNK
];
450 write_chunk
= (struct chunkdesc
*)&voicebuf
[PCMBUF_MIX_CHUNK
];
454 if(track_transition
){logf("pcmbuf: (init) track transition false");}
455 end_of_track
= false;
456 track_transition
= false;
458 pcmbuf_finish_crossfade_enable();
462 return pcmbuf_bufend
- pcmbuffer
;
468 /** PCM driver callback
469 * This function has 3 major logical parts (separated by brackets both for
470 * readability and variable scoping). The first part performs the
471 * operations related to finishing off the last buffer we fed to the DMA.
472 * The second part detects the end of playlist condition when the pcm
473 * buffer is empty except for uncommitted samples. Then they are committed.
474 * The third part performs the operations involved in sending a new buffer
476 static void pcmbuf_pcm_callback(unsigned char** start
, size_t* size
) ICODE_ATTR
;
477 static void pcmbuf_pcm_callback(unsigned char** start
, size_t* size
)
480 struct chunkdesc
*pcmbuf_current
= read_chunk
;
481 /* Take the finished buffer out of circulation */
482 read_chunk
= pcmbuf_current
->link
;
484 /* if during a track transition, update the elapsed time */
485 if (track_transition
)
486 audio_pcmbuf_position_callback(last_chunksize
);
488 /* if last buffer in the track, let the audio thread know */
489 if (pcmbuf_current
->end_of_track
)
490 finish_gapless_track_change();
492 /* Put the finished buffer back into circulation */
493 write_end_chunk
->link
= pcmbuf_current
;
494 write_end_chunk
= pcmbuf_current
;
496 /* If we've read over the mix chunk while it's still mixing there */
497 if (pcmbuf_current
== mix_chunk
)
499 /* If we've read over the crossfade chunk while it's still fading */
500 if (pcmbuf_current
== crossfade_chunk
)
501 crossfade_chunk
= read_chunk
;
505 /* Commit last samples at end of playlist */
506 if (pcmbuffer_fillpos
&& !read_chunk
)
508 logf("pcmbuf_pcm_callback: commit last samples");
514 /* Send the new buffer to the pcm */
515 struct chunkdesc
*pcmbuf_new
= read_chunk
;
516 size_t *realsize
= size
;
517 unsigned char** realstart
= start
;
520 size_t current_size
= pcmbuf_new
->size
;
522 pcmbuf_unplayed_bytes
-= current_size
;
523 last_chunksize
= current_size
;
524 *realsize
= current_size
;
525 *realstart
= pcmbuf_new
->addr
;
529 /* No more buffers */
534 finish_gapless_track_change();
537 DISPLAY_DESC("callback");
540 /* Force playback. */
541 void pcmbuf_play_start(void)
543 if (!pcm_is_playing() && pcmbuf_unplayed_bytes
&& read_chunk
!= NULL
)
545 last_chunksize
= read_chunk
->size
;
546 pcmbuf_unplayed_bytes
-= last_chunksize
;
547 pcm_play_data(pcmbuf_pcm_callback
,
548 (unsigned char *)read_chunk
->addr
, last_chunksize
);
552 void pcmbuf_play_stop(void)
556 pcmbuf_unplayed_bytes
= 0;
559 write_end_chunk
->link
= read_chunk
;
560 write_end_chunk
= read_end_chunk
;
561 read_chunk
= read_end_chunk
= NULL
;
564 pcmbuffer_fillpos
= 0;
565 crossfade_track_change_started
= false;
566 crossfade_active
= false;
567 flush_pcmbuf
= false;
568 DISPLAY_DESC("play_stop");
570 /* Can unboost the codec thread here no matter who's calling */
571 boost_codec_thread(false);
574 void pcmbuf_pause(bool pause
)
576 if (pcm_is_playing())
577 pcm_play_pause(!pause
);
585 /* The codec is moving on to the next track, but the current track is
586 * still playing. Set flags to make sure the elapsed time of the current
587 * track is updated properly, and mark the currently written chunk as the
588 * last one in the track. */
589 static void start_gapless_track_change(void)
591 /* we're starting a track transition */
592 track_transition
= true;
594 /* mark the last chunk in the track */
598 static void start_crossfade_track_change(bool auto_skip
)
600 /* Notify the wps that the track change starts now */
601 audio_post_track_change(false);
603 /* Can't do two crossfades at once and, no fade if pcm is off now */
604 if (pcmbuf_is_crossfade_active() || !pcm_is_playing())
612 /* Not enough data, or crossfade disabled, flush the old data instead */
613 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode
)
615 /* commit everything to this point and keep going, but... */
617 /* ... when the next chunk commits, throw away everything but itself */
622 /* Don't enable mix mode when skipping tracks manually. */
623 crossfade_mixmode
= auto_skip
&& global_settings
.crossfade_fade_out_mixmode
;
625 crossfade_track_change_started
= true;
628 void pcmbuf_start_track_change(bool auto_skip
)
630 /* Manual track change (always crossfade or flush audio). */
632 start_crossfade_track_change(false);
634 /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */
635 else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active()
636 && global_settings
.crossfade
!= CROSSFADE_ENABLE_TRACKSKIP
)
638 if (global_settings
.crossfade
== CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP
)
640 if (global_settings
.playlist_shuffle
)
641 /* shuffle mode is on, so crossfade: */
642 start_crossfade_track_change(true);
644 /* shuffle mode is off, so normal gapless playback */
645 start_gapless_track_change();
648 /* normal crossfade: */
649 start_crossfade_track_change(true);
652 /* normal gapless playback. */
653 start_gapless_track_change();
656 /* Called when the last chunk in the track has been played */
657 static void finish_gapless_track_change(void)
659 /* not in a track transition anymore */
660 if(track_transition
){logf("pcmbuf: (finish change) track transition false");}
661 track_transition
= false;
663 /* notify playback that the track has just finished */
664 audio_post_track_change(true);
670 /* Clip sample to signed 16 bit range */
671 static inline int32_t clip_sample_16(int32_t sample
)
673 if ((int16_t)sample
!= sample
)
674 sample
= 0x7fff ^ (sample
>> 31);
679 * Low memory targets don't have crossfade, so don't compile crossfade
680 * specific code in order to save some memory. */
682 #ifdef HAVE_CROSSFADE
684 * Completely process the crossfade fade out effect with current pcm buffer.
686 static void crossfade_process_buffer(size_t fade_in_delay
,
687 size_t fade_out_delay
, size_t fade_out_rem
)
689 if (!crossfade_mixmode
)
691 /* Fade out the specified amount of the already processed audio */
692 size_t total_fade_out
= fade_out_rem
;
693 size_t fade_out_sample
;
694 struct chunkdesc
*fade_out_chunk
= crossfade_chunk
;
696 /* Find the right chunk to start fading out */
697 fade_out_delay
+= crossfade_sample
* 2;
698 while (fade_out_delay
!= 0 && fade_out_delay
>= fade_out_chunk
->size
)
700 fade_out_delay
-= fade_out_chunk
->size
;
701 fade_out_chunk
= fade_out_chunk
->link
;
703 /* The start sample within the chunk */
704 fade_out_sample
= fade_out_delay
/ 2;
706 while (fade_out_rem
> 0)
708 /* Each 1/10 second of audio will have the same fade applied */
709 size_t block_rem
= MIN(NATIVE_FREQUENCY
* 4 / 10, fade_out_rem
);
710 int factor
= (fade_out_rem
<< 8) / total_fade_out
;
712 fade_out_rem
-= block_rem
;
714 /* Fade this block */
715 while (block_rem
> 0 && fade_out_chunk
!= NULL
)
717 /* Fade one sample */
718 int16_t *buf
= (int16_t *)fade_out_chunk
->addr
;
719 int32_t sample
= buf
[fade_out_sample
];
720 buf
[fade_out_sample
++] = (sample
* factor
) >> 8;
723 /* Move to the next chunk as needed */
724 if (fade_out_sample
* 2 >= fade_out_chunk
->size
)
726 fade_out_chunk
= fade_out_chunk
->link
;
733 /* Find the right chunk and sample to start fading in */
734 fade_in_delay
+= crossfade_sample
* 2;
735 while (fade_in_delay
!= 0 && fade_in_delay
>= crossfade_chunk
->size
)
737 fade_in_delay
-= crossfade_chunk
->size
;
738 crossfade_chunk
= crossfade_chunk
->link
;
740 crossfade_sample
= fade_in_delay
/ 2;
741 logf("process done!");
744 /* Initializes crossfader, calculates all necessary parameters and
745 * performs fade-out with the pcm buffer. */
746 static void crossfade_start(void)
748 size_t crossfade_rem
;
749 size_t crossfade_need
;
751 size_t fade_out_delay
;
752 size_t fade_in_delay
;
754 crossfade_track_change_started
= false;
755 /* Reject crossfade if less than .5s of data */
757 logf("crossfade rejected");
762 logf("crossfade_start");
764 crossfade_active
= true;
766 /* Initialize the crossfade buffer size to all of the buffered data that
767 * has not yet been sent to the DMA */
768 crossfade_rem
= pcmbuf_unplayed_bytes
;
769 crossfade_chunk
= read_chunk
->link
;
770 crossfade_sample
= 0;
772 /* Get fade out delay from settings. */
774 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_out_delay
* 4;
776 /* Get fade out duration from settings. */
778 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_out_duration
* 4;
780 crossfade_need
= fade_out_delay
+ fade_out_rem
;
781 /* We want only to modify the last part of the buffer. */
782 if (crossfade_rem
> crossfade_need
)
784 size_t crossfade_extra
= crossfade_rem
- crossfade_need
;
785 while (crossfade_extra
> crossfade_chunk
->size
)
787 crossfade_extra
-= crossfade_chunk
->size
;
788 crossfade_chunk
= crossfade_chunk
->link
;
790 crossfade_sample
= crossfade_extra
/ 2;
792 /* Truncate fade out duration if necessary. */
793 else if (crossfade_rem
< crossfade_need
)
795 size_t crossfade_short
= crossfade_need
- crossfade_rem
;
796 if (fade_out_rem
>= crossfade_short
)
797 fade_out_rem
-= crossfade_short
;
800 fade_out_delay
-= crossfade_short
- fade_out_rem
;
805 /* Get also fade in duration and delays from settings. */
806 crossfade_fade_in_total
=
807 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_in_duration
* 4;
808 crossfade_fade_in_rem
= crossfade_fade_in_total
;
811 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_in_delay
* 4;
813 crossfade_process_buffer(fade_in_delay
, fade_out_delay
, fade_out_rem
);
816 /* Returns the number of bytes _NOT_ mixed */
817 static size_t crossfade_mix(int factor
, const char *buf
, size_t length
)
819 const int16_t *input_buf
= (const int16_t *)buf
;
820 int16_t *output_buf
= (int16_t *)(crossfade_chunk
->addr
);
821 int16_t *chunk_end
= SKIPBYTES(output_buf
, crossfade_chunk
->size
);
822 output_buf
= &output_buf
[crossfade_sample
];
827 /* fade left and right channel at once to keep buffer alignment */
829 for (i
= 0; i
< 2; i
++)
831 sample
= *input_buf
++;
832 sample
= ((sample
* factor
) >> 8) + *output_buf
;
833 *output_buf
++ = clip_sample_16(sample
);
836 length
-= 4; /* 2 samples, each 16 bit -> 4 bytes */
838 if (output_buf
>= chunk_end
)
840 crossfade_chunk
= crossfade_chunk
->link
;
841 if (!crossfade_chunk
)
843 output_buf
= (int16_t *)crossfade_chunk
->addr
;
844 chunk_end
= SKIPBYTES(output_buf
, crossfade_chunk
->size
);
847 crossfade_sample
= output_buf
- (int16_t *)crossfade_chunk
->addr
;
851 static void flush_crossfade(char *buf
, size_t length
)
855 if (crossfade_fade_in_rem
)
860 /* Fade factor for this packet */
862 ((crossfade_fade_in_total
- crossfade_fade_in_rem
) << 8) /
863 crossfade_fade_in_total
;
865 size_t fade_rem
= MIN(length
, crossfade_fade_in_rem
);
867 /* We _will_ fade this many bytes */
868 crossfade_fade_in_rem
-= fade_rem
;
873 size_t fade_total
= fade_rem
;
874 fade_rem
= crossfade_mix(factor
, buf
, fade_rem
);
875 length
-= fade_total
- fade_rem
;
876 buf
+= fade_total
- fade_rem
;
881 samples
= fade_rem
/ 2;
882 input_buf
= (int16_t *)buf
;
883 /* Fade remaining samples in place */
886 int32_t sample
= *input_buf
;
887 *input_buf
++ = (sample
* factor
) >> 8;
894 size_t mix_total
= length
;
895 length
= crossfade_mix(256, buf
, length
);
896 buf
+= mix_total
- length
;
901 /* Flush samples to the buffer */
902 while (!prepare_insert(length
))
906 size_t pcmbuffer_index
= pcmbuffer_pos
+ pcmbuffer_fillpos
;
907 if (NEED_FLUSH(pcmbuffer_index
))
910 pcmbuffer_index
= pcmbuffer_pos
+ pcmbuffer_fillpos
;
912 size_t copy_n
= MIN(length
, pcmbuf_size
- pcmbuffer_index
);
913 memcpy(&pcmbuffer
[pcmbuffer_index
], buf
, copy_n
);
915 pcmbuffer_fillpos
+= copy_n
;
920 #endif /* HAVE_CROSSFADE */
922 static void pcmbuf_finish_crossfade_enable(void)
924 /* Copy the pending setting over now */
925 crossfade_enabled
= crossfade_enable_request
;
927 pcmbuf_watermark
= (crossfade_enabled
&& pcmbuf_size
) ?
928 /* If crossfading, try to keep the buffer full other than 1 second */
929 (pcmbuf_size
- (NATIVE_FREQUENCY
* 4 * 1)) :
930 /* Otherwise, just use the default */
934 static bool pcmbuf_is_crossfade_enabled(void)
936 if (global_settings
.crossfade
== CROSSFADE_ENABLE_SHUFFLE
)
937 return global_settings
.playlist_shuffle
;
939 return crossfade_enabled
;
942 bool pcmbuf_is_crossfade_active(void)
944 return crossfade_active
|| crossfade_track_change_started
;
947 void pcmbuf_request_crossfade_enable(bool on_off
)
949 /* Next setting to be used, not applied now */
950 crossfade_enable_request
= on_off
;
953 bool pcmbuf_is_same_size(void)
955 /* if pcmbuffer is NULL, then not set up yet even once so always */
956 bool same_size
= pcmbuffer
?
957 (get_next_required_pcmbuf_size() == pcmbuf_size
) : true;
959 /* no buffer change needed, so finish crossfade setup now */
961 pcmbuf_finish_crossfade_enable();
969 /* Returns pcm buffer usage in percents (0 to 100). */
970 static int pcmbuf_usage(void)
972 return pcmbuf_unplayed_bytes
* 100 / pcmbuf_size
;
975 static int pcmbuf_mix_free(void)
980 (size_t)&((int16_t *)mix_chunk
->addr
)[pcmbuf_mix_sample
];
981 size_t my_write_pos
= (size_t)&pcmbuffer
[pcmbuffer_pos
];
982 if (my_write_pos
< my_mix_end
)
983 my_write_pos
+= pcmbuf_size
;
984 return (my_write_pos
- my_mix_end
) * 100 / pcmbuf_unplayed_bytes
;
989 void *pcmbuf_request_voice_buffer(int *count
)
991 /* A get-it-to-work-for-now hack (audio status could change by
993 if (audio_status() & AUDIO_STATUS_PLAY
)
995 if (read_chunk
== NULL
)
999 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
1000 (mix_chunk
|| read_chunk
->link
))
1002 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
1012 return pcmbuf_request_buffer(count
);
1016 void pcmbuf_write_voice_complete(int count
)
1018 /* A get-it-to-work-for-now hack (audio status could have changed) */
1019 if (!(audio_status() & AUDIO_STATUS_PLAY
))
1021 pcmbuf_write_complete(count
);
1025 int16_t *ibuf
= (int16_t *)voicebuf
;
1027 size_t chunk_samples
;
1029 if (mix_chunk
== NULL
&& read_chunk
!= NULL
)
1031 mix_chunk
= read_chunk
->link
;
1032 /* Start 1/8s into the next chunk */
1033 pcmbuf_mix_sample
= NATIVE_FREQUENCY
* 4 / 16;
1039 obuf
= (int16_t *)mix_chunk
->addr
;
1040 chunk_samples
= mix_chunk
->size
/ sizeof (int16_t);
1046 int32_t sample
= *ibuf
++;
1048 if (pcmbuf_mix_sample
>= chunk_samples
)
1050 mix_chunk
= mix_chunk
->link
;
1053 pcmbuf_mix_sample
= 0;
1054 obuf
= mix_chunk
->addr
;
1055 chunk_samples
= mix_chunk
->size
/ 2;
1057 sample
+= obuf
[pcmbuf_mix_sample
] >> 2;
1058 obuf
[pcmbuf_mix_sample
++] = clip_sample_16(sample
);
1063 /* Debug menu, other metrics */
1065 /* Amount of bytes left in the buffer. */
1066 size_t pcmbuf_free(void)
1068 if (read_chunk
!= NULL
)
1070 void *read
= read_chunk
->addr
;
1071 void *write
= &pcmbuffer
[pcmbuffer_pos
+ pcmbuffer_fillpos
];
1073 return (size_t)(read
- write
) + pcmbuf_size
;
1075 return (size_t) (read
- write
);
1080 size_t pcmbuf_get_bufsize(void)
1085 int pcmbuf_used_descs(void)
1087 struct chunkdesc
*temp
= read_chunk
;
1096 int pcmbuf_descs(void)
1098 return CHUNK_DESCS(pcmbuf_size
);
1101 #ifdef ROCKBOX_HAS_LOGF
1102 unsigned char * pcmbuf_get_meminfo(size_t *length
)
1104 *length
= pcmbuf_bufend
- pcmbuffer
;
1112 bool pcmbuf_is_lowdata(void)
1114 if (!pcm_is_playing() || pcm_is_paused() || pcmbuf_is_crossfade_active())
1118 /* 1 seconds of buffer is low data */
1121 /* under watermark is low data */
1122 return (pcmbuf_unplayed_bytes
< pcmbuf_watermark
);
1126 void pcmbuf_set_low_latency(bool state
)
1128 low_latency_mode
= state
;
1131 unsigned long pcmbuf_get_latency(void)
1133 /* Be careful how this calculation is rearranged, it's easy to overflow */
1134 size_t bytes
= pcmbuf_unplayed_bytes
+ pcm_get_bytes_waiting();
1135 return bytes
/ 4 * 1000 / NATIVE_FREQUENCY
;
1138 #ifndef HAVE_HARDWARE_BEEP
1139 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
1140 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
1142 /* Generates a constant square wave sound with a given frequency
1143 in Hertz for a duration in milliseconds. */
1144 void pcmbuf_beep(unsigned int frequency
, size_t duration
, int amplitude
)
1146 unsigned int step
= 0xffffffffu
/ NATIVE_FREQUENCY
* frequency
;
1148 int16_t *bufptr
, *bufstart
, *bufend
;
1150 int nsamples
= NATIVE_FREQUENCY
/ 1000 * duration
;
1151 bool mix
= read_chunk
!= NULL
&& read_chunk
->link
!= NULL
;
1154 bufend
= SKIPBYTES((int16_t *)pcmbuffer
, pcmbuf_size
);
1156 /* Find the insertion point and set bufstart to the start of it */
1159 /* Get the currently playing chunk at the current position. */
1160 bufstart
= (int16_t *)pcm_play_dma_get_peak_buffer(&i
);
1162 /* If above isn't implemented or pcm is stopped, no beepeth. */
1163 if (!bufstart
|| !pcm_is_playing())
1166 /* Give 5ms clearance. */
1167 bufstart
+= NATIVE_FREQUENCY
* 4 / 200;
1169 #ifdef HAVE_PCM_DMA_ADDRESS
1170 /* Returned peak addresses are DMA addresses */
1171 bufend
= pcm_dma_addr(bufend
);
1174 /* Wrapped above? */
1175 if (bufstart
>= bufend
)
1176 bufstart
-= pcmbuf_size
;
1178 /* NOTE: On some targets using hardware DMA, cache range flushing may
1179 * be required or the writes may not be picked up by the controller.
1180 * An incremental flush should be done periodically during the mixdown. */
1182 else if (nsamples
<= MINIBUF_SAMPLES
)
1184 static int16_t minibuf
[MINIBUF_SAMPLES
*2] __attribute__((aligned(4)));
1185 /* Use mini buffer */
1187 bufend
= SKIPBYTES(bufstart
, MINIBUF_SIZE
);
1189 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED
)
1192 bufstart
= (int16_t *)pcmbuffer
;
1202 /* Mix square wave into buffer */
1203 for (i
= 0; i
< nsamples
; ++i
)
1205 int32_t amp
= (phase
>> 31) ^ (int32_t)amplitude
;
1206 sample
= mix
? *bufptr
: 0;
1207 *bufptr
++ = clip_sample_16(sample
+ amp
);
1208 if (bufptr
>= bufend
)
1209 bufptr
= (int16_t *)pcmbuffer
;
1210 sample
= mix
? *bufptr
: 0;
1211 *bufptr
++ = clip_sample_16(sample
+ amp
);
1212 if (bufptr
>= bufend
)
1213 bufptr
= (int16_t *)pcmbuffer
;
1219 #ifdef HAVE_RECORDING
1223 /* Kick off playback if required and it won't interfere */
1224 if (!pcm_is_playing()
1225 #ifdef HAVE_RECORDING
1226 && !pcm_is_recording()
1230 pcm_play_data(NULL
, (unsigned char *)bufstart
, nsamples
* 4);
1234 #ifdef HAVE_RECORDING
1238 #endif /* HAVE_HARDWARE_BEEP */