Perl Speak and Spell

brian_d_foy on 2002-11-17T08:44:08

I made a Perl program that spoke text, then Chris Nandor improved it with his Mac::Speech module that comes with the new Mac::Carbon. You need the Developer Tools for better documentation though. It's just a Perl version of the Carbon stuff.

I have been looking for an excuse to use Robby Walker's Tie::FileHandle::MultiPlex module, which he wrote about in The Perl Review v0 i5. Now I want any output to show up on the screen as well as the speakers. (Note: IO::Tee can do the same thing).

I create a Tie::IO::Speak that speaks anything I print to its handle, SPEAK. Once I have that I multiplex output to STDOUT and SPEAK. There is an initial delay for the first spoken text, so I take care of that when I tie the handle by speaking the empty string.

If I want to have use.perl spoken to me, I simply add this stuff to my journal reader program. :)

#!/usr/bin/perl

package main;

use Tie::FileHandle::Multiplex;

tie *SPEAK, 'Tie::IO::Speak'; tie *MULTI, 'Tie::FileHandle::MultiPlex', *STDOUT, *SPEAK;

print MULTI "$_\n" for ("Starting countdown, Dr. Evil", reverse ( 'Blastoff', 1 .. 10 ) );

BEGIN { package Tie::IO::Speak; $|++; use Mac::Speech; my $voice = $Mac::Speech::Voice{Victoria}; my $channel = NewSpeechChannel($voice); SpeakText( $channel, '' ); sub TIEHANDLE { my $class = shift; bless {}, $class; } sub PRINT { my $self = shift; SpeakText( $channel, $_[0] ); sleep 1 while SpeechBusy(); } sub PRINTF { my $self = shift; $self->PRINT( sprintf @_ ); } sub DESTROY { DisposeSpeechChannel($channel) } }