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 ****************************************************************************/
37 #include "voice_thread.h"
41 /* Clip sample to signed 16 bit range */
42 static inline int32_t clip_sample_16(int32_t sample
)
44 if ((int16_t)sample
!= sample
)
45 sample
= 0x7fff ^ (sample
>> 31);
50 /* Keep watermark high for iPods at least (2s) */
51 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
53 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 1) /* 0.25 seconds */
56 /* Structure we can use to queue pcm chunks in memory to be played
57 * by the driver code. */
62 struct pcmbufdesc
* link
;
63 /* Call this when the buffer has been played */
64 void (*callback
)(void);
67 #define PCMBUF_DESCS(bufsize) \
68 ((bufsize) / PCMBUF_MINAVG_CHUNK)
69 #define PCMBUF_DESCS_SIZE(bufsize) \
70 (PCMBUF_DESCS(bufsize)*sizeof(struct pcmbufdesc))
72 /* Size of the PCM buffer. */
73 static size_t pcmbuf_size IDATA_ATTR
= 0;
74 static char *pcmbuf_bufend IDATA_ATTR
;
75 static char *audiobuffer IDATA_ATTR
;
76 /* Current audio buffer write index. */
77 static size_t audiobuffer_pos IDATA_ATTR
;
78 /* Amount audiobuffer_pos will be increased.*/
79 static size_t audiobuffer_fillpos IDATA_ATTR
;
80 static char *fadebuf IDATA_ATTR
;
81 static char *voicebuf IDATA_ATTR
;
83 static void (*pcmbuf_event_handler
)(void) IDATA_ATTR
;
84 static void (*position_callback
)(size_t size
) IDATA_ATTR
;
86 /* Crossfade related state */
87 static bool crossfade_enabled
;
88 static bool crossfade_enabled_pending
;
89 static bool crossfade_mixmode
;
90 static bool crossfade_active IDATA_ATTR
;
91 static bool crossfade_init IDATA_ATTR
;
93 /* Track the current location for processing crossfade */
94 static struct pcmbufdesc
*crossfade_chunk IDATA_ATTR
;
95 static size_t crossfade_sample IDATA_ATTR
;
97 /* Counters for fading in new data */
98 static size_t crossfade_fade_in_total IDATA_ATTR
;
99 static size_t crossfade_fade_in_rem IDATA_ATTR
;
101 static struct pcmbufdesc
*pcmbuf_read IDATA_ATTR
;
102 static struct pcmbufdesc
*pcmbuf_read_end IDATA_ATTR
;
103 static struct pcmbufdesc
*pcmbuf_write IDATA_ATTR
;
104 static struct pcmbufdesc
*pcmbuf_write_end IDATA_ATTR
;
105 static size_t last_chunksize IDATA_ATTR
;
107 static size_t pcmbuf_unplayed_bytes IDATA_ATTR
;
108 static size_t pcmbuf_watermark IDATA_ATTR
;
110 static struct pcmbufdesc
*pcmbuf_mix_chunk IDATA_ATTR
;
111 static size_t pcmbuf_mix_sample IDATA_ATTR
;
113 static bool low_latency_mode
= false;
114 static bool pcmbuf_flush
;
116 #ifdef HAVE_PRIORITY_SCHEDULING
117 static int codec_thread_priority
= PRIORITY_PLAYBACK
;
120 extern unsigned int codec_thread_id
;
122 /* Helpful macros for use in conditionals this assumes some of the above
123 * static variable names */
124 #define NEED_FLUSH(position) \
125 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
126 #define LOW_DATA(quarter_secs) \
127 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
129 static bool prepare_insert(size_t length
);
130 static void pcmbuf_under_watermark(bool under
);
131 static bool pcmbuf_flush_fillpos(void);
133 #define CALL_IF_EXISTS(function, args...) if (function) function(args)
134 /* This function has 2 major logical parts (separated by brackets both for
135 * readability and variable scoping). The first part performs the
136 * operastions related to finishing off the last buffer we fed to the DMA.
137 * The second part performs the operations involved in sending a new buffer
138 * to the DMA. Finally the function checks the status of the buffer and
139 * boosts if necessary */
140 static void pcmbuf_callback(unsigned char** start
, size_t* size
) ICODE_ATTR
;
141 static void pcmbuf_callback(unsigned char** start
, size_t* size
)
144 struct pcmbufdesc
*pcmbuf_current
= pcmbuf_read
;
145 /* Take the finished buffer out of circulation */
146 pcmbuf_read
= pcmbuf_current
->link
;
148 /* The buffer is finished, call the callback functions */
149 CALL_IF_EXISTS(position_callback
, last_chunksize
);
150 CALL_IF_EXISTS(pcmbuf_current
->callback
);
152 /* Put the finished buffer back into circulation */
153 pcmbuf_write_end
->link
= pcmbuf_current
;
154 pcmbuf_write_end
= pcmbuf_current
;
156 /* If we've read over the mix chunk while it's still mixing there */
157 if (pcmbuf_current
== pcmbuf_mix_chunk
)
158 pcmbuf_mix_chunk
= NULL
;
159 /* If we've read over the crossfade chunk while it's still fading */
160 if (pcmbuf_current
== crossfade_chunk
)
161 crossfade_chunk
= pcmbuf_read
;
165 /* Send the new buffer to the pcm */
166 struct pcmbufdesc
*pcmbuf_new
= pcmbuf_read
;
167 size_t *realsize
= size
;
168 unsigned char** realstart
= start
;
171 size_t current_size
= pcmbuf_new
->size
;
173 pcmbuf_unplayed_bytes
-= current_size
;
174 last_chunksize
= current_size
;
175 *realsize
= current_size
;
176 *realstart
= pcmbuf_new
->addr
;
180 /* No more buffers */
184 CALL_IF_EXISTS(pcmbuf_event_handler
);
189 void pcmbuf_set_position_callback(void (*callback
)(size_t size
))
191 position_callback
= callback
;
194 static void pcmbuf_set_watermark_bytes(void)
196 pcmbuf_watermark
= (crossfade_enabled
&& pcmbuf_size
) ?
197 /* If crossfading, try to keep the buffer full other than 1 second */
198 (pcmbuf_size
- (NATIVE_FREQUENCY
* 4 * 1)) :
199 /* Otherwise, just use the default */
203 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
204 * in a separate function for the moment */
205 static inline void pcmbuf_add_chunk(void)
207 register size_t size
= audiobuffer_fillpos
;
208 /* Grab the next description to write, and change the write pointer */
209 register struct pcmbufdesc
*pcmbuf_current
= pcmbuf_write
;
210 pcmbuf_write
= pcmbuf_current
->link
;
211 /* Fill in the values in the new buffer chunk */
212 pcmbuf_current
->addr
= &audiobuffer
[audiobuffer_pos
];
213 pcmbuf_current
->size
= size
;
214 pcmbuf_current
->callback
= pcmbuf_event_handler
;
215 pcmbuf_current
->link
= NULL
;
216 /* This is single use only */
217 pcmbuf_event_handler
= NULL
;
218 if (pcmbuf_read
!= NULL
) {
221 pcmbuf_write_end
->link
= pcmbuf_read
->link
;
222 pcmbuf_read
->link
= pcmbuf_current
;
223 while (pcmbuf_write_end
->link
)
225 pcmbuf_write_end
= pcmbuf_write_end
->link
;
226 pcmbuf_unplayed_bytes
-= pcmbuf_write_end
->size
;
228 pcmbuf_flush
= false;
230 /* If there is already a read buffer setup, add to it */
232 pcmbuf_read_end
->link
= pcmbuf_current
;
234 /* Otherwise create the buffer */
235 pcmbuf_read
= pcmbuf_current
;
237 /* This is now the last buffer to read */
238 pcmbuf_read_end
= pcmbuf_current
;
240 /* Update bytes counters */
241 pcmbuf_unplayed_bytes
+= size
;
243 audiobuffer_pos
+= size
;
244 if (audiobuffer_pos
>= pcmbuf_size
)
245 audiobuffer_pos
-= pcmbuf_size
;
247 audiobuffer_fillpos
= 0;
250 #ifdef HAVE_PRIORITY_SCHEDULING
251 static void boost_codec_thread(bool boost
)
253 /* Keep voice and codec threads at the same priority or else voice
254 * will starve if the codec thread's priority is boosted. */
257 int priority
= (PRIORITY_PLAYBACK
- PRIORITY_PLAYBACK_MAX
)*pcmbuf_unplayed_bytes
258 / (2*NATIVE_FREQUENCY
) + PRIORITY_PLAYBACK_MAX
;
260 if (priority
!= codec_thread_priority
)
262 codec_thread_priority
= priority
;
263 thread_set_priority(codec_thread_id
, priority
);
264 voice_thread_set_priority(priority
);
267 else if (codec_thread_priority
!= PRIORITY_PLAYBACK
)
269 thread_set_priority(codec_thread_id
, PRIORITY_PLAYBACK
);
270 voice_thread_set_priority(PRIORITY_PLAYBACK
);
271 codec_thread_priority
= PRIORITY_PLAYBACK
;
274 #endif /* HAVE_PRIORITY_SCHEDULING */
276 static void pcmbuf_under_watermark(bool under
)
278 /* Only codec thread initiates boost - voice boosts the cpu when playing
281 if (thread_get_current() == codec_thread_id
)
282 #endif /* SIMULATOR */
286 /* Fill audio buffer by boosting cpu */
288 #ifdef HAVE_PRIORITY_SCHEDULING
289 /* If buffer is critically low, override UI priority, else
290 set back to the original priority. */
291 boost_codec_thread(LOW_DATA(2) && pcm_is_playing());
296 #ifdef HAVE_PRIORITY_SCHEDULING
297 boost_codec_thread(false);
302 /* Disable crossfade if < .5s of audio */
305 crossfade_active
= false;
309 void pcmbuf_set_event_handler(void (*event_handler
)(void))
311 pcmbuf_event_handler
= event_handler
;
314 unsigned int pcmbuf_get_latency(void)
316 /* Be careful how this calculation is rearranged, it's easy to overflow */
317 size_t bytes
= pcmbuf_unplayed_bytes
+ pcm_get_bytes_waiting();
318 return bytes
/ 4 / (NATIVE_FREQUENCY
/1000);
321 void pcmbuf_set_low_latency(bool state
)
323 low_latency_mode
= state
;
326 bool pcmbuf_is_lowdata(void)
328 if (!pcm_is_playing() || pcm_is_paused() ||
329 crossfade_init
|| crossfade_active
)
333 /* 1 seconds of buffer is low data */
336 /* under watermark is low data */
337 return (pcmbuf_unplayed_bytes
< pcmbuf_watermark
);
341 /* Amount of bytes left in the buffer. */
342 inline size_t pcmbuf_free(void)
344 if (pcmbuf_read
!= NULL
)
346 void *read
= pcmbuf_read
->addr
;
347 void *write
= &audiobuffer
[audiobuffer_pos
+ audiobuffer_fillpos
];
349 return (size_t)(read
- write
) + pcmbuf_size
;
351 return (size_t) (read
- write
);
356 bool pcmbuf_crossfade_init(bool manual_skip
)
358 /* Can't do two crossfades at once and, no fade if pcm is off now */
359 if (crossfade_init
|| crossfade_active
|| !pcm_is_playing())
367 /* Not enough data, or crossfade disabled, flush the old data instead */
368 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode
)
370 pcmbuf_flush_fillpos();
375 /* Don't enable mix mode when skipping tracks manually. */
377 crossfade_mixmode
= false;
379 crossfade_mixmode
= global_settings
.crossfade_fade_out_mixmode
;
381 crossfade_init
= true;
387 void pcmbuf_play_stop(void)
391 pcmbuf_unplayed_bytes
= 0;
392 pcmbuf_mix_chunk
= NULL
;
394 pcmbuf_write_end
->link
= pcmbuf_read
;
395 pcmbuf_write_end
= pcmbuf_read_end
;
396 pcmbuf_read
= pcmbuf_read_end
= NULL
;
399 audiobuffer_fillpos
= 0;
400 crossfade_init
= false;
401 crossfade_active
= false;
402 pcmbuf_flush
= false;
404 #ifdef HAVE_PRIORITY_SCHEDULING
405 /* Can unboost the codec thread here no matter who's calling */
406 boost_codec_thread(false);
410 int pcmbuf_used_descs(void)
412 struct pcmbufdesc
*pcmbuf_temp
= pcmbuf_read
;
414 while (pcmbuf_temp
) {
415 pcmbuf_temp
= pcmbuf_temp
->link
;
421 int pcmbuf_descs(void)
423 return PCMBUF_DESCS(pcmbuf_size
);
426 static void pcmbuf_init_pcmbuffers(void)
428 struct pcmbufdesc
*next
= pcmbuf_write
;
430 pcmbuf_write_end
= pcmbuf_write
;
431 while ((void *)next
< (void *)pcmbuf_bufend
) {
432 pcmbuf_write_end
->link
=next
;
433 pcmbuf_write_end
=next
;
438 static size_t pcmbuf_get_next_required_pcmbuf_size(void)
442 if (crossfade_enabled_pending
)
443 seconds
+= global_settings
.crossfade_fade_out_delay
444 + global_settings
.crossfade_fade_out_duration
;
447 /* Buffer has to be at least 2s long. */
450 logf("pcmbuf len: %ld", seconds
);
451 return seconds
* (NATIVE_FREQUENCY
*4); /* 2 channels + 2 bytes/sample */
454 static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize
)
456 return pcmbuf_bufend
- (bufsize
+ PCMBUF_MIX_CHUNK
* 2 +
457 PCMBUF_DESCS_SIZE(bufsize
));
460 bool pcmbuf_is_same_size(void)
462 if (audiobuffer
== NULL
)
463 return true; /* Not set up yet even once so always */
465 size_t bufsize
= pcmbuf_get_next_required_pcmbuf_size();
466 return pcmbuf_calc_audiobuffer_ptr(bufsize
) == audiobuffer
;
469 /* Initialize the pcmbuffer the structure looks like this:
470 * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */
471 size_t pcmbuf_init(unsigned char *bufend
)
473 pcmbuf_bufend
= bufend
;
474 pcmbuf_size
= pcmbuf_get_next_required_pcmbuf_size();
475 audiobuffer
= pcmbuf_calc_audiobuffer_ptr(pcmbuf_size
);
476 fadebuf
= &audiobuffer
[pcmbuf_size
];
477 voicebuf
= &fadebuf
[PCMBUF_MIX_CHUNK
];
478 pcmbuf_write
= (struct pcmbufdesc
*)&voicebuf
[PCMBUF_MIX_CHUNK
];
480 pcmbuf_init_pcmbuffers();
482 position_callback
= NULL
;
483 pcmbuf_event_handler
= NULL
;
485 pcmbuf_crossfade_enable_finished();
489 return pcmbuf_bufend
- audiobuffer
;
492 size_t pcmbuf_get_bufsize(void)
497 #ifdef ROCKBOX_HAS_LOGF
498 unsigned char * pcmbuf_get_meminfo(size_t *length
)
500 *length
= pcmbuf_bufend
- audiobuffer
;
505 void pcmbuf_pause(bool pause
)
507 if (pcm_is_playing())
508 pcm_play_pause(!pause
);
513 /* Force playback. */
514 void pcmbuf_play_start(void)
516 if (!pcm_is_playing() && pcmbuf_unplayed_bytes
&& pcmbuf_read
!= NULL
)
518 last_chunksize
= pcmbuf_read
->size
;
519 pcmbuf_unplayed_bytes
-= last_chunksize
;
520 pcm_play_data(pcmbuf_callback
,
521 (unsigned char *)pcmbuf_read
->addr
, last_chunksize
);
526 * Commit samples waiting to the pcm buffer.
528 static bool pcmbuf_flush_fillpos(void)
530 if (audiobuffer_fillpos
) {
531 /* Never use the last buffer descriptor */
532 while (pcmbuf_write
== pcmbuf_write_end
) {
533 /* If this happens, something is being stupid */
534 if (!pcm_is_playing()) {
535 logf("pcmbuf_flush_fillpos error");
538 /* Let approximately one chunk of data playback */
539 sleep(HZ
*PCMBUF_TARGET_CHUNK
/(NATIVE_FREQUENCY
*4));
548 * Completely process the crossfade fade out effect with current pcm buffer.
550 static void crossfade_process_buffer(size_t fade_in_delay
,
551 size_t fade_out_delay
, size_t fade_out_rem
)
553 if (!crossfade_mixmode
)
555 /* Fade out the specified amount of the already processed audio */
556 size_t total_fade_out
= fade_out_rem
;
557 size_t fade_out_sample
;
558 struct pcmbufdesc
*fade_out_chunk
= crossfade_chunk
;
560 /* Find the right chunk to start fading out */
561 fade_out_delay
+= crossfade_sample
* 2;
562 while (fade_out_delay
!= 0 && fade_out_delay
>= fade_out_chunk
->size
)
564 fade_out_delay
-= fade_out_chunk
->size
;
565 fade_out_chunk
= fade_out_chunk
->link
;
567 /* The start sample within the chunk */
568 fade_out_sample
= fade_out_delay
/ 2;
570 while (fade_out_rem
> 0)
572 /* Each 1/10 second of audio will have the same fade applied */
573 size_t block_rem
= MIN(NATIVE_FREQUENCY
* 4 / 10, fade_out_rem
);
574 int factor
= (fade_out_rem
<< 8) / total_fade_out
;
576 fade_out_rem
-= block_rem
;
578 /* Fade this block */
579 while (block_rem
> 0 && fade_out_chunk
!= NULL
)
581 /* Fade one sample */
582 int16_t *buf
= (int16_t *)fade_out_chunk
->addr
;
583 int32_t sample
= buf
[fade_out_sample
];
584 buf
[fade_out_sample
++] = (sample
* factor
) >> 8;
587 /* Move to the next chunk as needed */
588 if (fade_out_sample
* 2 >= fade_out_chunk
->size
)
590 fade_out_chunk
= fade_out_chunk
->link
;
597 /* Find the right chunk and sample to start fading in */
598 fade_in_delay
+= crossfade_sample
* 2;
599 while (fade_in_delay
!= 0 && fade_in_delay
>= crossfade_chunk
->size
)
601 fade_in_delay
-= crossfade_chunk
->size
;
602 crossfade_chunk
= crossfade_chunk
->link
;
604 crossfade_sample
= fade_in_delay
/ 2;
605 logf("process done!");
608 /* Initializes crossfader, calculates all necessary parameters and
609 * performs fade-out with the pcm buffer. */
610 static void crossfade_start(void)
612 size_t crossfade_rem
;
613 size_t crossfade_need
;
615 size_t fade_out_delay
;
616 size_t fade_in_delay
;
618 crossfade_init
= false;
619 /* Reject crossfade if less than .5s of data */
621 logf("crossfade rejected");
626 logf("crossfade_start");
627 pcmbuf_flush_fillpos();
628 crossfade_active
= true;
630 /* Initialize the crossfade buffer size to all of the buffered data that
631 * has not yet been sent to the DMA */
632 crossfade_rem
= pcmbuf_unplayed_bytes
;
633 crossfade_chunk
= pcmbuf_read
->link
;
634 crossfade_sample
= 0;
636 /* Get fade out delay from settings. */
638 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_out_delay
* 4;
640 /* Get fade out duration from settings. */
642 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_out_duration
* 4;
644 crossfade_need
= fade_out_delay
+ fade_out_rem
;
645 /* We want only to modify the last part of the buffer. */
646 if (crossfade_rem
> crossfade_need
)
648 size_t crossfade_extra
= crossfade_rem
- crossfade_need
;
649 while (crossfade_extra
> crossfade_chunk
->size
)
651 crossfade_extra
-= crossfade_chunk
->size
;
652 crossfade_chunk
= crossfade_chunk
->link
;
654 crossfade_sample
= crossfade_extra
/ 2;
656 /* Truncate fade out duration if necessary. */
657 else if (crossfade_rem
< crossfade_need
)
659 size_t crossfade_short
= crossfade_need
- crossfade_rem
;
660 if (fade_out_rem
>= crossfade_short
)
661 fade_out_rem
-= crossfade_short
;
664 fade_out_delay
-= crossfade_short
- fade_out_rem
;
669 /* Get also fade in duration and delays from settings. */
670 crossfade_fade_in_total
=
671 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_in_duration
* 4;
672 crossfade_fade_in_rem
= crossfade_fade_in_total
;
675 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_in_delay
* 4;
677 crossfade_process_buffer(fade_in_delay
, fade_out_delay
, fade_out_rem
);
680 /* Returns the number of bytes _NOT_ mixed */
681 static size_t crossfade_fade_mix(int factor
, const char *buf
, size_t fade_rem
)
683 const int16_t *input_buf
= (const int16_t *)buf
;
684 int16_t *output_buf
= (int16_t *)(crossfade_chunk
->addr
);
685 int16_t *chunk_end
= SKIPBYTES(output_buf
, crossfade_chunk
->size
);
686 output_buf
= &output_buf
[crossfade_sample
];
691 /* fade left and right channel at once to keep buffer alignment */
692 sample
= *input_buf
++;
693 sample
= ((sample
* factor
) >> 8) + *output_buf
;
694 *output_buf
++ = clip_sample_16(sample
);
696 sample
= *input_buf
++;
697 sample
= ((sample
* factor
) >> 8) + *output_buf
;
698 *output_buf
++ = clip_sample_16(sample
);
700 fade_rem
-= 4; /* 2 samples, each 16 bit -> 4 bytes */
702 if (output_buf
>= chunk_end
)
704 crossfade_chunk
= crossfade_chunk
->link
;
705 if (!crossfade_chunk
)
707 output_buf
= (int16_t *)crossfade_chunk
->addr
;
708 chunk_end
= SKIPBYTES(output_buf
, crossfade_chunk
->size
);
711 crossfade_sample
= output_buf
- (int16_t *)crossfade_chunk
->addr
;
715 /* Returns the number of bytes _NOT_ mixed */
716 static size_t crossfade_mix(const char *buf
, size_t length
)
718 const int16_t *input_buf
= (const int16_t *)buf
;
719 int16_t *output_buf
= (int16_t *)crossfade_chunk
->addr
;
720 int16_t *chunk_end
= SKIPBYTES(output_buf
, crossfade_chunk
->size
);
721 output_buf
= &output_buf
[crossfade_sample
];
726 /* fade left and right channel at once to keep buffer alignment */
727 sample
= *input_buf
++ + *output_buf
;
728 *output_buf
++ = clip_sample_16(sample
);
730 sample
= *input_buf
++ + *output_buf
;
731 *output_buf
++ = clip_sample_16(sample
);
733 length
-= 4; /* 2 samples, each 16 bit -> 4 bytes */
735 if (output_buf
>= chunk_end
)
737 crossfade_chunk
= crossfade_chunk
->link
;
738 if (!crossfade_chunk
)
741 output_buf
= (int16_t *)crossfade_chunk
->addr
;
742 chunk_end
= SKIPBYTES(output_buf
, crossfade_chunk
->size
);
745 crossfade_sample
= output_buf
- (int16_t *)crossfade_chunk
->addr
;
749 static void pcmbuf_flush_buffer(const char *buf
, size_t length
)
753 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
754 if (NEED_FLUSH(audiobuffer_index
))
756 pcmbuf_flush_fillpos();
757 audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
759 copy_n
= MIN(length
, pcmbuf_size
- audiobuffer_index
);
760 memcpy(&audiobuffer
[audiobuffer_index
], buf
, copy_n
);
762 audiobuffer_fillpos
+= copy_n
;
767 static void flush_crossfade(char *buf
, size_t length
)
771 if (crossfade_fade_in_rem
)
776 /* Fade factor for this packet */
778 ((crossfade_fade_in_total
- crossfade_fade_in_rem
) << 8) /
779 crossfade_fade_in_total
;
781 size_t fade_rem
= MIN(length
, crossfade_fade_in_rem
);
783 /* We _will_ fade this many bytes */
784 crossfade_fade_in_rem
-= fade_rem
;
789 size_t fade_total
= fade_rem
;
790 fade_rem
= crossfade_fade_mix(factor
, buf
, fade_rem
);
791 length
-= fade_total
- fade_rem
;
792 buf
+= fade_total
- fade_rem
;
799 samples
= fade_rem
/ 2;
800 input_buf
= (int16_t *)buf
;
801 /* Fade remaining samples in place */
804 int32_t sample
= *input_buf
;
805 *input_buf
++ = (sample
* factor
) >> 8;
814 size_t mix_total
= length
;
815 length
= crossfade_mix(buf
, length
);
816 buf
+= mix_total
- length
;
821 /* Flush samples to the buffer */
822 while (!prepare_insert(length
))
824 pcmbuf_flush_buffer(buf
, length
);
829 static bool prepare_insert(size_t length
)
831 if (low_latency_mode
)
834 if (!LOW_DATA(1) && pcm_is_playing())
838 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
839 if (pcmbuf_free() < length
+ PCMBUF_MIN_CHUNK
)
842 if (!pcm_is_playing())
846 /* Pre-buffer up to watermark */
850 if (pcmbuf_unplayed_bytes
> pcmbuf_watermark
)
853 logf("pcm starting");
854 if (!(audio_status() & AUDIO_STATUS_PAUSE
))
859 pcmbuf_under_watermark(pcmbuf_unplayed_bytes
<= pcmbuf_watermark
);
864 void* pcmbuf_request_buffer(int *count
)
869 if (crossfade_active
) {
870 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
875 if(prepare_insert(*count
<< 2))
877 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
878 if (pcmbuf_size
- audiobuffer_index
>= PCMBUF_MIN_CHUNK
)
880 /* Usual case, there's space here */
881 return &audiobuffer
[audiobuffer_index
];
885 /* Flush and wrap the buffer */
886 pcmbuf_flush_fillpos();
888 return &audiobuffer
[0];
898 void * pcmbuf_request_voice_buffer(int *count
)
900 /* A get-it-to-work-for-now hack (audio status could change by
902 if (audio_status() & AUDIO_STATUS_PLAY
)
904 if (pcmbuf_read
== NULL
)
908 else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 &&
909 (pcmbuf_mix_chunk
|| pcmbuf_read
->link
))
911 *count
= MIN(*count
, PCMBUF_MIX_CHUNK
/4);
921 return pcmbuf_request_buffer(count
);
925 bool pcmbuf_is_crossfade_active(void)
927 return crossfade_active
|| crossfade_init
;
930 void pcmbuf_write_complete(int count
)
932 size_t length
= (size_t)(unsigned int)count
<< 2;
934 if (crossfade_active
)
936 flush_crossfade(fadebuf
, length
);
937 if (!(crossfade_fade_in_rem
|| crossfade_chunk
))
938 crossfade_active
= false;
942 audiobuffer_fillpos
+= length
;
944 if (NEED_FLUSH(audiobuffer_pos
+ audiobuffer_fillpos
))
945 pcmbuf_flush_fillpos();
950 bool pcmbuf_insert_buffer(char *buf
, int count
)
952 size_t length
= (size_t)(unsigned int)count
<< 2;
954 if (crossfade_active
)
956 flush_crossfade(buf
, length
);
957 if (!(crossfade_fade_in_rem
|| crossfade_chunk
))
958 crossfade_active
= false;
962 if (!prepare_insert(length
))
964 pcmbuf_flush_buffer(buf
, length
);
970 #ifndef HAVE_HARDWARE_BEEP
971 #define MINIBUF_SAMPLES (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
972 #define MINIBUF_SIZE (MINIBUF_SAMPLES*4)
974 /* Generates a constant square wave sound with a given frequency
975 in Hertz for a duration in milliseconds. */
976 void pcmbuf_beep(unsigned int frequency
, size_t duration
, int amplitude
)
978 unsigned int step
= 0xffffffffu
/ NATIVE_FREQUENCY
* frequency
;
980 int16_t *bufptr
, *bufstart
, *bufend
;
982 int nsamples
= NATIVE_FREQUENCY
/ 1000 * duration
;
983 bool mix
= pcmbuf_read
!= NULL
&& pcmbuf_read
->link
!= NULL
;
986 bufend
= SKIPBYTES((int16_t *)audiobuffer
, pcmbuf_size
);
988 /* Find the insertion point and set bufstart to the start of it */
991 /* Get the currently playing chunk at the current position. */
992 bufstart
= (int16_t *)pcm_play_dma_get_peak_buffer(&i
);
994 /* If above isn't implemented or pcm is stopped, no beepeth. */
995 if (!bufstart
|| !pcm_is_playing())
998 /* Give 5ms clearance. */
999 bufstart
+= NATIVE_FREQUENCY
* 4 / 200;
1001 #ifdef HAVE_PCM_DMA_ADDRESS
1002 /* Returned peak addresses are DMA addresses */
1003 bufend
= pcm_dma_addr(bufend
);
1006 /* Wrapped above? */
1007 if (bufstart
>= bufend
)
1008 bufstart
-= pcmbuf_size
;
1010 /* NOTE: On some targets using hardware DMA, cache range flushing may
1011 * be required or the writes may not be picked up by the controller.
1012 * An incremental flush should be done periodically during the mixdown. */
1014 else if (nsamples
<= MINIBUF_SAMPLES
)
1016 static int16_t minibuf
[MINIBUF_SAMPLES
*2] __attribute__((aligned(4)));
1017 /* Use mini buffer */
1019 bufend
= SKIPBYTES(bufstart
, MINIBUF_SIZE
);
1021 else if (audio_buffer_state() != AUDIOBUF_STATE_TRASHED
)
1023 /* Use audiobuffer */
1024 bufstart
= (int16_t *)audiobuffer
;
1034 /* Mix square wave into buffer */
1035 for (i
= 0; i
< nsamples
; ++i
)
1037 int32_t amp
= (phase
>> 31) ^ (int32_t)amplitude
;
1038 sample
= mix
? *bufptr
: 0;
1039 *bufptr
++ = clip_sample_16(sample
+ amp
);
1040 if (bufptr
>= bufend
)
1041 bufptr
= (int16_t *)audiobuffer
;
1042 sample
= mix
? *bufptr
: 0;
1043 *bufptr
++ = clip_sample_16(sample
+ amp
);
1044 if (bufptr
>= bufend
)
1045 bufptr
= (int16_t *)audiobuffer
;
1051 #ifdef HAVE_RECORDING
1055 /* Kick off playback if required and it won't interfere */
1056 if (!pcm_is_playing()
1057 #ifdef HAVE_RECORDING
1058 && !pcm_is_recording()
1062 pcm_play_data(NULL
, (unsigned char *)bufstart
, nsamples
* 4);
1066 #ifdef HAVE_RECORDING
1070 #endif /* HAVE_HARDWARE_BEEP */
1072 /* Returns pcm buffer usage in percents (0 to 100). */
1073 int pcmbuf_usage(void)
1075 return pcmbuf_unplayed_bytes
* 100 / pcmbuf_size
;
1078 int pcmbuf_mix_free(void)
1080 if (pcmbuf_mix_chunk
)
1083 (size_t)&((int16_t *)pcmbuf_mix_chunk
->addr
)[pcmbuf_mix_sample
];
1084 size_t my_write_pos
= (size_t)&audiobuffer
[audiobuffer_pos
];
1085 if (my_write_pos
< my_mix_end
)
1086 my_write_pos
+= pcmbuf_size
;
1087 return (my_write_pos
- my_mix_end
) * 100 / pcmbuf_unplayed_bytes
;
1092 void pcmbuf_write_voice_complete(int count
)
1094 /* A get-it-to-work-for-now hack (audio status could have changed) */
1095 if (!(audio_status() & AUDIO_STATUS_PLAY
))
1097 pcmbuf_write_complete(count
);
1101 int16_t *ibuf
= (int16_t *)voicebuf
;
1103 size_t chunk_samples
;
1105 if (pcmbuf_mix_chunk
== NULL
&& pcmbuf_read
!= NULL
)
1107 pcmbuf_mix_chunk
= pcmbuf_read
->link
;
1108 /* Start 1/8s into the next chunk */
1109 pcmbuf_mix_sample
= NATIVE_FREQUENCY
* 4 / 16;
1112 if (!pcmbuf_mix_chunk
)
1115 obuf
= (int16_t *)pcmbuf_mix_chunk
->addr
;
1116 chunk_samples
= pcmbuf_mix_chunk
->size
/ sizeof (int16_t);
1122 int32_t sample
= *ibuf
++;
1124 if (pcmbuf_mix_sample
>= chunk_samples
)
1126 pcmbuf_mix_chunk
= pcmbuf_mix_chunk
->link
;
1127 if (!pcmbuf_mix_chunk
)
1129 pcmbuf_mix_sample
= 0;
1130 obuf
= pcmbuf_mix_chunk
->addr
;
1131 chunk_samples
= pcmbuf_mix_chunk
->size
/ 2;
1133 sample
+= obuf
[pcmbuf_mix_sample
] >> 2;
1134 obuf
[pcmbuf_mix_sample
++] = clip_sample_16(sample
);
1138 void pcmbuf_crossfade_enable(bool on_off
)
1140 /* Next setting to be used, not applied now */
1141 crossfade_enabled_pending
= on_off
;
1144 void pcmbuf_crossfade_enable_finished(void)
1146 /* Copy the pending setting over now */
1147 crossfade_enabled
= crossfade_enabled_pending
;
1148 pcmbuf_set_watermark_bytes();
1151 bool pcmbuf_is_crossfade_enabled(void)
1153 if (global_settings
.crossfade
== CROSSFADE_ENABLE_SHUFFLE
)
1154 return global_settings
.playlist_shuffle
;
1156 return crossfade_enabled
;