summaryrefslogtreecommitdiff
path: root/uisimulator
diff options
context:
space:
mode:
Diffstat (limited to 'uisimulator')
-rw-r--r--uisimulator/sdl/sound.c73
1 files changed, 43 insertions, 30 deletions
diff --git a/uisimulator/sdl/sound.c b/uisimulator/sdl/sound.c
index 757ebe13c7..4ab2fef697 100644
--- a/uisimulator/sdl/sound.c
+++ b/uisimulator/sdl/sound.c
@@ -31,7 +31,7 @@ static bool pcm_playing;
31static bool pcm_paused; 31static bool pcm_paused;
32 32
33static Uint8* pcm_data; 33static Uint8* pcm_data;
34static int pcm_data_size; 34static Uint32 pcm_data_size;
35 35
36extern bool debug_audio; 36extern bool debug_audio;
37 37
@@ -95,7 +95,7 @@ void pcm_play_stop(void)
95 95
96void pcm_play_pause(bool play) 96void pcm_play_pause(bool play)
97{ 97{
98 int next_size; 98 Uint32 next_size;
99 Uint8 *next_start; 99 Uint8 *next_start;
100 100
101 if (!pcm_playing) { 101 if (!pcm_playing) {
@@ -128,7 +128,7 @@ void pcm_play_pause(bool play)
128 128
129 SDL_PauseAudio(1); 129 SDL_PauseAudio(1);
130 } 130 }
131 131
132 pcm_paused = !play; 132 pcm_paused = !play;
133} 133}
134 134
@@ -142,47 +142,60 @@ bool pcm_is_playing(void)
142 return pcm_playing; 142 return pcm_playing;
143} 143}
144 144
145char overflow[8192]; 145Uint8 overflow[8192];
146int overflow_amount = 0; 146Uint32 overflow_amount = 0;
147 147
148void sdl_audio_callback(void *udata, Uint8 *stream, int len) 148void sdl_audio_callback(void *udata, Uint8 *stream, int len)
149{ 149{
150 int datalen, offset; 150 Uint32 pcm_data_played, need_to_play;
151 FILE *debug = (FILE *)udata; 151 FILE *debug = (FILE *)udata;
152 152
153 /* At all times we need to write a full 'len' bytes to stream. */ 153 /* At all times we need to write a full 'len' bytes to stream. */
154 154
155 if (pcm_data_size <= len) { 155 if (pcm_data_size > 0) {
156 /* Play what we have */ 156 /* We have some PCM data to play. Play as much as we can. */
157 memcpy(stream, pcm_data, pcm_data_size); 157
158 pcm_data_played = (((Uint32)len) > pcm_data_size) ? pcm_data_size : len;
159
160 memcpy(stream, pcm_data, pcm_data_played);
158 161
159 if (debug != NULL) { 162 if (debug != NULL) {
160 fwrite(pcm_data, sizeof(Uint8), pcm_data_size, debug); 163 fwrite(pcm_data, sizeof(Uint8), pcm_data_played, debug);
161 } 164 }
165
166 stream += pcm_data_played;
167 need_to_play = len - pcm_data_played;
168 pcm_data += pcm_data_played;
169 pcm_data_size -= pcm_data_played;
162 170
163 offset = pcm_data_size; 171 while(need_to_play > 0) {
164 datalen = len - pcm_data_size; 172 /* Loop until we have written enough */
165 173
166 /* Get some more */ 174 callback_for_more(&pcm_data, &pcm_data_size);
167 callback_for_more(&pcm_data, &pcm_data_size); 175
168 176 if (pcm_data_size > 0) {
169 /* Play enough of that to keep the audio buffer full */ 177 /* We got more data */
170 memcpy(stream + offset, pcm_data, datalen); 178 pcm_data_played = (need_to_play > pcm_data_size) ? pcm_data_size : need_to_play;
171 179
172 if (debug != NULL) { 180 memcpy(stream, pcm_data, pcm_data_played);
173 fwrite(pcm_data, sizeof(Uint8), datalen, debug); 181
182 if (debug != NULL) {
183 fwrite(pcm_data, sizeof(Uint8), pcm_data_played, debug);
184 }
185
186 stream += pcm_data_played;
187 need_to_play -= pcm_data_played;
188 pcm_data += pcm_data_played;
189 pcm_data_size -= pcm_data_played;
190 }
174 } 191 }
175 } else { 192 } else {
176 datalen = len; 193 pcm_data_size = 0;
177 memcpy(stream, pcm_data, len); 194 pcm_data = NULL;
178
179 if (debug != NULL) {
180 fwrite(pcm_data, sizeof(Uint8), len, debug);
181 }
182 }
183 195
184 pcm_data_size -= datalen; 196 /* No data, try and get some */
185 pcm_data += datalen; 197 callback_for_more(&pcm_data, &pcm_data_size);
198 }
186} 199}
187 200
188int pcm_init(void) 201int pcm_init(void)
@@ -198,7 +211,7 @@ int pcm_init(void)
198 fmt.freq = 44100; 211 fmt.freq = 44100;
199 fmt.format = AUDIO_S16SYS; 212 fmt.format = AUDIO_S16SYS;
200 fmt.channels = 2; 213 fmt.channels = 2;
201 fmt.samples = 512; 214 fmt.samples = 2048;
202 fmt.callback = sdl_audio_callback; 215 fmt.callback = sdl_audio_callback;
203 fmt.userdata = debug; 216 fmt.userdata = debug;
204 217