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"
40 /* Keep watermark high for iPods at least (2s) */
41 #define PCMBUF_WATERMARK (NATIVE_FREQUENCY * 4 * 2)
43 /* Structure we can use to queue pcm chunks in memory to be played
44 * by the driver code. */
49 struct pcmbufdesc
* link
;
50 /* Call this when the buffer has been played */
51 void (*callback
)(void);
54 /* Size of the PCM buffer. */
55 static size_t pcmbuf_size IDATA_ATTR
= 0;
57 static char *audiobuffer IDATA_ATTR
;
58 /* Current audio buffer write index. */
59 static size_t audiobuffer_pos IDATA_ATTR
;
60 /* Amount audiobuffer_pos will be increased.*/
61 static size_t audiobuffer_fillpos IDATA_ATTR
;
62 static char *fadebuf IDATA_ATTR
;
63 static char *voicebuf IDATA_ATTR
;
65 static void (*pcmbuf_event_handler
)(void) IDATA_ATTR
;
66 static void (*position_callback
)(size_t size
) IDATA_ATTR
;
68 /* Crossfade related state */
69 static bool crossfade_enabled
;
70 static bool crossfade_mixmode
;
71 static bool crossfade_active IDATA_ATTR
;
72 static bool crossfade_init IDATA_ATTR
;
74 /* Track the current location for processing crossfade */
75 static struct pcmbufdesc
*crossfade_chunk IDATA_ATTR
;
76 static size_t crossfade_sample IDATA_ATTR
;
78 /* Counters for fading in new data */
79 static size_t crossfade_fade_in_total IDATA_ATTR
;
80 static size_t crossfade_fade_in_rem IDATA_ATTR
;
82 static size_t pcmbuf_descsize
;
83 static struct pcmbufdesc
*pcmbuf_read IDATA_ATTR
;
84 static struct pcmbufdesc
*pcmbuf_read_end IDATA_ATTR
;
85 static struct pcmbufdesc
*pcmbuf_write IDATA_ATTR
;
86 static struct pcmbufdesc
*pcmbuf_write_end IDATA_ATTR
;
87 static size_t last_chunksize IDATA_ATTR
;
89 static size_t pcmbuf_unplayed_bytes IDATA_ATTR
;
90 static size_t pcmbuf_watermark IDATA_ATTR
;
92 static struct pcmbufdesc
*pcmbuf_mix_chunk IDATA_ATTR
;
93 static size_t pcmbuf_mix_sample IDATA_ATTR
;
95 static bool low_latency_mode
= false;
96 static bool pcmbuf_flush
;
98 extern struct thread_entry
*codec_thread_p
;
100 /* Helpful macros for use in conditionals this assumes some of the above
101 * static variable names */
102 #define NEED_FLUSH(position) \
103 (audiobuffer_fillpos > PCMBUF_TARGET_CHUNK || position >= pcmbuf_size)
104 #define LOW_DATA(quarter_secs) \
105 (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs)
107 static bool prepare_insert(size_t length
);
108 static void pcmbuf_under_watermark(void);
109 static bool pcmbuf_flush_fillpos(void);
111 #define CALL_IF_EXISTS(function, args...) if (function) function(args)
112 /* This function has 2 major logical parts (separated by brackets both for
113 * readability and variable scoping). The first part performs the
114 * operastions related to finishing off the last buffer we fed to the DMA.
115 * The second part performs the operations involved in sending a new buffer
116 * to the DMA. Finally the function checks the status of the buffer and
117 * boosts if necessary */
118 static void pcmbuf_callback(unsigned char** start
, size_t* size
) ICODE_ATTR
;
119 static void pcmbuf_callback(unsigned char** start
, size_t* size
)
122 struct pcmbufdesc
*pcmbuf_current
= pcmbuf_read
;
123 /* Take the finished buffer out of circulation */
124 pcmbuf_read
= pcmbuf_current
->link
;
126 /* The buffer is finished, call the callback functions */
127 CALL_IF_EXISTS(position_callback
, last_chunksize
);
128 CALL_IF_EXISTS(pcmbuf_current
->callback
);
130 /* Put the finished buffer back into circulation */
131 pcmbuf_write_end
->link
= pcmbuf_current
;
132 pcmbuf_write_end
= pcmbuf_current
;
134 /* If we've read over the mix chunk while it's still mixing there */
135 if (pcmbuf_current
== pcmbuf_mix_chunk
)
136 pcmbuf_mix_chunk
= NULL
;
137 /* If we've read over the crossfade chunk while it's still fading */
138 if (pcmbuf_current
== crossfade_chunk
)
139 crossfade_chunk
= pcmbuf_read
;
144 /* Send the new buffer to the pcm */
145 struct pcmbufdesc
*pcmbuf_new
= pcmbuf_read
;
146 size_t *realsize
= size
;
147 unsigned char** realstart
= start
;
150 size_t current_size
= pcmbuf_new
->size
;
152 pcmbuf_unplayed_bytes
-= current_size
;
153 last_chunksize
= current_size
;
154 *realsize
= current_size
;
155 *realstart
= pcmbuf_new
->addr
;
159 /* There may be more data waiting to flush, try to use it */
160 if (pcmbuf_flush_fillpos())
161 goto process_new_buffer
;
163 /* No more buffers */
167 CALL_IF_EXISTS(pcmbuf_event_handler
);
172 void pcmbuf_set_position_callback(void (*callback
)(size_t size
))
174 position_callback
= callback
;
177 static void pcmbuf_set_watermark_bytes(size_t numbytes
)
179 pcmbuf_watermark
= numbytes
;
182 /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep
183 * in a separate function for the moment */
184 static inline void pcmbuf_add_chunk(void)
186 register size_t size
= audiobuffer_fillpos
;
187 /* Grab the next description to write, and change the write pointer */
188 register struct pcmbufdesc
*pcmbuf_current
= pcmbuf_write
;
189 pcmbuf_write
= pcmbuf_current
->link
;
190 /* Fill in the values in the new buffer chunk */
191 pcmbuf_current
->addr
= &audiobuffer
[audiobuffer_pos
];
192 pcmbuf_current
->size
= size
;
193 pcmbuf_current
->callback
= pcmbuf_event_handler
;
194 pcmbuf_current
->link
= NULL
;
195 /* This is single use only */
196 pcmbuf_event_handler
= NULL
;
197 if (pcmbuf_read
!= NULL
) {
200 pcmbuf_write_end
->link
= pcmbuf_read
->link
;
201 pcmbuf_read
->link
= pcmbuf_current
;
202 while (pcmbuf_write_end
->link
)
204 pcmbuf_write_end
= pcmbuf_write_end
->link
;
205 pcmbuf_unplayed_bytes
-= pcmbuf_write_end
->size
;
207 pcmbuf_flush
= false;
209 /* If there is already a read buffer setup, add to it */
211 pcmbuf_read_end
->link
= pcmbuf_current
;
213 /* Otherwise create the buffer */
214 pcmbuf_read
= pcmbuf_current
;
216 /* This is now the last buffer to read */
217 pcmbuf_read_end
= pcmbuf_current
;
219 /* Update bytes counters */
220 pcmbuf_unplayed_bytes
+= size
;
222 audiobuffer_pos
+= size
;
223 if (audiobuffer_pos
>= pcmbuf_size
)
224 audiobuffer_pos
-= pcmbuf_size
;
226 audiobuffer_fillpos
= 0;
229 static void pcmbuf_under_watermark(void)
231 #ifdef HAVE_PRIORITY_SCHEDULING
232 static int old_priority
= 0;
234 if (LOW_DATA(2) && !old_priority
&& pcm_is_playing())
236 /* Buffer is critically low so override UI priority. */
237 old_priority
= thread_set_priority(codec_thread_p
, PRIORITY_REALTIME
);
239 else if (old_priority
)
241 /* Set back the original priority. */
242 thread_set_priority(codec_thread_p
, old_priority
);
247 /* Fill audio buffer by boosting cpu */
250 /* Disable crossfade if < .5s of audio */
253 crossfade_active
= false;
257 void pcmbuf_set_event_handler(void (*event_handler
)(void))
259 pcmbuf_event_handler
= event_handler
;
262 unsigned int pcmbuf_get_latency(void)
264 /* Be careful how this calculation is rearranted, it's easy to overflow */
265 size_t bytes
= pcmbuf_unplayed_bytes
+ pcm_get_bytes_waiting();
266 return bytes
/ 4 / (NATIVE_FREQUENCY
/1000);
269 void pcmbuf_set_low_latency(bool state
)
271 low_latency_mode
= state
;
274 bool pcmbuf_is_lowdata(void)
276 if (!pcm_is_playing() || pcm_is_paused() ||
277 crossfade_init
|| crossfade_active
)
280 /* 1 seconds of buffer is low data */
284 /* Amount of bytes left in the buffer. */
285 inline size_t pcmbuf_free(void)
287 if (pcmbuf_read
!= NULL
)
289 void *read
= pcmbuf_read
->addr
;
290 void *write
= &audiobuffer
[audiobuffer_pos
+ audiobuffer_fillpos
];
292 return (size_t)(read
- write
) + pcmbuf_size
;
294 return (size_t) (read
- write
);
299 bool pcmbuf_crossfade_init(bool manual_skip
)
301 /* Can't do two crossfades at once and, no fade if pcm is off now */
302 if (crossfade_init
|| crossfade_active
|| !pcm_is_playing())
310 /* Not enough data, or crossfade disabled, flush the old data instead */
311 if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode
)
313 pcmbuf_flush_fillpos();
318 /* Don't enable mix mode when skipping tracks manually. */
320 crossfade_mixmode
= false;
322 crossfade_mixmode
= global_settings
.crossfade_fade_out_mixmode
;
324 crossfade_init
= true;
330 void pcmbuf_play_stop(void)
332 /** Prevent a very tiny pop from happening by muting audio
333 * until dma has been initialized. */
338 pcmbuf_unplayed_bytes
= 0;
339 pcmbuf_mix_chunk
= NULL
;
341 pcmbuf_write_end
->link
= pcmbuf_read
;
342 pcmbuf_write_end
= pcmbuf_read_end
;
343 pcmbuf_read
= pcmbuf_read_end
= NULL
;
346 audiobuffer_fillpos
= 0;
347 crossfade_init
= false;
348 crossfade_active
= false;
349 pcmbuf_flush
= false;
352 int pcmbuf_used_descs(void) {
353 struct pcmbufdesc
*pcmbuf_temp
= pcmbuf_read
;
355 while (pcmbuf_temp
) {
356 pcmbuf_temp
= pcmbuf_temp
->link
;
362 int pcmbuf_descs(void) {
363 return pcmbuf_size
/ PCMBUF_MINAVG_CHUNK
;
366 size_t get_pcmbuf_descsize(void) {
367 return pcmbuf_descsize
;
370 static void pcmbuf_init_pcmbuffers(void) {
371 struct pcmbufdesc
*next
= pcmbuf_write
;
373 pcmbuf_write_end
= pcmbuf_write
;
374 while ((void *)next
< (void *)audiobufend
) {
375 pcmbuf_write_end
->link
=next
;
376 pcmbuf_write_end
=next
;
381 /* Initialize the pcmbuffer the structure looks like this:
382 * ...CODECBUFFER|---------PCMBUF---------|GUARDBUF|DESCS| */
383 void pcmbuf_init(size_t bufsize
)
385 pcmbuf_size
= bufsize
;
386 pcmbuf_descsize
= pcmbuf_descs()*sizeof(struct pcmbufdesc
);
387 audiobuffer
= (char *)&audiobuf
[(audiobufend
- audiobuf
) -
388 (pcmbuf_size
+ PCMBUF_MIX_CHUNK
* 2 + pcmbuf_descsize
)];
389 fadebuf
= &audiobuffer
[pcmbuf_size
];
390 voicebuf
= &fadebuf
[PCMBUF_MIX_CHUNK
];
391 pcmbuf_write
= (struct pcmbufdesc
*)(&voicebuf
[PCMBUF_MIX_CHUNK
]);
392 pcmbuf_init_pcmbuffers();
393 position_callback
= NULL
;
394 pcmbuf_event_handler
= NULL
;
398 size_t pcmbuf_get_bufsize(void)
403 void pcmbuf_pause(bool pause
) {
406 pcm_play_pause(!pause
);
412 /* Force playback. */
413 void pcmbuf_play_start(void)
415 if (!pcm_is_playing() && pcmbuf_unplayed_bytes
)
417 /** Prevent a very tiny pop from happening by muting audio
418 * until dma has been initialized. */
421 if (pcmbuf_read
!= NULL
)
423 last_chunksize
= pcmbuf_read
->size
;
424 pcmbuf_unplayed_bytes
-= last_chunksize
;
425 pcm_play_data(pcmbuf_callback
,
426 (unsigned char *)pcmbuf_read
->addr
, last_chunksize
);
429 /* Now unmute the audio. */
435 * Commit samples waiting to the pcm buffer.
437 static bool pcmbuf_flush_fillpos(void)
439 if (audiobuffer_fillpos
) {
440 /* Never use the last buffer descriptor */
441 while (pcmbuf_write
== pcmbuf_write_end
) {
442 /* If this happens, something is being stupid */
443 if (!pcm_is_playing()) {
444 logf("pcmbuf_flush_fillpos error");
447 /* Let approximately one chunk of data playback */
448 sleep(HZ
*PCMBUF_TARGET_CHUNK
/(NATIVE_FREQUENCY
*4));
457 * Completely process the crossfade fade out effect with current pcm buffer.
459 static void crossfade_process_buffer(size_t fade_in_delay
,
460 size_t fade_out_delay
, size_t fade_out_rem
)
462 if (!crossfade_mixmode
)
464 /* Fade out the specified amount of the already processed audio */
465 size_t total_fade_out
= fade_out_rem
;
466 size_t fade_out_sample
;
467 struct pcmbufdesc
*fade_out_chunk
= crossfade_chunk
;
469 /* Find the right chunk to start fading out */
470 fade_out_delay
+= crossfade_sample
* 2;
471 while (fade_out_delay
!= 0 && fade_out_delay
>= fade_out_chunk
->size
)
473 fade_out_delay
-= fade_out_chunk
->size
;
474 fade_out_chunk
= fade_out_chunk
->link
;
476 /* The start sample within the chunk */
477 fade_out_sample
= fade_out_delay
/ 2;
479 while (fade_out_rem
> 0)
481 /* Each 1/10 second of audio will have the same fade applied */
482 size_t block_rem
= MIN(NATIVE_FREQUENCY
* 4 / 10, fade_out_rem
);
483 int factor
= (fade_out_rem
<< 8) / total_fade_out
;
485 fade_out_rem
-= block_rem
;
487 /* Fade this block */
488 while (block_rem
> 0 && fade_out_chunk
!= NULL
)
490 /* Fade one sample */
491 short *buf
= (short *)(fade_out_chunk
->addr
);
492 int sample
= buf
[fade_out_sample
];
493 buf
[fade_out_sample
++] = (sample
* factor
) >> 8;
496 /* Move to the next chunk as needed */
497 if (fade_out_sample
* 2 >= fade_out_chunk
->size
)
499 fade_out_chunk
= fade_out_chunk
->link
;
506 /* Find the right chunk and sample to start fading in */
507 fade_in_delay
+= crossfade_sample
* 2;
508 while (fade_in_delay
!= 0 && fade_in_delay
>= crossfade_chunk
->size
)
510 fade_in_delay
-= crossfade_chunk
->size
;
511 crossfade_chunk
= crossfade_chunk
->link
;
513 crossfade_sample
= fade_in_delay
/ 2;
514 logf("process done!");
517 /* Initializes crossfader, calculates all necessary parameters and
518 * performs fade-out with the pcm buffer. */
519 static void crossfade_start(void)
521 size_t crossfade_rem
;
522 size_t crossfade_need
;
524 size_t fade_out_delay
;
525 size_t fade_in_delay
;
527 crossfade_init
= false;
528 /* Reject crossfade if less than .5s of data */
530 logf("crossfade rejected");
535 logf("crossfade_start");
536 pcmbuf_flush_fillpos();
537 crossfade_active
= true;
539 /* Initialize the crossfade buffer size to all of the buffered data that
540 * has not yet been sent to the DMA */
541 crossfade_rem
= pcmbuf_unplayed_bytes
;
542 crossfade_chunk
= pcmbuf_read
->link
;
543 crossfade_sample
= 0;
545 /* Get fade out delay from settings. */
547 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_out_delay
* 4;
549 /* Get fade out duration from settings. */
551 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_out_duration
* 4;
553 crossfade_need
= fade_out_delay
+ fade_out_rem
;
554 /* We want only to modify the last part of the buffer. */
555 if (crossfade_rem
> crossfade_need
)
557 size_t crossfade_extra
= crossfade_rem
- crossfade_need
;
558 while (crossfade_extra
> crossfade_chunk
->size
)
560 crossfade_extra
-= crossfade_chunk
->size
;
561 crossfade_chunk
= crossfade_chunk
->link
;
563 crossfade_sample
= crossfade_extra
/ 2;
565 /* Truncate fade out duration if necessary. */
566 else if (crossfade_rem
< crossfade_need
)
568 size_t crossfade_short
= crossfade_need
- crossfade_rem
;
569 if (fade_out_rem
>= crossfade_short
)
570 fade_out_rem
-= crossfade_short
;
573 fade_out_delay
-= crossfade_short
- fade_out_rem
;
578 /* Get also fade in duration and delays from settings. */
579 crossfade_fade_in_total
=
580 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_in_duration
* 4;
581 crossfade_fade_in_rem
= crossfade_fade_in_total
;
584 NATIVE_FREQUENCY
* global_settings
.crossfade_fade_in_delay
* 4;
586 crossfade_process_buffer(fade_in_delay
, fade_out_delay
, fade_out_rem
);
589 /* Returns the number of bytes _NOT_ mixed */
590 static size_t crossfade_fade_mix(int factor
, const char *buf
, size_t fade_rem
)
592 const short *input_buf
= (const short *)buf
;
593 short *output_buf
= (short *)(crossfade_chunk
->addr
);
594 short *chunk_end
= (short *)((size_t)output_buf
+ crossfade_chunk
->size
);
595 output_buf
= &output_buf
[crossfade_sample
];
599 int sample
= *input_buf
++;
600 sample
= ((sample
* factor
) >> 8) + *output_buf
;
601 *output_buf
++ = MIN(32767, MAX(-32768, sample
));
604 if (output_buf
>= chunk_end
)
606 crossfade_chunk
= crossfade_chunk
->link
;
607 if (!crossfade_chunk
)
609 output_buf
= (short *)(crossfade_chunk
->addr
);
610 chunk_end
= (short *)((size_t)output_buf
+ crossfade_chunk
->size
);
613 crossfade_sample
= (size_t)(output_buf
- (short *)(crossfade_chunk
->addr
));
617 /* Returns the number of bytes _NOT_ mixed */
618 static size_t crossfade_mix(const char *buf
, size_t length
)
620 const short *input_buf
= (const short *)buf
;
621 short *output_buf
= (short *)(crossfade_chunk
->addr
);
622 short *chunk_end
= (short *)((size_t)output_buf
+ crossfade_chunk
->size
);
623 output_buf
= &output_buf
[crossfade_sample
];
627 int sample
= *input_buf
++ + *output_buf
;
628 *output_buf
++ = MIN(32767, MAX(-32768, sample
));
631 if (output_buf
>= chunk_end
)
633 crossfade_chunk
= crossfade_chunk
->link
;
634 if (!crossfade_chunk
)
636 output_buf
= (short *)(crossfade_chunk
->addr
);
637 chunk_end
= (short *)((size_t)output_buf
+ crossfade_chunk
->size
);
640 crossfade_sample
= (size_t)(output_buf
- (short *)(crossfade_chunk
->addr
));
644 static void pcmbuf_flush_buffer(const char *buf
, size_t length
)
648 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
649 if (NEED_FLUSH(audiobuffer_index
))
651 pcmbuf_flush_fillpos();
652 audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
654 copy_n
= MIN(length
, pcmbuf_size
- audiobuffer_index
);
655 memcpy(&audiobuffer
[audiobuffer_index
], buf
, copy_n
);
657 audiobuffer_fillpos
+= copy_n
;
662 static void flush_crossfade(char *buf
, size_t length
)
666 if (crossfade_fade_in_rem
)
671 /* Fade factor for this packet */
673 ((crossfade_fade_in_total
- crossfade_fade_in_rem
) << 8) /
674 crossfade_fade_in_total
;
676 size_t fade_rem
= MIN(length
, crossfade_fade_in_rem
);
678 /* We _will_ fade this many bytes */
679 crossfade_fade_in_rem
-= fade_rem
;
684 size_t fade_total
= fade_rem
;
685 fade_rem
= crossfade_fade_mix(factor
, buf
, fade_rem
);
686 length
-= fade_total
- fade_rem
;
687 buf
+= fade_total
- fade_rem
;
694 samples
= fade_rem
/ 2;
695 input_buf
= (short *)buf
;
696 /* Fade remaining samples in place */
699 int sample
= *input_buf
;
700 *input_buf
++ = (sample
* factor
) >> 8;
709 size_t mix_total
= length
;
710 length
= crossfade_mix(buf
, length
);
711 buf
+= mix_total
- length
;
716 /* Flush samples to the buffer */
717 while (!prepare_insert(length
))
719 pcmbuf_flush_buffer(buf
, length
);
724 static bool prepare_insert(size_t length
)
726 if (low_latency_mode
)
729 if (pcmbuf_unplayed_bytes
> NATIVE_FREQUENCY
* 4 / 4
734 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
735 if (pcmbuf_free() < length
+ PCMBUF_MIN_CHUNK
)
738 if (!pcm_is_playing())
749 logf("pcm starting");
753 else if (pcmbuf_unplayed_bytes
<= pcmbuf_watermark
)
754 pcmbuf_under_watermark();
759 void* pcmbuf_request_buffer(size_t length
, size_t *realsize
)
764 if (crossfade_active
) {
765 *realsize
= MIN(length
, PCMBUF_MIX_CHUNK
);
770 if(prepare_insert(length
))
772 size_t audiobuffer_index
= audiobuffer_pos
+ audiobuffer_fillpos
;
774 if (pcmbuf_size
- audiobuffer_index
>= PCMBUF_MIN_CHUNK
)
776 /* Usual case, there's space here */
777 return &audiobuffer
[audiobuffer_index
];
781 /* Flush and wrap the buffer */
782 pcmbuf_flush_fillpos();
784 return &audiobuffer
[0];
795 void* pcmbuf_request_voice_buffer(size_t length
, size_t *realsize
, bool mix
)
799 if (pcmbuf_read
== NULL
)
804 else if (pcmbuf_mix_chunk
|| pcmbuf_read
->link
)
806 *realsize
= MIN(length
, PCMBUF_MIX_CHUNK
);
816 return pcmbuf_request_buffer(length
, realsize
);
819 bool pcmbuf_is_crossfade_active(void)
821 return crossfade_active
|| crossfade_init
;
824 void pcmbuf_write_complete(size_t length
)
826 if (crossfade_active
)
828 flush_crossfade(fadebuf
, length
);
829 if (!(crossfade_fade_in_rem
|| crossfade_chunk
))
830 crossfade_active
= false;
834 audiobuffer_fillpos
+= length
;
836 if (NEED_FLUSH(audiobuffer_pos
+ audiobuffer_fillpos
))
837 pcmbuf_flush_fillpos();
841 bool pcmbuf_insert_buffer(char *buf
, size_t length
)
843 if (crossfade_active
)
845 flush_crossfade(buf
, length
);
846 if (!(crossfade_fade_in_rem
|| crossfade_chunk
))
847 crossfade_active
= false;
851 if (!prepare_insert(length
))
853 pcmbuf_flush_buffer(buf
, length
);
858 /* Generates a constant square wave sound with a given frequency
859 in Hertz for a duration in milliseconds. */
860 void pcmbuf_beep(unsigned int frequency
, size_t duration
, int amplitude
)
862 unsigned int count
= 0, i
= 0;
863 unsigned int interval
= NATIVE_FREQUENCY
/ frequency
;
866 short *pcmbuf_end
= (short *)fadebuf
;
867 size_t samples
= NATIVE_FREQUENCY
/ 1000 * duration
;
869 if (pcm_is_playing() && pcmbuf_read
!= NULL
)
871 if (pcmbuf_read
->link
)
873 /* Get the next chunk */
874 char *pcmbuf_mix_buf
= pcmbuf_read
->link
->addr
;
875 /* Give at least 1/8s clearance. */
876 buf
= (short *)&pcmbuf_mix_buf
[NATIVE_FREQUENCY
* 4 / 8];
880 logf("No place to beep");
884 while (i
++ < samples
)
887 *buf
++ = MIN(MAX(sample
+ amplitude
, -32768), 32767);
888 if (buf
> pcmbuf_end
)
889 buf
= (short *)audiobuffer
;
891 *buf
++ = MIN(MAX(sample
+ amplitude
, -32768), 32767);
893 /* Toggle square wav side */
894 if (++count
>= interval
)
897 amplitude
= -amplitude
;
899 if (buf
> pcmbuf_end
)
900 buf
= (short *)audiobuffer
;
905 buf
= (short *)audiobuffer
;
906 while (i
++ < samples
)
909 if (buf
> pcmbuf_end
)
910 buf
= (short *)audiobuffer
;
913 /* Toggle square wav side */
914 if (++count
>= interval
)
917 amplitude
= -amplitude
;
919 if (buf
> pcmbuf_end
)
920 buf
= (short *)audiobuffer
;
922 pcm_play_data(NULL
, (unsigned char *)audiobuffer
, samples
* 4);
926 /* Returns pcm buffer usage in percents (0 to 100). */
927 int pcmbuf_usage(void)
929 return pcmbuf_unplayed_bytes
* 100 / pcmbuf_size
;
932 int pcmbuf_mix_free(void)
934 if (pcmbuf_mix_chunk
)
937 (size_t)&((short *)pcmbuf_mix_chunk
->addr
)[pcmbuf_mix_sample
];
938 size_t my_write_pos
= (size_t)&audiobuffer
[audiobuffer_pos
];
939 if (my_write_pos
< my_mix_end
)
940 my_write_pos
+= pcmbuf_size
;
941 return (my_write_pos
- my_mix_end
) * 100 / pcmbuf_unplayed_bytes
;
946 void pcmbuf_mix_voice(size_t length
)
948 short *ibuf
= (short *)voicebuf
;
950 size_t chunk_samples
;
952 if (pcmbuf_mix_chunk
== NULL
&& pcmbuf_read
!= NULL
)
954 pcmbuf_mix_chunk
= pcmbuf_read
->link
;
955 /* Start 1/8s into the next chunk */
956 pcmbuf_mix_sample
= NATIVE_FREQUENCY
* 4 / 16;
958 if (!pcmbuf_mix_chunk
)
961 obuf
= (short *)pcmbuf_mix_chunk
->addr
;
962 chunk_samples
= pcmbuf_mix_chunk
->size
/ 2;
966 while (length
-- > 0) {
967 int sample
= *ibuf
++;
968 if (pcmbuf_mix_sample
>= chunk_samples
)
970 pcmbuf_mix_chunk
= pcmbuf_mix_chunk
->link
;
971 if (!pcmbuf_mix_chunk
)
973 pcmbuf_mix_sample
= 0;
974 obuf
= pcmbuf_mix_chunk
->addr
;
975 chunk_samples
= pcmbuf_mix_chunk
->size
/ 2;
977 sample
+= obuf
[pcmbuf_mix_sample
] >> 2;
978 obuf
[pcmbuf_mix_sample
++] = MIN(MAX(sample
, -32768), 32767);
982 void pcmbuf_crossfade_enable(bool on_off
)
984 crossfade_enabled
= on_off
;
986 if (crossfade_enabled
) {
987 /* If crossfading, try to keep the buffer full other than 1 second */
988 pcmbuf_set_watermark_bytes(pcmbuf_size
- (NATIVE_FREQUENCY
* 4 * 1));
990 /* Otherwise, just keep it above 2 second */
991 pcmbuf_set_watermark_bytes(PCMBUF_WATERMARK
);
995 bool pcmbuf_is_crossfade_enabled(void)
997 if (global_settings
.crossfade
== CROSSFADE_ENABLE_SHUFFLE
)
998 return global_settings
.playlist_shuffle
;
1000 return crossfade_enabled
;