Speech synthesis: Difference between revisions
→{{header|Perl 6}}: add entry |
Adds Clojure solution |
||
Line 107: | Line 107: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Clojure}}== |
|||
{{libheader|facts/speech-synthesis}} |
|||
<lang clojure>(use 'speech-synthesis.say) |
|||
(say "This is an example of speech synthesis.")</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
Revision as of 03:46, 6 August 2014
![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>
BASIC256
<lang BASIC256>say "Goodbye, World for the " + 123456 + "th time."</lang>
BBC BASIC
This calls the SAPI5 API directly, it does not need an external program. <lang bbcbasic> SPF_ASYNC = 1
ON ERROR SYS `CoUninitialize` : PRINT 'REPORT$ : END ON CLOSE SYS `CoUninitialize` : QUIT SYS "LoadLibrary","OLE32.DLL" TO O% SYS "GetProcAddress",O%,"CoInitialize" TO `CoInitialize` SYS "GetProcAddress",O%,"CoUninitialize" TO `CoUninitialize` SYS "GetProcAddress",O%,"CoCreateInstance" TO `CoCreateInstance` SYS `CoInitialize`,0 voice% = FN_voice_create PROC_voice_speak(voice%, "This is an example of speech synthesis") PROC_voice_wait(voice%) PROC_voice_free(voice%) SYS `CoUninitialize` END DEF FN_voice_create LOCAL C%, D%, F%, I%, M%, P%, V% DIM C% LOCAL 15, I% LOCAL 15 C%!0 = &96749377 : C%!4 = &11D23391 : C%!8 = &C000E39E : C%!12 = &9673794F I%!0 = &6C44DF74 : I%!4 = &499272B9 : I%!8 = &99EFECA1 : I%!12 = &D422046E SYS `CoCreateInstance`, C%, 0, 5, I%, ^V% IF V%=0 ERROR 100, "SAPI5 not available" = V% DEF PROC_voice_speak(V%, M$) DIM M% LOCAL 2*LENM$+1 SYS "MultiByteToWideChar", 0, 0, M$, -1, M%, LENM$+1 SYS !(!V%+80), V%, M%, SPF_ASYNC, 0 ENDPROC DEF PROC_voice_wait(V%) SYS !(!V%+128), V% ENDPROC DEF PROC_voice_free(V%) SYS !(!V%+8), V% ENDPROC</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(const 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>
Clojure
<lang clojure>(use 'speech-synthesis.say) (say "This is an example of speech synthesis.")</lang>
Groovy
Mac only: <lang groovy>'say "This is an example of speech synthesis."'.execute()</lang>
Haskell
<lang haskell> import System
say x = system $ "espeak " ++ show x
main = say "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
Locomotive Basic
Both hardware and software-only speech synthesizers exist for the CPC. A software-only solution, Speech 1.1 by Superior Software (1986), supplies three BASIC extension commands (RSXes), "|say", "|speak", and "|pitch":
<lang locobasic>|say,"This is an example of speech synthesis."</lang>
Mathematica
<lang Mathematica>Speak["This is an example of speech synthesis."]</lang>
Perl
<lang perl>use Speech::Synthesis;
($engine) = Speech::Synthesis->InstalledEngines(); ($voice) = Speech::Synthesis->InstalledVoices(engine => $engine);
Speech::Synthesis
->new(engine => $engine, voice => $voice->{id}) ->speak("This is an example of speech synthesis.");</lang>
Perl 6
<lang perl6>run 'espeak', 'This is an example of speech synthesis.';</lang>
PicoLisp
<lang PicoLisp>(call 'espeak "This is an example of speech synthesis.")</lang>
Racket
Should work on all platforms. <lang racket>
- lang racket
(require racket/lazy-require) (lazy-require [ffi/com (com-create-instance com-release com-invoke)]) (define (speak text)
(cond [(eq? 'windows (system-type)) (define c (com-create-instance "SAPI.SpVoice")) (com-invoke c "Speak" text) (com-release c)] [(ormap find-executable-path '("say" "espeak")) => (λ(exe) (void (system* exe text)))] [else (error 'speak "I'm speechless!")]))
(speak "This is an example of speech synthesis.") </lang>
REXX
Programming note: This REXX program uses a freeware program NIRCMD to interface with the Microsoft Windows speech synthesizer program SAM, a text to speech using a male voice. Sam can possibly be configured to use other voices with later releases of Windows. More recent Microsoft Windows have another speech synthesizer program: ANNA. <lang rexx>/*REXX pgm uses a CLI cmd to invoke Windows/XP SAM for speech synthesis.*/ parse arg t; t=space(t) /*get the (optional) text from CL*/ if t== then exit /*Nothing to say? Then exit pgm.*/ homedrive=value('HOMEDRIVE',,'SYSTEM') /*get HOMEDRIVE location of \TEMP*/ tmp =value('TEMP',,'SYSTEM') /* " TEMP directory name. */ if homedrive== then homedrive='C:' /*use default if none found. */ if tmp== then tmp=homedrive'\TEMP' /* " " " " " */
/*code could be added here to get*/ /*a unique name for the TEMP file*/
tFN='SPEAK_IT'; tFT='$$$' /*use this for the TEMP's fileID.*/ tFID=homedrive||'\TEMP\' || tFN"."tFT /*create temp name for the output*/ call lineout tFID,t /*write text──►a temp output file*/ call lineout tFID /*close the file just to be neat.*/ 'NIRCMD' "speak file" tFID /*NIRCMD invokes the MS Sam voice*/ 'ERASE' tfid /*clean up (delete) the TEMP file*/
/*stick a fork in it, we're done.*/</lang>
Note: The name of the above REXX program is speak.rex
usage using the command:
speak This is an example of speech synthesis.
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>
Scala
<lang scala>import java.util.Locale import com.sun.speech.freetts._
import javax.speech.Central import javax.speech.synthesis.{ Synthesizer, SynthesizerModeDesc }
object ScalaSpeaker extends App {
def speech(text: String) = { if (!text.trim.isEmpty()) { val VOICENAME = "kevin16"
System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory") Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral")
val synth = Central.createSynthesizer(new SynthesizerModeDesc(Locale.US)) synth.allocate()
val desc = synth.getEngineModeDesc() match { case g2: SynthesizerModeDesc => g2 } synth.getSynthesizerProperties() .setVoice(desc.getVoices().find(_.toString() == VOICENAME).get) synth.speak(text, null)
synth.waitEngineState(Synthesizer.QUEUE_EMPTY) synth.deallocate() } }
speech("If it ain't Dutch, It ain't much.")
}</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>
- Programming Tasks
- Temporal media
- Speech synthesis
- AutoHotkey
- BASIC256
- BBC BASIC
- C
- POSIX
- C sharp
- Clojure
- Facts/speech-synthesis
- Groovy
- Haskell
- Liberty BASIC
- Locomotive Basic
- Mathematica
- Perl
- Perl 6
- PicoLisp
- Racket
- REXX
- Ruby
- Win32-utils
- Scala
- Scala Implementations
- FreeTTS
- Tcl
- Tcom
- UNIX Shell
- VBScript
- ZX Spectrum Basic
- TI-83 BASIC/Omit
- Maxima/Omit
- ML/I/Omit
- Zkl/Omit