none
TTS SAPI Change Voice at Runtime RRS feed

  • Question

  • Hi All,

    Plz excuse my sending this, I am only a nearby beginner at this forum thing and VB!

    I am trying to write a TTS Program in Visual Basic 2012 Express using SAPI, I would like the program to change the voice from Male to Female if say the user selects Male from a listbox it will speak in a male voice and same for a female option.

    So far all I can get it to do is speak in the computers default voice, I have checked and rechecked Youtube videos for an answer but nothing works, I found one and followed it letter by letter and still the only voice I get is the default one!

    I'm using Windows 10 with vs express for windows (VB)

    Can anyone plz help?

    Friday, May 12, 2017 1:09 AM

All replies

  • I have checked and rechecked Youtube videos for an answer but nothing works, I found one and followed it letter by letter and still the only voice I get is the default one!

    Show the code you are using to change the voice.  The problem might be in the way that you are handling the selection rather than anything in SAPI.

    However YouTube videos are not a reliable source for VB code, so there may be a problem with what you downloaded.

    Friday, May 12, 2017 1:49 AM
  • -EDIT-

    Sorry, wasn't thinking... you are using the old API.  If you want to use the managed speech API (add a reference to System.Speech) then the following would solve the problem.

    -/EDIT-

    Use the SelectVoiceByHints(VoiceGender) method to select the gender of the current voice.

    Here is an example using two radio buttons to change gender:

    Option Strict On
    Imports System.Speech.Synthesis
    
    Public Class Form1
    
        Friend WithEvents InputTextBox As New TextBox With {.Text = "Hello World! This is a test.", .Width = 200}
        Friend WithEvents SpeakButton As New Button With {.AutoSize = True, .Text = "Speak"}
        Friend WithEvents MaleVoiceRadioButton As New RadioButton With {.AutoSize = True, .Checked = True, .Text = "Male"}
        Friend WithEvents FemaleVoiceRadioButton As New RadioButton With {.AutoSize = True, .Text = "Female"}
        Friend WithEvents FlowLayoutPanel1 As New FlowLayoutPanel With {.Dock = DockStyle.Fill}
    
        Private speaker As New SpeechSynthesizer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            InitializeExampleGUI()
        End Sub
    
        Private Sub InitializeExampleGUI()
            Controls.Add(FlowLayoutPanel1)
            With FlowLayoutPanel1
                .Controls.Add(InputTextBox)
                .SetFlowBreak(InputTextBox, True)
                .Controls.Add(SpeakButton)
                .SetFlowBreak(SpeakButton, True)
                .Controls.Add(MaleVoiceRadioButton)
                .Controls.Add(FemaleVoiceRadioButton)
            End With
        End Sub
    
        Private Sub SpeakButton_Click(sender As Object, e As EventArgs) Handles SpeakButton.Click
            speaker.Speak(InputTextBox.Text)
        End Sub
    
        Private Sub MaleVoiceRadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles MaleVoiceRadioButton.CheckedChanged
            speaker.SelectVoiceByHints(VoiceGender.Male)
        End Sub
    
        Private Sub FemaleVoiceRadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles FemaleVoiceRadioButton.CheckedChanged
            speaker.SelectVoiceByHints(VoiceGender.Female)
        End Sub
    End Class


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Friday, May 12, 2017 2:12 AM
    Moderator
  • Thank you all for your prompt replies.

    I tried the last reply by cheating, I copied and pasted the code into a new project.................the code returned an error..........the line of code in the:

    Private Sub InitializeExampleGUI()
            Controls.Add(FlowLayoutPanel1)

    produced a "Add is not a member of system.windows.forms.control" message and a wavy blue line under the line of code. Everything else in the code is ok. When I tried to run the code I got two errors in it, the above message and one for the "Load" event in the Form_Load sub!

    I know I am asking a lot but please help?

    Wednesday, May 31, 2017 8:07 PM
  • ... and a wavy blue line under the line of code.>

    That's an indication of a compile error and my guess is that you forgot to add a reference to System.Speech?

    If so, it's easy: Right-click on your project in Solution Explorer and one of the options will be "Add Reference". Choose that, then select the tab that shows ".NET". You might want to give it a few seconds to populate then if you'll scroll through the list, you should see "System.Speech.

    Select that and "OK' your way back then give it a go.

    I hope that helps.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 31, 2017 9:46 PM