VB.net NAudio problem with mixing two wav files

31 Views Asked by At

I am not a c# programmer, but I have successfully implemented several NAudio functions in my VB project.

I am stuck on how to set up the signal chain for sending two wave files to a mixer and then to the playback engine which I got from the splendid NAudio Course Mark has on PluralSight.

Can anyone help me find the right combination of attributes to get this working?

Module1 ' declarations

Public myBackMSP As MixingSampleProvider
Public myLeadMSP As MixingSampleProvider
Public myBackWaveout As WaveOut
Public myLeadWaveout As WaveOut
Public mybackIWP As IWaveProvider
Public myleadIWP As IWaveProvider
Public mybackAFR As AudioFileReader
Public myleadAFR As AudioFileReader
Public myBackVolume As Double
Public myLeadVolume As Double
Public mymixer As MixingSampleProvider
Public myWaveout As WaveOut

' module1 end

Private Sub playbackengine()
    myWaveout = New WaveOut
    mymixer = New MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(44100, 2))
    mymixer.ReadFully = True
    myWaveout.Init(mymixer)
    myWaveout.Play()
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click, btnStart.Click
    dim myBackFileName As String
    dim myLeadFileName As String

    mybackfilename = "c:\vb\back.wav"
    myleadfilename = "c:\vb\lead.wav"

    playbackengine()
    btnStart.Enabled = False    ' to prevent Start being pressed twice

    myleadAFR = New AudioFileReader(myLeadFileName)        
    mybackAFR = New AudioFileReader(myBackFileName)

    myleadIWP = New IWaveProvider(myLeadFileName)
    mybackIWP = New IWaveProvider(myBackFileName)

    myleadIWP = New IWaveProvider(myleadAFR.ToWaveProvider) ' <-- these throw a 'New' Cannot 
    mybackIWP = New IWaveProvider(mybackAFR.ToWaveProvider) ' <-- be used on an interface

    myLeadMSP = New MixingSampleProvider(myleadAFR)
    myBackMSP = New MixingSampleProvider(mybackAFR)

    myleadAFR.Volume = 0.5
    mybackAFR.Volume = 0.5

    mymixer.AddMixerInput(myLeadMSP)
    mymixer.AddMixerInput(myBackMSP)

End Sub

I had working sending the two files to two instances of Waveout, but couldn't change the volume of the files independantly.

1

There are 1 best solutions below

0
On

With a lot of digging, and referring back to Mark course, I spotted my error.

Here is the corrected code.

Public mybackISP As ISampleProvider
Public myleadISP As ISampleProvider
Public mybackAFR As AudioFileReader
Public myleadAFR As AudioFileReader
Public myBackVolume As Double
Public myLeadVolume As Double

Public mymixer As MixingSampleProvider

Public myWaveout As WaveOut

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click, btnStart.Click

    playbackengine()

    btnStart.Enabled = False

    mybackAFR = New AudioFileReader(myBackFileName)
    myleadAFR = New AudioFileReader(myLeadFileName)

    myleadISP = myleadAFR.ToSampleProvider  //cast to Sampleprovider !
    mybackISP = mybackAFR.ToSampleProvider  //cast to Sampleprovider !

    mymixer.AddMixerInput(mybackISP)
    mymixer.AddMixerInput(myleadISP)

End Sub
// playback engine unchangeed
Private Sub playbackengine()
    myWaveout = New WaveOut
    mymixer = New MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(44100, 2))
    mymixer.ReadFully = True
    myWaveout.Init(mymixer)
    myWaveout.Play()
End Sub