Highlight Text as it is Spoken (TextBox with Pause Handling)

Category: Sound

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
 
%IDC_Start   = 500
%IDC_TextBox = 501
%IDC_Stop    = 502
%IDC_Pause   = 503
%IDC_Resume  = 504
 
%Unicode=1
#Include "win32api.inc"
#Include "sapi.inc"
 
%Msg_SAPI_Event = %WM_User + 1
 
Global hDlg As Dword, wText As WString, pSp As ISpVoice, IsPaused As Long
 
Function PBMain() As Long
   Local style&
   style& = %WS_TabStop Or    _
            %WS_Border Or     _
            %ES_Left Or       _
            %ES_NoHideSel Or  _
            %ES_WantReturn Or _      '<--- optional, needed for manual editing
            %ES_MultiLine
   Dialog Default Font "Tahoma", 14,1
   Dialog New Pixels, 0, "SAPI Test",300,300,350,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, %IDC_Start,"Start", 10,10,70,20
   Control Add Button, hDlg, %IDC_Stop,"Stop", 90,10,70,20
   Control Add Button, hDlg, %IDC_Pause, "Pause", 170,10,70,20
   Control Add Button, hDlg, %IDC_Resume, "Resume", 250,10,70,20
   Control Add TextBox, hDlg, %IDC_TextBox, "", 10,50,220,200, style&
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local w,h As Long, nStart, nEnd As Dword
   Local eventItem As SPEVENT, eventStatus As SPVOICESTATUS
   Select Case Cb.Msg
      Case %WM_InitDialog
         pSp = NewCom "SAPI.SpVoice"
         pSp.SetInterest(SPFEI(%SPEI_WORD_BOUNDARY), SPFEI(%SPEI_WORD_BOUNDARY))
         pSp.SetNotifyWindowMessage(hDlg, %MSG_SAPI_EVENT, 0, 0)
         wText = "Now is the time for all good men to read"
         wText += " this sentence and head to the enlistment"
         wText += " center to help their country fight for justice!"
         Control Set Text hDlg, %IDC_TextBox, wText
      Case %WM_Size
         Dialog Get Client hDlg To w,h
         Control Set Size hDlg, %IDC_Textbox, w-20,h-60
      Case %WM_Command
         Select Case Cb.Ctl
            Case %IDC_Start
               If isPaused Then
                  isPaused = 0
                  psp.Resume
                  pSp.Speak(ByVal StrPtr(wText), %SVSFPurgeBeforeSpeak, ByVal %NULL)
               End If
               pSp.Speak(ByVal StrPtr(wText), %SPF_Async, ByVal %Null)
            Case %IDC_Pause  :
               If isPaused = 0 Then isPaused = 1 : psp.Pause
            Case %IDC_Resume
               psp.Resume : IsPaused = 0
            Case %IDC_Stop
               If isPaused Then isPaused = 0 : psp.Resume
               pSp.Speak(ByVal StrPtr(wText), %SVSFPurgeBeforeSpeak, ByVal %NULL)
         End Select
      Case %Msg_SAPI_Event
         Do
            If pSp.GetEvents(1, eventItem, ByVal %NULL) <> %S_Ok Then Exit Do
            If eventItem.eEventId = %SPEI_WORD_BOUNDARY Then
               pSp.GetStatus(eventStatus, ByVal %NULL)
               nStart = eventStatus.ulInputWordPos
               nEnd = eventStatus.ulInputWordLen
               Control Send hDlg, %IDC_TextBox, %EM_SetSel, nStart, nStart+nEnd
               Dialog Set Text hDlg, "Start: " & Str$(nStart) & " - Len: " & Str$(nEnd)
            End If
         Loop
   End Select
End Function
 


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm