Results 1 to 2 of 2

Thread: MS Speech Object Library (sync vs Async)

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,602

    MS Speech Object Library (sync vs Async)

    Hi all.

    I've posted a few questions on issues with XVOICE (DirectSS), but I'm in the process of switching over to the MS Speech Object Library (SAPI).

    I've run into one problem that is probably due to inexperience with this library.

    In my app there is a text box and an array of buttons (a keyboard).
    When keys are pressed, the textbox is updated to simulate typing.
    To enhance the effects, I had DirectSS saying the letters as they were typed.

    Now that I've switched over, the spVoice is not working like I want it to.
    If I have it speak synchronously, the series of letters "typed" will all be spoken one after the other, and only then after that is done will the textbox update with all the letters typed (even though the textbox edit command comes before the voice command). So that's no good.
    If I do it asynchronously, I can type as fast as I want as before, and the textbox updates in real time, but then the voice commands drone on in the background long after the textbox has updated.

    Neither of these matches what I want and what I had with DirectSS.

    The ASYNC above matches more closely with what I want it to do, but the difference is, I want the voice from any previous button click to truncated as soon as another button is clicked. I need the letters in the textbox to update in realtime, and the voice to keep pace with the command clicks, even if the voice has to be cut off to achieve it.

    Is there a property or method that makes this work?
    It's actually the default behavior with DirectSS, and I have to use a DoEvents loop with the TextDataDone event to achieve SYNC.

    Anybody know this?
    Thanks.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: MS Speech Object Library (sync vs Async)

    Quote Originally Posted by wengang View Post
    The ASYNC above matches more closely with what I want it to do, but the difference is, I want the voice from any previous button click to truncated as soon as another button is clicked...
    For truncation, you could try to enable the SVSPurgeBeforeSpeak=2 in addition to the AsyncFlag.

    But the MS-SAPI has quite a delay, before it will react to your new Char-Input.

    Below is a timer-based approach, which will gather all incoming Chars, which will then be "spoken faster" -
    to not "lose any information" (in case this is for blind people).
    Code:
    Option Explicit
    
    Private Voice As Object, WithEvents tmrSpeak As VB.Timer
     
    Private Sub Form_Load()
      Set Voice = CreateObject("SAPI.SpVoice") 'create the Speech-API-HelperObject
      Set tmrSpeak = Controls.Add("VB.Timer", "tmrSpeak")
          tmrSpeak.Interval = 200
    End Sub
    
    Private Sub Form_KeyPress(KeyAscii As Integer)
      tmrSpeak.Enabled = False
          tmrSpeak.Tag = tmrSpeak.Tag & " " & Chr(KeyAscii)
      tmrSpeak.Enabled = True
    End Sub
    
    Sub Speak(ByVal Text As String, Optional ByVal Purge As Boolean)
      On Error Resume Next
        Voice.Speak Text, 1 Or IIf(Purge, 2, 0) '...SVSFlagsAsync OR - optionally - SVSFPurgeBeforeSpeak
      If Err Then Err.Clear
    End Sub
    
    Private Sub tmrSpeak_Timer()
      If Len(tmrSpeak.Tag) Then Speak tmrSpeak.Tag: tmrSpeak.Tag = ""
    End Sub

    If you don't want to use the timer (and try the "purging" instead),
    you could replace the Form_KeyPress Handler with the following:
    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
      Speak Chr(KeyAscii), True
    End Sub
    But as said, due to the quite high delay until the SAPI reacts, you will hear "next to nothing",
    when you type "fast enough" (because you "purge already, before it was spoken")...
    ... and that's what the timer-based method tries to avoid...

    HTH

    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width