wilhelm kurz software

home
application notes

raga application notes

WriteWav

This example demonstrates how to write recorded audio data to a separate wave file for each channel  in real time under Visual Basic.

The standard way to save audio data to disk in Raga is to define the recorder media as raFILE and to specify the file name like in

Raga1.Recorder.Record.Media(Channel) = raFILE
Raga1.Recorder.Record.FileName = WaveFileName

With this setting Raga places all channel data in one file. If each channel shall have its own file, however, a little programming is required. To create and write wav file this example uses VB Module WriteWaveFile. The functions in this module are very generic and can be used in other applications as well.

This is the user interface of our demo application.

WriteWav

When the button Start is clicked the program begins recording audio data until Stop is pressed or the maximum number of samples has been recorded (this is to avoid flooding your hard disk with audio data). When you click on button Play the program plays the file for the channel which you selected with the Channel combo box.

This is how the program works:

In method Form_Load we first tell Raga how many channels we want to record.

Raga1.Recorder.Format.Channels = Channels

Next we disable storing to any media by setting the media property to raNONE. By setting the number of samples to record = 0 we tell Raga to record continuously.

For c = 0 To Channels - 1
   Raga1.Recorder.Record.Media(c) = raNONE
Next
  
Raga1.Recorder.Record.Samples = 0

In the click event handler for button Start we create and initialize a file for each channel.

For c = 0 To Channels - 1
   Sampleswritten(c) = 0
   Fileno(c) = CreateWavFile("c:\temp\testrec" & c + 1 & ".wav", 1, BitsPerSample, Raga1.Recorder.Format.SamplesPerSecond, -1)
Next
 

Function CreateWavHeader from module WriteWaveFile opens the file and writes the file header using the passed in information for the number of channels, the number of bits in a sample, the sampling rate and the number of samples to record. Because we do not know the duration of the recording yet we pass in -1 for the latter parameter. If the sample count is not known in advance it can be supplied to function close which patches the wave header with the proper number before closing the file.

The actual recording is done in Rags’s RecorderMonitorData event handler function.

Private Sub Raga1_RecorderMonitorData(ByVal Buffer As Long)
   Dim n As Long, i As Long
   Dim c As Integer
  
   n = Raga1.Recorder.Format.SamplesPerBuffer
  
   For c = 0 To Channels - 1
       Raga1.Recorder.Monitor.GetDataVB Buffer, c, Indata
       For i = 0 To n - 1
           WriteWavChannel Fileno(c), BitsPerSample, Indata(i)
           Sampleswritten(c) = Sampleswritten(c) + 1
           If Sampleswritten(c) >= Maxsamples Then Exit For
       Next
   Next
   If Sampleswritten(0) >= Maxsamples Then StopButton_Click
End Sub

Raga calls this handler whenever a buffer with audio data has been filled by the recorder and can now be processed by the client application.

Download demo project.

 

[home] [products] [news] [contact] [links]