View Single Post
 
Old 01-15-2017, 12:04 AM
jcvamp jcvamp is offline
Newcomer
 
Join Date: Jun 2009
Posts: 1
Default Choose between different sound cards for playback in VB.net

Edit by Moderator: Re: Choose between different sound cards for playback in VB.net
I just came across this. I'm working on a voice control system for home automation, and I wanted to have the ability to route different audio to different devices depending on where I'm controlling the program from.

I had to modify some of the code to get it to work for me (and had to overcome a lockload error), but the method works.

Code:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectSound
Imports System.IO
Imports System.Speech
Imports System.Speech.AudioFormat

Public Class Form1
    Dim synth As New Synthesis.SpeechSynthesizer
    Dim DsDevice As DirectSound.Device, mydevices As New DevicesCollection

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each dev As DeviceInformation In mydevices
            lstDevices.Items.Add(dev.Description)
        Next
    End Sub

    Private Sub lstDevices_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDevices.SelectedIndexChanged
        If lstDevices.SelectedIndex = -1 Then Exit Sub
        DsDevice = New Device(mydevices(lstDevices.SelectedIndex).DriverGuid)
        DsDevice.SetCooperativeLevel(Me, CooperativeLevel.Priority)
        Dim bufferDesc As New Microsoft.DirectX.DirectSound.BufferDescription
        With bufferDesc
            .GlobalFocus = True
            .ControlVolume = True
            .ControlPan = True
            .ControlFrequency = True
        End With

        Dim VoiceStream As Stream = New MemoryStream()
        Dim sinfo As New SpeechAudioFormatInfo(128, AudioBitsPerSample.Eight, AudioChannel.Stereo)
        synth.SetOutputToWaveStream(VoiceStream)

        synth.Speak("Test")
        VoiceStream.Position = 0
        Dim _buffer As New SecondaryBuffer(VoiceStream, bufferDesc, DsDevice)
        _buffer.Play(0, BufferPlayFlags.Default)
    End Sub
To test, I have it saying 'test' over the selected audio device.

Thanks for the work you put in.

Last edited by Cerian Knight; 02-23-2017 at 10:28 AM.
Reply With Quote