#!c:/perl/bin/perl.exe -w use strict; #uncomment the next line if you want the script to write a wave file for 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 = ; 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 failed"; 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 failed"; $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 failed"; 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); } }