Added note about 2.5 being Archos only
[kugel-rb.git] / apps / pcmbuf.c
blob31b59f88f656a965c6e96f65ac0eff1e233b4cd6
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 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.
70 enum {
71 CFM_CROSSFADE,
72 CFM_MIX,
73 CFM_FLUSH
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. */
82 struct pcmbufdesc
84 void *addr;
85 size_t size;
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)
121 return;
123 if (state != boost_state) {
124 cpu_boost(state);
125 boost_state = state;
129 void pcmbuf_set_boost_mode(bool state)
131 if (state)
132 pcmbuf_boost(true);
133 boost_mode = state;
135 #endif
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;
171 if(pcmbuf_new)
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;
179 else
181 /* No more buffers */
182 last_chunksize = 0;
183 *realsize = 0;
184 *realstart = NULL;
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;
215 if (pcmbuf_read) {
216 /* If there is already a read buffer setup, add to it */
217 pcmbuf_read_end->link = pcmbuf_current;
218 } else {
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;
229 else
230 pcmbuf_mix_used_bytes = 0;
232 audiobuffer_pos += size;
233 if (audiobuffer_pos >= pcmbuf_size)
234 audiobuffer_pos = 0;
236 audiobuffer_fillpos = 0;
239 static void pcmbuf_under_watermark(void)
241 /* Fill audio buffer by boosting cpu */
242 pcmbuf_boost(true);
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)
269 return false;
271 /* 0.5 seconds of buffer is low data */
272 return LOW_DATA(2);
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();
281 return false;
283 logf("pcmbuf_crossfade_init");
284 pcmbuf_boost(true);
286 /* Don't enable mix mode when skipping tracks manually. */
287 if (manual_skip)
288 crossfade_mode = CFM_CROSSFADE;
289 else
290 crossfade_mode = global_settings.crossfade_fade_out_mixmode
291 ? CFM_MIX : CFM_CROSSFADE;
292 crossfade_init = true;
294 return 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. */
303 pcm_mute(true);
304 pcm_play_stop();
305 pcm_mute(false);
307 pcmbuf_unplayed_bytes = 0;
308 pcmbuf_mix_used_bytes = 0;
309 if (pcmbuf_read) {
310 pcmbuf_write_end->link = pcmbuf_read;
311 pcmbuf_write_end = pcmbuf_read_end;
312 pcmbuf_read = pcmbuf_read_end = NULL;
314 audiobuffer_pos = 0;
315 audiobuffer_fillpos = 0;
316 audiobuffer_free = pcmbuf_size;
317 crossfade_init = false;
318 crossfade_active = false;
320 pcmbuf_set_boost_mode(false);
321 pcmbuf_boost(false);
323 mutex_unlock(&pcmbuf_mutex);
326 int pcmbuf_used_descs(void) {
327 struct pcmbufdesc *pcmbuf_temp = pcmbuf_read;
328 unsigned int i = 0;
329 while (pcmbuf_temp) {
330 pcmbuf_temp = pcmbuf_temp->link;
331 i++;
333 return i;
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;
346 next++;
347 pcmbuf_write_end = pcmbuf_write;
348 while ((void *)next < (void *)audiobufend) {
349 pcmbuf_write_end->link=next;
350 pcmbuf_write_end=next;
351 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;
369 pcmbuf_play_stop();
372 size_t pcmbuf_get_bufsize(void)
374 return pcmbuf_size;
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()) {
383 pcmbuf_play_stop();
384 return ;
387 pcmbuf_boost(true);
388 crossfade_mode = CFM_FLUSH;
389 crossfade_init = true;
392 void pcmbuf_pause(bool pause) {
393 pcm_mute(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. */
407 pcm_mute(true);
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. */
415 pcm_mute(false);
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 */
433 pcmbuf_boost(false);
434 /* Let someone else have fun in the meantime */
435 sleep(1);
436 /* This is a fatal error situation that should never happen. */
437 if (!pcm_is_playing()) {
438 logf("pcmbuf_flush_fillpos error");
439 pcmbuf_play_start();
440 return ;
443 pcmbuf_add_chunk();
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)
455 size_t amount;
456 size_t pos;
457 short *buf;
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;
479 *buf++;
480 blocksize--;
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;
502 crossfade_init = 0;
503 /* Reject crossfade if less than .5s of data */
504 if (LOW_DATA(2)) {
505 logf("crossfade rejected");
506 pcmbuf_play_stop();
507 return ;
510 logf("crossfade_start");
511 pcmbuf_boost(true);
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) {
520 case CFM_MIX:
521 case CFM_CROSSFADE:
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)
552 fade_out_delay -=
553 (fade_out_rem + fade_out_delay) - crossfade_rem;
554 break ;
556 case CFM_FLUSH:
557 crossfade_fade_in_rem = 0;
558 crossfade_fade_in_amount = 0;
559 break ;
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
575 * to the pcm buffer.
577 static void fade_insert(const short *inbuf, size_t length)
579 size_t copy_n;
580 int factor;
581 unsigned int i, samples;
582 short *buf;
584 factor = ((crossfade_fade_in_amount-crossfade_fade_in_rem)<<8)
585 /crossfade_fade_in_amount;
587 while (audiobuffer_free < length)
589 pcmbuf_boost(false);
590 sleep(1);
592 audiobuffer_free -= length;
594 while (length > 0) {
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;
610 inbuf += samples;
611 audiobuffer_fillpos += copy_n;
612 length -= copy_n;
617 * Fades in buf2 and mixes it with buf.
619 static int crossfade(short *buf, const short *buf2, unsigned int length)
621 size_t size;
622 unsigned int i;
623 size_t size_insert = 0;
624 int factor;
626 size = MIN(length, crossfade_rem);
627 switch (crossfade_mode) {
628 /* Fade in the current stream and mix it. */
629 case CFM_MIX:
630 case CFM_CROSSFADE:
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)));
638 break ;
640 /* Join two streams. */
641 case CFM_FLUSH:
642 for (i = 0; i < size; i++) {
643 buf[i] = buf2[i];
645 //memcpy((char *)buf, (char *)buf2, size*2);
646 break ;
649 if (crossfade_fade_in_rem > size)
650 crossfade_fade_in_rem = crossfade_fade_in_rem - size;
651 else
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)
673 size_t copy_n;
674 audiobuffer_free -= length;
675 while (length > 0) {
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);
684 buf += copy_n;
685 audiobuffer_fillpos += copy_n;
686 length -= copy_n;
690 static void flush_crossfade(const char *buf, size_t length) {
691 size_t copy_n;
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);
697 buf += copy_n;
698 length -= copy_n;
699 crossfade_pos += copy_n;
700 if (crossfade_pos >= pcmbuf_size)
701 crossfade_pos = 0;
704 pcmbuf_flush_buffer(buf, length);
707 static bool prepare_insert(size_t length)
709 if (crossfade_init)
710 crossfade_start();
712 if (low_latency_mode)
714 /* 1/4s latency. */
715 if (pcmbuf_unplayed_bytes > NATIVE_FREQUENCY * 4 / 4
716 && pcm_is_playing())
717 return false;
720 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
721 if (audiobuffer_free < length + PCMBUF_MIN_CHUNK && !crossfade_active)
723 pcmbuf_boost(false);
724 return false;
727 if (!pcm_is_playing())
729 pcmbuf_boost(true);
730 crossfade_active = false;
731 /* Pre-buffer 1s. */
732 if (!LOW_DATA(4))
734 logf("pcm starting");
735 pcmbuf_play_start();
737 } else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
738 pcmbuf_under_watermark();
740 return true;
743 void* pcmbuf_request_buffer(size_t length, size_t *realsize)
745 if (crossfade_active) {
746 *realsize = MIN(length, PCMBUF_FADE_CHUNK);
747 return &guardbuf[0];
749 else
751 if(prepare_insert(length))
753 size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
754 *realsize = length;
755 if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK)
757 /* Usual case, there's space here */
758 return &audiobuffer[audiobuffer_index];
760 else
762 /* Flush and wrap the buffer */
763 pcmbuf_flush_fillpos();
764 audiobuffer_pos = 0;
765 return &audiobuffer[0];
768 else
770 *realsize = 0;
771 return NULL;
776 void* pcmbuf_request_voice_buffer(size_t length, size_t *realsize, bool mix)
778 if (mix)
780 *realsize = MIN(length, PCMBUF_FADE_CHUNK);
781 return &guardbuf[0];
783 else
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);
798 else
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))
811 return false;
813 if (crossfade_active) {
814 flush_crossfade(buf,length);
816 else
818 pcmbuf_flush_buffer(buf, length);
820 return true;
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];
832 else
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;
841 bool state = false;
842 unsigned int interval = NATIVE_FREQUENCY / frequency;
843 short *buf;
844 short *pcmbuf_end = (short *)guardbuf;
845 bool playing = pcm_is_playing();
846 size_t samples = NATIVE_FREQUENCY / 1000 * duration;
848 if (playing) {
849 buf = get_mix_insert_pos();
850 } else {
851 buf = (short *)audiobuffer;
853 while (i++ < samples)
855 long sample = *buf;
856 if (state) {
857 *buf++ = MIN(MAX(sample + amplitude, -32768), 32767);
858 if (buf > pcmbuf_end)
859 buf = (short *)audiobuffer;
860 sample = *buf;
861 *buf++ = MIN(MAX(sample + amplitude, -32768), 32767);
862 } else {
863 *buf++ = MIN(MAX(sample - amplitude, -32768), 32767);
864 if (buf > pcmbuf_end)
865 buf = (short *)audiobuffer;
866 sample = *buf;
867 *buf++ = MIN(MAX(sample - amplitude, -32768), 32767);
870 if (++count >= interval)
872 count = 0;
873 state = !state;
875 if (buf > pcmbuf_end)
876 buf = (short *)audiobuffer;
878 if (!playing) {
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;
909 length /= 2;
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);
928 } else {
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;