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 /* Crossfade modes. If CFM_CROSSFADE is selected, normal
65 * crossfader will activate. Selecting CFM_FLUSH is a special
66 * operation that only overwrites the pcm buffer without crossfading.
74 static size_t crossfade_fade_in_amount IDATA_ATTR
;
75 static size_t crossfade_fade_in_rem IDATA_ATTR
;
78 /* Structure we can use to queue pcm chunks in memory to be played
79 * by the driver code. */
84 struct pcmbufdesc
* link
;
85 /* Call this when the buffer has been played */
86 void (*callback
)(void);
89 static size_t pcmbuf_descsize
;
90 static struct pcmbufdesc
*pcmbuf_read IDATA_ATTR
;
91 static struct pcmbufdesc
*pcmbuf_read_end IDATA_ATTR
;
92 static struct pcmbufdesc
*pcmbuf_write IDATA_ATTR
;
93 static struct pcmbufdesc
*pcmbuf_write_end IDATA_ATTR
;
94 static size_t last_chunksize IDATA_ATTR
;
95 static size_t pcmbuf_unplayed_bytes IDATA_ATTR
;
96 static size_t pcmbuf_mix_used_bytes IDATA_ATTR
;
97 static size_t pcmbuf_watermark IDATA_ATTR
;
98 static short *mixpos IDATA_ATTR
;
99 static bool low_latency_mode
= false;
101 /* Helpful macros for use in conditionals this assumes some of the above
102 * static variable names */
103 #define NEED_FLUSH(position) \
104 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
105 #define LOW_DATA(quarter_secs) \
106 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
108 static void pcmbuf_flush_audio(void);
109 static void pcmbuf_under_watermark(void);
111 #if defined(HAVE_ADJUSTABLE_CPU_FREQ) && !defined(SIMULATOR)
112 static bool boost_mode
;
114 void pcmbuf_boost(bool state
)
116 static bool boost_state
= false;
118 if (crossfade_init
|| crossfade_active
|| boost_mode
)
121 if (state
!= boost_state
) {
127 void pcmbuf_set_boost_mode(bool state
)
135 #define CALL_IF_EXISTS(function, args...) if (function) function(args)
136 /* This function has 2 major logical parts (separated by brackets both for
137 * readability and variable scoping). The first part performs the
138 * operastions related to finishing off the last buffer we fed to the DMA.
139 * The second part performs the operations involved in sending a new buffer
140 * to the DMA. Finally the function checks the status of the buffer and
141 * boosts if necessary */
142 static void pcmbuf_callback(unsigned char** start
, size_t* size
) ICODE_ATTR
;
143 static void pcmbuf_callback(unsigned char** start
, size_t* size
)
146 struct pcmbufdesc
*pcmbuf_current
= pcmbuf_read
;
147 /* Take the finished buffer out of circulation */
148 pcmbuf_read
= pcmbuf_current
->link
;
151 size_t finished_size
= last_chunksize
;
152 audiobuffer_free
+= finished_size
;
154 /* The buffer is finished, call the callback functions */
155 CALL_IF_EXISTS(position_callback
, finished_size
);
157 CALL_IF_EXISTS(pcmbuf_current
->callback
);
159 /* Put the finished buffer back into circulation */
160 pcmbuf_write_end
->link
= pcmbuf_current
;
161 pcmbuf_write_end
= pcmbuf_current
;
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
;
172 pcmbuf_unplayed_bytes
-= current_size
;
173 *realsize
= current_size
;
174 last_chunksize
= current_size
;
175 *realstart
= pcmbuf_new
->addr
;
179 /* No more buffers */
183 CALL_IF_EXISTS(pcmbuf_event_handler
);
188 void pcmbuf_set_position_callback(void (*callback
)(size_t size
))
190 position_callback
= callback
;
193 static void pcmbuf_set_watermark_bytes(size_t numbytes
)
195 pcmbuf_watermark
= numbytes
;
198 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
199 * in a separate function for the moment */
200 static inline void pcmbuf_add_chunk(void)
202 register size_t size
= audiobuffer_fillpos
;
203 /* Grab the next description to write, and change the write pointer */
204 register struct pcmbufdesc
*pcmbuf_current
= pcmbuf_write
;
205 pcmbuf_write
= pcmbuf_current
->link
;
206 /* Fill in the values in the new buffer chunk */
207 pcmbuf_current
->addr
= &audiobuffer
[audiobuffer_pos
];
208 pcmbuf_current
->size
= size
;
209 pcmbuf_current
->callback
= pcmbuf_event_handler
;
210 pcmbuf_current
->link
= NULL
;
211 /* This is single use only */
212 pcmbuf_event_handler
= NULL
;
214 /* If there is already a read buffer setup, add to it */
215 pcmbuf_read_end
->link
= pcmbuf_current
;
217 /* Otherwise create the buffer */
218 pcmbuf_read
= pcmbuf_current
;
220 /* This is now the last buffer to read */
221 pcmbuf_read_end
= pcmbuf_current
;
223 /* Update bytes counters */
224 pcmbuf_unplayed_bytes
+= size
;
225 if (pcmbuf_mix_used_bytes
> size
)
226 pcmbuf_mix_used_bytes
-= size
;
228 pcmbuf_mix_used_bytes
= 0;
230 audiobuffer_pos
+= size
;
231 if (audiobuffer_pos
>= pcmbuf_size
)
234 audiobuffer_fillpos
= 0;
237 static void pcmbuf_under_watermark(void)
239 /* Fill audio buffer by boosting cpu */
241 /* Disable crossfade if < .5s of audio */
242 if (LOW_DATA(2) && crossfade_mode
!= CFM_FLUSH
)
243 crossfade_active
= false;
246 void pcmbuf_set_event_handler(void (*event_handler
)(void))
248 pcmbuf_event_handler
= event_handler
;
251 unsigned int pcmbuf_get_latency(void)
253 /* Be careful how this calculation is rearranted, it's easy to overflow */
254 size_t bytes
= pcmbuf_unplayed_bytes
+ pcm_get_bytes_waiting();
255 return bytes
/ 4 / (NATIVE_FREQUENCY
/1000);
258 void pcmbuf_set_low_latency(bool state
)
260 low_latency_mode
= state
;
263 bool pcmbuf_is_lowdata(void)
265 if (!pcm_is_playing() || pcm_is_paused() ||
266 crossfade_init
|| crossfade_active
)
269 /* 0.5 seconds of buffer is low data */
273 bool pcmbuf_crossfade_init(bool manual_skip
)
275 if (pcmbuf_unplayed_bytes
< PCMBUF_TARGET_CHUNK
* 8
276 || !pcmbuf_is_crossfade_enabled()
277 || crossfade_active
|| crossfade_init
|| low_latency_mode
) {
278 pcmbuf_flush_audio();
281 logf("pcmbuf_crossfade_init");
284 /* Don't enable mix mode when skipping tracks manually. */
286 crossfade_mode
= CFM_CROSSFADE
;
288 crossfade_mode
= global_settings
.crossfade_fade_out_mixmode
289 ? CFM_MIX
: CFM_CROSSFADE
;
290 crossfade_init
= true;
296 void pcmbuf_play_stop(void)
298 /** Prevent a very tiny pop from happening by muting audio
299 * until dma has been initialized. */
304 pcmbuf_unplayed_bytes
= 0;
305 pcmbuf_mix_used_bytes
= 0;
307 pcmbuf_write_end
->link
= pcmbuf_read
;
308 pcmbuf_write_end
= pcmbuf_read_end
;
309 pcmbuf_read
= pcmbuf_read_end
= NULL
;
312 audiobuffer_fillpos
= 0;
313 audiobuffer_free
= pcmbuf_size
;
314 crossfade_init
= false;
315 crossfade_active
= false;
317 pcmbuf_set_boost_mode(false);
322 int pcmbuf_used_descs(void) {
323 struct pcmbufdesc
*pcmbuf_temp
= pcmbuf_read
;
325 while (pcmbuf_temp
) {
326 pcmbuf_temp
= pcmbuf_temp
->link
;
332 int pcmbuf_descs(void) {
333 return pcmbuf_size
/ PCMBUF_MINAVG_CHUNK
;
336 size_t get_pcmbuf_descsize(void) {
337 return pcmbuf_descsize
;
340 static void pcmbuf_init_pcmbuffers(void) {
341 struct pcmbufdesc
*next
= pcmbuf_write
;
343 pcmbuf_write_end
= pcmbuf_write
;
344 while ((void *)next
< (void *)audiobufend
) {
345 pcmbuf_write_end
->link
=next
;
346 pcmbuf_write_end
=next
;
351 /* Initialize the pcmbuffer the structure looks like this:
352 * ...CODECBUFFER|---------PCMBUF---------|GUARDBUF|DESCS| */
353 void pcmbuf_init(size_t bufsize
)
355 pcmbuf_size
= bufsize
;
356 pcmbuf_descsize
= pcmbuf_descs()*sizeof(struct pcmbufdesc
);
357 audiobuffer
= (char *)&audiobuf
[(audiobufend
- audiobuf
) -
358 (pcmbuf_size
+ PCMBUF_FADE_CHUNK
+ pcmbuf_descsize
)];
359 guardbuf
= &audiobuffer
[pcmbuf_size
];
360 pcmbuf_write
= (struct pcmbufdesc
*)(&guardbuf
[PCMBUF_FADE_CHUNK
]);
361 pcmbuf_init_pcmbuffers();
362 position_callback
= NULL
;
363 pcmbuf_event_handler
= NULL
;
367 size_t pcmbuf_get_bufsize(void)
372 /** Initialize a track switch so that audio playback will not stop but
373 * the switch to next track would happen as soon as possible.
375 static void pcmbuf_flush_audio(void)
377 if (crossfade_init
|| crossfade_active
|| !pcm_is_playing()) {
383 crossfade_mode
= CFM_FLUSH
;
384 crossfade_init
= true;
387 void pcmbuf_pause(bool pause
) {
390 pcm_play_pause(!pause
);
393 pcmbuf_boost(!pause
);
396 /* Force playback. */
397 void pcmbuf_play_start(void)
399 if (!pcm_is_playing() && pcmbuf_unplayed_bytes
)
401 /** Prevent a very tiny pop from happening by muting audio
402 * until dma has been initialized. */
405 last_chunksize
= pcmbuf_read
->size
;
406 pcmbuf_unplayed_bytes
-= last_chunksize
;
407 pcm_play_data(pcmbuf_callback
,
408 (unsigned char *)pcmbuf_read
->addr
, last_chunksize
);
410 /* Now unmute the audio. */
416 * Commit samples waiting to the pcm buffer.
418 static void pcmbuf_flush_fillpos(void)
420 if (audiobuffer_fillpos
) {
421 /* Never use the last buffer descriptor */
422 while (pcmbuf_write
== pcmbuf_write_end
) {
423 logf("pcmbuf_flush_fillpos no descriptors");
424 /* Deboost to let the playback catchup */
426 /* If this happens, something is being stupid */
427 if (!pcm_is_playing()) {
428 logf("pcmbuf_flush_fillpos error");
431 /* Let approximately one chunk of data playback */
432 sleep(PCMBUF_TARGET_CHUNK
/(NATIVE_FREQUENCY
* 4) / 5);
439 * Completely process the crossfade fade out effect with current pcm buffer.
441 static void crossfade_process_buffer(size_t fade_in_delay
,
442 size_t fade_out_delay
, size_t fade_out_rem
)
444 if (crossfade_mode
== CFM_CROSSFADE
)
446 /* Fade out the specified amount of the already processed audio */
447 size_t total_fade_out
= fade_out_rem
;
448 short *buf
= (short *)&audiobuffer
[crossfade_pos
+ fade_out_delay
* 2];
449 short *buf_end
= (short *)guardbuf
;
451 /* Wrap the starting position if needed */
452 if (buf
>= buf_end
) buf
-= pcmbuf_size
/ 2;
454 while (fade_out_rem
> 0)
456 /* Each 1/10 second of audio will have the same fade applied */
457 size_t block_rem
= MIN(NATIVE_FREQUENCY
* 2 / 10, fade_out_rem
);
458 unsigned int factor
= (fade_out_rem
<< 8) / total_fade_out
;
459 short *block_end
= buf
+ block_rem
;
461 fade_out_rem
-= block_rem
;
463 /* Fade this block */
464 while (buf
< block_end
)
466 /* Fade one sample */
467 *buf
= (*buf
* factor
) >> 8;
472 /* Wrap the pcmbuffer */
473 buf
-= pcmbuf_size
/ 2;
474 /* Wrap the end pointer to ensure proper termination */
475 block_end
-= pcmbuf_size
/ 2;
481 /* And finally set the mixing position where we should start fading in. */
482 crossfade_rem
-= fade_in_delay
;
483 crossfade_pos
+= fade_in_delay
*2;
484 if (crossfade_pos
>= pcmbuf_size
)
485 crossfade_pos
-= pcmbuf_size
;
486 logf("process done!");
490 * Initializes crossfader, calculates all necessary parameters and
491 * performs fade-out with the pcm buffer.
493 static void crossfade_start(void)
495 size_t fade_out_rem
= 0;
496 unsigned int fade_out_delay
= 0;
497 unsigned fade_in_delay
= 0;
500 /* Reject crossfade if less than .5s of data */
502 logf("crossfade rejected");
507 logf("crossfade_start");
509 pcmbuf_flush_fillpos();
510 crossfade_active
= true;
511 crossfade_pos
= audiobuffer_pos
;
512 /* Initialize the crossfade buffer size to all of the buffered data that
513 * has not yet been sent to the DMA */
514 crossfade_rem
= pcmbuf_unplayed_bytes
/ 2;
516 switch (crossfade_mode
) {
519 /* Get fade out delay from settings. */
520 fade_out_delay
= NATIVE_FREQUENCY
521 * global_settings
.crossfade_fade_out_delay
* 2;
523 /* Get fade out duration from settings. */
524 fade_out_rem
= NATIVE_FREQUENCY
525 * global_settings
.crossfade_fade_out_duration
* 2;
527 /* We want only to modify the last part of the buffer. */
528 if (crossfade_rem
> fade_out_rem
+ fade_out_delay
)
529 crossfade_rem
= fade_out_rem
+ fade_out_delay
;
531 /* Truncate fade out duration if necessary. */
532 if (crossfade_rem
< fade_out_rem
+ fade_out_delay
)
533 fade_out_rem
-= (fade_out_rem
+ fade_out_delay
) - crossfade_rem
;
535 /* Get also fade in duration and delays from settings. */
536 crossfade_fade_in_rem
= NATIVE_FREQUENCY
537 * global_settings
.crossfade_fade_in_duration
* 2;
538 crossfade_fade_in_amount
= crossfade_fade_in_rem
;
540 /* We should avoid to divide by zero. */
541 if (crossfade_fade_in_amount
== 0)
542 crossfade_fade_in_amount
= 1;
544 fade_in_delay
= NATIVE_FREQUENCY
545 * global_settings
.crossfade_fade_in_delay
* 2;
547 /* Decrease the fade out delay if necessary. */
548 if (crossfade_rem
< fade_out_rem
+ fade_out_delay
)
550 (fade_out_rem
+ fade_out_delay
) - crossfade_rem
;
554 crossfade_fade_in_rem
= 0;
555 crossfade_fade_in_amount
= 0;
559 if (crossfade_pos
< crossfade_rem
* 2)
560 crossfade_pos
+= pcmbuf_size
;
561 crossfade_pos
-= crossfade_rem
*2;
563 if (crossfade_mode
!= CFM_FLUSH
) {
564 /* Process the fade out part of the crossfade. */
565 crossfade_process_buffer(fade_in_delay
, fade_out_delay
, fade_out_rem
);
571 * Fades in samples passed to the function and inserts them
574 static void fade_insert(const short *inbuf
, size_t length
)
578 unsigned int i
, samples
;
581 factor
= ((crossfade_fade_in_amount
-crossfade_fade_in_rem
)<<8)
582 /crossfade_fade_in_amount
;
584 while (audiobuffer_free
< length
)
589 audiobuffer_free
-= length
;
592 unsigned int audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
593 /* Flush as needed */
594 if (NEED_FLUSH(audiobuffer_index
))
596 pcmbuf_flush_fillpos();
597 audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
600 copy_n
= MIN(length
, pcmbuf_size
- audiobuffer_index
);
602 buf
= (short *)&audiobuffer
[audiobuffer_index
];
603 samples
= copy_n
/ 2;
604 for (i
= 0; i
< samples
; i
++)
605 buf
[i
] = (inbuf
[i
] * factor
) >> 8;
608 audiobuffer_fillpos
+= copy_n
;
614 * Fades in buf2 and mixes it with buf.
616 static int crossfade(short *buf
, const short *buf2
, unsigned int length
)
620 size_t size_insert
= 0;
623 size
= MIN(length
, crossfade_rem
);
624 switch (crossfade_mode
) {
625 /* Fade in the current stream and mix it. */
628 factor
= ((crossfade_fade_in_amount
-crossfade_fade_in_rem
)<<8) /
629 crossfade_fade_in_amount
;
631 for (i
= 0; i
< size
; i
++) {
632 buf
[i
] = MIN(32767, MAX(-32768,
633 buf
[i
] + ((buf2
[i
] * factor
) >> 8)));
637 /* Join two streams. */
639 for (i
= 0; i
< size
; i
++) {
642 //memcpy((char *)buf, (char *)buf2, size*2);
646 if (crossfade_fade_in_rem
> size
)
647 crossfade_fade_in_rem
= crossfade_fade_in_rem
- size
;
649 crossfade_fade_in_rem
= 0;
651 crossfade_rem
-= size
;
652 if (crossfade_rem
== 0)
654 if (crossfade_fade_in_rem
> 0 && crossfade_fade_in_amount
> 0)
656 size_insert
= MIN(crossfade_fade_in_rem
, length
- size
);
657 fade_insert(&buf2
[size
], size_insert
*2);
658 crossfade_fade_in_rem
-= size_insert
;
661 if (crossfade_fade_in_rem
== 0)
662 crossfade_active
= false;
665 return size
+ size_insert
;
668 static void pcmbuf_flush_buffer(const char *buf
, size_t length
)
671 audiobuffer_free
-= length
;
673 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
674 if (NEED_FLUSH(audiobuffer_index
))
676 pcmbuf_flush_fillpos();
677 audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
679 copy_n
= MIN(length
, pcmbuf_size
- audiobuffer_index
);
680 memcpy(&audiobuffer
[audiobuffer_index
], buf
, copy_n
);
682 audiobuffer_fillpos
+= copy_n
;
687 static void flush_crossfade(const char *buf
, size_t length
) {
690 while (length
> 0 && crossfade_active
) {
691 copy_n
= MIN(length
, pcmbuf_size
- crossfade_pos
);
692 copy_n
= 2 * crossfade((short *)&audiobuffer
[crossfade_pos
],
693 (const short *)buf
, copy_n
/2);
696 crossfade_pos
+= copy_n
;
697 if (crossfade_pos
>= pcmbuf_size
)
701 pcmbuf_flush_buffer(buf
, length
);
704 static bool prepare_insert(size_t length
)
709 if (low_latency_mode
)
712 if (pcmbuf_unplayed_bytes
> NATIVE_FREQUENCY
* 4 / 4
717 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
718 if (audiobuffer_free
< length
+ PCMBUF_MIN_CHUNK
&& !crossfade_active
)
724 if (!pcm_is_playing())
727 crossfade_active
= false;
731 logf("pcm starting");
734 } else if (pcmbuf_unplayed_bytes
<= pcmbuf_watermark
)
735 pcmbuf_under_watermark();
740 void* pcmbuf_request_buffer(size_t length
, size_t *realsize
)
742 if (crossfade_active
) {
743 *realsize
= MIN(length
, PCMBUF_FADE_CHUNK
);
748 if(prepare_insert(length
))
750 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
752 if (pcmbuf_size
- audiobuffer_index
>= PCMBUF_MIN_CHUNK
)
754 /* Usual case, there's space here */
755 return &audiobuffer
[audiobuffer_index
];
759 /* Flush and wrap the buffer */
760 pcmbuf_flush_fillpos();
762 return &audiobuffer
[0];
773 void* pcmbuf_request_voice_buffer(size_t length
, size_t *realsize
, bool mix
)
777 *realsize
= MIN(length
, PCMBUF_FADE_CHUNK
);
781 return pcmbuf_request_buffer(length
, realsize
);
784 bool pcmbuf_is_crossfade_active(void)
786 return crossfade_active
|| crossfade_init
;
789 void pcmbuf_write_complete(size_t length
)
791 if (crossfade_active
) {
792 length
= MIN(length
, PCMBUF_FADE_CHUNK
);
793 flush_crossfade(&guardbuf
[0],length
);
797 audiobuffer_free
-= length
;
798 audiobuffer_fillpos
+= length
;
800 if (NEED_FLUSH(audiobuffer_pos
+ audiobuffer_fillpos
))
801 pcmbuf_flush_fillpos();
805 bool pcmbuf_insert_buffer(const char *buf
, size_t length
)
807 if (!prepare_insert(length
))
810 if (crossfade_active
) {
811 flush_crossfade(buf
,length
);
815 pcmbuf_flush_buffer(buf
, length
);
820 /* Get a pointer to where to mix immediate audio */
821 static inline short* get_mix_insert_pos(void) {
822 /* Give at least 1/8s clearance here */
823 size_t pcmbuf_mix_back_pos
=
824 pcmbuf_unplayed_bytes
- NATIVE_FREQUENCY
* 4 / 8;
826 if (audiobuffer_pos
< pcmbuf_mix_back_pos
)
827 return (short *)&audiobuffer
[pcmbuf_size
+
828 audiobuffer_pos
- pcmbuf_mix_back_pos
];
830 return (short *)&audiobuffer
[audiobuffer_pos
- pcmbuf_mix_back_pos
];
833 /* Generates a constant square wave sound with a given frequency
834 in Hertz for a duration in milliseconds. */
835 void pcmbuf_beep(unsigned int frequency
, size_t duration
, int amplitude
)
837 unsigned int count
= 0, i
= 0;
838 unsigned int interval
= NATIVE_FREQUENCY
/ frequency
;
841 short *pcmbuf_end
= (short *)guardbuf
;
842 size_t samples
= NATIVE_FREQUENCY
/ 1000 * duration
;
844 if (pcm_is_playing())
846 buf
= get_mix_insert_pos();
847 while (i
++ < samples
)
850 *buf
++ = MIN(MAX(sample
+ amplitude
, -32768), 32767);
851 if (buf
> pcmbuf_end
)
852 buf
= (short *)audiobuffer
;
854 *buf
++ = MIN(MAX(sample
+ amplitude
, -32768), 32767);
856 /* Toggle square wav side */
857 if (++count
>= interval
)
860 amplitude
= -amplitude
;
862 if (buf
> pcmbuf_end
)
863 buf
= (short *)audiobuffer
;
868 buf
= (short *)audiobuffer
;
869 while (i
++ < samples
)
872 if (buf
> pcmbuf_end
)
873 buf
= (short *)audiobuffer
;
876 /* Toggle square wav side */
877 if (++count
>= interval
)
880 amplitude
= -amplitude
;
882 if (buf
> pcmbuf_end
)
883 buf
= (short *)audiobuffer
;
885 pcm_play_data(NULL
, (unsigned char *)audiobuffer
, samples
* 4);
889 /* Returns pcm buffer usage in percents (0 to 100). */
890 int pcmbuf_usage(void)
892 return pcmbuf_unplayed_bytes
* 100 / pcmbuf_size
;
895 int pcmbuf_mix_usage(void)
897 return pcmbuf_mix_used_bytes
* 100 / pcmbuf_unplayed_bytes
;
900 void pcmbuf_reset_mixpos(void)
902 mixpos
= get_mix_insert_pos();
903 pcmbuf_mix_used_bytes
= 0;
906 void pcmbuf_mix(char *buf
, size_t length
)
908 short *ibuf
= (short *)buf
;
909 short *pcmbuf_end
= (short *)guardbuf
;
911 if (pcmbuf_mix_used_bytes
== 0)
912 pcmbuf_reset_mixpos();
914 pcmbuf_mix_used_bytes
+= length
;
917 while (length
-- > 0) {
918 long sample
= *ibuf
++;
919 sample
+= *mixpos
>> 2;
920 *mixpos
++ = MIN(MAX(sample
, -32768), 32767);
922 if (mixpos
>= pcmbuf_end
)
923 mixpos
= (short *)audiobuffer
;
927 void pcmbuf_crossfade_enable(bool on_off
)
929 crossfade_enabled
= on_off
;
931 if (crossfade_enabled
) {
932 /* If crossfading, try to keep the buffer full other than 2 second */
933 pcmbuf_set_watermark_bytes(pcmbuf_size
- PCMBUF_WATERMARK
* 2);
935 /* Otherwise, just keep it above 1 second */
936 pcmbuf_set_watermark_bytes(PCMBUF_WATERMARK
);
940 bool pcmbuf_is_crossfade_enabled(void)
942 if (global_settings
.crossfade
== CROSSFADE_ENABLE_SHUFFLE
)
943 return global_settings
.playlist_shuffle
;
945 return crossfade_enabled
;