summaryrefslogtreecommitdiff
path: root/apps/pcmbuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/pcmbuf.c')
-rw-r--r--apps/pcmbuf.c64
1 files changed, 32 insertions, 32 deletions
diff --git a/apps/pcmbuf.c b/apps/pcmbuf.c
index 79a412c869..c0791987ce 100644
--- a/apps/pcmbuf.c
+++ b/apps/pcmbuf.c
@@ -116,10 +116,10 @@ static bool boost_mode;
116void pcmbuf_boost(bool state) 116void pcmbuf_boost(bool state)
117{ 117{
118 static bool boost_state = false; 118 static bool boost_state = false;
119 119
120 if (crossfade_init || crossfade_active || boost_mode) 120 if (crossfade_init || crossfade_active || boost_mode)
121 return; 121 return;
122 122
123 if (state != boost_state) { 123 if (state != boost_state) {
124 cpu_boost(state); 124 cpu_boost(state);
125 boost_state = state; 125 boost_state = state;
@@ -138,7 +138,7 @@ void pcmbuf_set_boost_mode(bool state)
138/* This function has 2 major logical parts (separated by brackets both for 138/* This function has 2 major logical parts (separated by brackets both for
139 * readability and variable scoping). The first part performs the 139 * readability and variable scoping). The first part performs the
140 * operastions related to finishing off the last buffer we fed to the DMA. 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 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 142 * to the DMA. Finally the function checks the status of the buffer and
143 * boosts if necessary */ 143 * boosts if necessary */
144static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR; 144static void pcmbuf_callback(unsigned char** start, size_t* size) ICODE_ATTR;
@@ -267,7 +267,7 @@ bool pcmbuf_is_lowdata(void)
267 if (!pcm_is_playing() || pcm_is_paused() || 267 if (!pcm_is_playing() || pcm_is_paused() ||
268 crossfade_init || crossfade_active) 268 crossfade_init || crossfade_active)
269 return false; 269 return false;
270 270
271 /* 0.5 seconds of buffer is low data */ 271 /* 0.5 seconds of buffer is low data */
272 return LOW_DATA(2); 272 return LOW_DATA(2);
273} 273}
@@ -290,9 +290,9 @@ bool pcmbuf_crossfade_init(bool manual_skip)
290 crossfade_mode = global_settings.crossfade_fade_out_mixmode 290 crossfade_mode = global_settings.crossfade_fade_out_mixmode
291 ? CFM_MIX : CFM_CROSSFADE; 291 ? CFM_MIX : CFM_CROSSFADE;
292 crossfade_init = true; 292 crossfade_init = true;
293 293
294 return true; 294 return true;
295 295
296} 296}
297 297
298void pcmbuf_play_stop(void) 298void pcmbuf_play_stop(void)
@@ -303,7 +303,7 @@ void pcmbuf_play_stop(void)
303 pcm_mute(true); 303 pcm_mute(true);
304 pcm_play_stop(); 304 pcm_play_stop();
305 pcm_mute(false); 305 pcm_mute(false);
306 306
307 pcmbuf_unplayed_bytes = 0; 307 pcmbuf_unplayed_bytes = 0;
308 pcmbuf_mix_used_bytes = 0; 308 pcmbuf_mix_used_bytes = 0;
309 if (pcmbuf_read) { 309 if (pcmbuf_read) {
@@ -316,7 +316,7 @@ void pcmbuf_play_stop(void)
316 audiobuffer_free = pcmbuf_size; 316 audiobuffer_free = pcmbuf_size;
317 crossfade_init = false; 317 crossfade_init = false;
318 crossfade_active = false; 318 crossfade_active = false;
319 319
320 pcmbuf_set_boost_mode(false); 320 pcmbuf_set_boost_mode(false);
321 pcmbuf_boost(false); 321 pcmbuf_boost(false);
322 322
@@ -340,7 +340,7 @@ int pcmbuf_descs(void) {
340size_t get_pcmbuf_descsize(void) { 340size_t get_pcmbuf_descsize(void) {
341 return pcmbuf_descsize; 341 return pcmbuf_descsize;
342} 342}
343 343
344static void pcmbuf_init_pcmbuffers(void) { 344static void pcmbuf_init_pcmbuffers(void) {
345 struct pcmbufdesc *next = pcmbuf_write; 345 struct pcmbufdesc *next = pcmbuf_write;
346 next++; 346 next++;
@@ -351,7 +351,7 @@ static void pcmbuf_init_pcmbuffers(void) {
351 next++; 351 next++;
352 } 352 }
353} 353}
354 354
355/* Initialize the pcmbuffer the structure looks like this: 355/* Initialize the pcmbuffer the structure looks like this:
356 * ...CODECBUFFER|---------PCMBUF---------|GUARDBUF|DESCS| */ 356 * ...CODECBUFFER|---------PCMBUF---------|GUARDBUF|DESCS| */
357void pcmbuf_init(size_t bufsize) 357void pcmbuf_init(size_t bufsize)
@@ -383,7 +383,7 @@ static void pcmbuf_flush_audio(void)
383 pcmbuf_play_stop(); 383 pcmbuf_play_stop();
384 return ; 384 return ;
385 } 385 }
386 386
387 pcmbuf_boost(true); 387 pcmbuf_boost(true);
388 crossfade_mode = CFM_FLUSH; 388 crossfade_mode = CFM_FLUSH;
389 crossfade_init = true; 389 crossfade_init = true;
@@ -408,10 +408,10 @@ void pcmbuf_play_start(void)
408 /** Prevent a very tiny pop from happening by muting audio 408 /** Prevent a very tiny pop from happening by muting audio
409 * until dma has been initialized. */ 409 * until dma has been initialized. */
410 pcm_mute(true); 410 pcm_mute(true);
411 411
412 last_chunksize = pcmbuf_read->size; 412 last_chunksize = pcmbuf_read->size;
413 pcmbuf_unplayed_bytes -= last_chunksize; 413 pcmbuf_unplayed_bytes -= last_chunksize;
414 pcm_play_data(pcmbuf_callback, 414 pcm_play_data(pcmbuf_callback,
415 (unsigned char *)pcmbuf_read->addr, last_chunksize); 415 (unsigned char *)pcmbuf_read->addr, last_chunksize);
416 416
417 /* Now unmute the audio. */ 417 /* Now unmute the audio. */
@@ -470,7 +470,7 @@ static void crossfade_process_buffer(size_t fade_in_delay,
470 size_t block_rem = MIN(NATIVE_FREQUENCY * 2 / 10, fade_out_rem); 470 size_t block_rem = MIN(NATIVE_FREQUENCY * 2 / 10, fade_out_rem);
471 unsigned int factor = (fade_out_rem << 8) / total_fade_out; 471 unsigned int factor = (fade_out_rem << 8) / total_fade_out;
472 short *block_end = buf + block_rem; 472 short *block_end = buf + block_rem;
473 473
474 fade_out_rem -= block_rem; 474 fade_out_rem -= block_rem;
475 475
476 /* Fade this block */ 476 /* Fade this block */
@@ -508,7 +508,7 @@ static void crossfade_start(void)
508 size_t fade_out_rem = 0; 508 size_t fade_out_rem = 0;
509 unsigned int fade_out_delay = 0; 509 unsigned int fade_out_delay = 0;
510 unsigned fade_in_delay = 0; 510 unsigned fade_in_delay = 0;
511 511
512 crossfade_init = 0; 512 crossfade_init = 0;
513 /* Reject crossfade if less than .5s of data */ 513 /* Reject crossfade if less than .5s of data */
514 if (LOW_DATA(2)) { 514 if (LOW_DATA(2)) {
@@ -556,7 +556,7 @@ static void crossfade_start(void)
556 556
557 fade_in_delay = NATIVE_FREQUENCY 557 fade_in_delay = NATIVE_FREQUENCY
558 * global_settings.crossfade_fade_in_delay * 2; 558 * global_settings.crossfade_fade_in_delay * 2;
559 559
560 /* Decrease the fade out delay if necessary. */ 560 /* Decrease the fade out delay if necessary. */
561 if (crossfade_rem < fade_out_rem + fade_out_delay) 561 if (crossfade_rem < fade_out_rem + fade_out_delay)
562 fade_out_delay -= 562 fade_out_delay -=
@@ -568,16 +568,16 @@ static void crossfade_start(void)
568 crossfade_fade_in_amount = 0; 568 crossfade_fade_in_amount = 0;
569 break ; 569 break ;
570 } 570 }
571 571
572 if (crossfade_pos < crossfade_rem * 2) 572 if (crossfade_pos < crossfade_rem * 2)
573 crossfade_pos += pcmbuf_size; 573 crossfade_pos += pcmbuf_size;
574 crossfade_pos -= crossfade_rem*2; 574 crossfade_pos -= crossfade_rem*2;
575 575
576 if (crossfade_mode != CFM_FLUSH) { 576 if (crossfade_mode != CFM_FLUSH) {
577 /* Process the fade out part of the crossfade. */ 577 /* Process the fade out part of the crossfade. */
578 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem); 578 crossfade_process_buffer(fade_in_delay, fade_out_delay, fade_out_rem);
579 } 579 }
580 580
581} 581}
582 582
583/** 583/**
@@ -590,7 +590,7 @@ static void fade_insert(const short *inbuf, size_t length)
590 int factor; 590 int factor;
591 unsigned int i, samples; 591 unsigned int i, samples;
592 short *buf; 592 short *buf;
593 593
594 factor = ((crossfade_fade_in_amount-crossfade_fade_in_rem)<<8) 594 factor = ((crossfade_fade_in_amount-crossfade_fade_in_rem)<<8)
595 /crossfade_fade_in_amount; 595 /crossfade_fade_in_amount;
596 596
@@ -600,7 +600,7 @@ static void fade_insert(const short *inbuf, size_t length)
600 sleep(1); 600 sleep(1);
601 } 601 }
602 audiobuffer_free -= length; 602 audiobuffer_free -= length;
603 603
604 while (length > 0) { 604 while (length > 0) {
605 unsigned int audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; 605 unsigned int audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
606 /* Flush as needed */ 606 /* Flush as needed */
@@ -609,14 +609,14 @@ static void fade_insert(const short *inbuf, size_t length)
609 pcmbuf_flush_fillpos(); 609 pcmbuf_flush_fillpos();
610 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; 610 audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos;
611 } 611 }
612 612
613 copy_n = MIN(length, pcmbuf_size - audiobuffer_index); 613 copy_n = MIN(length, pcmbuf_size - audiobuffer_index);
614 614
615 buf = (short *)&audiobuffer[audiobuffer_index]; 615 buf = (short *)&audiobuffer[audiobuffer_index];
616 samples = copy_n / 2; 616 samples = copy_n / 2;
617 for (i = 0; i < samples; i++) 617 for (i = 0; i < samples; i++)
618 buf[i] = (inbuf[i] * factor) >> 8; 618 buf[i] = (inbuf[i] * factor) >> 8;
619 619
620 inbuf += samples; 620 inbuf += samples;
621 audiobuffer_fillpos += copy_n; 621 audiobuffer_fillpos += copy_n;
622 length -= copy_n; 622 length -= copy_n;
@@ -632,7 +632,7 @@ static int crossfade(short *buf, const short *buf2, unsigned int length)
632 unsigned int i; 632 unsigned int i;
633 size_t size_insert = 0; 633 size_t size_insert = 0;
634 int factor; 634 int factor;
635 635
636 size = MIN(length, crossfade_rem); 636 size = MIN(length, crossfade_rem);
637 switch (crossfade_mode) { 637 switch (crossfade_mode) {
638 /* Fade in the current stream and mix it. */ 638 /* Fade in the current stream and mix it. */
@@ -640,9 +640,9 @@ static int crossfade(short *buf, const short *buf2, unsigned int length)
640 case CFM_CROSSFADE: 640 case CFM_CROSSFADE:
641 factor = ((crossfade_fade_in_amount-crossfade_fade_in_rem)<<8) / 641 factor = ((crossfade_fade_in_amount-crossfade_fade_in_rem)<<8) /
642 crossfade_fade_in_amount; 642 crossfade_fade_in_amount;
643 643
644 for (i = 0; i < size; i++) { 644 for (i = 0; i < size; i++) {
645 buf[i] = MIN(32767, MAX(-32768, 645 buf[i] = MIN(32767, MAX(-32768,
646 buf[i] + ((buf2[i] * factor) >> 8))); 646 buf[i] + ((buf2[i] * factor) >> 8)));
647 } 647 }
648 break ; 648 break ;
@@ -670,7 +670,7 @@ static int crossfade(short *buf, const short *buf2, unsigned int length)
670 fade_insert(&buf2[size], size_insert*2); 670 fade_insert(&buf2[size], size_insert*2);
671 crossfade_fade_in_rem -= size_insert; 671 crossfade_fade_in_rem -= size_insert;
672 } 672 }
673 673
674 if (crossfade_fade_in_rem == 0) 674 if (crossfade_fade_in_rem == 0)
675 crossfade_active = false; 675 crossfade_active = false;
676 } 676 }
@@ -726,7 +726,7 @@ static bool prepare_insert(size_t length)
726 && pcm_is_playing()) 726 && pcm_is_playing())
727 return false; 727 return false;
728 } 728 }
729 729
730 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */ 730 /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */
731 if (audiobuffer_free < length + PCMBUF_MIN_CHUNK && !crossfade_active) 731 if (audiobuffer_free < length + PCMBUF_MIN_CHUNK && !crossfade_active)
732 { 732 {
@@ -835,7 +835,7 @@ static inline short* get_mix_insert_pos(void) {
835 /* Give at least 1/8s clearance here */ 835 /* Give at least 1/8s clearance here */
836 size_t pcmbuf_mix_back_pos = 836 size_t pcmbuf_mix_back_pos =
837 pcmbuf_unplayed_bytes - NATIVE_FREQUENCY * 4 / 8; 837 pcmbuf_unplayed_bytes - NATIVE_FREQUENCY * 4 / 8;
838 838
839 if (audiobuffer_pos < pcmbuf_mix_back_pos) 839 if (audiobuffer_pos < pcmbuf_mix_back_pos)
840 return (short *)&audiobuffer[pcmbuf_size + 840 return (short *)&audiobuffer[pcmbuf_size +
841 audiobuffer_pos - pcmbuf_mix_back_pos]; 841 audiobuffer_pos - pcmbuf_mix_back_pos];
@@ -854,7 +854,7 @@ void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
854 short *pcmbuf_end = (short *)guardbuf; 854 short *pcmbuf_end = (short *)guardbuf;
855 bool playing = pcm_is_playing(); 855 bool playing = pcm_is_playing();
856 size_t samples = NATIVE_FREQUENCY / 1000 * duration; 856 size_t samples = NATIVE_FREQUENCY / 1000 * duration;
857 857
858 if (playing) { 858 if (playing) {
859 buf = get_mix_insert_pos(); 859 buf = get_mix_insert_pos();
860 } else { 860 } else {
@@ -876,7 +876,7 @@ void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
876 sample = *buf; 876 sample = *buf;
877 *buf++ = MIN(MAX(sample - amplitude, -32768), 32767); 877 *buf++ = MIN(MAX(sample - amplitude, -32768), 32767);
878 } 878 }
879 879
880 if (++count >= interval) 880 if (++count >= interval)
881 { 881 {
882 count = 0; 882 count = 0;
@@ -914,7 +914,7 @@ void pcmbuf_mix(char *buf, size_t length)
914 914
915 if (pcmbuf_mix_used_bytes == 0) 915 if (pcmbuf_mix_used_bytes == 0)
916 pcmbuf_reset_mixpos(); 916 pcmbuf_reset_mixpos();
917 917
918 pcmbuf_mix_used_bytes += length; 918 pcmbuf_mix_used_bytes += length;
919 length /= 2; 919 length /= 2;
920 920