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

Visual Basic

May 11, 2011 at 5:13 AM

Hi,

I'm attempting to use the NAudio library to create a simple program to record and analyse data in Visual basic using visual studio 2010 on a 64 bit OS. I am having a lot of trouble with various points mostly as a result of my low experience with programming in general. One problem i am having is that it is confusing trying to follow the tutorials and code on the net as there are a few versions out there and they don't always work. One example is the delegating of sample rate and channels when creating an instance of a WaveIn object such as :

waveIn = new WaveIn(44100,2) 

which isn't how it is done on the 1.4 version as far as i am aware. If someone could help or has some pointers with regard to use of the latest version I would appreciate any feedback. 

 

THe other main obstacle preventing me moving forward at the moment is using event handlers. I see in most examples there are statements to define the WaveIn.dataavailable as an event handler with the code:

waveIn.DataAvailable += waveIn_DataAvailable;
unfortunately however i do not have the option to use the event (.dataavailable) as part of the wavein object. Can someone help me out. Under what conditions should the .dataavailable option be available?
thanks in advance. 
Cheers Matt

 

Coordinator
May 11, 2011 at 8:22 AM

have a look at the NAudioDemo project that is part of NAudio, which shows how to do recording. Why can't you use DataAvailable?

Mark

May 11, 2011 at 10:54 AM

Imports System

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Text

Imports System.Windows.Forms

Imports NAudio

Imports NAudio.Wave

Imports System.Diagnostics

Imports NAudio.CoreAudioApi

Imports System.Collections.ObjectModel

 

 

 

 

Public Class Form1

   Dim WaveIn As WaveIn

   Dim writer As WaveFileWriter

   Dim filename As String

   Dim eve As WaveInEventArgs

   Dim mix As WaveMixerStream32

   Dim format As WaveFormat

   Dim secrec As Integer

   Dim davail As EventHandler

 

  

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

       WaveIn = New WaveIn(44100, 2)

       filename = "C:\Dougs Fingerjointer\audio programming\test\not working.wav"

       writer = New WaveFileWriter(filename, WaveIn.WaveFormat)

       WaveIn.d() 'no dataavailable ????

 

       WaveIn.StartRecording()

 

   End Sub

  

   Sub data(ByVal sender As System.Object, ByVal e As WaveInEventArgs) Handles Me.Activated

 

       writer.WriteData(e.Buffer, 0, e.BytesRecorded)

       secrec = writer.Length

       Debug.Print(secrec)

 

   End Sub

 

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

       WaveIn.StopRecording()

       WaveIn.Dispose()

       'Debug.Print(WaveIn

   End Sub

End Class

 

Ideas???

 

May 11, 2011 at 4:54 PM

Hi again,

Just for your information i have messed around with the code to get it to record. The use of event handlers is becoming a little clearer however i'm still not totally sure how to convert the following into VB. 

waveIn.DataAvailable += waveIn_DataAvailable;

 

 

Anyway here is the code i came up with

 

Imports System

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Text

Imports System.Windows.Forms

'Imports NAudio

Imports NAudio.Wave

Imports System.Diagnostics

Imports NAudio.CoreAudioApi

Imports System.Collections.ObjectModel

 

 

 

 

Public Class Form1

   Public WithEvents WaveIn As WaveIn

   'Dim WaveIn As WaveIn

   Dim writer As WaveFileWriter

   Dim filename As String

   Dim eve As WaveInEventArgs

   Dim mix As WaveMixerStream32

   Dim format As WaveFormat

   Dim secrec As Integer

 

 

  

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

       WaveIn = New WaveIn()

       'WaveIn.DeviceNumber = 0

       WaveIn.WaveFormat = New WaveFormat(44100, 2)

       filename = "C:\Dougs Fingerjointer\audio programming\test\not working.wav"

 

 

       'AddHandler WaveIn.DataAvailable, WaveInEventArgs

       writer = New WaveFileWriter(filename, WaveIn.WaveFormat)

 

 

       WaveIn.StartRecording()

       Debug.Print("1")

 

   End Sub

  

   Sub Wavein_dataavailable(ByVal sender As System.Object, ByVal e As WaveInEventArgs) Handles WaveIn.DataAvailable

 

 

       writer.WriteData(e.Buffer, 0, e.BytesRecorded)

       'secrec = writer.Length

       'Debug.Print(secrec)

 

 

 

 

 

   End Sub

 

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

       WaveIn.StopRecording()

 

       WaveIn.Dispose()

       writer.Close()

 

       Debug.Print("3")

   End Sub

 

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

   End Sub

End Class

 

I noticed a few things:

file created will not be readable till writer.close is performed

eventhandler sub referenced to event wavein.dataavailable continues to perform over and over again. 

cheers Matt

 

Coordinator
May 12, 2011 at 8:13 AM

>> I noticed a few things:

>> file created will not be readable till writer.close is performed

>> eventhandler sub referenced to event wavein.dataavailable continues to perform over and over again. 

yes, both of these things are supposed to happen