Using a speech engine to highlight words: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added AutoHotkey example)
Line 1: Line 1:
{{draft task}}[[Category:Speech synthesis]][[Category:Temporal media]]
{{draft task}}[[Category:Speech synthesis]][[Category:Temporal media]]
The task is to highlight and speak each word as it is spoken by the speech engine.
The task is to highlight and speak each word as it is spoken by the speech engine.
=={{header|AutoHotkey}}==
We use the simple SAPI.SPVoice COM Object and a parsing loop.
The highlighting is done with [http://msdn.microsoft.com/en-us/library/bb761661 EM_SETSEL] and Notepad. Rather crude, but it works. Due to the simplistic nature of the parsing loop, the text ends with a space.
<lang AutoHotkey>SetTitleMatchMode 2
EM_SETSEL := 0x00B1

Run notepad,,,pid
WinWaitActive ahk_pid %pid%
ControlSetText, Edit1, % text := "AutoHotkey was the first to implement this task! ", ahk_pid %pid%

pVoice := ComObjCreate("Sapi.spvoice"), i := 1 ; the spvoice COM Object ships with the OS

; parse the text
While lf := SubStr(text, i, 1)
{
If lf = %A_Space%
{
SendMessage, EM_SetSel, % i-StrLen(word)-1, % i-1, Edit1, ahk_pid %pid%
pVoice.speak(word), word := "", i++
}
Else word .= lf, i++
}</lang>

Revision as of 02:40, 10 September 2011

Using a speech engine to highlight words is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to highlight and speak each word as it is spoken by the speech engine.

AutoHotkey

We use the simple SAPI.SPVoice COM Object and a parsing loop. The highlighting is done with EM_SETSEL and Notepad. Rather crude, but it works. Due to the simplistic nature of the parsing loop, the text ends with a space. <lang AutoHotkey>SetTitleMatchMode 2 EM_SETSEL := 0x00B1

Run notepad,,,pid WinWaitActive ahk_pid %pid% ControlSetText, Edit1, % text := "AutoHotkey was the first to implement this task! ", ahk_pid %pid%

pVoice := ComObjCreate("Sapi.spvoice"), i := 1 ; the spvoice COM Object ships with the OS

parse the text

While lf := SubStr(text, i, 1) {

  If lf = %A_Space%
  {
     SendMessage, EM_SetSel, % i-StrLen(word)-1, % i-1, Edit1, ahk_pid %pid%
     pVoice.speak(word), word := "", i++
  }
  Else word .= lf, i++

}</lang>