From 4bd871544908aaf2d6258315923c2d95ec9b7725 Mon Sep 17 00:00:00 2001 From: Jens Arnold Date: Sat, 11 Mar 2006 10:22:20 +0000 Subject: Don't crash the simulator when the pcm callback runs out of data. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8998 a1c6a512-1295-4272-9138-f99709370657 --- uisimulator/sdl/sound.c | 63 +++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/uisimulator/sdl/sound.c b/uisimulator/sdl/sound.c index d23bcc8094..a95b19d417 100644 --- a/uisimulator/sdl/sound.c +++ b/uisimulator/sdl/sound.c @@ -24,6 +24,7 @@ #include #include #include +#include "debug.h" #include "kernel.h" #include "sound.h" #include "SDL.h" @@ -209,54 +210,50 @@ void pcm_calculate_peaks(int *left, int *right) void sdl_audio_callback(void *udata, Uint8 *stream, int len) { - Uint32 pcm_data_played, need_to_play; + Uint32 have_now; FILE *debug = (FILE *)udata; /* At all times we need to write a full 'len' bytes to stream. */ if (pcm_data_size > 0) { - /* We have some PCM data to play. Play as much as we can. */ + have_now = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len; - pcm_data_played = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len; - - memcpy(stream, pcm_data, pcm_data_played); + memcpy(stream, pcm_data, have_now); if (debug != NULL) { - fwrite(pcm_data, sizeof(Uint8), pcm_data_played, debug); + fwrite(pcm_data, sizeof(Uint8), have_now, debug); } - - stream += pcm_data_played; - need_to_play = len - pcm_data_played; - pcm_data += pcm_data_played; - pcm_data_size -= pcm_data_played; - - while(need_to_play > 0) { - /* Loop until we have written enough */ + stream += have_now; + len -= have_now; + pcm_data += have_now; + pcm_data_size -= have_now; + } + while (len > 0) + { + if (callback_for_more) { callback_for_more(&pcm_data, &pcm_data_size); + } else { + pcm_data = NULL; + pcm_data_size = 0; + } + if (pcm_data_size > 0) { + have_now = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len; - if (pcm_data_size > 0) { - /* We got more data */ - pcm_data_played = (need_to_play > pcm_data_size) ? pcm_data_size : need_to_play; - - memcpy(stream, pcm_data, pcm_data_played); - - if (debug != NULL) { - fwrite(pcm_data, sizeof(Uint8), pcm_data_played, debug); - } + memcpy(stream, pcm_data, have_now); - stream += pcm_data_played; - need_to_play -= pcm_data_played; - pcm_data += pcm_data_played; - pcm_data_size -= pcm_data_played; + if (debug != NULL) { + fwrite(pcm_data, sizeof(Uint8), have_now, debug); } + stream += have_now; + len -= have_now; + pcm_data += have_now; + pcm_data_size -= have_now; + } else { + DEBUGF("sdl_audio_callback: No Data.\n"); + sdl_dma_stop(); + break; } - } else { - pcm_data_size = 0; - pcm_data = NULL; - - /* No data, try and get some */ - callback_for_more(&pcm_data, &pcm_data_size); } } -- cgit v1.2.3