Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

SAPI Text-to-speech in Perl

by Massyn (Hermit)
on Nov 20, 2003 at 10:54 UTC ( [id://308544]=CUFP: print w/replies, xml ) Need Help??

#!/fellow/monks.pl

I finally found the solution to dump text as a wave file, using Microsoft SAPI Text to Speech. The original post was 247398

#!c:/perl/bin/perl.exe -w use strict; #uncomment the next line if you want the script to write a wave file f +or you # &SAPIwave("This is the wave file created by Phil's sapi Perl script" +,"0","c:\\temp\\mary.wav"); &SAPItalk("Hello, and welcome to Phil's sapi perl script",0); &SAPItalk("The following voices are installed.",1); #------------------------------------------------ # Run test procedure #------------------------------------------------ &SAPItest; #------------------------------------------------ # Display all installed voices #------------------------------------------------ my %voicelist = &SAPIgetVoices; my $a = ''; print "\n"; foreach $a (sort keys %voicelist) { print "$voicelist{$a} = $a\n"; } &SAPItalk("Who would you like to speak next?",0); my $voice = &ask;; &SAPItalk("What would you like me to say?",$voice); my $text = &ask(); &SAPItalk($text,$voice); &SAPItalk("Thank you, and have a nice day.",0); exit(0); #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#= +#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#= # E N D O F M A I N P R O G R A M #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#= +#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#= sub ask { print "$_[0]> "; my $answer = <STDIN>; chomp($answer); return $answer; } sub SAPIgetVoices { ################################################################## +### # SAPIgetVoices #----------------------------------------------------------------- +--# # Returns all SAPI voices via a hash ################################################################## +### use Win32::OLE; my $tts = Win32::OLE->new("Sapi.SpVoice") or die "Sapi.SpVoice fai +led"; my %VOICES; for(my $VoiceCnt=0;$VoiceCnt < $tts->GetVoices->Count();$VoiceCnt+ ++) { my $desc = $tts->GetVoices->Item($VoiceCnt)->GetDescription; $VOICES{"$desc"} = $VoiceCnt; } return %VOICES; } sub SAPItalk { ################################################################## +### # SAPItalk #----------------------------------------------------------------- +--# # Speaks the text in the voice number specified ################################################################## +### use Win32::OLE; my ($text,$voice) = @_; my $tts = Win32::OLE->new("Sapi.SpVoice") or die "Sapi.SpVoice fai +led"; $tts->{Voice} = $tts->GetVoices->Item($voice); print "[ $text ]\n"; $tts->Speak("$text", 1); $tts->WaitUntilDone(-1); } sub SAPIwave { ################################################################## +### # SAPIwave #----------------------------------------------------------------- +--# # Creates a wave file, worksjust like SAPItalk ################################################################## +### my ($text,$voice,$wave) = @_; use Win32::OLE; my $type=Win32::OLE->new("SAPI.SpAudioFormat"); # stereo = add 1 # 16-bit = add 2 # 8KHz = 4 # 11KHz = 8 # 12KHz = 12 # 16KHz = 16 # 22KHz = 20 # 24KHz = 24 # 32KHz = 28 # 44KHz = 32 # 48KHz = 36 $type->{Type}=30; my $stream=Win32::OLE->new("SAPI.SpFileStream"); $stream->{Format}=$type; $stream->Open("$wave",3,undef); my $tts=Win32::OLE->new("SAPI.SpVoice"); $tts->{AudioOutputStream}=$stream; $tts->Speak($text,1); $tts->WaitUntilDone(-1); $stream->Close( ); } sub SAPItest { ################################################################## +### # SAPItest #----------------------------------------------------------------- +--# # Speaks each voice installed in the SAPI environment. ################################################################## +### use Win32::OLE; my $tts = Win32::OLE->new("Sapi.SpVoice") or die "Sapi.SpVoice fai +led"; for(my $VoiceCnt=0;$VoiceCnt < $tts->GetVoices->Count();$VoiceCnt+ ++) { $tts->{Voice} = $tts->GetVoices->Item($VoiceCnt); my $desc = $tts->GetVoices->Item($VoiceCnt)->GetDescription; $text = "This is $desc, voice number $VoiceCnt"; print "[ $text ]\n"; $tts->Speak("$text", 1); $tts->WaitUntilDone(-1); } }

Thanks!

#!/massyn.pl

Don't -- me without telling me why. If you asked a stupid question, I wouldn't down vote you.

Replies are listed 'Best First'.
Re: SAPI Text-to-speech in Perl
by zentara (Archbishop) on Nov 20, 2003 at 15:48 UTC
    This is related, but may interest you or others. It is perltk voice recognition. PerlBox-Voice

    "Perlbox Voice is a voice command system, aiming to provide an easy to use, easy to configure application that connects spoken human words to computer commands, and vice versa. It allows the user to easily configure vocabularies that consist of "commands" and "responses". When the operator speaks a command (for example, "web browser"), the computer will execute the designated response (for example, "mozilla")."

    I'm still looking at it, but the "phonetics" of how they recognize words is pretty interesting

Re: SAPI Text-to-speech in Perl
by Jouke (Curate) on Oct 04, 2004 at 09:26 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://308544]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-19 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found