Speech synthesis: Difference between revisions

m
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible, added online link)
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 15:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">os:(‘espeak 'Hello world!'’)</langsyntaxhighlight>
 
=={{header|AmigaBASIC}}==
 
<langsyntaxhighlight lang="amigabasic">text$=TRANSLATE$("This is an example of speech synthesis.")
SAY text$</langsyntaxhighlight>
 
=={{header|AppleScript}}==
Probably the only language where output to console is harder than output to a sound device.
<syntaxhighlight lang="applescript">
<lang AppleScript>
say "This is an example of speech synthesis"
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
 
<langsyntaxhighlight lang="ahk">talk := ComObjCreate("sapi.spvoice")
talk.Speak("This is an example of speech synthesis.")</langsyntaxhighlight>
 
=={{header|AutoIt}}==
 
<langsyntaxhighlight AutoItlang="autoit">$voice = ObjCreate("SAPI.SpVoice")
$voice.Speak("This is an example of speech synthesis.")</langsyntaxhighlight>
 
=={{header|BASIC256}}==
 
<langsyntaxhighlight BASIC256lang="basic256">say "Goodbye, World for the " + 123456 + "th time."
say "This is an example of speech synthesis."</langsyntaxhighlight>
 
=={{header|Batch File}}==
Sorry for cheating. This is Batch/JScript hybrid.
<langsyntaxhighlight lang="dos">@set @dummy=0 /*
::Batch File section
@echo off
Line 54:
//The JScript section
var objVoice = new ActiveXObject("SAPI.SpVoice");
objVoice.speak(WScript.Arguments(0));</langsyntaxhighlight>
{{Out}}
Saved as SPEAK.BAT
Line 64:
{{works with|BBC BASIC for Windows}}
This calls the SAPI5 API directly, it does not need an external program.
<langsyntaxhighlight lang="bbcbasic"> SPF_ASYNC = 1
ON ERROR SYS `CoUninitialize` : PRINT 'REPORT$ : END
ON CLOSE SYS `CoUninitialize` : QUIT
Line 102:
DEF PROC_voice_free(V%)
SYS !(!V%+8), V%
ENDPROC</langsyntaxhighlight>
 
=={{header|C}}==
Line 108:
 
{{libheader|POSIX}}
<langsyntaxhighlight lang="c">#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
Line 139:
talk("This is an example of speech synthesis.");
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
You need to 'Add Reference' to the COM "Microsoft Speech Object Library" in your Preferences.
<langsyntaxhighlight lang="csharp">using SpeechLib;
 
namespace Speaking_Computer
Line 155:
}
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
{{libheader|facts/speech-synthesis}}
<langsyntaxhighlight lang="clojure">(use 'speech-synthesis.say)
(say "This is an example of speech synthesis.")</langsyntaxhighlight>
 
 
Line 167:
 
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 178:
speak "Vamos a contar " + str(123456)
speak "This is an example of speech synthesis."
Sleep</langsyntaxhighlight>
 
 
=={{header|FutureBasic}}==
FB offers easy access to excellent quality native synthesized voices in a variety of accents and languages available on Macs as demonstrated here. For a more comprehensive demonstration of speech synthesis, see the FutureBasic Rosetta Code task solution to: [[https://rosettacode.org/wiki/Old_lady_swallowed_a_fly#FutureBasic|FB's Old Laday Swallowed a Fly.]]
<syntaxhighlight lang="futurebasic">
 
SpeechSynthesizerRef ref
ref = fn SpeechSynthesizerWithVoice( @"com.apple.speech.synthesis.voice.daniel.premium" )
fn SpeechSynthesizerStartSpeakingString( ref, @"This is an example of speech synthesis." )
 
HandleEvents
</syntaxhighlight>
 
=={{header|GlovePIE}}==
<langsyntaxhighlight lang="glovepie">if var.number=0 then
var.number=1
say("This is an example of speech synthesis.")
endif</langsyntaxhighlight>
 
=={{header|Go}}==
Here's a library solution, but using a library written from scratch in Go.
<langsyntaxhighlight lang="go">package main
 
import (
Line 216 ⟶ 227:
synthesized := gospeech.DefaultVoice.Synthesize(phonetics)
wav.WriteFile(synthesized, "output.wav")
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Mac only:
<langsyntaxhighlight lang="groovy">'say "This is an example of speech synthesis."'.execute()</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">
import System.Process (callProcess) -- From “process” library
 
Line 229 ⟶ 240:
 
main = say "This is an example of speech synthesis."
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}}==
This should work in most major browsers
{{works with|Javascript}}
<langsyntaxhighlight lang="javascript">
var utterance = new SpeechSynthesisUtterance("This is an example of speech synthesis.");
window.speechSynthesis.speak(utterance);
</syntaxhighlight>
</lang>
 
Windows only:
{{works with|JScript}}
<langsyntaxhighlight lang="javascript">var voice = new ActiveXObject("SAPI.SpVoice");
voice.speak("This is an example of speech synthesis.");</langsyntaxhighlight>
 
=={{header|Julia}}==
It seems that this and similar tasks can reduce to how the language can call an external program. Using the Julia REPL:
<langsyntaxhighlight lang="julia">
julia> a = "hello world"
"hello world"
 
julia> run(`espeak $a`)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
Line 262 ⟶ 273:
Whilst it is often possible to wrap such macros in 'ordinary' C functions and then expose the latter to Kotlin via a .klib, it is not worth the effort here. I have therefore confined myself to simply reporting a non-zero error status.
 
<langsyntaxhighlight lang="scala">// Kotlin Native v0.6.2
 
import kotlinx.cinterop.*
Line 287 ⟶ 298:
fun main(args: Array<String>) {
talk("This is an example of speech synthesis.")
}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
Assumes that 'espeak' is available at the path shown.
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
run "C:\Program Files\eSpeak\command_line\espeak "; chr$( 34); "This is an example of speech synthesis."; chr$( 34)
end
</langsyntaxhighlight>
Another dll has been posted to do the same job, at [http://basic.wikispaces.com/SpeechLibrary LB Community Wiki]
 
Line 302 ⟶ 313:
Both hardware and software-only speech synthesizers exist for the CPC. A software-only solution, [http://www.cpc-power.com/index.php?page=detail&num=4372 Speech 1.1] by Superior Software (1986), supplies three BASIC extension commands (RSXes), "|say", "|speak", and "|pitch":
 
<langsyntaxhighlight lang="locobasic">|say,"This is an example of speech synthesis."</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 309 ⟶ 320:
 
===Using Statement Speech===
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module UsingStatementSpeech {
Volume 100
Line 315 ⟶ 326:
}
UsingStatementSpeech
</syntaxhighlight>
</lang>
===Print each word as speak===
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module UsingEvents {
Declare WithEvents sp "SAPI.SpVoice"
Line 354 ⟶ 365:
}
UsingEvents
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Speak["This is an example of speech synthesis."]</langsyntaxhighlight>
 
=={{header|Nim}}==
Using same method as Julia.
<langsyntaxhighlight Nimlang="nim">import osproc
 
discard execCmd("espeak 'Hello world!'")</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
 
Define a function that is using espeak package from Linux.
<langsyntaxhighlight lang="parigp">speak(txt,opt="")=extern(concat(["espeak ",opt," \"",txt,"\""]));</langsyntaxhighlight>
 
Now let it speak:
<langsyntaxhighlight lang="parigp">speak("This is an example of speech synthesis")</langsyntaxhighlight>
 
A monster speech tongue-twister:
<langsyntaxhighlight lang="parigp">speak("The seething sea ceaseth and thus the seething sea sufficeth us.","-p10 -s100")</langsyntaxhighlight>
 
A foreign language "Zungenbrecher":
<langsyntaxhighlight lang="parigp">speak("Fischers Fritz fischt frische Fische.","-vmb/mb-de2 -s130")</langsyntaxhighlight>
 
=={{header|Perl}}==
 
<langsyntaxhighlight lang="perl">use Speech::Synthesis;
 
($engine) = Speech::Synthesis->InstalledEngines();
Line 388 ⟶ 399:
Speech::Synthesis
->new(engine => $engine, voice => $voice->{id})
->speak("This is an example of speech synthesis.");</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 394 ⟶ 405:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Speak.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Speak.exw
Line 423 ⟶ 434:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
Note that speech synthesis has refused to operate in a browser without user activation since 2018, hence the tiny GUI with a button.<br>
Should you for some strange reason want it on desktop/Phix without any GUI, you'd need to arrange for COM initialisation etc yourself.
Line 435 ⟶ 446:
 
{{works with|Mac OS & Linux OS}}
<syntaxhighlight lang="php">
<lang PHP>
<?php
<?php
Line 497 ⟶ 508:
// Save it to a File
exec("$voice '$statement' $save_file_args");
</syntaxhighlight>
</lang>
 
 
{{works with|Windows OS}}
<syntaxhighlight lang="php">
<lang PHP>
<?php
 
Line 619 ⟶ 630:
// Have $voice (Zira) announce file stream completion
$voice->Speak('File stream complete');
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(call 'espeak "This is an example of speech synthesis.")</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
Add-Type -AssemblyName System.Speech
 
Line 632 ⟶ 643:
$anna.Speak("I'm sorry Dave, I'm afraid I can't do that.")
$anna.Dispose()
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
import pyttsx
 
Line 641 ⟶ 652:
engine.say("It was all a dream.")
engine.runAndWait()
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
 
Mac specific.
 
<syntaxhighlight lang="Quackery"> [ $ /
import subprocess
subprocess.run(["say",
string_from_stack()])
/ python ] is speak ( $ --> )
 
$ "This is an example of speech synthesis" speak</syntaxhighlight>
 
=={{header|Racket}}==
Should work on all platforms.
<langsyntaxhighlight lang="racket">
#lang racket
(require racket/lazy-require)
Line 658 ⟶ 681:
[else (error 'speak "I'm speechless!")]))
(speak "This is an example of speech synthesis.")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>run 'espeak', 'This is an example of speech synthesis.';</langsyntaxhighlight>
 
=={{header|REXX}}==
{{works with|Windowx/XP or later}}
Programming note: &nbsp; This REXX program uses a freeware program &nbsp; NIRCMD &nbsp; to interface with the Microsoft Windows speech synthesizer program &nbsp; '''SAM''', &nbsp; a text to speech using a male voice. &nbsp; SAM can possibly be configured to use other voices with later releases of Windows. &nbsp; More recent Microsoft Windows have another speech synthesizer program: &nbsp; ANNA.
<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.*/
if t='' then exit /*Nothing to say? Then exit program.*/
Line 674 ⟶ 697:
/* [↓] where the rubber meets the road*/
'NIRCMD' "speak text" dquote t dquote rate /*NIRCMD invokes Microsoft's Sam voice*/
/*stick a fork in it, we're all done. */</langsyntaxhighlight>
Note: &nbsp; The name of the above REXX program is &nbsp; '''speak.rex'''<br>
'''usage''' &nbsp; using the command:
Line 682 ⟶ 705:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
 
load "guilib.ring"
Line 695 ⟶ 718:
}
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 711 ⟶ 734:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "guilib.ring"
load "stdlib.ring"
Line 763 ⟶ 786:
Func pClose
MyApp.quit()
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 777 ⟶ 800:
=={{header|Ruby}}==
Using this module to encapsulate operating system lookup
<langsyntaxhighlight lang="ruby">module OperatingSystem
require 'rbconfig'
module_function
Line 797 ⟶ 820:
def windows?; operating_system == :windows; end
def mac?; operating_system == :mac; end
end</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="ruby">load 'operating_system.rb'
 
def speak(text)
Line 818 ⟶ 841:
end
 
speak 'This is an example of speech synthesis.'</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|FreeTTS|1.2}}
<langsyntaxhighlight lang="scala">import javax.speech.Central
import javax.speech.synthesis.{Synthesizer, SynthesizerModeDesc}
 
Line 871 ⟶ 894:
|with its lapping disasters
|is feared and hearkened.""".stripMargin)
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func text2speech(text, lang='en') {
Sys.run("espeak -v #{lang} -w /dev/stdout #{text.escape} | aplay");
}
text2speech("This is an example of speech synthesis.");</langsyntaxhighlight>
 
=={{header|Swift}}==
OS X comes with a program called "say," that does speech.
<langsyntaxhighlight Swiftlang="swift">import Foundation
 
let task = NSTask()
task.launchPath = "/usr/bin/say"
task.arguments = ["This is an example of speech synthesis."]
task.launch()</langsyntaxhighlight>
 
=={{header|Tcl}}==
This just passes the string into the Festival system:
<langsyntaxhighlight lang="tcl">exec festival --tts << "This is an example of speech synthesis."</langsyntaxhighlight>
Alternatively, on MacOS X, you'd use the system <code>say</code> program:
<langsyntaxhighlight lang="tcl">exec say << "This is an example of speech synthesis."</langsyntaxhighlight>
On Windows, there is a service available by COM for speech synthesis:
{{libheader|tcom}}
<langsyntaxhighlight 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</langsyntaxhighlight>
Putting these together into a helper procedure, we get:
<langsyntaxhighlight lang="tcl">proc speak {msg} {
global tcl_platform
if {$tcl_platform(platform) eq "windows"} {
Line 913 ⟶ 936:
}
}
speak "This is an example of speech synthesis."</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 919 ⟶ 942:
 
{{works with|Bourne Shell}} {{works with|bash}}
<langsyntaxhighlight lang="bash">#!/bin/sh
espeak "This is an example of speech synthesis."</langsyntaxhighlight>
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vbs">
Dim message, sapi
message = "This is an example of speech synthesis."
Set sapi = CreateObject("sapi.spvoice")
sapi.Speak message
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
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.
<langsyntaxhighlight ecmascriptlang="wren">/* speech_synthesisSpeech_synthesis.wren */
 
class C {
Line 942 ⟶ 965:
System.write("Enter something to say (up to 100 characters) : ")
var s = C.getInput(100)
C.espeak(s)</langsyntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc Speech_synthesis.c -o Speech_synthesis -lwren -lm */
<lang c>#include <stdio.h>
 
<lang c>#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
Line 1,007 ⟶ 1,032:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "speech_synthesisSpeech_synthesis.wren";
char *script = readFile(fileName);
wrenInterpret(vm, module, script);
Line 1,013 ⟶ 1,038:
free(script);
return 0;
}</langsyntaxhighlight>
 
=={{header|Zoomscript}}==
For typing:
<langsyntaxhighlight Zoomscriptlang="zoomscript">speak "This is an example of speech synthesis."</langsyntaxhighlight>
For importing:
 
Line 1,026 ⟶ 1,051:
This example makes use of the Currah Speech Synthesizer peripheral device.
 
<langsyntaxhighlight lang="zx basic">10 LET s$="(th)is is an exampul of sp(ee)(ch) sin(th)esis":PAUSE 1</langsyntaxhighlight>
{{omit from|TI-83 BASIC}}
{{omit from|Maxima}}
9,488

edits