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*/
40 #include "voice_thread.h"
44 /* Clip sample to signed 16 bit range */
45 static inline int32_t clip_sample_16(int32_t sample
)
47 if ((int16_t)sample
!= sample
)
48 sample
= 0x7fff ^ (sample
>> 31);
53 /* Keep watermark high for iPods at least (2s) */
54 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
56 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
59 /* Structure we can use to queue pcm chunks in memory to be played
60 * by the driver code. */
65 struct pcmbufdesc
* link
;
66 /* true if last chunk in the track */
70 #define PCMBUF_DESCS(bufsize) \
71 ((bufsize) / PCMBUF_MINAVG_CHUNK)
72 #define PCMBUF_DESCS_SIZE(bufsize) \
73 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
75 /* Size of the PCM buffer. */
76 static size_t pcmbuf_size IDATA_ATTR
= 0;
77 static char *pcmbuf_bufend IDATA_ATTR
;
78 static char *audiobuffer IDATA_ATTR
;
79 /* Current audio buffer write index. */
80 static size_t audiobuffer_pos IDATA_ATTR
;
81 /* Amount audiobuffer_pos will be increased.*/
82 static size_t audiobuffer_fillpos IDATA_ATTR
;
83 static char *fadebuf IDATA_ATTR
;
84 static char *voicebuf IDATA_ATTR
;
86 static bool end_of_track IDATA_ATTR
;
87 bool track_transition IDATA_ATTR
;
89 /* Crossfade related state */
90 static bool crossfade_enabled
;
91 static bool crossfade_enabled_pending
;
92 static bool crossfade_mixmode
;
93 static bool crossfade_active IDATA_ATTR
;
94 static bool crossfade_init IDATA_ATTR
;
96 /* Track the current location for processing crossfade */
97 static struct pcmbufdesc
*crossfade_chunk IDATA_ATTR
;
99 static size_t crossfade_sample IDATA_ATTR
;
101 /* Counters for fading in new data */
102 static size_t crossfade_fade_in_total IDATA_ATTR
;
103 static size_t crossfade_fade_in_rem IDATA_ATTR
;
106 static struct pcmbufdesc
*pcmbuf_read IDATA_ATTR
;
107 static struct pcmbufdesc
*pcmbuf_read_end IDATA_ATTR
;
108 static struct pcmbufdesc
*pcmbuf_write IDATA_ATTR
;
109 static struct pcmbufdesc
*pcmbuf_write_end IDATA_ATTR
;
110 static size_t last_chunksize IDATA_ATTR
;
112 static size_t pcmbuf_unplayed_bytes IDATA_ATTR
;
113 static size_t pcmbuf_watermark IDATA_ATTR
;
115 static struct pcmbufdesc
*pcmbuf_mix_chunk IDATA_ATTR
;
116 static size_t pcmbuf_mix_sample IDATA_ATTR
;
118 static bool low_latency_mode
= false;
119 static bool pcmbuf_flush
;
121 #ifdef HAVE_PRIORITY_SCHEDULING
122 static int codec_thread_priority
= PRIORITY_PLAYBACK
;
125 extern unsigned int codec_thread_id
;
127 /* Helpful macros for use in conditionals this assumes some of the above
128 * static variable names */
129 #define NEED_FLUSH(position) \
130 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
131 #define LOW_DATA(quarter_secs) \
132 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
134 static bool prepare_insert(size_t length
);
135 static void pcmbuf_under_watermark(bool under
);
136 static bool pcmbuf_flush_fillpos(void);
139 /* Track change functions */
141 /* The codec is moving on to the next track, but the current track is
142 * still playing. Set flags to make sure the elapsed time of the current
143 * track is updated properly, and mark the currently written chunk as the
144 * last one in the track. */
145 void pcmbuf_start_track_change(void)
147 /* we're starting a track transition */
148 track_transition
= true;
150 /* mark the last chunk in the track */
154 /* Called when the last chunk in the track has been played */
155 static void pcmbuf_finish_track_change(void)
157 /* not in a track transition anymore */
158 if(track_transition
){logf("pcmbuf: (finish change) track transition false");}
159 track_transition
= false;
161 /* notify playback that the track has just finished */
162 audio_post_track_change();
166 /** PCM driver callback
167 * This function has 3 major logical parts (separated by brackets both for
168 * readability and variable scoping). The first part performs the
169 * operations related to finishing off the last buffer we fed to the DMA.
170 * The second part detects the end of playlist condition when the pcm
171 * buffer is empty except for uncommitted samples. Then they are committed.
172 * The third part performs the operations involved in sending a new buffer
174 static void pcmbuf_pcm_callback(unsigned char** start
, size_t* size
) ICODE_ATTR
;
175 static void pcmbuf_pcm_callback(unsigned char** start
, size_t* size
)
178 struct pcmbufdesc
*pcmbuf_current
= pcmbuf_read
;
179 /* Take the finished buffer out of circulation */
180 pcmbuf_read
= pcmbuf_current
->link
;
182 /* if during a track transition, update the elapsed time */
183 if (track_transition
)
184 audio_pcmbuf_position_callback(last_chunksize
);
186 /* if last buffer in the track, let the audio thread know */
187 if (pcmbuf_current
->end_of_track
)
188 pcmbuf_finish_track_change();
190 /* Put the finished buffer back into circulation */
191 pcmbuf_write_end
->link
= pcmbuf_current
;
192 pcmbuf_write_end
= pcmbuf_current
;
194 /* If we've read over the mix chunk while it's still mixing there */
195 if (pcmbuf_current
== pcmbuf_mix_chunk
)
196 pcmbuf_mix_chunk
= NULL
;
197 /* If we've read over the crossfade chunk while it's still fading */
198 if (pcmbuf_current
== crossfade_chunk
)
199 crossfade_chunk
= pcmbuf_read
;
203 /* Commit last samples at end of playlist */
204 if (audiobuffer_fillpos
&& !pcmbuf_read
)
206 logf("pcmbuf_pcm_callback: commit last samples");
207 pcmbuf_flush_fillpos();
212 /* Send the new buffer to the pcm */
213 struct pcmbufdesc
*pcmbuf_new
= pcmbuf_read
;
214 size_t *realsize
= size
;
215 unsigned char** realstart
= start
;
218 size_t current_size
= pcmbuf_new
->size
;
220 pcmbuf_unplayed_bytes
-= current_size
;
221 last_chunksize
= current_size
;
222 *realsize
= current_size
;
223 *realstart
= pcmbuf_new
->addr
;
227 /* No more buffers */
232 pcmbuf_finish_track_change();
237 static void pcmbuf_set_watermark_bytes(void)
239 pcmbuf_watermark
= (crossfade_enabled
&& pcmbuf_size
) ?
240 /* If crossfading, try to keep the buffer full other than 1 second */
241 (pcmbuf_size
- (NATIVE_FREQUENCY
* 4 * 1)) :
242 /* Otherwise, just use the default */
246 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
247 * in a separate function for the moment */
248 static inline void pcmbuf_add_chunk(void)
250 register size_t size
= audiobuffer_fillpos
;
251 /* Grab the next description to write, and change the write pointer */
252 register struct pcmbufdesc
*pcmbuf_current
= pcmbuf_write
;
253 pcmbuf_write
= pcmbuf_current
->link
;
254 /* Fill in the values in the new buffer chunk */
255 pcmbuf_current
->addr
= &audiobuffer
[audiobuffer_pos
];
256 pcmbuf_current
->size
= size
;
257 pcmbuf_current
->end_of_track
= end_of_track
;
258 pcmbuf_current
->link
= NULL
;
259 end_of_track
= false; /* This is single use only */
260 if (pcmbuf_read
!= NULL
) {
263 pcmbuf_write_end
->link
= pcmbuf_read
->link
;
264 pcmbuf_read
->link
= pcmbuf_current
;
265 while (pcmbuf_write_end
->link
)
267 pcmbuf_write_end
= pcmbuf_write_end
->link
;
268 pcmbuf_unplayed_bytes
-= pcmbuf_write_end
->size
;
270 pcmbuf_flush
= false;
272 /* If there is already a read buffer setup, add to it */
274 pcmbuf_read_end
->link
= pcmbuf_current
;
276 /* Otherwise create the buffer */
277 pcmbuf_read
= pcmbuf_current
;
279 /* This is now the last buffer to read */
280 pcmbuf_read_end
= pcmbuf_current
;
282 /* Update bytes counters */
283 pcmbuf_unplayed_bytes
+= size
;
285 audiobuffer_pos
+= size
;
286 if (audiobuffer_pos
>= pcmbuf_size
)
287 audiobuffer_pos
-= pcmbuf_size
;
289 audiobuffer_fillpos
= 0;
292 #ifdef HAVE_PRIORITY_SCHEDULING
293 static void boost_codec_thread(bool boost
)
295 /* Keep voice and codec threads at the same priority or else voice
296 * will starve if the codec thread's priority is boosted. */
299 int priority
= (PRIORITY_PLAYBACK
- PRIORITY_PLAYBACK_MAX
)*pcmbuf_unplayed_bytes
300 / (2*NATIVE_FREQUENCY
) + PRIORITY_PLAYBACK_MAX
;
302 if (priority
!= codec_thread_priority
)
304 codec_thread_priority
= priority
;
305 thread_set_priority(codec_thread_id
, priority
);
306 voice_thread_set_priority(priority
);
309 else if (codec_thread_priority
!= PRIORITY_PLAYBACK
)
311 thread_set_priority(codec_thread_id
, PRIORITY_PLAYBACK
);
312 voice_thread_set_priority(PRIORITY_PLAYBACK
);
313 codec_thread_priority
= PRIORITY_PLAYBACK
;
316 #endif /* HAVE_PRIORITY_SCHEDULING */
318 static void pcmbuf_under_watermark(bool under
)
320 /* Only codec thread initiates boost - voice boosts the cpu when playing
323 if (thread_get_current() == codec_thread_id
)
324 #endif /* SIMULATOR */
328 /* Fill audio buffer by boosting cpu */
330 #ifdef HAVE_PRIORITY_SCHEDULING
331 /* If buffer is critically low, override UI priority, else
332 set back to the original priority. */
333 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
338 #ifdef HAVE_PRIORITY_SCHEDULING
339 boost_codec_thread(false);
344 /* Disable crossfade if < .5s of audio */
347 crossfade_active
= false;
351 unsigned int pcmbuf_get_latency(void)
353 /* Be careful how this calculation is rearranged, it's easy to overflow */
354 size_t bytes
= pcmbuf_unplayed_bytes
+ pcm_get_bytes_waiting();
355 return bytes
/ 4 / (NATIVE_FREQUENCY
/1000);
358 void pcmbuf_set_low_latency(bool state
)
360 low_latency_mode
= state
;
363 bool pcmbuf_is_lowdata(void)
365 if (!pcm_is_playing() || pcm_is_paused() ||
366 crossfade_init
|| crossfade_active
)
370 /* 1 seconds of buffer is low data */
373 /* under watermark is low data */
374 return (pcmbuf_unplayed_bytes
< pcmbuf_watermark
);
378 /* Amount of bytes left in the buffer. */
379 inline size_t pcmbuf_free(void)
381 if (pcmbuf_read
!= NULL
)
383 void *read
= pcmbuf_read
->addr
;
384 void *write
= &audiobuffer
[audiobuffer_pos
+ audiobuffer_fillpos
];
386 return (size_t)(read
- write
) + pcmbuf_size
;
388 return (size_t) (read
- write
);
393 bool pcmbuf_crossfade_init(bool manual_skip
)
395 /* Can't do two crossfades at once and, no fade if pcm is off now */
396 if (crossfade_init
|| crossfade_active
|| !pcm_is_playing())
404 /* Not enough data, or crossfade disabled, flush the old data instead */
405 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode
)
407 pcmbuf_flush_fillpos();
412 /* Don't enable mix mode when skipping tracks manually. */
414 crossfade_mixmode
= false;
416 crossfade_mixmode
= global_settings
.crossfade_fade_out_mixmode
;
418 crossfade_init
= true;
424 void pcmbuf_play_stop(void)
428 pcmbuf_unplayed_bytes
= 0;
429 pcmbuf_mix_chunk
= NULL
;
431 pcmbuf_write_end
->link
= pcmbuf_read
;
432 pcmbuf_write_end
= pcmbuf_read_end
;
433 pcmbuf_read
= pcmbuf_read_end
= NULL
;
436 audiobuffer_fillpos
= 0;
437 crossfade_init
= false;
438 crossfade_active
= false;
439 pcmbuf_flush
= false;
441 #ifdef HAVE_PRIORITY_SCHEDULING
442 /* Can unboost the codec thread here no matter who's calling */
443 boost_codec_thread(false);
447 int pcmbuf_used_descs(void)
449 struct pcmbufdesc
*pcmbuf_temp
= pcmbuf_read
;
451 while (pcmbuf_temp
) {
452 pcmbuf_temp
= pcmbuf_temp
->link
;
458 int pcmbuf_descs(void)
460 return PCMBUF_DESCS(pcmbuf_size
);
463 static void pcmbuf_init_pcmbuffers(void)
465 struct pcmbufdesc
*next
= pcmbuf_write
;
467 pcmbuf_write_end
= pcmbuf_write
;
468 while ((void *)next
< (void *)pcmbuf_bufend
) {
469 pcmbuf_write_end
->link
=next
;
470 pcmbuf_write_end
=next
;
475 static size_t pcmbuf_get_next_required_pcmbuf_size(void)
479 if (crossfade_enabled_pending
)
480 seconds
+= global_settings
.crossfade_fade_out_delay
481 + global_settings
.crossfade_fade_out_duration
;
484 /* Buffer has to be at least 2s long. */
487 logf("pcmbuf len: %ld", (long)seconds
);
488 return seconds
* (NATIVE_FREQUENCY
*4); /* 2 channels + 2 bytes/sample */
491 static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize
)
493 return pcmbuf_bufend
- (bufsize
+ PCMBUF_MIX_CHUNK
* 2 +
494 PCMBUF_DESCS_SIZE(bufsize
));
497 bool pcmbuf_is_same_size(void)
499 if (audiobuffer
== NULL
)
500 return true; /* Not set up yet even once so always */
502 size_t bufsize
= pcmbuf_get_next_required_pcmbuf_size();
503 return pcmbuf_calc_audiobuffer_ptr(bufsize
) == audiobuffer
;
506 /* Initialize the pcmbuffer the structure looks like this:
507 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
508 size_t pcmbuf_init(unsigned char *bufend
)
510 pcmbuf_bufend
= bufend
;
511 pcmbuf_size
= pcmbuf_get_next_required_pcmbuf_size();
512 audiobuffer
= pcmbuf_calc_audiobuffer_ptr(pcmbuf_size
);
513 fadebuf
= &audiobuffer
[pcmbuf_size
];
514 voicebuf
= &fadebuf
[PCMBUF_MIX_CHUNK
];
515 pcmbuf_write
= (struct pcmbufdesc
*)&voicebuf
[PCMBUF_MIX_CHUNK
];
517 pcmbuf_init_pcmbuffers();
519 if(track_transition
){logf("pcmbuf: (init) track transition false");}
520 end_of_track
= false;
521 track_transition
= false;
523 pcmbuf_crossfade_enable_finished();
527 return pcmbuf_bufend
- audiobuffer
;
530 size_t pcmbuf_get_bufsize(void)
535 #ifdef ROCKBOX_HAS_LOGF
536 unsigned char * pcmbuf_get_meminfo(size_t *length
)
538 *length
= pcmbuf_bufend
- audiobuffer
;
543 void pcmbuf_pause(bool pause
)
545 if (pcm_is_playing())
546 pcm_play_pause(!pause
);
551 /* Force playback. */
552 void pcmbuf_play_start(void)
554 if (!pcm_is_playing() && pcmbuf_unplayed_bytes
&& pcmbuf_read
!= NULL
)
556 last_chunksize
= pcmbuf_read
->size
;
557 pcmbuf_unplayed_bytes
-= last_chunksize
;
558 pcm_play_data(pcmbuf_pcm_callback
,
559 (unsigned char *)pcmbuf_read
->addr
, last_chunksize
);
564 * Commit samples waiting to the pcm buffer.
566 static bool pcmbuf_flush_fillpos(void)
568 if (audiobuffer_fillpos
) {
569 /* Never use the last buffer descriptor */
570 while (pcmbuf_write
== pcmbuf_write_end
) {
571 /* If this happens, something is being stupid */
572 if (!pcm_is_playing()) {
573 logf("pcmbuf_flush_fillpos error");
576 /* Let approximately one chunk of data playback */
577 sleep(HZ
*PCMBUF_TARGET_CHUNK
/(NATIVE_FREQUENCY
*4));
586 * Low memory targets don't have crossfade, so don't compile crossfade
587 * specific code in order to save some memory. */
589 #ifdef HAVE_CROSSFADE
591 * Completely process the crossfade fade out effect with current pcm buffer.
593 static void crossfade_process_buffer(size_t fade_in_delay
,
594 size_t fade_out_delay
, size_t fade_out_rem
)
596 if (!crossfade_mixmode
)
598 /* Fade out the specified amount of the already processed audio */
599 size_t total_fade_out
= fade_out_rem
;
600 size_t fade_out_sample
;
601 struct pcmbufdesc
*fade_out_chunk
= crossfade_chunk
;
603 /* Find the right chunk to start fading out */
604 fade_out_delay
+= crossfade_sample
* 2;
605 while (fade_out_delay
!= 0 && fade_out_delay
>= fade_out_chunk
->size
)
607 fade_out_delay
-= fade_out_chunk
->size
;
608 fade_out_chunk
= fade_out_chunk
->link
;
610 /* The start sample within the chunk */
611 fade_out_sample
= fade_out_delay
/ 2;
613 while (fade_out_rem
> 0)
615 /* Each 1/10 second of audio will have the same fade applied */
616 size_t block_rem
= MIN(NATIVE_FREQUENCY
* 4 / 10, fade_out_rem
);
617 int factor
= (fade_out_rem
<< 8) / total_fade_out
;
619 fade_out_rem
-= block_rem
;
621 /* Fade this block */
622 while (block_rem
> 0 && fade_out_chunk
!= NULL
)
624 /* Fade one sample */
625 int16_t *buf
= (int16_t *)fade_out_chunk
->addr
;
626 int32_t sample
= buf
[fade_out_sample
];
627 buf
[fade_out_sample
++] = (sample
* factor
) >> 8;
630 /* Move to the next chunk as needed */
631 if (fade_out_sample
* 2 >= fade_out_chunk
->size
)
633 fade_out_chunk
= fade_out_chunk
->link
;
640 /* Find the right chunk and sample to start fading in */
641 fade_in_delay
+= crossfade_sample
* 2;
642 while (fade_in_delay
!= 0 && fade_in_delay
>= crossfade_chunk
->size
)
644 fade_in_delay
-= crossfade_chunk
->size
;
645 crossfade_chunk
= crossfade_chunk
->link
;
647 crossfade_sample
= fade_in_delay
/ 2;
648 logf("process done!");
651 /* Initializes crossfader, calculates all necessary parameters and
652 * performs fade-out with the pcm buffer. */
653 static void crossfade_start(void)
655 size_t crossfade_rem
;
656 size_t crossfade_need
;
658 size_t fade_out_delay
;
659 size_t fade_in_delay
;
661 crossfade_init
= false;
662 /* Reject crossfade if less than .5s of data */
664 logf("crossfade rejected");
669 logf("crossfade_start");
670 pcmbuf_flush_fillpos();
671 crossfade_active
= true;
673 /* Initialize the crossfade buffer size to all of the buffered data that
674 * has not yet been sent to the DMA */
675 crossfade_rem
= pcmbuf_unplayed_bytes
;
676 crossfade_chunk
= pcmbuf_read
->link
;
677 crossfade_sample
= 0;
679 /* Get fade out delay from settings. */
681 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_out_delay
* 4;
683 /* Get fade out duration from settings. */
685 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_out_duration
* 4;
687 crossfade_need
= fade_out_delay
+ fade_out_rem
;
688 /* We want only to modify the last part of the buffer. */
689 if (crossfade_rem
> crossfade_need
)
691 size_t crossfade_extra
= crossfade_rem
- crossfade_need
;
692 while (crossfade_extra
> crossfade_chunk
->size
)
694 crossfade_extra
-= crossfade_chunk
->size
;
695 crossfade_chunk
= crossfade_chunk
->link
;
697 crossfade_sample
= crossfade_extra
/ 2;
699 /* Truncate fade out duration if necessary. */
700 else if (crossfade_rem
< crossfade_need
)
702 size_t crossfade_short
= crossfade_need
- crossfade_rem
;
703 if (fade_out_rem
>= crossfade_short
)
704 fade_out_rem
-= crossfade_short
;
707 fade_out_delay
-= crossfade_short
- fade_out_rem
;
712 /* Get also fade in duration and delays from settings. */
713 crossfade_fade_in_total
=
714 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_in_duration
* 4;
715 crossfade_fade_in_rem
= crossfade_fade_in_total
;
718 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_in_delay
* 4;
720 crossfade_process_buffer(fade_in_delay
, fade_out_delay
, fade_out_rem
);
723 /* Returns the number of bytes _NOT_ mixed */
724 static size_t crossfade_mix(int factor
, const char *buf
, size_t length
)
726 const int16_t *input_buf
= (const int16_t *)buf
;
727 int16_t *output_buf
= (int16_t *)(crossfade_chunk
->addr
);
728 int16_t *chunk_end
= SKIPBYTES(output_buf
, crossfade_chunk
->size
);
729 output_buf
= &output_buf
[crossfade_sample
];
734 /* fade left and right channel at once to keep buffer alignment */
736 for (i
= 0; i
< 2; i
++)
738 sample
= *input_buf
++;
739 sample
= ((sample
* factor
) >> 8) + *output_buf
;
740 *output_buf
++ = clip_sample_16(sample
);
743 length
-= 4; /* 2 samples, each 16 bit -> 4 bytes */
745 if (output_buf
>= chunk_end
)
747 crossfade_chunk
= crossfade_chunk
->link
;
748 if (!crossfade_chunk
)
750 output_buf
= (int16_t *)crossfade_chunk
->addr
;
751 chunk_end
= SKIPBYTES(output_buf
, crossfade_chunk
->size
);
754 crossfade_sample
= output_buf
- (int16_t *)crossfade_chunk
->addr
;
758 static void pcmbuf_flush_buffer(const char *buf
, size_t length
)
762 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
763 if (NEED_FLUSH(audiobuffer_index
))
765 pcmbuf_flush_fillpos();
766 audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
768 copy_n
= MIN(length
, pcmbuf_size
- audiobuffer_index
);
769 memcpy(&audiobuffer
[audiobuffer_index
], buf
, copy_n
);
771 audiobuffer_fillpos
+= copy_n
;
776 static void flush_crossfade(char *buf
, size_t length
)
780 if (crossfade_fade_in_rem
)
785 /* Fade factor for this packet */
787 ((crossfade_fade_in_total
- crossfade_fade_in_rem
) << 8) /
788 crossfade_fade_in_total
;
790 size_t fade_rem
= MIN(length
, crossfade_fade_in_rem
);
792 /* We _will_ fade this many bytes */
793 crossfade_fade_in_rem
-= fade_rem
;
798 size_t fade_total
= fade_rem
;
799 fade_rem
= crossfade_mix(factor
, buf
, fade_rem
);
800 length
-= fade_total
- fade_rem
;
801 buf
+= fade_total
- fade_rem
;
806 samples
= fade_rem
/ 2;
807 input_buf
= (int16_t *)buf
;
808 /* Fade remaining samples in place */
811 int32_t sample
= *input_buf
;
812 *input_buf
++ = (sample
* factor
) >> 8;
819 size_t mix_total
= length
;
820 length
= crossfade_mix(256, buf
, length
);
821 buf
+= mix_total
- length
;
826 /* Flush samples to the buffer */
827 while (!prepare_insert(length
))
829 pcmbuf_flush_buffer(buf
, length
);
835 static bool prepare_insert(size_t length
)
837 if (low_latency_mode
)
840 if (!LOW_DATA(1) && pcm_is_playing())
844 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
845 if (pcmbuf_free() < length
+ PCMBUF_MIN_CHUNK
)
848 if (!pcm_is_playing())
852 /* Pre-buffer up to watermark */
856 if (pcmbuf_unplayed_bytes
> pcmbuf_watermark
)
859 logf("pcm starting");
860 if (!(audio_status() & AUDIO_STATUS_PAUSE
))
865 pcmbuf_under_watermark(pcmbuf_unplayed_bytes
<= pcmbuf_watermark
);
870 void* pcmbuf_request_buffer(int *count
)
872 #ifdef HAVE_CROSSFADE
877 if (crossfade_active
) {
878 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
883 if(prepare_insert(*count
<< 2))
885 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
886 if (pcmbuf_size
- audiobuffer_index
>= PCMBUF_MIN_CHUNK
)
888 /* Usual case, there's space here */
889 return &audiobuffer
[audiobuffer_index
];
893 /* Flush and wrap the buffer */
894 pcmbuf_flush_fillpos();
896 return &audiobuffer
[0];
906 void * pcmbuf_request_voice_buffer(int *count
)
908 /* A get-it-to-work-for-now hack (audio status could change by
910 if (audio_status() & AUDIO_STATUS_PLAY
)
912 if (pcmbuf_read
== NULL
)
916 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
917 (pcmbuf_mix_chunk
|| pcmbuf_read
->link
))
919 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
929 return pcmbuf_request_buffer(count
);
933 bool pcmbuf_is_crossfade_active(void)
935 return crossfade_active
|| crossfade_init
;
938 void pcmbuf_write_complete(int count
)
940 size_t length
= (size_t)(unsigned int)count
<< 2;
941 #ifdef HAVE_CROSSFADE
942 if (crossfade_active
)
944 flush_crossfade(fadebuf
, length
);
945 if (!(crossfade_fade_in_rem
|| crossfade_chunk
))
946 crossfade_active
= false;
951 audiobuffer_fillpos
+= length
;
953 if (NEED_FLUSH(audiobuffer_pos
+ audiobuffer_fillpos
))
954 pcmbuf_flush_fillpos();
958 #ifndef HAVE_HARDWARE_BEEP
959 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
960 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
962 /* Generates a constant square wave sound with a given frequency
963 in Hertz for a duration in milliseconds. */
964 void pcmbuf_beep(unsigned int frequency
, size_t duration
, int amplitude
)
966 unsigned int step
= 0xffffffffu
/ NATIVE_FREQUENCY
* frequency
;
968 int16_t *bufptr
, *bufstart
, *bufend
;
970 int nsamples
= NATIVE_FREQUENCY
/ 1000 * duration
;
971 bool mix
= pcmbuf_read
!= NULL
&& pcmbuf_read
->link
!= NULL
;
974 bufend
= SKIPBYTES((int16_t *)audiobuffer
, pcmbuf_size
);
976 /* Find the insertion point and set bufstart to the start of it */
979 /* Get the currently playing chunk at the current position. */
980 bufstart
= (int16_t *)pcm_play_dma_get_peak_buffer(&i
);
982 /* If above isn't implemented or pcm is stopped, no beepeth. */
983 if (!bufstart
|| !pcm_is_playing())
986 /* Give 5ms clearance. */
987 bufstart
+= NATIVE_FREQUENCY
* 4 / 200;
989 #ifdef HAVE_PCM_DMA_ADDRESS
990 /* Returned peak addresses are DMA addresses */
991 bufend
= pcm_dma_addr(bufend
);
995 if (bufstart
>= bufend
)
996 bufstart
-= pcmbuf_size
;
998 /* NOTE: On some targets using hardware DMA, cache range flushing may
999 * be required or the writes may not be picked up by the controller.
1000 * An incremental flush should be done periodically during the mixdown. */
1002 else if (nsamples
<= MINIBUF_SAMPLES
)
1004 static int16_t minibuf
[MINIBUF_SAMPLES
*2] __attribute__((aligned(4)));
1005 /* Use mini buffer */
1007 bufend
= SKIPBYTES(bufstart
, MINIBUF_SIZE
);
1009 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED
)
1011 /* Use audiobuffer */
1012 bufstart
= (int16_t *)audiobuffer
;
1022 /* Mix square wave into buffer */
1023 for (i
= 0; i
< nsamples
; ++i
)
1025 int32_t amp
= (phase
>> 31) ^ (int32_t)amplitude
;
1026 sample
= mix
? *bufptr
: 0;
1027 *bufptr
++ = clip_sample_16(sample
+ amp
);
1028 if (bufptr
>= bufend
)
1029 bufptr
= (int16_t *)audiobuffer
;
1030 sample
= mix
? *bufptr
: 0;
1031 *bufptr
++ = clip_sample_16(sample
+ amp
);
1032 if (bufptr
>= bufend
)
1033 bufptr
= (int16_t *)audiobuffer
;
1039 #ifdef HAVE_RECORDING
1043 /* Kick off playback if required and it won't interfere */
1044 if (!pcm_is_playing()
1045 #ifdef HAVE_RECORDING
1046 && !pcm_is_recording()
1050 pcm_play_data(NULL
, (unsigned char *)bufstart
, nsamples
* 4);
1054 #ifdef HAVE_RECORDING
1058 #endif /* HAVE_HARDWARE_BEEP */
1060 /* Returns pcm buffer usage in percents (0 to 100). */
1061 int pcmbuf_usage(void)
1063 return pcmbuf_unplayed_bytes
* 100 / pcmbuf_size
;
1066 int pcmbuf_mix_free(void)
1068 if (pcmbuf_mix_chunk
)
1071 (size_t)&((int16_t *)pcmbuf_mix_chunk
->addr
)[pcmbuf_mix_sample
];
1072 size_t my_write_pos
= (size_t)&audiobuffer
[audiobuffer_pos
];
1073 if (my_write_pos
< my_mix_end
)
1074 my_write_pos
+= pcmbuf_size
;
1075 return (my_write_pos
- my_mix_end
) * 100 / pcmbuf_unplayed_bytes
;
1080 void pcmbuf_write_voice_complete(int count
)
1082 /* A get-it-to-work-for-now hack (audio status could have changed) */
1083 if (!(audio_status() & AUDIO_STATUS_PLAY
))
1085 pcmbuf_write_complete(count
);
1089 int16_t *ibuf
= (int16_t *)voicebuf
;
1091 size_t chunk_samples
;
1093 if (pcmbuf_mix_chunk
== NULL
&& pcmbuf_read
!= NULL
)
1095 pcmbuf_mix_chunk
= pcmbuf_read
->link
;
1096 /* Start 1/8s into the next chunk */
1097 pcmbuf_mix_sample
= NATIVE_FREQUENCY
* 4 / 16;
1100 if (!pcmbuf_mix_chunk
)
1103 obuf
= (int16_t *)pcmbuf_mix_chunk
->addr
;
1104 chunk_samples
= pcmbuf_mix_chunk
->size
/ sizeof (int16_t);
1110 int32_t sample
= *ibuf
++;
1112 if (pcmbuf_mix_sample
>= chunk_samples
)
1114 pcmbuf_mix_chunk
= pcmbuf_mix_chunk
->link
;
1115 if (!pcmbuf_mix_chunk
)
1117 pcmbuf_mix_sample
= 0;
1118 obuf
= pcmbuf_mix_chunk
->addr
;
1119 chunk_samples
= pcmbuf_mix_chunk
->size
/ 2;
1121 sample
+= obuf
[pcmbuf_mix_sample
] >> 2;
1122 obuf
[pcmbuf_mix_sample
++] = clip_sample_16(sample
);
1126 void pcmbuf_crossfade_enable(bool on_off
)
1128 /* Next setting to be used, not applied now */
1129 crossfade_enabled_pending
= on_off
;
1132 void pcmbuf_crossfade_enable_finished(void)
1134 /* Copy the pending setting over now */
1135 crossfade_enabled
= crossfade_enabled_pending
;
1136 pcmbuf_set_watermark_bytes();
1139 bool pcmbuf_is_crossfade_enabled(void)
1141 if (global_settings
.crossfade
== CROSSFADE_ENABLE_SHUFFLE
)
1142 return global_settings
.playlist_shuffle
;
1144 return crossfade_enabled
;