20 Jun '13 - 03:33
Pages: [1]
 Author Topic: How to compose a stereo stream  (Read 4477 times)
pingo
Posts: 3

 « on: 12 Aug '03 - 16:49 » Quote

I would like to generate a stereo stream (in Delphi), similar as in StreamTest (Delphi example), but with different left and right sounds. This is the code from the StreamTest example which generates an equal left/right stereo sine stream:

function MakeSine(handle: HSTREAM; buffer: Pointer; length: DWORD; user: DWORD): DWORD; stdcall;
var
buf: ^WORD;
i, len: Integer;
begin
buf := buffer;
len := length div 2;
// write the sine function to the output stream
for i := 0 to len - 1 do begin
buf^ := Trunc(Sin(SineCount * PI) * Amplitude);
Inc(buf);
SineCount := SineCount + (Frequency / 44100);
end;
Result := length;
end;

Can anybody suggest me, how to generate different left and right sounds?

Another question is about the length of the buffer:
len := length div 2
Why division by 2?

Thanks!
 Logged
pingo
Posts: 3

 « Reply #1 on: 26 Aug '03 - 13:40 » Quote

 Logged
Ian @ un4seen
Posts: 15366

 « Reply #2 on: 26 Aug '03 - 15:47 » Quote

Quote
Can anybody suggest me, how to generate different left and right sounds?

When you write a stereo stream, you simply interleave the left and right samples - left1,right1,left2,right2,left3,etc...

Modifying the example above...

function MakeSine(handle: HSTREAM; buffer: Pointer; length: DWORD; user: DWORD): DWORD; stdcall;
var
buf: ^WORD;
i, len: Integer;
begin
buf := buffer;
len := length div 4;
// write the sine function to the output stream
for i := 0 to len - 1 do begin
buf^ := Trunc(Sin(SineCount * PI) * Amplitude); // left
Inc(buf);
buf^ := Trunc(Sin(SineCount * PI) * Amplitude); // right
Inc(buf);
SineCount := SineCount + (Frequency / 44100);
end;
Result := length;
end;

Quote
Another question is about the length of the buffer:
len := length div 2
Why division by 2?

Because it's using 16-bit sample data, ie. 2 bytes per sample.

It becomes "div 4" in the modification above, because now it's stereo, ie. 2x 2 bytes per sample.
 Logged
pingo
Posts: 3

 « Reply #3 on: 27 Aug '03 - 14:43 » Quote

works fine, thank you very much!
 Logged
Pages: [1]