Commit 008bd9f

Nick Faro committed on
Buffer data while rendering
commit 008bd9f5bfef013543ce10b9c2fa0dc2200c6601 parent e759313
1 changed files +19−1
Modifiedsrc/sound.pas +19−1
@@ -182,6 +182,7 @@ uses mainloop, vars;
182 const 182 const
183 SampleSize = SizeOf(Single)*2; 183 SampleSize = SizeOf(Single)*2;
184 SampleCycles: LongInt = (8192 * 1024) div PlaybackFrequency; 184 SampleCycles: LongInt = (8192 * 1024) div PlaybackFrequency;
185 StreamFlushBytes = 32768;
185 186
186 var 187 var
187 PlayStream: TSDL_AudioDeviceID; 188 PlayStream: TSDL_AudioDeviceID;
@@ -195,6 +196,8 @@ var
195 196
196 WritingSoundToStream: Boolean; 197 WritingSoundToStream: Boolean;
197 SoundStreams: array[0..4] of TStream; 198 SoundStreams: array[0..4] of TStream;
199 StreamBuffers: array[0..4] of array[0..StreamFlushBytes-1] of Byte;
200 StreamBufferUsed: array[0..4] of Integer;
198 201
199 procedure ResetSound; 202 procedure ResetSound;
200 var 203 var
@@ -215,22 +218,33 @@ begin
215 end; 218 end;
216 end; 219 end;
217 220
221 procedure FlushStreamBuffer(Idx: Integer);
222 begin
223 if (SoundStreams[Idx] <> nil) and (StreamBufferUsed[Idx] > 0) then
224 SoundStreams[Idx].Write(StreamBuffers[Idx], StreamBufferUsed[Idx]);
225 StreamBufferUsed[Idx] := 0;
226 end;
227
218 procedure BeginWritingSoundToStream(Stream: TStream); 228 procedure BeginWritingSoundToStream(Stream: TStream);
219 var 229 var
220 I: Integer; 230 I: Integer;
221 begin 231 begin
222 SoundStreams[0] := Stream; 232 SoundStreams[0] := Stream;
223 for I := 1 to 4 do SoundStreams[I] := nil; 233 for I := 1 to 4 do SoundStreams[I] := nil;
234 for I := 0 to 4 do StreamBufferUsed[I] := 0;
224 WritingSoundToStream := True; 235 WritingSoundToStream := True;
225 end; 236 end;
226 237
227 procedure BeginWritingChannelsToStreams(MixStream, Ch1Stream, Ch2Stream, Ch3Stream, Ch4Stream: TStream); 238 procedure BeginWritingChannelsToStreams(MixStream, Ch1Stream, Ch2Stream, Ch3Stream, Ch4Stream: TStream);
239 var
240 I: Integer;
228 begin 241 begin
229 SoundStreams[0] := MixStream; 242 SoundStreams[0] := MixStream;
230 SoundStreams[1] := Ch1Stream; 243 SoundStreams[1] := Ch1Stream;
231 SoundStreams[2] := Ch2Stream; 244 SoundStreams[2] := Ch2Stream;
232 SoundStreams[3] := Ch3Stream; 245 SoundStreams[3] := Ch3Stream;
233 SoundStreams[4] := Ch4Stream; 246 SoundStreams[4] := Ch4Stream;
247 for I := 0 to 4 do StreamBufferUsed[I] := 0;
234 WritingSoundToStream := True; 248 WritingSoundToStream := True;
235 end; 249 end;
236 250
@@ -238,6 +252,7 @@ procedure EndWritingSoundToStream;
238 var 252 var
239 I: Integer; 253 I: Integer;
240 begin 254 begin
255 for I := 0 to 4 do FlushStreamBuffer(I);
241 WritingSoundToStream := False; 256 WritingSoundToStream := False;
242 for I := 0 to 4 do SoundStreams[I] := nil; 257 for I := 0 to 4 do SoundStreams[I] := nil;
243 end; 258 end;
@@ -327,7 +342,10 @@ begin
327 if SoundStreams[I] <> nil then begin 342 if SoundStreams[I] <> nil then begin
328 buf[0] := ((bufRVals[I] div sampleCycles) / 512.0); 343 buf[0] := ((bufRVals[I] div sampleCycles) / 512.0);
329 buf[1] := ((bufLVals[I] div sampleCycles) / 512.0); 344 buf[1] := ((bufLVals[I] div sampleCycles) / 512.0);
330 SoundStreams[I].Write(buf, SizeOf(Single)*2); 345 Move(buf, StreamBuffers[I][StreamBufferUsed[I]], SampleSize);
346 Inc(StreamBufferUsed[I], SampleSize);
347 if StreamBufferUsed[I] >= StreamFlushBytes then
348 FlushStreamBuffer(I);
331 end; 349 end;
332 end; 350 end;
333 end 351 end