summaryrefslogtreecommitdiff
path: root/apps/pcmbuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/pcmbuf.c')
-rw-r--r--apps/pcmbuf.c41
1 files changed, 24 insertions, 17 deletions
diff --git a/apps/pcmbuf.c b/apps/pcmbuf.c
index f8fd2af82a..0aaa49f825 100644
--- a/apps/pcmbuf.c
+++ b/apps/pcmbuf.c
@@ -232,7 +232,7 @@ static inline void pcmbuf_add_chunk(void)
232 pcmbuf_current->link = NULL; 232 pcmbuf_current->link = NULL;
233 /* This is single use only */ 233 /* This is single use only */
234 pcmbuf_event_handler = NULL; 234 pcmbuf_event_handler = NULL;
235 if (pcmbuf_read) { 235 if (pcmbuf_read != NULL) {
236 if (pcmbuf_flush) 236 if (pcmbuf_flush)
237 { 237 {
238 pcmbuf_write_end->link = pcmbuf_read->link; 238 pcmbuf_write_end->link = pcmbuf_read->link;
@@ -305,14 +305,14 @@ bool pcmbuf_is_lowdata(void)
305/* Amount of bytes left in the buffer. */ 305/* Amount of bytes left in the buffer. */
306inline size_t pcmbuf_free(void) 306inline size_t pcmbuf_free(void)
307{ 307{
308 if (pcmbuf_read) 308 if (pcmbuf_read != NULL)
309 { 309 {
310 size_t read = (size_t)pcmbuf_read->addr; 310 void *read = pcmbuf_read->addr;
311 size_t write = 311 void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
312 (size_t)&audiobuffer[audiobuffer_pos + audiobuffer_fillpos];
313 if (read < write) 312 if (read < write)
314 read += pcmbuf_size; 313 return (size_t)(read - write) + pcmbuf_size;
315 return read - write; 314 else
315 return (size_t) (read - write);
316 } 316 }
317 return pcmbuf_size; 317 return pcmbuf_size;
318} 318}
@@ -335,7 +335,6 @@ bool pcmbuf_crossfade_init(bool manual_skip)
335 return false; 335 return false;
336 } 336 }
337 337
338 logf("pcmbuf_crossfade_init");
339 pcmbuf_boost(true); 338 pcmbuf_boost(true);
340 339
341 /* Don't enable mix mode when skipping tracks manually. */ 340 /* Don't enable mix mode when skipping tracks manually. */
@@ -444,10 +443,13 @@ void pcmbuf_play_start(void)
444 * until dma has been initialized. */ 443 * until dma has been initialized. */
445 pcm_mute(true); 444 pcm_mute(true);
446 445
447 last_chunksize = pcmbuf_read->size; 446 if (pcmbuf_read != NULL)
448 pcmbuf_unplayed_bytes -= last_chunksize; 447 {
449 pcm_play_data(pcmbuf_callback, 448 last_chunksize = pcmbuf_read->size;
449 pcmbuf_unplayed_bytes -= last_chunksize;
450 pcm_play_data(pcmbuf_callback,
450 (unsigned char *)pcmbuf_read->addr, last_chunksize); 451 (unsigned char *)pcmbuf_read->addr, last_chunksize);
452 }
451 453
452 /* Now unmute the audio. */ 454 /* Now unmute the audio. */
453 pcm_mute(false); 455 pcm_mute(false);
@@ -462,7 +464,6 @@ static bool pcmbuf_flush_fillpos(void)
462 if (audiobuffer_fillpos) { 464 if (audiobuffer_fillpos) {
463 /* Never use the last buffer descriptor */ 465 /* Never use the last buffer descriptor */
464 while (pcmbuf_write == pcmbuf_write_end) { 466 while (pcmbuf_write == pcmbuf_write_end) {
465 logf("pcmbuf_flush_fillpos no descriptors");
466 /* Deboost to let the playback catchup */ 467 /* Deboost to let the playback catchup */
467 pcmbuf_boost(false); 468 pcmbuf_boost(false);
468 /* If this happens, something is being stupid */ 469 /* If this happens, something is being stupid */
@@ -511,7 +512,7 @@ static void crossfade_process_buffer(size_t fade_in_delay,
511 fade_out_rem -= block_rem; 512 fade_out_rem -= block_rem;
512 513
513 /* Fade this block */ 514 /* Fade this block */
514 while (block_rem > 0) 515 while (block_rem > 0 && fade_out_chunk != NULL)
515 { 516 {
516 /* Fade one sample */ 517 /* Fade one sample */
517 short *buf = (short *)(fade_out_chunk->addr); 518 short *buf = (short *)(fade_out_chunk->addr);
@@ -777,7 +778,8 @@ static bool prepare_insert(size_t length)
777 logf("pcm starting"); 778 logf("pcm starting");
778 pcmbuf_play_start(); 779 pcmbuf_play_start();
779 } 780 }
780 } else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark) 781 }
782 else if (pcmbuf_unplayed_bytes <= pcmbuf_watermark)
781 pcmbuf_under_watermark(); 783 pcmbuf_under_watermark();
782 784
783 return true; 785 return true;
@@ -823,7 +825,12 @@ void* pcmbuf_request_voice_buffer(size_t length, size_t *realsize, bool mix)
823{ 825{
824 if (mix) 826 if (mix)
825 { 827 {
826 if (pcmbuf_mix_chunk || pcmbuf_read->link) 828 if (pcmbuf_read == NULL)
829 {
830 *realsize = 0;
831 return NULL;
832 }
833 else if (pcmbuf_mix_chunk || pcmbuf_read->link)
827 { 834 {
828 *realsize = MIN(length, PCMBUF_MIX_CHUNK); 835 *realsize = MIN(length, PCMBUF_MIX_CHUNK);
829 return voicebuf; 836 return voicebuf;
@@ -888,7 +895,7 @@ void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
888 short *pcmbuf_end = (short *)fadebuf; 895 short *pcmbuf_end = (short *)fadebuf;
889 size_t samples = NATIVE_FREQUENCY / 1000 * duration; 896 size_t samples = NATIVE_FREQUENCY / 1000 * duration;
890 897
891 if (pcm_is_playing()) 898 if (pcm_is_playing() && pcmbuf_read != NULL)
892 { 899 {
893 if (pcmbuf_read->link) 900 if (pcmbuf_read->link)
894 { 901 {
@@ -971,7 +978,7 @@ void pcmbuf_mix_voice(size_t length)
971 short *obuf; 978 short *obuf;
972 size_t chunk_samples; 979 size_t chunk_samples;
973 980
974 if (!pcmbuf_mix_chunk && pcmbuf_read) 981 if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL)
975 { 982 {
976 pcmbuf_mix_chunk = pcmbuf_read->link; 983 pcmbuf_mix_chunk = pcmbuf_read->link;
977 /* Start 1/8s into the next chunk */ 984 /* Start 1/8s into the next chunk */