Speech synthesis: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Tcl}}: Added Windows support)
Line 22: Line 22:
Alternatively, on MacOS X, you'd use the system <code>say</code> program:
Alternatively, on MacOS X, you'd use the system <code>say</code> program:
<lang tcl>exec say << "This is an example of speech synthesis."</lang>
<lang tcl>exec say << "This is an example of speech synthesis."</lang>
On Windows, there is a service available by COM for speech synthesis:
{{libheader|tcom}}
<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:
Putting these together into a helper procedure, we get:
<lang tcl>proc speak {msg} {
<lang tcl>proc speak {msg} {
global tcl_platform
global tcl_platform
if {$tcl_platform(os) eq "Darwin"} {
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
exec say << $msg
} else {
} else {

Revision as of 07:03, 25 April 2011

Speech synthesis 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.

Render the text “This is an example of speech synthesis.” as speech.

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>

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:

Library: tcom

<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:

Works with: Bourne Shell
Works with: bash

<lang sh>#!/bin/sh espeak "This is an example of speech synthesis."</lang>