@@ -154,6 +154,7 @@ procedure ResetSound; |
| 154 |
procedure SoundUpdate(cycles: integer); |
154 |
procedure SoundUpdate(cycles: integer); |
| 155 |
|
155 |
|
| 156 |
procedure BeginWritingSoundToStream(Stream: TStream); |
156 |
procedure BeginWritingSoundToStream(Stream: TStream); |
|
|
157 |
procedure BeginWritingChannelsToStreams(MixStream, Ch1Stream, Ch2Stream, Ch3Stream, Ch4Stream: TStream); |
| 157 |
procedure EndWritingSoundToStream; |
158 |
procedure EndWritingSoundToStream; |
| 158 |
|
159 |
|
| 159 |
var |
160 |
var |
@@ -184,7 +185,8 @@ const |
| 184 |
|
185 |
|
| 185 |
var |
186 |
var |
| 186 |
PlayStream: TSDL_AudioDeviceID; |
187 |
PlayStream: TSDL_AudioDeviceID; |
| 187 |
bufCycles, bufLVal, bufRVal: integer; |
188 |
bufCycles: integer; |
|
|
189 |
bufLVals, bufRVals: array[0..4] of Integer; |
| 188 |
|
190 |
|
| 189 |
sndBuffer: ^Single; |
191 |
sndBuffer: ^Single; |
| 190 |
sndBytesWritten: Integer; |
192 |
sndBytesWritten: Integer; |
@@ -192,7 +194,7 @@ var |
| 192 |
lfsr: Cardinal = 0; |
194 |
lfsr: Cardinal = 0; |
| 193 |
|
195 |
|
| 194 |
WritingSoundToStream: Boolean; |
196 |
WritingSoundToStream: Boolean; |
| 195 |
SoundStream: TStream; |
197 |
SoundStreams: array[0..4] of TStream; |
| 196 |
|
198 |
|
| 197 |
procedure ResetSound; |
199 |
procedure ResetSound; |
| 198 |
var |
200 |
var |
@@ -214,14 +216,30 @@ begin |
| 214 |
end; |
216 |
end; |
| 215 |
|
217 |
|
| 216 |
procedure BeginWritingSoundToStream(Stream: TStream); |
218 |
procedure BeginWritingSoundToStream(Stream: TStream); |
|
|
219 |
var |
|
|
220 |
I: Integer; |
|
|
221 |
begin |
|
|
222 |
SoundStreams[0] := Stream; |
|
|
223 |
for I := 1 to 4 do SoundStreams[I] := nil; |
|
|
224 |
WritingSoundToStream := True; |
|
|
225 |
end; |
|
|
226 |
|
|
|
227 |
procedure BeginWritingChannelsToStreams(MixStream, Ch1Stream, Ch2Stream, Ch3Stream, Ch4Stream: TStream); |
| 217 |
begin |
228 |
begin |
|
|
229 |
SoundStreams[0] := MixStream; |
|
|
230 |
SoundStreams[1] := Ch1Stream; |
|
|
231 |
SoundStreams[2] := Ch2Stream; |
|
|
232 |
SoundStreams[3] := Ch3Stream; |
|
|
233 |
SoundStreams[4] := Ch4Stream; |
| 218 |
WritingSoundToStream := True; |
234 |
WritingSoundToStream := True; |
| 219 |
SoundStream := Stream; |
|
|
| 220 |
end; |
235 |
end; |
| 221 |
|
236 |
|
| 222 |
procedure EndWritingSoundToStream; |
237 |
procedure EndWritingSoundToStream; |
|
|
238 |
var |
|
|
239 |
I: Integer; |
| 223 |
begin |
240 |
begin |
| 224 |
WritingSoundToStream := False; |
241 |
WritingSoundToStream := False; |
|
|
242 |
for I := 0 to 4 do SoundStreams[I] := nil; |
| 225 |
end; |
243 |
end; |
| 226 |
|
244 |
|
| 227 |
procedure StartPlayback; |
245 |
procedure StartPlayback; |
@@ -277,8 +295,8 @@ begin |
| 277 |
|
295 |
|
| 278 |
soundEnable := True; |
296 |
soundEnable := True; |
| 279 |
bufCycles := 0; |
297 |
bufCycles := 0; |
| 280 |
bufLVal := 0; |
298 |
FillChar(bufLVals, SizeOf(bufLVals), 0); |
| 281 |
bufRVal := 0; |
299 |
FillChar(bufRVals, SizeOf(bufRVals), 0); |
| 282 |
end; |
300 |
end; |
| 283 |
|
301 |
|
| 284 |
procedure DisableSound; |
302 |
procedure DisableSound; |
@@ -292,35 +310,46 @@ begin |
| 292 |
soundEnable := False; |
310 |
soundEnable := False; |
| 293 |
end; |
311 |
end; |
| 294 |
|
312 |
|
| 295 |
procedure SoundDoOut(l, r: Integer; cycles: integer); |
313 |
procedure SoundDoOut(const ls, rs: array of Integer; cycles: integer); |
| 296 |
var |
314 |
var |
|
|
315 |
I: Integer; |
| 297 |
buf: array[0..1] of Single; |
316 |
buf: array[0..1] of Single; |
| 298 |
begin |
317 |
begin |
| 299 |
Inc(bufLVal, l * cycles); |
318 |
for I := 0 to 4 do begin |
| 300 |
Inc(bufRVal, r * cycles); |
319 |
Inc(bufLVals[I], ls[I] * cycles); |
|
|
320 |
Inc(bufRVals[I], rs[I] * cycles); |
|
|
321 |
end; |
| 301 |
Inc(bufCycles, cycles); |
322 |
Inc(bufCycles, cycles); |
| 302 |
if bufCycles >= sampleCycles then |
323 |
if bufCycles >= sampleCycles then |
| 303 |
begin |
324 |
begin |
| 304 |
buf[0] := ((bufRVal div sampleCycles) / 512.0); |
|
|
| 305 |
buf[1] := ((bufLVal div sampleCycles) / 512.0); |
|
|
| 306 |
bufCycles := 0; |
|
|
| 307 |
bufLVal := 0; |
|
|
| 308 |
bufRVal := 0; |
|
|
| 309 |
|
|
|
| 310 |
if WritingSoundToStream then begin |
325 |
if WritingSoundToStream then begin |
| 311 |
SoundStream.Write(buf, SizeOf(Single)*2); |
326 |
for I := 0 to 4 do begin |
|
|
327 |
if SoundStreams[I] <> nil then begin |
|
|
328 |
buf[0] := ((bufRVals[I] div sampleCycles) / 512.0); |
|
|
329 |
buf[1] := ((bufLVals[I] div sampleCycles) / 512.0); |
|
|
330 |
SoundStreams[I].Write(buf, SizeOf(Single)*2); |
|
|
331 |
end; |
|
|
332 |
end; |
| 312 |
end |
333 |
end |
| 313 |
else begin |
334 |
else begin |
|
|
335 |
buf[0] := ((bufRVals[0] div sampleCycles) / 512.0); |
|
|
336 |
buf[1] := ((bufLVals[0] div sampleCycles) / 512.0); |
| 314 |
sndBuffer^ := buf[0]; |
337 |
sndBuffer^ := buf[0]; |
| 315 |
Inc(sndBuffer); |
338 |
Inc(sndBuffer); |
| 316 |
sndBuffer^ := buf[1]; |
339 |
sndBuffer^ := buf[1]; |
| 317 |
Inc(sndBuffer); |
340 |
Inc(sndBuffer); |
| 318 |
Inc(sndBytesWritten, SampleSize); |
341 |
Inc(sndBytesWritten, SampleSize); |
| 319 |
end; |
342 |
end; |
|
|
343 |
|
|
|
344 |
bufCycles := 0; |
|
|
345 |
for I := 0 to 4 do begin |
|
|
346 |
bufLVals[I] := 0; |
|
|
347 |
bufRVals[I] := 0; |
|
|
348 |
end; |
| 320 |
end; |
349 |
end; |
| 321 |
end; |
350 |
end; |
| 322 |
|
351 |
|
| 323 |
procedure SoundOutBits(l, r: Integer; cycles: integer); |
352 |
procedure SoundOutBits(const ls, rs: array of Integer; cycles: integer); |
| 324 |
var |
353 |
var |
| 325 |
left: integer; |
354 |
left: integer; |
| 326 |
begin |
355 |
begin |
@@ -329,10 +358,10 @@ begin |
| 329 |
while bufCycles + cycles > sampleCycles do |
358 |
while bufCycles + cycles > sampleCycles do |
| 330 |
begin |
359 |
begin |
| 331 |
left := sampleCycles - bufCycles; |
360 |
left := sampleCycles - bufCycles; |
| 332 |
SoundDoOut(l, r, left); |
361 |
SoundDoOut(ls, rs, left); |
| 333 |
Dec(cycles, left); |
362 |
Dec(cycles, left); |
| 334 |
end; |
363 |
end; |
| 335 |
SoundDoOut(l, r, cycles); |
364 |
SoundDoOut(ls, rs, cycles); |
| 336 |
end; |
365 |
end; |
| 337 |
|
366 |
|
| 338 |
const |
367 |
const |
@@ -374,6 +403,7 @@ var |
| 374 |
n, stage: integer; |
403 |
n, stage: integer; |
| 375 |
ls: array[1..4] of Integer = (0, 0, 0, 0); |
404 |
ls: array[1..4] of Integer = (0, 0, 0, 0); |
| 376 |
rs: array[1..4] of Integer = (0, 0, 0, 0); |
405 |
rs: array[1..4] of Integer = (0, 0, 0, 0); |
|
|
406 |
chanLs, chanRs: array[0..4] of Integer; |
| 377 |
l, r: Integer; |
407 |
l, r: Integer; |
| 378 |
I: Integer; |
408 |
I: Integer; |
| 379 |
begin |
409 |
begin |
@@ -705,7 +735,13 @@ begin |
| 705 |
SampleBuffers[0].BufferR[SampleBuffers[0].Cursor] := r; |
735 |
SampleBuffers[0].BufferR[SampleBuffers[0].Cursor] := r; |
| 706 |
SampleBuffers[0].Cursor := (SampleBuffers[0].Cursor + 1) mod SAMPLE_BUFFER_SIZE; |
736 |
SampleBuffers[0].Cursor := (SampleBuffers[0].Cursor + 1) mod SAMPLE_BUFFER_SIZE; |
| 707 |
|
737 |
|
| 708 |
SoundOutBits(l, r, cycles); |
738 |
chanLs[0] := l; |
|
|
739 |
chanRs[0] := r; |
|
|
740 |
for I := 1 to 4 do begin |
|
|
741 |
chanLs[I] := ls[I]; |
|
|
742 |
chanRs[I] := rs[I]; |
|
|
743 |
end; |
|
|
744 |
SoundOutBits(chanLs, chanRs, cycles); |
| 709 |
end; |
745 |
end; |
| 710 |
|
746 |
|
| 711 |
begin |
747 |
begin |