Author Topic: How to compose a stereo stream  (Read 9925 times)

pingo

  • Posts: 3
How to compose a stereo stream
« on: 12 Aug '03 - 16:49 »
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!

pingo

  • Posts: 3
Re: How to compose a stereo stream
« Reply #1 on: 26 Aug '03 - 13:40 »
please help

Ian @ un4seen

  • Administrator
  • Posts: 26252
Re: How to compose a stereo stream
« Reply #2 on: 26 Aug '03 - 15:47 »
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...
Code: [Select]

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.

pingo

  • Posts: 3
Re: How to compose a stereo stream
« Reply #3 on: 27 Aug '03 - 14:43 »
works fine, thank you very much!