How to compose a stereo stream

Started by pingo,

pingo

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!


Ian @ un4seen

QuoteCan 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;

QuoteAnother 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.