This project has moved. For the latest updates, please go here.

wav > Mp3 and bit rates (vb.net)

Jun 13, 2009 at 6:28 PM

Hi there!

First of all - you're providing the coolest audio library I've seen!  Using naudio, I've been able to make a recording function in VB.NET that really works!  (searching on the net, you'll find that nobody else has a clear concise example)

Now, I was wondering if there is any chance of create a function in naudio to convert waves to mp3?  Does it already - I read that it can convert mp3s to waves.  I ask that because my app is using windows media player.  Media player has a speed control which works great with mp3s but not with waves.  The waves are recorded in my app using naudio... So they're not copyrighted or anything.

 

Also, how do I change the recording samplerate for the reording of a wave? 

is this the command to change?

Dim recordingFormat As New WaveFormat(8000, 16, 1) - what are the 3 numbers regarding?

I've decided to paste my VB.net code here so that vb.net users can also taste the fruits of NAudio!

A) first go and download the naudio dll file and add it as a reference in your project!!!!  It's completely Free!
http://www.codeplex.com/naudio

also add system.data as a reference

B) The code is was all in C# but I converted to VB code here's the code (add the buttons to you form where listed:

You'll need 3 buttons on your form

buttonStartRecording
buttonSelectOutputFile
buttonStopRecording

a checkbox (checkBoxAutoPlay)

a progressbar (progressbar1)

a savedialog (saveFileDialog)

and viola!  This example records for 30 seconds before playing the wave back.  If you want indefinit recording, get rid of

        Dim secondsRecorded As Integer = CInt((writer.Length / writer.WaveFormat.AverageBytesPerSecond))
        If secondsRecorded >= 30 Then
            StopRecording()
        Else
            progressBar1.Value = secondsRecorded
        End If

from the waveInStream_DataAvailable sub


C) I would include an extra button to change the default sound input - this will call up your recording devices

Here's the code for that

    Private Sub recordDevicesBTTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordDevicesBTTN.Click
           Shell("rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,1")
    End Sub

That last number - if you change it to 0 you get the payback devices :)






Here's the code

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports NAudio.Wave
Imports System.Diagnostics
Public Class Form1

    Private waveInStream As WaveInStream
    Private writer As WaveFileWriter
    Private outputFilename As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub buttonStartRecording_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonStartRecording.Click
        If waveInStream Is Nothing Then
            If outputFilename Is Nothing Then
                buttonSelectOutputFile_Click(sender, e)
            End If
            If outputFilename Is Nothing Then
                Exit Sub
            End If
            Dim recordingFormat As New WaveFormat(8000, 16, 1)
            writer = New WaveFileWriter(outputFilename, recordingFormat)

            waveInStream = New WaveInStream(0, recordingFormat, Me)
            AddHandler waveInStream.DataAvailable, AddressOf waveInStream_DataAvailable
            waveInStream.StartRecording()
            buttonStartRecording.Enabled = False
        End If
    End Sub

    Private Sub waveInStream_DataAvailable(ByVal sender As Object, ByVal e As WaveInEventArgs)
        writer.WriteData(e.Buffer, 0, e.BytesRecorded)
        Dim secondsRecorded As Integer = CInt((writer.Length / writer.WaveFormat.AverageBytesPerSecond))
        If secondsRecorded >= 30 Then
            StopRecording()
        Else
            progressBar1.Value = secondsRecorded
        End If
    End Sub

    Private Sub StopRecording()
        waveInStream.StopRecording()
        waveInStream.Dispose()
        waveInStream = Nothing
        writer.Close()
        writer = Nothing
        buttonStartRecording.Enabled = True
        progressBar1.Value = 0
        If checkBoxAutoPlay.Checked Then
            Process.Start(outputFilename)
        End If
    End Sub

    Private Sub buttonStopRecording_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonStopRecording.Click
        If waveInStream IsNot Nothing Then
            StopRecording()
        End If
    End Sub

    Private Sub buttonSelectOutputFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSelectOutputFile.Click
        Dim saveFileDialog As New SaveFileDialog()
        saveFileDialog.Title = "Select output file:"
        saveFileDialog.Filter = "WAV Files (*.wav)|*.wav"
        saveFileDialog.FileName = outputFilename
        If saveFileDialog.ShowDialog() = DialogResult.OK Then
            outputFilename = saveFileDialog.FileName
        End If
    End Sub
End Class

 

Cheers,

Sean (www.prolevelguitar.com)