Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET General (http://www.xtremevbtalk.com/-net-general/)
-   -   Choose between different sound cards for playback in VB.net (http://www.xtremevbtalk.com/-net-general/328586-choose-sound-cards-playback-vb-net.html)

jcvamp 01-14-2017 11:04 PM

Choose between different sound cards for playback in VB.net
 
Edit by Moderator: Re: http://www.xtremevbtalk.com/-net-gen...ck-vb-net.html
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.


All times are GMT -6. The time now is 06:53 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.