Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

text2speech and speech recognition for perl in 2023

by Discipulus (Canon)
on Jan 19, 2023 at 09:13 UTC ( #11149681=perlquestion: print w/replies, xml ) Need Help??

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

Hello folks!

with age advancing I'm getting even more lazy than before and my eyes get tired soon reading a zillion of emails, so I modified my IMAP monitor to use voice.exe so I can ear the subject of important messages while I'm working on other things (true laziness is hard work ;)

The implementation is quick and dirty as system (1, $voicecmd, $txt) or die "Unable to speak!"; but raised once again my interest on text2speech and speech recognition state of the art, obviously Perl side of.

As I understand it, both text2speech and speech recognition are always provided by external programs.

At work I use microsoft so I picked up the above mentioned voice.exe wich is simply a wrapper around System.Speech.Synthesis but I remember eons ago I used SAPI voices.

Win32::SAPI4 matrix is blatantly red but Win32::SAPI5 one looks promising, even being 2005. Not its documentation :(

SAPI5 SDK latest release is from 2022 and its 3895 friendly pages manual after many CTRL-F attempts lead me to something working:

perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->Speak('finally hello world')"

I'm not satisfied: I'd like to know the state of the art of software with some or easy implementable perl interface:

  • text2speech: I found PerlSpeak-2.01 latest release 2008, pointing to festival (latest release 2015). There is something actively developped? There is some cross-platform project usable in perl?
  • speech recognition: what software can I explore to use within perl? Both Linux and windows ideas are welcome but a cross-platform one will be the best.


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.

Replies are listed 'Best First'.
Re: text2speech and speech recognition for perl in 2023
by Corion (Patriarch) on Jan 19, 2023 at 09:54 UTC

    I wrote Speech::Recognition::Vosk, but it certainly needs some love.

    I also am looking at whisper.cpp, which should be easier to build, but I haven't put it into use. I plan a start with Inline::C, simply embedding/including the header file, and then trying to port over the stream.cpp example file into an API.

    Text-to-speech seems to be a very convoluted setup in every implementation I've looked at. There is mimic-3, but I haven't found a library that doesn't have lots and lots of build prerequisites.

Re: text2speech and speech recognition for perl in 2023
by cavac (Vicar) on Jan 19, 2023 at 16:00 UTC

    I found that the hardest part for voice recognition is not implementing it in your program. At least for me and my own speech patterns, most systems go as far as "i recognize that this is a voice you are using" before giving up and selecting some random action in the vague hope that this is what i asked for.

    The best solution so far (at least for me) is using one of those Echo dot thingies and creating a module/app/whateveritsname on their site that RPCs my own home automation system. Google also has good voice recognition.

    The problem ATM is that any locally installed version will almost always be inferior to what Amazon and Google can do, simply by throwing Petabytes of training data into their data centers to train some neural network. And then run that neural network on their custom-made neural network processor hardware.

    If you don't mind Google listening in, you can try their speech-to-text models for free if i'm not mistaken. After that, it costs 2-3 cents per minute. The sad thing about this is, with current energy prices, it's probably more expensive to run an (inferior) speech recognition locally. There are client libs for a few different programming languages, shouldn't be impossible to re-implement one of those in Perl (or just control an external program)

    There is also the possibility to use your browsers speech recognition (also uses the Google cloud in most cases) and sending the recocognized text to the perl webserver of your choice. BTW, browsers also have a build-in text-to-speech API.

    Putting it in a website could make your application a bit more flexible, since you probably have a matching client in your pocket (=smartphone), so you can control your stuff by speech from everywhere. Basically, you outsource all the speech stuff to your browser, and do all the cool and interesting stuff in your perl backend.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Re: text2speech and speech recognition for perl in 2023
by Discipulus (Canon) on Jan 31, 2023 at 11:51 UTC
    some further research findings:

    About Win32::SAPI5 (module code indecryptable by me: I cannot even find the Speak method :) ..just to have somewhere to record my tries:

    perl -MWin32::SAPI5 -MClass::Inspector -MData::Dumper -e "print Dumper + \Class::Inspector->methods('Win32::SAPI5::SpVoice','expanded')" [..] # or perl -MWin32::SAPI5 -e "for (sort keys %Win32::SAPI5::SpVoice::){print + qq($_\n) if exists &{qq(Win32::SAPI5::SpVoice::$_)}}" # working perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); print for + $voice->GetInstalledLanguages" Italian (Italy)English (United States of America) # NOT working perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); print for + $voice->GetInstalledVoices" # working pitch perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->S +peak( q{ <pitch absmiddle='10'>This is a test.</pitch>})" perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->S +peak( q{ <pitch absmiddle='-10'>This is a test.</pitch>})" # working absspeed from -10 .. 10 ? perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->S +peak( q{ <rate absspeed='-10'>this is some input text</rate>})" # working silence and spelling perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->S +peak( q{<silencemsec='500'/> <spell>UN</spell>})" # working fortmat date perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->S +peak( q{ <says-as interpret-as='date'> 4/12/1972 })" #wondering if it works :) perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->S +peak( q{ <prompt>If I want I can <prosody rate='+150%'>spee +d up the rate</prosody></prompt> })" # not working perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->S +peak( q{ Today is <Context ID='date'>01/31/2023</Context> + })" # working but not with text in front of tags perl -MWin32::SAPI5 -e "$voice=Win32::SAPI5::SpVoice->new(); $voice->S +peak( q{ <context ID = 'web_url'></context> <co +ntext ID = 'time'>12:30</context> })"


    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.
Re: text2speech and speech recognition for perl in 2023
by Anonymous Monk on Jan 19, 2023 at 14:09 UTC
Re: text2speech and speech recognition for perl in 2023
by harangzsolt33 (Hermit) on Jan 20, 2023 at 04:14 UTC
    On Windows, text-to-speech is easy. Windows has a builtin text-to-speech service. Even Windows XP has it.

    If you save the following code as a text file and rename it to SPEAK.VBS and then call it from Perl...your Windows PC will speak! The first argument should be a number between 1 and 100. The second argument should be some English words enclosed with double quotes. (If you omit the double quotes, then it will only read the first word.)

    IF Wscript.Arguments.length > 1 THEN SET VOICE = CreateObject("SAPI.SpVoice") VOICE.Volume = Wscript.Arguments(0) VOICE.Speak Wscript.Arguments(1) WScript.Quit 1 ELSE WScript.Echo "Usage: speak.vbs <VOLUME> <TEXT>" END IF WScript.Quit 0

    (Note: Don't send too much text all at one time, because the computer will freeze up while it speaks. At least, that's what happens in Windows XP. So, if you have a bunch of things you want the computer to read, then call SPEAK.VBS with just a few words at a time. That way if you want to press CTRL+BREAK, you can interrupt your program, and it will stop speaking. But if you call SPEAK.VBS with a whole paragraph, it will not stop until it has finished reading the entire paragraph.)

      Again, you can cut out the middle man of writing files then calling them through perl by automating using Win32::OLE, cleaner, more control etc.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11149681]
Approved by marto
Front-paged by kcott
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2023-03-31 02:20 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (74 votes). Check out past polls.