BASS library: playing from memory

877 views Asked by At

I am attempting to play sound clip held in a MemStream: TMemoryStream using the BASS dll. I can read it from a wav file and it plays OK, but I want to get it to read the memory stream directly.

I have initialized the sound device on FormCreate

procedure TForm1.FormCreate(Sender: TObject;  
begin  
SafeLoadLibrary ('bass.dll');      //load the bass dll
BASS_Init(-1, 22050, BASS_DEVICE_16BITS, Form1.Handle, nil);   //Initialize an output device
end;

Then I create a sine wave in a memory stream and add the .WAV header so the memory stream contains the sound in the correct format:

MemStream := WriteWAVFromStream(MemStream, SAMPLERATE, BITSPERSAMPLE);
MemStream.Position := 0;

Then I try get the stream to play using BASS:

var
ch: HSTREAM;
P: ^TMemoryStream;

P := @MemStream;
ch := BASS_StreamCreateFile(TRUE, P, 0, 0, BASS_STREAM_AUTOFREE);
BASS_ChannelPlay(ch, False);

finally
Memstream.Free;

Any ideas why BASS won't play the memstream?

1

There are 1 answers

1
Andreas Rejbrand On

You are giving BASS the address of a Delphi TMemoryStream object. Are you sure you are supposed to do that? I don't think BASS knows anything about such Delphi classes.

Instead, the library probably expects a pointer to the actual waveform audio data managed by the TMemoryStream object: MemStream.Memory.