HAVE_ADJUSTABLE_CPU_FREQ isn't defined for simulators, so we don't have to check...
[Rockbox.git] / apps / pcmbuf.c
blobfbd980369e1273e2a22cecb0b6045a8b92c2faa2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include "config.h"
23 #include "debug.h"
24 #include "panic.h"
25 #include <kernel.h>
26 #include "pcmbuf.h"
27 #include "pcm_playback.h"
28 #include "logf.h"
29 #ifndef SIMULATOR
30 #include "cpu.h"
31 #endif
32 #include "system.h"
33 #include <string.h>
34 #include "buffer.h"
35 #include "settings.h"
36 #include "audio.h"
37 #include "dsp.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.
68 enum {
69 CFM_CROSSFADE,
70 CFM_MIX,
71 CFM_FLUSH
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. */
80 struct pcmbufdesc
82 void *addr;
83 size_t size;
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)
119 return;
121 if (state != boost_state) {
122 cpu_boost(state);
123 boost_state = state;
127 void pcmbuf_set_boost_mode(bool state)
129 if (state)
130 pcmbuf_boost(true);
131 boost_mode = state;
133 #endif
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;
169 if(pcmbuf_new)
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;
177 else
179 /* No more buffers */
180 last_chunksize = 0;
181 *realsize = 0;
182 *realstart = NULL;
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;
213 if (pcmbuf_read) {
214 /* If there is already a read buffer setup, add to it */
215 pcmbuf_read_end->link = pcmbuf_current;
216 } else {
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;
227 else
228 pcmbuf_mix_used_bytes = 0;
230 audiobuffer_pos += size;
231 if (audiobuffer_pos >= pcmbuf_size)
232 audiobuffer_pos = 0;
234 audiobuffer_fillpos = 0;
237 static void pcmbuf_under_watermark(void)
239 /* Fill audio buffer by boosting cpu */
240 pcmbuf_boost(true);
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)
267 return false;
269 /* 0.5 seconds of buffer is low data */
270 return LOW_DATA(2);
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();
279 return false;
281 logf("pcmbuf_crossfade_init");
282 pcmbuf_boost(true);
284 /* Don't enable mix mode when skipping tracks manually. */
285 if (manual_skip)
286 crossfade_mode = CFM_CROSSFADE;
287 else
288 crossfade_mode = global_settings.crossfade_fade_out_mixmode
289 ? CFM_MIX : CFM_CROSSFADE;
290 crossfade_init = true;
292 return true;
296 void pcmbuf_play_stop(void)
298 /** Prevent a very tiny pop from happening by muting audio
299 * until dma has been initialized. */
300 pcm_mute(true);
301 pcm_play_stop();
302 pcm_mute(false);
304 pcmbuf_unplayed_bytes = 0;
305 pcmbuf_mix_used_bytes = 0;
306 if (pcmbuf_read) {
307 pcmbuf_write_end->link = pcmbuf_read;
308 pcmbuf_write_end = pcmbuf_read_end;
309 pcmbuf_read = pcmbuf_read_end = NULL;
311 audiobuffer_pos = 0;
312 audiobuffer_fillpos = 0;
313 audiobuffer_free = pcmbuf_size;
314 crossfade_init = false;
315 crossfade_active = false;
317 pcmbuf_set_boost_mode(false);
318 pcmbuf_boost(false);
322 int pcmbuf_used_descs(void) {
323 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
324 unsigned int i = 0;
325 while (pcmbuf_temp) {
326 pcmbuf_temp = pcmbuf_temp->link;
327 i++;
329 return i;
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;
342 next++;
343 pcmbuf_write_end = pcmbuf_write;
344 while ((void *)next < (void *)audiobufend) {
345 pcmbuf_write_end->link=next;
346 pcmbuf_write_end=next;
347 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;
364 pcmbuf_play_stop();
367 size_t pcmbuf_get_bufsize(void)
369 return pcmbuf_size;
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()) {
378 pcmbuf_play_stop();
379 return ;
382 pcmbuf_boost(true);
383 crossfade_mode = CFM_FLUSH;
384 crossfade_init = true;
387 void pcmbuf_pause(bool pause) {
388 if (pause)
389 pcm_mute(true);
390 pcm_play_pause(!pause);
391 if (!pause)
392 pcm_mute(false);
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. */
403 pcm_mute(true);
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. */
411 pcm_mute(false);
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 */
425 pcmbuf_boost(false);
426 /* If this happens, something is being stupid */
427 if (!pcm_is_playing()) {
428 logf("pcmbuf_flush_fillpos error");
429 pcmbuf_play_start();
431 /* Let approximately one chunk of data playback */
432 sleep(PCMBUF_TARGET_CHUNK/(NATIVE_FREQUENCY * 4) / 5);
434 pcmbuf_add_chunk();
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;
468 buf++;
470 if (buf >= buf_end)
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;
499 crossfade_init = 0;
500 /* Reject crossfade if less than .5s of data */
501 if (LOW_DATA(2)) {
502 logf("crossfade rejected");
503 pcmbuf_play_stop();
504 return ;
507 logf("crossfade_start");
508 pcmbuf_boost(true);
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) {
517 case CFM_MIX:
518 case CFM_CROSSFADE:
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)
549 fade_out_delay -=
550 (fade_out_rem + fade_out_delay) - crossfade_rem;
551 break ;
553 case CFM_FLUSH:
554 crossfade_fade_in_rem = 0;
555 crossfade_fade_in_amount = 0;
556 break ;
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
572 * to the pcm buffer.
574 static void fade_insert(const short *inbuf, size_t length)
576 size_t copy_n;
577 int factor;
578 unsigned int i, samples;
579 short *buf;
581 factor = ((crossfade_fade_in_amount-crossfade_fade_in_rem)<<8)
582 /crossfade_fade_in_amount;
584 while (audiobuffer_free < length)
586 pcmbuf_boost(false);
587 sleep(1);
589 audiobuffer_free -= length;
591 while (length > 0) {
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;
607 inbuf += samples;
608 audiobuffer_fillpos += copy_n;
609 length -= copy_n;
614 * Fades in buf2 and mixes it with buf.
616 static int crossfade(short *buf, const short *buf2, unsigned int length)
618 size_t size;
619 unsigned int i;
620 size_t size_insert = 0;
621 int factor;
623 size = MIN(length, crossfade_rem);
624 switch (crossfade_mode) {
625 /* Fade in the current stream and mix it. */
626 case CFM_MIX:
627 case CFM_CROSSFADE:
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)));
635 break ;
637 /* Join two streams. */
638 case CFM_FLUSH:
639 for (i = 0; i < size; i++) {
640 buf[i] = buf2[i];
642 //memcpy((char *)buf, (char *)buf2, size*2);
643 break ;
646 if (crossfade_fade_in_rem > size)
647 crossfade_fade_in_rem = crossfade_fade_in_rem - size;
648 else
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)
670 size_t copy_n;
671 audiobuffer_free -= length;
672 while (length > 0) {
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);
681 buf += copy_n;
682 audiobuffer_fillpos += copy_n;
683 length -= copy_n;
687 static void flush_crossfade(const char *buf, size_t length) {
688 size_t copy_n;
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);
694 buf += copy_n;
695 length -= copy_n;
696 crossfade_pos += copy_n;
697 if (crossfade_pos >= pcmbuf_size)
698 crossfade_pos = 0;
701 pcmbuf_flush_buffer(buf, length);
704 static bool prepare_insert(size_t length)
706 if (crossfade_init)
707 crossfade_start();
709 if (low_latency_mode)
711 /* 1/4s latency. */
712 if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 4
713 && pcm_is_playing())
714 return false;
717 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
718 if (audiobuffer_free < length + PCMBUF_MIN_CHUNK && !crossfade_active)
720 pcmbuf_boost(false);
721 return false;
724 if (!pcm_is_playing())
726 pcmbuf_boost(true);
727 crossfade_active = false;
728 /* Pre-buffer 1s. */
729 if (!LOW_DATA(4))
731 logf("pcm starting");
732 pcmbuf_play_start();
734 } else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
735 pcmbuf_under_watermark();
737 return true;
740 void* pcmbuf_request_buffer(size_t length, size_t *realsize)
742 if (crossfade_active) {
743 *realsize = MIN(length, PCMBUF_FADE_CHUNK);
744 return &guardbuf[0];
746 else
748 if(prepare_insert(length))
750 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
751 *realsize = length;
752 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
754 /* Usual case, there's space here */
755 return &audiobuffer[audiobuffer_index];
757 else
759 /* Flush and wrap the buffer */
760 pcmbuf_flush_fillpos();
761 audiobuffer_pos = 0;
762 return &audiobuffer[0];
765 else
767 *realsize = 0;
768 return NULL;
773 void* pcmbuf_request_voice_buffer(size_t length, size_t *realsize, bool mix)
775 if (mix)
777 *realsize = MIN(length, PCMBUF_FADE_CHUNK);
778 return &guardbuf[0];
780 else
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);
795 else
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))
808 return false;
810 if (crossfade_active) {
811 flush_crossfade(buf,length);
813 else
815 pcmbuf_flush_buffer(buf, length);
817 return true;
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];
829 else
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;
839 long sample;
840 short *buf;
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)
849 sample = *buf;
850 *buf++ = MIN(MAX(sample + amplitude, -32768), 32767);
851 if (buf > pcmbuf_end)
852 buf = (short *)audiobuffer;
853 sample = *buf;
854 *buf++ = MIN(MAX(sample + amplitude, -32768), 32767);
856 /* Toggle square wav side */
857 if (++count >= interval)
859 count = 0;
860 amplitude = -amplitude;
862 if (buf > pcmbuf_end)
863 buf = (short *)audiobuffer;
866 else
868 buf = (short *)audiobuffer;
869 while (i++ < samples)
871 *buf++ = amplitude;
872 if (buf > pcmbuf_end)
873 buf = (short *)audiobuffer;
874 *buf++ = amplitude;
876 /* Toggle square wav side */
877 if (++count >= interval)
879 count = 0;
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;
915 length /= 2;
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);
934 } else {
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;