1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
27 #include "pcm_playback.h"
39 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 1)
41 /* Size of the PCM buffer. */
42 static size_t pcmbuf_size IDATA_ATTR
= 0;
44 static char *audiobuffer IDATA_ATTR
;
45 /* Current audio buffer write index. */
46 static size_t audiobuffer_pos IDATA_ATTR
;
47 /* Amount of bytes left in the buffer. */
48 size_t audiobuffer_free IDATA_ATTR
;
49 /* Amount audiobuffer_pos will be increased.*/
50 static size_t audiobuffer_fillpos IDATA_ATTR
;
51 static char *guardbuf IDATA_ATTR
;
53 static void (*pcmbuf_event_handler
)(void) IDATA_ATTR
;
54 static void (*position_callback
)(size_t size
) IDATA_ATTR
;
56 /* Crossfade related. */
57 static int crossfade_mode IDATA_ATTR
;
58 static bool crossfade_enabled IDATA_ATTR
;
59 static bool crossfade_active IDATA_ATTR
;
60 static bool crossfade_init IDATA_ATTR
;
61 static size_t crossfade_pos IDATA_ATTR
;
62 static size_t crossfade_rem IDATA_ATTR
;
64 static struct mutex pcmbuf_mutex IDATA_ATTR
;
66 /* Crossfade modes. If CFM_CROSSFADE is selected, normal
67 * crossfader will activate. Selecting CFM_FLUSH is a special
68 * operation that only overwrites the pcm buffer without crossfading.
76 static size_t crossfade_fade_in_amount IDATA_ATTR
;
77 static size_t crossfade_fade_in_rem IDATA_ATTR
;
80 /* Structure we can use to queue pcm chunks in memory to be played
81 * by the driver code. */
86 struct pcmbufdesc
* link
;
87 /* Call this when the buffer has been played */
88 void (*callback
)(void);
91 static size_t pcmbuf_descsize
;
92 static struct pcmbufdesc
*pcmbuf_read IDATA_ATTR
;
93 static struct pcmbufdesc
*pcmbuf_read_end IDATA_ATTR
;
94 static struct pcmbufdesc
*pcmbuf_write IDATA_ATTR
;
95 static struct pcmbufdesc
*pcmbuf_write_end IDATA_ATTR
;
96 static size_t last_chunksize IDATA_ATTR
;
97 static size_t pcmbuf_unplayed_bytes IDATA_ATTR
;
98 static size_t pcmbuf_mix_used_bytes IDATA_ATTR
;
99 static size_t pcmbuf_watermark IDATA_ATTR
;
100 static short *mixpos IDATA_ATTR
;
101 static bool low_latency_mode
= false;
103 /* Helpful macros for use in conditionals this assumes some of the above
104 * static variable names */
105 #define NEED_FLUSH(position) \
106 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
107 #define LOW_DATA(quarter_secs) \
108 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
110 static void pcmbuf_flush_audio(void);
111 static void pcmbuf_under_watermark(void);
113 #if defined(HAVE_ADJUSTABLE_CPU_FREQ) && !defined(SIMULATOR)
114 static bool boost_mode
;
116 void pcmbuf_boost(bool state
)
118 static bool boost_state
= false;
120 if (crossfade_init
|| crossfade_active
|| boost_mode
)
123 if (state
!= boost_state
) {
129 void pcmbuf_set_boost_mode(bool state
)
137 #define CALL_IF_EXISTS(function, args...) if (function) function(args)
138 /* This function has 2 major logical parts (separated by brackets both for
139 * readability and variable scoping). The first part performs the
140 * operastions related to finishing off the last buffer we fed to the DMA.
141 * The second part performs the operations involved in sending a new buffer
142 * to the DMA. Finally the function checks the status of the buffer and
143 * boosts if necessary */
144 static void pcmbuf_callback(unsigned char** start
, size_t* size
) ICODE_ATTR
;
145 static void pcmbuf_callback(unsigned char** start
, size_t* size
)
148 struct pcmbufdesc
*pcmbuf_current
= pcmbuf_read
;
149 /* Take the finished buffer out of circulation */
150 pcmbuf_read
= pcmbuf_current
->link
;
153 size_t finished_size
= last_chunksize
;
154 audiobuffer_free
+= finished_size
;
156 /* The buffer is finished, call the callback functions */
157 CALL_IF_EXISTS(position_callback
, finished_size
);
159 CALL_IF_EXISTS(pcmbuf_current
->callback
);
161 /* Put the finished buffer back into circulation */
162 pcmbuf_write_end
->link
= pcmbuf_current
;
163 pcmbuf_write_end
= pcmbuf_current
;
167 /* Send the new buffer to the pcm */
168 struct pcmbufdesc
*pcmbuf_new
= pcmbuf_read
;
169 size_t *realsize
= size
;
170 unsigned char** realstart
= start
;
173 size_t current_size
= pcmbuf_new
->size
;
174 pcmbuf_unplayed_bytes
-= current_size
;
175 *realsize
= current_size
;
176 last_chunksize
= current_size
;
177 *realstart
= pcmbuf_new
->addr
;
181 /* No more buffers */
185 CALL_IF_EXISTS(pcmbuf_event_handler
);
190 void pcmbuf_set_position_callback(void (*callback
)(size_t size
))
192 position_callback
= callback
;
195 static void pcmbuf_set_watermark_bytes(size_t numbytes
)
197 pcmbuf_watermark
= numbytes
;
200 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
201 * in a separate function for the moment */
202 static inline void pcmbuf_add_chunk(void)
204 register size_t size
= audiobuffer_fillpos
;
205 /* Grab the next description to write, and change the write pointer */
206 register struct pcmbufdesc
*pcmbuf_current
= pcmbuf_write
;
207 pcmbuf_write
= pcmbuf_current
->link
;
208 /* Fill in the values in the new buffer chunk */
209 pcmbuf_current
->addr
= &audiobuffer
[audiobuffer_pos
];
210 pcmbuf_current
->size
= size
;
211 pcmbuf_current
->callback
= pcmbuf_event_handler
;
212 pcmbuf_current
->link
= NULL
;
213 /* This is single use only */
214 pcmbuf_event_handler
= NULL
;
216 /* If there is already a read buffer setup, add to it */
217 pcmbuf_read_end
->link
= pcmbuf_current
;
219 /* Otherwise create the buffer */
220 pcmbuf_read
= pcmbuf_current
;
222 /* This is now the last buffer to read */
223 pcmbuf_read_end
= pcmbuf_current
;
225 /* Update bytes counters */
226 pcmbuf_unplayed_bytes
+= size
;
227 if (pcmbuf_mix_used_bytes
> size
)
228 pcmbuf_mix_used_bytes
-= size
;
230 pcmbuf_mix_used_bytes
= 0;
232 audiobuffer_pos
+= size
;
233 if (audiobuffer_pos
>= pcmbuf_size
)
236 audiobuffer_fillpos
= 0;
239 static void pcmbuf_under_watermark(void)
241 /* Fill audio buffer by boosting cpu */
243 /* Disable crossfade if < .5s of audio */
244 if (LOW_DATA(2) && crossfade_mode
!= CFM_FLUSH
)
245 crossfade_active
= false;
248 void pcmbuf_set_event_handler(void (*event_handler
)(void))
250 pcmbuf_event_handler
= event_handler
;
253 unsigned int pcmbuf_get_latency(void)
255 /* Be careful how this calculation is rearranted, it's easy to overflow */
256 size_t bytes
= pcmbuf_unplayed_bytes
+ pcm_get_bytes_waiting();
257 return bytes
/ 4 / (NATIVE_FREQUENCY
/1000);
260 void pcmbuf_set_low_latency(bool state
)
262 low_latency_mode
= state
;
265 bool pcmbuf_is_lowdata(void)
267 if (!pcm_is_playing() || pcm_is_paused() ||
268 crossfade_init
|| crossfade_active
)
271 /* 0.5 seconds of buffer is low data */
275 bool pcmbuf_crossfade_init(bool manual_skip
)
277 if (pcmbuf_unplayed_bytes
< PCMBUF_TARGET_CHUNK
* 8
278 || !pcmbuf_is_crossfade_enabled()
279 || crossfade_active
|| crossfade_init
|| low_latency_mode
) {
280 pcmbuf_flush_audio();
283 logf("pcmbuf_crossfade_init");
286 /* Don't enable mix mode when skipping tracks manually. */
288 crossfade_mode
= CFM_CROSSFADE
;
290 crossfade_mode
= global_settings
.crossfade_fade_out_mixmode
291 ? CFM_MIX
: CFM_CROSSFADE
;
292 crossfade_init
= true;
298 void pcmbuf_play_stop(void)
300 mutex_lock(&pcmbuf_mutex
);
301 /** Prevent a very tiny pop from happening by muting audio
302 * until dma has been initialized. */
307 pcmbuf_unplayed_bytes
= 0;
308 pcmbuf_mix_used_bytes
= 0;
310 pcmbuf_write_end
->link
= pcmbuf_read
;
311 pcmbuf_write_end
= pcmbuf_read_end
;
312 pcmbuf_read
= pcmbuf_read_end
= NULL
;
315 audiobuffer_fillpos
= 0;
316 audiobuffer_free
= pcmbuf_size
;
317 crossfade_init
= false;
318 crossfade_active
= false;
320 pcmbuf_set_boost_mode(false);
323 mutex_unlock(&pcmbuf_mutex
);
326 int pcmbuf_used_descs(void) {
327 struct pcmbufdesc
*pcmbuf_temp
= pcmbuf_read
;
329 while (pcmbuf_temp
) {
330 pcmbuf_temp
= pcmbuf_temp
->link
;
336 int pcmbuf_descs(void) {
337 return pcmbuf_size
/ PCMBUF_MINAVG_CHUNK
;
340 size_t get_pcmbuf_descsize(void) {
341 return pcmbuf_descsize
;
344 static void pcmbuf_init_pcmbuffers(void) {
345 struct pcmbufdesc
*next
= pcmbuf_write
;
347 pcmbuf_write_end
= pcmbuf_write
;
348 while ((void *)next
< (void *)audiobufend
) {
349 pcmbuf_write_end
->link
=next
;
350 pcmbuf_write_end
=next
;
355 /* Initialize the pcmbuffer the structure looks like this:
356 * ...CODECBUFFER|---------PCMBUF---------|GUARDBUF|DESCS| */
357 void pcmbuf_init(size_t bufsize
)
359 mutex_init(&pcmbuf_mutex
);
360 pcmbuf_size
= bufsize
;
361 pcmbuf_descsize
= pcmbuf_descs()*sizeof(struct pcmbufdesc
);
362 audiobuffer
= (char *)&audiobuf
[(audiobufend
- audiobuf
) -
363 (pcmbuf_size
+ PCMBUF_FADE_CHUNK
+ pcmbuf_descsize
)];
364 guardbuf
= &audiobuffer
[pcmbuf_size
];
365 pcmbuf_write
= (struct pcmbufdesc
*)(&guardbuf
[PCMBUF_FADE_CHUNK
]);
366 pcmbuf_init_pcmbuffers();
367 position_callback
= NULL
;
368 pcmbuf_event_handler
= NULL
;
372 size_t pcmbuf_get_bufsize(void)
377 /** Initialize a track switch so that audio playback will not stop but
378 * the switch to next track would happen as soon as possible.
380 static void pcmbuf_flush_audio(void)
382 if (crossfade_init
|| crossfade_active
|| !pcm_is_playing()) {
388 crossfade_mode
= CFM_FLUSH
;
389 crossfade_init
= true;
392 void pcmbuf_pause(bool pause
) {
394 pcm_play_pause(!pause
);
395 pcmbuf_boost(!pause
);
398 /* Force playback. */
399 void pcmbuf_play_start(void)
401 mutex_lock(&pcmbuf_mutex
);
403 if (!pcm_is_playing() && pcmbuf_unplayed_bytes
)
405 /** Prevent a very tiny pop from happening by muting audio
406 * until dma has been initialized. */
409 last_chunksize
= pcmbuf_read
->size
;
410 pcmbuf_unplayed_bytes
-= last_chunksize
;
411 pcm_play_data(pcmbuf_callback
,
412 (unsigned char *)pcmbuf_read
->addr
, last_chunksize
);
414 /* Now unmute the audio. */
418 mutex_unlock(&pcmbuf_mutex
);
422 * Commit samples waiting to the pcm buffer.
424 static void pcmbuf_flush_fillpos(void)
426 mutex_lock(&pcmbuf_mutex
);
428 if (audiobuffer_fillpos
) {
429 /* Never use the last buffer descriptor */
430 while (pcmbuf_write
== pcmbuf_write_end
) {
431 logf("pcmbuf_flush_fillpos no descriptors");
432 /* Deboost to let the playback catchup */
434 /* Let someone else have fun in the meantime */
436 /* This is a fatal error situation that should never happen. */
437 if (!pcm_is_playing()) {
438 logf("pcmbuf_flush_fillpos error");
446 mutex_unlock(&pcmbuf_mutex
);
450 * Completely process the crossfade fade out effect with current pcm buffer.
452 static void crossfade_process_buffer(unsigned int fade_in_delay
,
453 unsigned int fade_out_delay
, size_t fade_out_rem
)
459 /* Fade out the entire current buffer according to settings. */
460 amount
= fade_out_rem
;
461 pos
= crossfade_pos
+ fade_out_delay
*2;
463 while (fade_out_rem
> 0 && crossfade_mode
== CFM_CROSSFADE
)
465 size_t blocksize
= MIN(8192, fade_out_rem
);
466 int factor
= (fade_out_rem
<<8)/amount
;
468 /* Prevent pcmbuffer from wrapping. */
469 if (pos
>= pcmbuf_size
) pos
-= pcmbuf_size
;
471 blocksize
= MIN((pcmbuf_size
- pos
)/2, blocksize
);
472 buf
= (short *)&audiobuffer
[pos
];
474 fade_out_rem
-= blocksize
;
475 pos
+= blocksize
* 2;
476 while (blocksize
> 0)
478 *buf
= (*buf
* factor
) >> 8;
484 /* And finally set the mixing position where we should start fading in. */
485 crossfade_rem
-= fade_in_delay
;
486 crossfade_pos
+= fade_in_delay
*2;
487 if (crossfade_pos
>= pcmbuf_size
)
488 crossfade_pos
-= pcmbuf_size
;
489 logf("process done!");
493 * Initializes crossfader, calculates all necessary parameters and
494 * performs fade-out with the pcm buffer.
496 static void crossfade_start(void)
498 size_t fade_out_rem
= 0;
499 unsigned int fade_out_delay
= 0;
500 unsigned fade_in_delay
= 0;
503 /* Reject crossfade if less than .5s of data */
505 logf("crossfade rejected");
510 logf("crossfade_start");
512 pcmbuf_flush_fillpos();
513 crossfade_active
= true;
514 crossfade_pos
= audiobuffer_pos
;
515 /* Initialize the crossfade buffer size to all of the buffered data that
516 * has not yet been sent to the DMA */
517 crossfade_rem
= pcmbuf_unplayed_bytes
/ 2;
519 switch (crossfade_mode
) {
522 /* Get fade out delay from settings. */
523 fade_out_delay
= NATIVE_FREQUENCY
524 * global_settings
.crossfade_fade_out_delay
* 2;
526 /* Get fade out duration from settings. */
527 fade_out_rem
= NATIVE_FREQUENCY
528 * global_settings
.crossfade_fade_out_duration
* 2;
530 /* We want only to modify the last part of the buffer. */
531 if (crossfade_rem
> fade_out_rem
+ fade_out_delay
)
532 crossfade_rem
= fade_out_rem
+ fade_out_delay
;
534 /* Truncate fade out duration if necessary. */
535 if (crossfade_rem
< fade_out_rem
+ fade_out_delay
)
536 fade_out_rem
-= (fade_out_rem
+ fade_out_delay
) - crossfade_rem
;
538 /* Get also fade in duration and delays from settings. */
539 crossfade_fade_in_rem
= NATIVE_FREQUENCY
540 * global_settings
.crossfade_fade_in_duration
* 2;
541 crossfade_fade_in_amount
= crossfade_fade_in_rem
;
543 /* We should avoid to divide by zero. */
544 if (crossfade_fade_in_amount
== 0)
545 crossfade_fade_in_amount
= 1;
547 fade_in_delay
= NATIVE_FREQUENCY
548 * global_settings
.crossfade_fade_in_delay
* 2;
550 /* Decrease the fade out delay if necessary. */
551 if (crossfade_rem
< fade_out_rem
+ fade_out_delay
)
553 (fade_out_rem
+ fade_out_delay
) - crossfade_rem
;
557 crossfade_fade_in_rem
= 0;
558 crossfade_fade_in_amount
= 0;
562 if (crossfade_pos
< crossfade_rem
* 2)
563 crossfade_pos
+= pcmbuf_size
;
564 crossfade_pos
-= crossfade_rem
*2;
566 if (crossfade_mode
!= CFM_FLUSH
) {
567 /* Process the fade out part of the crossfade. */
568 crossfade_process_buffer(fade_in_delay
, fade_out_delay
, fade_out_rem
);
574 * Fades in samples passed to the function and inserts them
577 static void fade_insert(const short *inbuf
, size_t length
)
581 unsigned int i
, samples
;
584 factor
= ((crossfade_fade_in_amount
-crossfade_fade_in_rem
)<<8)
585 /crossfade_fade_in_amount
;
587 while (audiobuffer_free
< length
)
592 audiobuffer_free
-= length
;
595 unsigned int audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
596 /* Flush as needed */
597 if (NEED_FLUSH(audiobuffer_index
))
599 pcmbuf_flush_fillpos();
600 audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
603 copy_n
= MIN(length
, pcmbuf_size
- audiobuffer_index
);
605 buf
= (short *)&audiobuffer
[audiobuffer_index
];
606 samples
= copy_n
/ 2;
607 for (i
= 0; i
< samples
; i
++)
608 buf
[i
] = (inbuf
[i
] * factor
) >> 8;
611 audiobuffer_fillpos
+= copy_n
;
617 * Fades in buf2 and mixes it with buf.
619 static int crossfade(short *buf
, const short *buf2
, unsigned int length
)
623 size_t size_insert
= 0;
626 size
= MIN(length
, crossfade_rem
);
627 switch (crossfade_mode
) {
628 /* Fade in the current stream and mix it. */
631 factor
= ((crossfade_fade_in_amount
-crossfade_fade_in_rem
)<<8) /
632 crossfade_fade_in_amount
;
634 for (i
= 0; i
< size
; i
++) {
635 buf
[i
] = MIN(32767, MAX(-32768,
636 buf
[i
] + ((buf2
[i
] * factor
) >> 8)));
640 /* Join two streams. */
642 for (i
= 0; i
< size
; i
++) {
645 //memcpy((char *)buf, (char *)buf2, size*2);
649 if (crossfade_fade_in_rem
> size
)
650 crossfade_fade_in_rem
= crossfade_fade_in_rem
- size
;
652 crossfade_fade_in_rem
= 0;
654 crossfade_rem
-= size
;
655 if (crossfade_rem
== 0)
657 if (crossfade_fade_in_rem
> 0 && crossfade_fade_in_amount
> 0)
659 size_insert
= MIN(crossfade_fade_in_rem
, length
- size
);
660 fade_insert(&buf2
[size
], size_insert
*2);
661 crossfade_fade_in_rem
-= size_insert
;
664 if (crossfade_fade_in_rem
== 0)
665 crossfade_active
= false;
668 return size
+ size_insert
;
671 static void pcmbuf_flush_buffer(const char *buf
, size_t length
)
674 audiobuffer_free
-= length
;
676 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
677 if (NEED_FLUSH(audiobuffer_index
))
679 pcmbuf_flush_fillpos();
680 audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
682 copy_n
= MIN(length
, pcmbuf_size
- audiobuffer_index
);
683 memcpy(&audiobuffer
[audiobuffer_index
], buf
, copy_n
);
685 audiobuffer_fillpos
+= copy_n
;
690 static void flush_crossfade(const char *buf
, size_t length
) {
693 while (length
> 0 && crossfade_active
) {
694 copy_n
= MIN(length
, pcmbuf_size
- crossfade_pos
);
695 copy_n
= 2 * crossfade((short *)&audiobuffer
[crossfade_pos
],
696 (const short *)buf
, copy_n
/2);
699 crossfade_pos
+= copy_n
;
700 if (crossfade_pos
>= pcmbuf_size
)
704 pcmbuf_flush_buffer(buf
, length
);
707 static bool prepare_insert(size_t length
)
712 if (low_latency_mode
)
715 if (pcmbuf_unplayed_bytes
> NATIVE_FREQUENCY
* 4 / 4
720 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
721 if (audiobuffer_free
< length
+ PCMBUF_MIN_CHUNK
&& !crossfade_active
)
727 if (!pcm_is_playing())
730 crossfade_active
= false;
734 logf("pcm starting");
737 } else if (pcmbuf_unplayed_bytes
<= pcmbuf_watermark
)
738 pcmbuf_under_watermark();
743 void* pcmbuf_request_buffer(size_t length
, size_t *realsize
)
745 if (crossfade_active
) {
746 *realsize
= MIN(length
, PCMBUF_FADE_CHUNK
);
751 if(prepare_insert(length
))
753 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
755 if (pcmbuf_size
- audiobuffer_index
>= PCMBUF_MIN_CHUNK
)
757 /* Usual case, there's space here */
758 return &audiobuffer
[audiobuffer_index
];
762 /* Flush and wrap the buffer */
763 pcmbuf_flush_fillpos();
765 return &audiobuffer
[0];
776 void* pcmbuf_request_voice_buffer(size_t length
, size_t *realsize
, bool mix
)
780 *realsize
= MIN(length
, PCMBUF_FADE_CHUNK
);
784 return pcmbuf_request_buffer(length
, realsize
);
787 bool pcmbuf_is_crossfade_active(void)
789 return crossfade_active
|| crossfade_init
;
792 void pcmbuf_write_complete(size_t length
)
794 if (crossfade_active
) {
795 length
= MIN(length
, PCMBUF_FADE_CHUNK
);
796 flush_crossfade(&guardbuf
[0],length
);
800 audiobuffer_free
-= length
;
801 audiobuffer_fillpos
+= length
;
803 if (NEED_FLUSH(audiobuffer_pos
+ audiobuffer_fillpos
))
804 pcmbuf_flush_fillpos();
808 bool pcmbuf_insert_buffer(const char *buf
, size_t length
)
810 if (!prepare_insert(length
))
813 if (crossfade_active
) {
814 flush_crossfade(buf
,length
);
818 pcmbuf_flush_buffer(buf
, length
);
823 /* Get a pointer to where to mix immediate audio */
824 static inline short* get_mix_insert_pos(void) {
825 /* Give at least 1/8s clearance here */
826 size_t pcmbuf_mix_back_pos
=
827 pcmbuf_unplayed_bytes
- NATIVE_FREQUENCY
* 4 / 8;
829 if (audiobuffer_pos
< pcmbuf_mix_back_pos
)
830 return (short *)&audiobuffer
[pcmbuf_size
+
831 audiobuffer_pos
- pcmbuf_mix_back_pos
];
833 return (short *)&audiobuffer
[audiobuffer_pos
- pcmbuf_mix_back_pos
];
836 /* Generates a constant square wave sound with a given frequency
837 in Hertz for a duration in milliseconds. */
838 void pcmbuf_beep(unsigned int frequency
, size_t duration
, int amplitude
)
840 unsigned int count
= 0, i
= 0;
842 unsigned int interval
= NATIVE_FREQUENCY
/ frequency
;
844 short *pcmbuf_end
= (short *)guardbuf
;
845 bool playing
= pcm_is_playing();
846 size_t samples
= NATIVE_FREQUENCY
/ 1000 * duration
;
849 buf
= get_mix_insert_pos();
851 buf
= (short *)audiobuffer
;
853 while (i
++ < samples
)
857 *buf
++ = MIN(MAX(sample
+ amplitude
, -32768), 32767);
858 if (buf
> pcmbuf_end
)
859 buf
= (short *)audiobuffer
;
861 *buf
++ = MIN(MAX(sample
+ amplitude
, -32768), 32767);
863 *buf
++ = MIN(MAX(sample
- amplitude
, -32768), 32767);
864 if (buf
> pcmbuf_end
)
865 buf
= (short *)audiobuffer
;
867 *buf
++ = MIN(MAX(sample
- amplitude
, -32768), 32767);
870 if (++count
>= interval
)
875 if (buf
> pcmbuf_end
)
876 buf
= (short *)audiobuffer
;
879 pcm_play_data(NULL
, (unsigned char *)audiobuffer
, samples
* 4);
883 /* Returns pcm buffer usage in percents (0 to 100). */
884 int pcmbuf_usage(void)
886 return pcmbuf_unplayed_bytes
* 100 / pcmbuf_size
;
889 int pcmbuf_mix_usage(void)
891 return pcmbuf_mix_used_bytes
* 100 / pcmbuf_unplayed_bytes
;
894 void pcmbuf_reset_mixpos(void)
896 mixpos
= get_mix_insert_pos();
897 pcmbuf_mix_used_bytes
= 0;
900 void pcmbuf_mix(char *buf
, size_t length
)
902 short *ibuf
= (short *)buf
;
903 short *pcmbuf_end
= (short *)guardbuf
;
905 if (pcmbuf_mix_used_bytes
== 0)
906 pcmbuf_reset_mixpos();
908 pcmbuf_mix_used_bytes
+= length
;
911 while (length
-- > 0) {
912 long sample
= *ibuf
++;
913 sample
+= *mixpos
>> 2;
914 *mixpos
++ = MIN(MAX(sample
, -32768), 32767);
916 if (mixpos
>= pcmbuf_end
)
917 mixpos
= (short *)audiobuffer
;
921 void pcmbuf_crossfade_enable(bool on_off
)
923 crossfade_enabled
= on_off
;
925 if (crossfade_enabled
) {
926 /* If crossfading, try to keep the buffer full other than 2 second */
927 pcmbuf_set_watermark_bytes(pcmbuf_size
- PCMBUF_WATERMARK
* 2);
929 /* Otherwise, just keep it above 1 second */
930 pcmbuf_set_watermark_bytes(PCMBUF_WATERMARK
);
934 bool pcmbuf_is_crossfade_enabled(void)
936 if (global_settings
.crossfade
== CROSSFADE_ENABLE_SHUFFLE
)
937 return global_settings
.playlist_shuffle
;
939 return crossfade_enabled
;