summaryrefslogtreecommitdiff
path: root/uisimulator/sdl
diff options
context:
space:
mode:
Diffstat (limited to 'uisimulator/sdl')
-rw-r--r--uisimulator/sdl/sound.c63
1 files 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 @@
24#include <stdlib.h> 24#include <stdlib.h>
25#include <stdbool.h> 25#include <stdbool.h>
26#include <memory.h> 26#include <memory.h>
27#include "debug.h"
27#include "kernel.h" 28#include "kernel.h"
28#include "sound.h" 29#include "sound.h"
29#include "SDL.h" 30#include "SDL.h"
@@ -209,54 +210,50 @@ void pcm_calculate_peaks(int *left, int *right)
209 210
210void sdl_audio_callback(void *udata, Uint8 *stream, int len) 211void sdl_audio_callback(void *udata, Uint8 *stream, int len)
211{ 212{
212 Uint32 pcm_data_played, need_to_play; 213 Uint32 have_now;
213 FILE *debug = (FILE *)udata; 214 FILE *debug = (FILE *)udata;
214 215
215 /* At all times we need to write a full 'len' bytes to stream. */ 216 /* At all times we need to write a full 'len' bytes to stream. */
216 217
217 if (pcm_data_size > 0) { 218 if (pcm_data_size > 0) {
218 /* We have some PCM data to play. Play as much as we can. */ 219 have_now = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len;
219 220
220 pcm_data_played = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len; 221 memcpy(stream, pcm_data, have_now);
221
222 memcpy(stream, pcm_data, pcm_data_played);
223 222
224 if (debug != NULL) { 223 if (debug != NULL) {
225 fwrite(pcm_data, sizeof(Uint8), pcm_data_played, debug); 224 fwrite(pcm_data, sizeof(Uint8), have_now, debug);
226 } 225 }
227 226 stream += have_now;
228 stream += pcm_data_played; 227 len -= have_now;
229 need_to_play = len - pcm_data_played; 228 pcm_data += have_now;
230 pcm_data += pcm_data_played; 229 pcm_data_size -= have_now;
231 pcm_data_size -= pcm_data_played; 230 }
232
233 while(need_to_play > 0) {
234 /* Loop until we have written enough */
235 231
232 while (len > 0)
233 {
234 if (callback_for_more) {
236 callback_for_more(&pcm_data, &pcm_data_size); 235 callback_for_more(&pcm_data, &pcm_data_size);
236 } else {
237 pcm_data = NULL;
238 pcm_data_size = 0;
239 }
240 if (pcm_data_size > 0) {
241 have_now = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len;
237 242
238 if (pcm_data_size > 0) { 243 memcpy(stream, pcm_data, have_now);
239 /* We got more data */
240 pcm_data_played = (need_to_play > pcm_data_size) ? pcm_data_size : need_to_play;
241
242 memcpy(stream, pcm_data, pcm_data_played);
243
244 if (debug != NULL) {
245 fwrite(pcm_data, sizeof(Uint8), pcm_data_played, debug);
246 }
247 244
248 stream += pcm_data_played; 245 if (debug != NULL) {
249 need_to_play -= pcm_data_played; 246 fwrite(pcm_data, sizeof(Uint8), have_now, debug);
250 pcm_data += pcm_data_played;
251 pcm_data_size -= pcm_data_played;
252 } 247 }
248 stream += have_now;
249 len -= have_now;
250 pcm_data += have_now;
251 pcm_data_size -= have_now;
252 } else {
253 DEBUGF("sdl_audio_callback: No Data.\n");
254 sdl_dma_stop();
255 break;
253 } 256 }
254 } else {
255 pcm_data_size = 0;
256 pcm_data = NULL;
257
258 /* No data, try and get some */
259 callback_for_more(&pcm_data, &pcm_data_size);
260 } 257 }
261} 258}
262 259