Speech synthesis: Difference between revisions
(→{{header|Ruby}}: additional operating systems) |
(→{{header|Ruby}}: I have espeak for OpenBSD, but this script failed because it only checked for Linux.) |
||
Line 83: | Line 83: | ||
<lang ruby>module OperatingSystem |
<lang ruby>module OperatingSystem |
||
require 'rbconfig' |
require 'rbconfig' |
||
module_function |
|||
def |
def operating_system |
||
case RbConfig::CONFIG["host_os"] |
case RbConfig::CONFIG["host_os"] |
||
when /linux/i |
when /linux/i |
||
Line 97: | Line 98: | ||
end |
end |
||
end |
end |
||
def |
def linux?; operating_system == :linux; end |
||
def |
def windows?; operating_system == :windows; end |
||
def |
def mac?; operating_system == :mac; end |
||
end</lang> |
end</lang> |
||
{{libheader|win32-utils}} |
{{libheader|win32-utils}} |
||
{{works with|Ruby|1.9}} |
|||
Uses <code>espeak</code> on Linux, <code>say</code> on Mac, and the win32 SAPI library on Windows. |
Uses <code>espeak</code> on Linux, <code>say</code> on Mac, and the win32 SAPI library on Windows. |
||
<lang ruby>load 'operating_system.rb' |
<lang ruby>load 'operating_system.rb' |
||
Line 111: | Line 113: | ||
v = Win32::SpVoice.new |
v = Win32::SpVoice.new |
||
v.Speak(text) |
v.Speak(text) |
||
elsif OperatingSystem.linux? |
|||
⚫ | |||
elsif OperatingSystem.mac? |
elsif OperatingSystem.mac? |
||
IO.popen(["say"], "w") {|pipe| pipe.puts text} |
IO.popen(["say"], "w") {|pipe| pipe.puts text} |
||
else |
|||
# Try to run "espeak". No OperatingSystem check: "espeak" is |
|||
# for Linux but is also an optional package for BSD. |
|||
⚫ | |||
end |
end |
||
end |
end |
Revision as of 03:18, 14 November 2011
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Render the text “This is an example of speech synthesis.” as speech.
AutoHotkey
<lang ahk> talk := ComObjCreate("sapi.spvoice") talk.Speak("This is an example of speech synthesis.") </lang>
C
Following shining examples of exec
ing external programs around here:
<lang c>#include <sys/wait.h>
- include <stdio.h>
- include <stdlib.h>
- include <unistd.h>
void talk(char *s) { pid_t pid; int status;
pid = fork(); if (pid < 0) { perror("fork"); exit(1); }
if (pid == 0) { execlp("espeak", "espeak", s, (void*)0); perror("espeak"); _exit(1); }
waitpid(pid, &status, 0); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) exit(1); }
int main() { talk("This is an example of speech synthesis."); return 0; }</lang>
C#
You need to 'Add Reference' to the COM "Microsoft Speech Object Library" in your Preferences. <lang csharp>using SpeechLib;
namespace Speaking_Computer {
public class Program { private static void Main() { var voice = new SpVoice(); voice.Speak("This is an example of speech synthesis."); } }
}</lang>
Liberty BASIC
Assumes that 'espeak' is available at the path shown. <lang lb> nomainwin run "C:\Program Files\eSpeak\command_line\espeak "; chr$( 34); "This is an example of speech synthesis."; chr$( 34) end
</lang>
Another dll has been posted to do the same job, at LB Community Wiki
Mathematica
<lang Mathematica>Speak["This is an example of speech synthesis."]</lang>
PicoLisp
<lang PicoLisp>(call 'espeak "This is an example of speech synthesis.")</lang>
Ruby
Using this module to encapsulate operating system lookup <lang ruby>module OperatingSystem
require 'rbconfig' module_function def operating_system case RbConfig::CONFIG["host_os"] when /linux/i :linux when /cygwin|mswin|mingw|windows/i :windows when /darwin/i :mac when /solaris/i :solaris else nil end end def linux?; operating_system == :linux; end def windows?; operating_system == :windows; end def mac?; operating_system == :mac; end
end</lang>
Uses espeak
on Linux, say
on Mac, and the win32 SAPI library on Windows.
<lang ruby>load 'operating_system.rb'
def speak(text)
if OperatingSystem.windows? require 'win32/sapi5' v = Win32::SpVoice.new v.Speak(text) elsif OperatingSystem.mac? IO.popen(["say"], "w") {|pipe| pipe.puts text} else # Try to run "espeak". No OperatingSystem check: "espeak" is # for Linux but is also an optional package for BSD. IO.popen(["espeak", "-stdin"], "w") {|pipe| pipe.puts text} end
end
speak 'This is an example of speech synthesis.'</lang>
Tcl
This just passes the string into the Festival system:
<lang tcl>exec festival --tts << "This is an example of speech synthesis."</lang>
Alternatively, on MacOS X, you'd use the system say
program:
<lang tcl>exec say << "This is an example of speech synthesis."</lang>
On Windows, there is a service available by COM for speech synthesis:
<lang tcl>package require tcom
set msg "This is an example of speech synthesis." set voice [::tcom::ref createobject Sapi.SpVoice] $voice Speak $msg 0</lang> Putting these together into a helper procedure, we get: <lang tcl>proc speak {msg} {
global tcl_platform if {$tcl_platform(platform) eq "windows"} { package require tcom set voice [::tcom::ref createobject Sapi.SpVoice] $voice Speak $msg 0 } elseif {$tcl_platform(os) eq "Darwin"} { exec say << $msg } else { exec festival --tts << $msg }
} speak "This is an example of speech synthesis."</lang>
UNIX Shell
Here we use the open source espeak tool:
<lang bash>#!/bin/sh espeak "This is an example of speech synthesis."</lang>
VBScript
<lang vbs> Dim message, sapi message = "This is an example of speech synthesis." Set sapi = CreateObject("sapi.spvoice") sapi.Speak message </lang>
ZX Spectrum Basic
This example makes use of the Currah Speech Synthesizer peripheral device.
<lang zx basic>10 LET s$="(th)is is an exampul of sp(ee)(ch) sin(th)esis":PAUSE 1</lang>