25 May '13 - 17:45 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: How to compose a stereo stream  (Read 4444 times)
pingo
Posts: 3


« on: 12 Aug '03 - 16:49 »
Reply with quoteQuote

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 »
Reply with quoteQuote

please help
Logged
Ian @ un4seen
Administrator
Posts: 15276


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

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 »
Reply with quoteQuote

works fine, thank you very much!
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines