Thread: Smoother SAPI
View Single Post
 
Old 02-22-2017, 09:08 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default Smoother SAPI

It's been a verrrry long time since I have written any code and I am feeling quite rusty so please bear with my stupid questions and basic coding ability.

Below is the result of several days research and trying to remember.

When I use a text box to provide the input the SAPI voice seems a lot smoother than when I put each of the words into a string array.

But I want to be able to smoothly alter the volume, pitch and rate Trackbar Controls and the only way I know how to do that is to add a DoEvents that triggers in between every word.

I tried to work out some Asynchronous garble but every Microsoft example I could find was either incomplete or just plain didn't work (for me anyway).

Could you please look over my code and tell me if there is a smoother way to do this. Running Win 7 64bit and VS2012.

Code:
Option Explicit On

Public Class Config

    Dim intVolume As Int16 = 7, intSpeed As Int16 = 5, intPitch As Int16 = 5
    Dim SAPI = CreateObject("SAPI.spvoice")
    Dim txtArray() As String

    Private Sub Config_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For Each Voice In SAPI.getvoices
            Dim i As Integer
            cboVoices.Items.Add("" & (i) & " - " & Voice.GetDescription)
            i += 1
        Next

        tbVolume.Value = intVolume
        tbSpeed.Value = intSpeed
        tbPitch.Value = intPitch
        cboVoices.SelectedIndex = 0

        Dim intCount As Int16 = 0, intEndPos As Int16 = 0
        Dim strProcess As String = txtDialog.Text

        Do While strProcess.Length <> 0
            ReDim Preserve txtArray(intCount)
            If InStr(strProcess, " ") > 0 Then
                intEndPos = InStr(strProcess, " ")
                txtArray(intCount) = strProcess.Substring(0, intEndPos - 1)
                strProcess = strProcess.Remove(0, intEndPos)
            Else
                intEndPos = strProcess.Length
                txtArray(intCount) = strProcess.Substring(0, intEndPos)
                strProcess = strProcess.Remove(0, intEndPos)
            End If
            intCount += 1
        Loop

    End Sub

    Private Sub cboVoices_SelectedIndexChanged() Handles cboVoices.SelectedIndexChanged
        txtDialog.BackColor = Color.White
    End Sub

    Private Sub tbVolume_Scroll(sender As Object, e As EventArgs) Handles tbVolume.Scroll
        intVolume = sender.value
    End Sub

    Private Sub tbSpeed_Scroll(sender As Object, e As EventArgs) Handles tbSpeed.Scroll
        intSpeed = sender.Value
    End Sub

    Private Sub tbPitch_Scroll(sender As Object, e As EventArgs) Handles tbPitch.Scroll
        intPitch = sender.value
    End Sub

    Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click

        With SAPI
            .voice = .getvoices.item(cboVoices.SelectedIndex)
        End With

        Try
            For i = 0 To txtArray.Count - 1
                My.Application.DoEvents()
                SAPI.Speak("<volume level='" & (intVolume * 10) & "'>" & "<rate speed='" & (intSpeed * 2 + -10) & "'>" & _
                    "<pitch middle='" & (intPitch * 5 + -25) & "'>" & txtArray(i))
            Next
            txtDialog.BackColor = Color.LightGreen
        Catch ex As Exception
            txtDialog.BackColor = Color.Red
        End Try

    End Sub

End Class

Last edited by CodeCruncher; 02-22-2017 at 09:47 AM.
Reply With Quote