Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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.


In reply to SAPI Text-to-speech in Perl by Massyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found