Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Win32::OLE get list of installed voices

by Takamoto (Sexton)
on May 15, 2019 at 08:59 UTC ( #11100009=perlquestion: print w/replies, xml ) Need Help??

Takamoto has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

On a Windows machine I need to get all installed voices for TTS in order to pick one and let the machine convert text to speech. I am using the following script, which is fine. I can set the voice I want. What I am failing is to read the available voice. Probably I am committing a very naive error.

use strict; use warnings; use Data::Dumper; use Win32::OLE; my $speech = Win32::OLE->new("SAPI.SpVoice") or die ("Error with TTS") +; #trying to get installed voices my $AvailableVoices = $speech->GetVoices(); print Dumper $AvailableVoices; say ("Hello user."); # I am using the default voice say ("Hello user.", "Microsoft David Desktop"); # I am setting a diffe +rent voice which is installed on the machine sub say { my ($text, $voice) = @_; $speech->{voice} = $speech->GetVoices("name = $voice")->Item(0) if $ +voice; $speech->Speak($text); }

This is the output of $AvailableVoices:

$VAR1 = bless( { 'Count' => 5 }, 'Win32::OLE' );

At least is the count right, I have 5 voices installed. Any suggestions?

Replies are listed 'Best First'.
Re: Win32::OLE get list of installed voices
by Discipulus (Abbot) on May 15, 2019 at 09:19 UTC
    Hello Takamoto,

    I found a sub in an experiment of mines dating 2007.. I rearranged for you:

    use strict; use warnings; use Data::Dumper; use Win32::OLE; my %voices = SAPIgetVoices(); print Dumper \%voices; sub SAPIgetVoices{ 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; #print "descr= $desc\n"; $VOICES{"$desc"} = $VoiceCnt; } return %VOICES; } # OUTPUT $VAR1 = { 'Microsoft Elsa Desktop - Italian (Italy)' => 0, 'Microsoft Zira Desktop - English (United States)' => 1 };

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Lovely, Discipulus. Simply perfect!

Re: Win32::OLE get list of installed voices
by NetWallah (Canon) on May 16, 2019 at 05:42 UTC
    Discipulus' code simplified (UPDATE: and enhanced - it also SPEAKS the info using the voice):
    use strict; use warnings; use Data::Dumper; use Win32::OLE qw(in); GetVoiceInfo(); sub GetVoiceInfo{ my $tts = Win32::OLE->new("Sapi.SpVoice") or die "Sapi.SpVoice faile +d"; for my $voice (in $tts->GetVoices){ print "Found voice: ", $voice->GetDescription(), "\n\tID:", $voice->Id(), "\n\n"; $tts->{Voice} = $voice; # Sets the "Voice" PROPERTY $tts->Speak("I am the voice of " . $voice->GetDescription()); } }
    Output:
    Found voice: Microsoft David Desktop - English (United States) ID:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\ +TTS_MS_EN-US_DAVID_11.0 Found voice: Microsoft Zira Desktop - English (United States) ID:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\ +TTS_MS_EN-US_ZIRA_11.0

                    "It's ten o'clock... Do you know where your AI programs are?"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11100009]
Approved by Discipulus
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2019-05-25 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (151 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!