Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Text to Speech Win32::OLE Error

by TROGDOR (Scribe)
on Nov 01, 2004 at 23:08 UTC ( #404451=perlquestion: print w/replies, xml ) Need Help??
TROGDOR has asked for the wisdom of the Perl Monks concerning the following question:

Greetings esteemed Monks,

I'm trying to get Text to Speech working in perl on a Win32 box, but I'm running into an "Invalid class string" error. First, here are my vitals:

OS: MS Professional XP, service pack 1.
Perl: ActivePerl v5.8.4, Binary build 810
Win32-OLE: version 0.1403, installed with ppm

I'm working with the verified code from this thread. Here's the code:
use strict; use warnings; use Win32::OLE; # Set up speech. my $voice = Win32::OLE->new("Speech.VoiceText") or die("TTS failed"); $voice->Register("", "$0"); $voice->{Enabled} = 1; # optional my $question = <<__EOS__; Shall we play a game? __EOS__ my $answer = <<__EOS__; How about Global Thermonuclear Text to Speech. __EOS__ $voice->Speak($question, 1); $voice->Speak($answer, 1); while ($voice->IsSpeaking()) { sleep 1; }

When I run this code, I get the following error message:
C:\Apps\Perl\dev>perl Win32::OLE(0.1403) error 0x800401f3: "Invalid class string" at speech. +pl line 7 eval {...} called at line 7 TTS failed at line 7.
Line 7 is the Win32::OLE->new call. This looks like a module dependency error. If I'm reading this error correctly, the OLE module is trying to instantiate a string, and the class is not defined. So I reran ppm, did a search for string, and found 111 results. I installed Set-String, but I still get this error. Is this a module dependency error, and if so, what module do I need to install to prevent this error? Is ppm smart enough to grab missing dependent modules at install time?

Thanks for your help,

Replies are listed 'Best First'.
Re: Text to Speech Win32::OLE Solution
by elwarren (Curate) on Nov 02, 2004 at 00:07 UTC
    Every version of windows seems to do this different. Microsoft broke TTS when XP came out. Broken as far as I'm concerened because my code that worked on win2k doesn't on XP. I've given up on asking why. Try this , it works on my XP machine. I pipe things into it via STDIN.
    #!/usr/bin/perl use strict; use warnings; use Win32::OLE; my $v=Win32::OLE->new('SAPI.SpVoice'); while (<>) { # space out numbers so they'll # be spoken individually my $single = s/(\d)/$1 /g; $v->Speak($_); }
    haha, and ++ for "Would you like to play a game", the "Hello, World" of text to speech :-)
      Thanks elwarren, that worked. Can you recommend a reference for the OLE interface? How did you find out about this change?

      Leave it to MS to hose backward compatibility. I'm never quite sure if this kind of stuff happens due to incompetence, or intentional marketing strategy.

      Anyhow, it worked. And I've learned that "Invalid class string" means OLE didn't recognize the request.

      Thanks Wise Ones!
        Do not ascribe to malice that which is easily explained by incompetence --Napolean



      See my reply to the previous thread. The 'why' is that XP has SAPI5 (which you are using in your example), and you probably used VoiceText on your Win2K machine (which is SAPI4). These two are incompatible

      Jouke Visser
      Using Perl to enable the disabled: pVoice
Re: Text to Speech Win32::OLE Error
by tachyon (Chancellor) on Nov 01, 2004 at 23:48 UTC

    The invalid class string is "Speech.VoiceText". This is the M$ application not Perl. For example to fool around with Excel you do:

    my $Excel = Win32::OLE->new("Excel.Application" , sub { $_[0]->Quit } +)

    So you don't appear to have the TTS engine installed. It works for me on Win2k but if I change the string to Speech1.VoiceText" (which of course does not exists) I get the invalid class string error. You can (probably) find the engine here



      Hi Tachyon,

      Thanks for the suggestion. However, I know that TTS is already installed on my system. I'm pretty sure it ships standard with XP. In the control pannel, I can select "Speech", and preview the installed "LH Michael", "LH Michelle", and "Microsoft SAM" voices. Also, in Acrobat I can select View->Read Out Loud, and the page is spoken. So the TTS is there, I'm just not calling it right. Any idea how I can figure out the magic string?

      Speak friend and enter?

      Thanks, TROGDOR
        You're using Windows XP, which has SAPI5 installed by default. However, the class you're trying to load is SAPI4. Even if you'd install the SAPI4 runtime, you still won't be able to use the voices you mentioned, since SAPI5 voices can't be used in SAPI4.
        I recommend you use Win32::SAPI5. It does the hard work for you.

        Kind regards,

        Jouke Visser
        Using Perl to enable the disabled: pVoice
Re: Text to Speech Win32::OLE Error
by Jouke (Curate) on Nov 02, 2004 at 05:48 UTC
    Maybe you'll want to take a look at one of my modules that can provide Text to Speech, Win32::SAPI4, Win32::SAPI5 or Win32::MSAgent.

    They handle the creation of the OLE objects, and allow you to call any of their methods and set any of their properties (using Win32::OLE's SetProperty method).

    Jouke Visser
    Using Perl to enable the disabled: pVoice

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://404451]
Approved by tachyon
Front-paged by diotalevi
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-06-24 09:40 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (557 votes). Check out past polls.