Using a speech engine to highlight words: Difference between revisions

m
(→‎{{header|Phix}}: syntax coloured, made p2js compatible, added online link)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 14:
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.
<langsyntaxhighlight AutoHotkeylang="autohotkey">SetTitleMatchMode 2
EM_SETSEL := 0x00B1
 
Line 32:
}
Else word .= lf, i++
}</langsyntaxhighlight>
 
 
Line 39:
 
We are going to invoke vbscript directly
<langsyntaxhighlight lang="freebasic">''This works on Windows. Does anyone know how it would be done in Linux?
 
Sub Speak(Texto As String)
Line 64:
Print palabra(n)
Speak palabra(n)
Next n</langsyntaxhighlight>
 
 
Line 73:
 
Very robotic but it works.
<langsyntaxhighlight lang="go">package main
 
import (
Line 103:
time.Sleep(time.Second)
fmt.Printf("%s%s\n", bs, prev)
}</langsyntaxhighlight>
 
 
=={{header|Julia}}==
{{trans|Go}}
<langsyntaxhighlight lang="julia">function speak(sentence, cmd = "/utl/espeak.bat")
for word in split(sentence)
s = replace(lowercase(word), r"[^a-z]" => "")
Line 122:
 
speak("Are those 144 shy Eurasian footwear, cowboy chaps, or jolly earthmoving headgear?")
</syntaxhighlight>
</lang>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module UsingEvents {
Form 60, 32
Line 167:
UsingEvents
 
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="text">DynamicModule[{text = "This is some text.", words, i = 0},
Panel@Column@{Dynamic[
Row[Riffle[
Line 177:
Button["Speak",
While[i < Length@words, i++; FinishDynamic[]; Speak[words[[i]]];
Pause[Max[0.7, 0.12 StringLength[words[[i]]]]]]; i = 0]}]</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Go}}
Works on Linux but may also work on other platforms provided "espeak" is installed.
<langsyntaxhighlight Nimlang="nim">import os, osproc, strutils
 
const S = "Actions speak louder than words."
Line 200:
bs = repeat('\b', prevlen)
sleep(1000)
echo bs, prev</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 206:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Speech.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Speech.exw
Line 260:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Raku}}==
{{trans|Go}}
<syntaxhighlight lang="raku" perl6line># 20200826 Raku programming solution
 
my \s = "Actions speak louder than words.";
Line 279:
}
 
printf "%s%s\n", "\b" x prevLen, prev;</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 286:
 
Each word of the text is highlighted (by showing the word in uppercase). &nbsp; the terminal screen is cleared before showing the text that is being spoken; &nbsp; the repeated calls to the (Windows) speech engine makes for a slower speech rate.
<langsyntaxhighlight lang="rexx">/*REXX program uses a command line interface to invoke Windows SAM for speech synthesis.*/
parse arg t /*get the (optional) text from the C.L.*/
#= words(t)
Line 303:
oneWord= dq x dq /*surround a word in double quotes (").*/
'NIRCMD' "speak text" oneWord rate /*NIRCMD invokes Microsoft's Sam voice*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
Note: &nbsp; The name of the above REXX program is &nbsp; '''SPEAKHI.REX'''<br>
 
Line 312:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "guilib.ring"
 
Line 362:
Func pClose
MyApp.quit()
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 378:
 
Uses the Ruby code from [[Speech synthesis]]
<langsyntaxhighlight lang="ruby">load 'speechsynthesis.rb'
 
if ARGV.length == 1
Line 408:
@sentence.replace($words.each_with_index.map {|word, idx| idx == @idx ? strong(word + " ") : span(word + " ")})
end
end</langsyntaxhighlight>
 
=={{header|Tcl}}==
This code uses the external <code>/usr/bin/say</code> program (known available on Mac OS X) as its interface to the speech engine; this produces rather stilted speech because it forces the text to be spoken one word at a time instead of as a whole sentence (in order to keep the highlighting synchronized).
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk 8.5
proc say {text button} {
Line 437:
pack [text .t]
pack [button .b -text "Speak, computer!" -command {say .t .b}] -fill x
.t insert 1.0 "This is an example of speech synthesis with Tcl/Tk."</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 443:
{{libheader|Wren-str}}
The ability to call external processes such as ''espeak'' is expected to be added to Wren-cli in the next release. In the meantime, we embed the following Wren script in a minimal C host (no error checking) to complete this task.
<syntaxhighlight lang="wren">/* Using_a_speech_engine_to_highlight_words.wren */
<lang ecmascript>/* speech_engine_highlight_words.wren */
 
import "./str" for Str
Line 469:
bs = "\b" * prevLen
C.usleep(1000)
System.print("%(bs)%(prev)")</langsyntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc Using_a_speech_engine_to_highlight_words.c -o Using_a_speech_engine_to_highlight_words -lwren -lm */
<lang c>#include <stdio.h>
 
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 552 ⟶ 554:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "speech_engine_highlight_wordsUsing_a_speech_engine_to_highlight_words.wren";
char *script = readFile(fileName);
wrenInterpret(vm, module, script);
Line 558 ⟶ 560:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{omit from|EasyLang}}
9,476

edits