#!/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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.