This project has moved and is read-only. For the latest updates, please go here.

Record wav from single channel source

May 22, 2013 at 11:24 AM
First of all, thank you for making such an easy to use audio library. As a rookie in programming, NAudio helps me a lot in my project.

I need to record a mono signal from Line-in jack, the source is only connected to the tip(left channel) of the 3.5mm plug, while the ring(right channel) is unused. I use the following code, written in Visual Basic Express 2010, to record and save the signal to wave file.
Public WithEvents WaveIn As NAudio.Wave.WaveIn
Public WaveWriter As NAudio.Wave.WaveFileWriter
Private Sub ButtonRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRecord.Click
        Dim NoOfChannel As Integer = 2
        WaveIn = New Naudio.Wave.WaveIn
        WaveIn.DeviceNumber = 0
        WaveIn.WaveFormat = New NAudio.Wave.WaveFormat(96000, 8, NoOfChannel)
        WaveWriter = New Wave.WaveFileWriter("Output.wav", WaveIn.WaveFormat)
End Sub

Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
End Sub

Private Sub WaveIn_DataAvailable(ByVal sender As System.Object, ByVal e As WaveInEventArgs) Handles WaveIn.DataAvailable
        WaveWriter.Write(e.Buffer, 0, e.BytesRecorded)
End Sub
After setting the record volume to a suitable level by Windows mixer, if I record in stereo format, NoOfChannel=2, I can get maximum amplitude from the left channel, each sample byte can swing all the way between 0 to 255, while each right channel sample byte stay at around 128. Since I only need the left channel, writing in stereo format double the file size which waste a lot of disk space.

If I record in mono format, NoOfChannel=1, the recorded amplitude is reduced by half, each sample byte can only swing between 64 and 192 approximately. I get the same result when using other audio recording program such as Audacity. It seems that when recording in mono format, the computer try to average the left and right signal, and since there is no signal applies to right channel, therefore the recorded amplitude is reduced by half. Even if I turn up the record volume, the recorded wave file shows clipping at level 64 and 192.

I want to maintain maximum amplitude swing, but keep the filesize small. Is it possible to record the left channel signal only and write into mono format wave file?

Jun 4, 2013 at 11:22 AM
Edited Jun 4, 2013 at 11:26 AM

From ButtonRecord, set the WaveWriter format to mono, then change the WaveIn_DataAvailable as follow:
Private Sub WaveIn_DataAvailable(ByVal sender As System.Object, ByVal e As NAudio.Wave.WaveInEventArgs) Handles WaveIn.DataAvailable
        For i = 0 To e.BytesRecorded / 2 - 1
            e.Buffer(i) = e.Buffer(2 * i)     ' keep the left channel
            ' or, e.Buffer(i)=e.Buffer(2*i+1), to keep the right channel
        WaveWriter.Write(e.Buffer, 0, e.BytesRecorded / 2)
End Sub