Page 1 of 1

Using LiveAudioRecorder to record a Wav file

PostPosted: March 10th, 2005, 3:28 pm
by Rron
I am trying to record using LiveAudioRecorder but can't seem to make it work to create a Wav file (they are always empty). I have read all messages on this board and looked at the source code and the Broadcaster example and passed hours trying averything and anything I can think of without success.

I am almost certain that its my OnData event that I am coding wrong. Does anyone have a sample code that could get me on the right track. I imagine its the way to use the Buffer value and/or the TWave that I am not getting.

Thank you

PostPosted: March 10th, 2005, 6:09 pm
by Kambiz
It would be nice if you post your code here to figure out what's wrong.

By the way, why you don't use TStockAudioRecorder to simplify this task?

Kambiz

PostPosted: March 10th, 2005, 8:03 pm
by Rron
I am using LiveAudioRecorder because I want the recording to stop if the noise level is below a certain level as in the Broadcaster example. I had no problem using the TStockAudioRecorder but cannot find a way to prevent the recording of low sound level portions.

I have tried so many things that I am posting the simplest code I have tried (and I am not good with pointers and maybe why I cannot get this to work)

Code: Select all
procedure TMainForm.LiveAudioRecorder1Data(Sender: TObject;
  const Buffer: Pointer; BufferSize: Cardinal; var FreeIt: Boolean);
begin
  if TheAudioLevel > MaxSilenceLevel then
  Begin
    AudioRecorder1.Wave.Write(Buffer, BufferSize);
  end;
end;


I have also tried using the Audiobuffer as in the Receiver example with no success, and a lot of other things. So I think that what is comes down to is how to use the buffer information to create a Wave file of the live recording.

Thank you for your help, your time and the Components you have created and put out as Freeware.

PostPosted: March 11th, 2005, 8:30 am
by Kambiz
Firstly, you should consider that Buffer is a pointer, so you should write the buffer in to the stream as follow:

Code: Select all
Wave.Write(Buffer^, BufferSize);

Secondly, the RIFF format doesn't let you to create a wave file in this way.

Here is the procedure you should follow to create a wave file:

1. Include the required units to the uses clause of your unit.

Code: Select all
uses
  mmSystem, WaveUtils;

2. Declare the helper variables.

Code: Select all
var
  WaveFormatEx: TWaveFormatEx;
  ckInfo, ckData: TMMCKInfo;
  mmIO: HMMIO;

3. Initialize the wave content in OnActivate event of the LiveAudioRecorder.

Code: Select all
SetPCMAudioFormatS(@WaveFormatEx, LiveAudioRecorder.PCMFormat);
mmIO := CreateStreamWaveAudio(Stream, @WaveFormatEx, ckInfo, ckData);

Stream: could be any writable stream. For your case, I suggest to use TFileStream.

4. Add wave data to the stream in OnData event of the LiveAudioRecorder.

Code: Select all
mmioWrite(mmIO, Buffer, BufferSize);

5. Finalize the task in OnDeactivate event of the LiveAudioRecorder.

Code: Select all
CloseWaveAudio(mmio, ckInfo, ckData);


Hope that it helps.
Kambiz

PostPosted: March 11th, 2005, 1:33 pm
by Rron
WOW, it works. A BIG thank you for your time.
I Created Stream as a TWave and just added : Stream.SaveToFile('MyPath\Name.WAV'); after the CloseWaveAudio.

With this code I also understood a couple of your other routines and some TWave manipulation.

Once again thank you