Click here to Skip to main content
15,356,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written an app in Visual Basic 2017 that was meant to help me understand speech synthesis. The app works OK except for one thing, there is a 1 - 2 second delay in speaking part during the first iteration but not in any subsequent iterations. We don't hear anything until the statement is half done. What am I missing?
The code is included here:
Imports System.Speech.Synthesis

Public Class Form1

    Dim synth As New SpeechSynthesizer()
    Dim prompt1 As String = ""
    Dim prompt2 As String = ""
    Dim user As String = "Darby"
    Dim currentTime As Date = TimeOfDay
    Dim Sayit1 As String = ""
    Dim Sayit2 As String = ""
    Dim greeting1 As String = ""
    Dim greeting2 As String = ""


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        greeting1 = "Hi " & user & ", "
        If currentTime >= #12:00:00 AM# And currentTime < #12:00:00 PM# Then
            greeting2 = " How is your Morning going?"
        ElseIf currentTime >= #12:00:00 PM# And currentTime < #6:00:00 PM# Then
            greeting2 = " How is your Afternoon going?  "
        ElseIf currentTime >= #6:00:00 PM# Then
            greeting2 = " How is your Evening going? "
        End If
        TalkToMe(greeting1, greeting2)
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        greeting1 = "Goodbye " & user & ", "
        If currentTime >= #12:00:00 AM# And currentTime < #12:00:00 PM# Then
            greeting2 = " you Have a Good Morning  "
        ElseIf currentTime >= #12:00:00 PM# And currentTime < #6:00:00 PM# Then
            greeting2 = " you Have a Blessed Afternoon "
        ElseIf currentTime >= #6:00:00 PM# Then
            greeting2 = " you Have a Good Evening "
        End If
        TalkToMe(greeting1, greeting2)
    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Sayit1 = TextBox1.Text
        TalkToMe(Sayit1, "")
    End Sub

    Private Sub TalkToMe(prompt1, prompt2)
        synth.SelectVoice("Microsoft David Desktop")
        synth.Volume = 40
        synth.Rate = -1
        synth.Speak(prompt1 + prompt2)
        prompt1 = ""
        prompt2 = ""
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        End
    End Sub

End Class



What I have tried:

I have moved the initialization sequence to various positions but the delay is the same. (ie:
synth.SelectVoice("Microsoft David Desktop")
   synth.Volume = 40
   synth.Rate = -1

Posted
Updated 24-Jun-21 9:58am

It's probably taking time to load the speech engine.

In any event, "speech" should be done asynchronously for best performance; using "speak asynch" or a background worker.
   
Comments
Member 10376725 11-Feb-19 10:26am
   
SpeakAsync solved one problem I was having, where the program halted until the speak command finished, but didn't help the delay problem.
I had the same Problem. A delay before each "Speak" solved the missing first words problem.
now i have some latency, but it is usable.

My Solution:

SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SpeakStarted += new EventHandler<speakstartedeventargs>(synth_SpeakStarted);

private static void synth_SpeakStarted(object sender, SpeakStartedEventArgs e)
{
synth.Pause();
System.Threading.Thread.Sleep(400);
synth.Resume();
}
   
I have an easy solution, just put a dot (point ".") at the beggining of the text and it will add a little delay before start reading ;)
   
For me, resolving this was turning off the audio software I put on my computer. Once running without the software controlling my audio I stopped having it cut the beginning of the TTS
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900