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

Controlling multiple waveOut instances

Jan 10, 2012 at 7:29 AM

Hello!

I've created a program that uses a 8 channel external audio card to play audio through waveOut. I was wondering how would it be possible to create several instances of audio. I would prefer if these additional streams could be created indefinitely, so I don't have to manually program it for each channel.

Right now everything works fine until I have to stop the audio. If I select two different devices and play, everything is OK, but stopping them messes everything up.

I currently have this (in VB.Net):

   Private Function CreateInputStream(ByVal fileName As String) As WaveStream
        Dim inputStream As WaveChannel32

        If fileName.EndsWith(".mp3") Then
            Dim mp3Reader As New Mp3FileReader(fileName)
            inputStream = New WaveChannel32(mp3Reader)
        ElseIf fileName.EndsWith(".wav") Then
            Dim wavReader As New WaveFileReader(fileName)
            inputStream = New WaveChannel32(wavReader)
        Else
            Throw New InvalidOperationException("Unsupported extension")
        End If

        volumeStream = inputStream

        Return volumeStream

    End Function

    Private Function PlayAudio(ByVal file As String, ByVal devNo As Integer)

        waveOutDevice = New WaveOut
        mainOutputStream = CreateInputStream(file)

        waveOutDevice.DeviceNumber = devNo
        waveOutDevice.Init(mainOutputStream)
        waveOutDevice.Play()

        Return waveOutDevice.PlaybackState
    End Function

    Private Function StopAudio()
        If Not waveOutDevice Is Nothing Then
            waveOutDevice.Stop()
        End If
        If Not mainOutputStream Is Nothing Then
            volumeStream.Close()
            volumeStream = Nothing
            mainOutputStream.Close()
            mainOutputStream = Nothing
        End If
        If Not waveOutDevice Is Nothing Then
            waveOutDevice.Dispose()
            waveOutDevice = Nothing
        End If
        Return Nothing
    End Function

Here's a picture of my UI work in progress to clarify. 

http://www.upload.ee/image/1970564/ap.jpg

Any tips would be appreciated.

Coordinator
Jan 10, 2012 at 7:34 AM

it looks like your class just has one instance of waveOutDevice, volumeStream and mainOutputStream. However, there needs to be one per device. A simple way to do this would be to create a usercontrol with your play, pause, volume buttons on it, and then have multiple instances of that usercontrol on your main form.

Jan 10, 2012 at 9:50 AM

Thanks! I am quite new to .NET programming and didn't even think about usercontrols. I am making progress now, however could you give me a hint on what would be the easiest way to control all of the user controls at once?

Coordinator
Jan 10, 2012 at 9:52 AM

well your usercontrol would have public methods, like Play() Stop() etc. Then the host form would have a list of your user controls, and loop through it calling the appropriate method on each one in turn.

Jan 10, 2012 at 9:54 AM

Thank you a lot for the help!

All the best for you and your project! You're doing awesome job.