1

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?

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • 3
    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. I think the library would prefer a pointer to the actual waveform audio data managed by the `TMemoryStream` object: `MemStream.Memory`. – Andreas Rejbrand May 12 '21 at 15:57
  • Also note this warning in the [documentation](http://www.un4seen.com/doc/#bass/BASS_StreamCreateFile.html): "*When streaming from memory (mem = TRUE), the memory must not be freed before the stream is freed.*" – Remy Lebeau May 12 '21 at 21:50
  • @AndreasRejbrand you should post that as an answer – Remy Lebeau May 12 '21 at 21:53

1 Answers1

2

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.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 1
    Thanks. I have it working now. I needed to add the MemStream.memory and size of the stream by MemStream.size: channel := BASS_StreamCreateFile(true, MemStream.Memory, 0, memstream.Size, BASS_STREAM_AUTOFREE); BASS_ChannelPlay(channel, False); – Tarry Waterson May 13 '21 at 21:19