I am recording sound from a microphone in TMemoryStream. Playback from the stream is normal. However, when saving to a file, nothing is played. The data in the file are written.

Record Code:

WaveStream := TMemoryStream.Create; WaveStream.Position := 0; with WaveHdr do begin riff := 'RIFF'; len := 36; cWavFmt := 'WAVEfmt '; dwHdrLen := 16; wFormat := 1; wNumChannels := 2; dwSampleRate := 44100; wBlockAlign := 4; dwBytesPerSec := 176400; wBitsPerSample := 16; cData := 'data'; dwDataLen := 0; end; // Запись заголовка в поток WaveStream.Write(WaveHdr, SizeOf(WAVHDR)); //Собственно запись rchan := BASS_RecordStart(44100, 2, 0, @RecordingCallback, nil); 

Callback function when writing standard:

 function RecordingCallback(Handle: HRECORD; buffer: Pointer; length: DWord; user: Pointer): boolean; stdcall; begin Main.WaveStream.Write(buffer^, length); Result := True; end; 

Save to file as follows:

 WaveStream.SaveToFile('test.wav'); 

When using a TFileStream result is the same.

What am I doing wrong?

  • one
    Are other / third-party wav being played? - Kromster
  • 2
    Maybe after the end of the recording, you need to specify the correct data size in the header? You have the same dwDataLen := 0 - kot-da-vinci
  • @ kot-da-vinci, thanks. As it turned out, this is the problem. - Streletz

1 answer 1

In general, figured out.

It is necessary after the completion of the recording to enter information about the duration. Another thing is that this is for some reason not critical for the flow.

Just in case, lay out the result. Suddenly, someone will come in handy.

 WaveStream.Position := 4; i := w.Size - 8; WaveStream.Write(i, 4); i := i - $24; WaveStream.Position := 40; WaveStream.Write(i, 4); WaveStream.SaveToFile('test.wav'); 

Thanks to everyone who responded.