Speechd - Perl Module wrapper for speech-dispatcher.


Speechd documentation Contained in the Speechd distribution.

Index


Code Index:

NAME

Top

Speechd - Perl Module wrapper for speech-dispatcher.

DESCRIPTION

Top

Speechd is a Perl module to make it easy to use speech-dispatcher for text to speech functions.

SYNOPSIS

Top

my $sd = Speachd->new([property => value, property => value, ...]);

PROPERTIES

port : Port number.

Default is 6560

ip : IP address.

Default is 127.0.0.1

voice : Voice name.

Default is MALE1. Possible voice names are: MALE1 MALE2 MALE3 FEMALE1 FEMALE2 FEMALE3 CHILD_MALE CHILD_FEMALE

rate : Speaking rate.

Default is 0. Possible values are from -100 to 100.

volume : Speaking volume.

Default is 0. Possible values are from -100 to 100.

pitch : Speaking pitch.

Default is 0. Possible values are from -100 to 100.

lang : Speaking lanuage.

Default value is en (english).

METHODS

new

my $sd = Speachd->new([property => value, property => value, ...]);

Creates a new instance of the Speachd object.

connect

$sd->connect();

Connect a socket to speech-dispatcher. This must be called before methods say, cancel, voice, volume, pitch, lang, and config_voice can be used.

disconnect

$sd->disconnect();

Disconnect socket from speech-dispatcher.

port

$sd->port([$port_number]);

If port number is given; sets port number and returns previos value. If no port number is given; returns value. Default value is 6560

ip

$sd->ip([$ip_address]);

If ip address is given; sets ip address and returns previos value. If no ip address is given; returns value. Default value is 127.0.0.1

voice

$sd->voice([$voice]);

If voice is given; sets voice and returns previos value. If no voice is given; returns value. Default value is MALE1. Possible values are: MALE1 MALE2 MALE3 FEMALE1 FEMALE2 FEMALE3 CHILD_MALE CHILD_FEMALE If not connected, method msg will return error.

rate

$sd->rate([$rate]);

If rate is given; sets rate and returns previos value. If no rate is given; returns value. Default value is 0. Possible values are from -100 to 100. If not connected, method msg will return error.

volume

$sd->volume([$volume]);

If volume is given; sets volume and returns previos value. If no volume is given; returns value. Default value is 0. Possible values are from -100 to 100. If not connected, method msg will return error.

pitch

$sd->pitch([$pitch]);

If volume is given; sets volume and returns previos value. If no volume is given; returns value. Default value is 0. Possible values are from -100 to 100. If not connected, method msg will return error.

lang

$sd->lang([$lang]);

If lang is given; sets language and returns previos value. If no lang is given; returns value. Default value is en. If not connected, method msg will return error.

config_voice

$sd->config_voice($voice, $lang, [$rate, $volume, $pitch]);

Sets parameters for speech-dispatcher. See individual methods for possible values.

msg

my $message = $sd->msg();

Returns and clears messages from previous command sent to speechd.

say

$sd->say($text_to_speak);

Sends text to speech-dispatcher to be spoken.

cancel

$sd->cancel();

Kills speech.

get_voices

my $voices = $sd->get_voices();

Returns a reference to an array holding all possible voice names.

sendraw

$sd->sendraw($command_to_send_to_speech-dispatcher);

Available to send commands directly to speech-dispatcher. Puts return messages into msg.

EXAMPLE

Top

 #!/usr/bin/perl

 use strict;
 use warnings;

 use Speechd;

 my $rate = 0;
 my $vol = 0;
 my $pitch = 0;
 my $lang = "en";
 my $voice = "MALE1";

 my $sd = Speechd->new(
    'rate' => $rate,
    'volume' => $vol,
    'lang' => $lang,
    'voice' => $voice,
 );

 $sd->connect();

 while (1) {
    print "Enter text to speak:\n";
    my $text = <>;
    $sd->say($text);
    my $message = $sd->msg();
    print $message;
    chomp $text;
    $text = lc($text);
    last if $text eq "goodbye";
 }

 $sd->disconnect();

 exit 0;

SEE ALSO

Top

More information about speech-dispatcher can be fount at: http://www.freebsoft.org

AUTHOR

Top

Joe Kamphaus, <joe@joekamphaus.net>

COPYRIGHT AND LICENSE

Top


Speechd documentation Contained in the Speechd distribution.
package Speechd;
use 5.006;
use strict;
use warnings;
use vars qw($VERSION);
$VERSION = '0.50';

use IO::Socket::INET;


sub new {
	my $pkg = shift;
	my $self = {
		"port" => 6560,
		"ip" => "127.0.0.1",
                "voice" => "MALE1",
                "rate" => 0,
                "volume" => 0,
                "pitch" => 0,
                "lang" => "en",
		@_};
	return bless $self, $pkg;
}

sub connect {
    my $self = shift;
    $self->{socket} = IO::Socket::INET->new(
				PeerAddr => $self->{ip},
                                PeerPort => $self->{port},
                                Proto    => "tcp",
                                Type     => SOCK_STREAM,
                                Blocking => 0,
    ) or die "
        Couldn't connect to speechd on port $self->{'port'} and IP $self->{'ip'}: 
        $@\n
        Perhaps speech-dispatcher is not running.";
}
sub disconnect {
    my $self = shift;
    $self->sendraw("quit\r\n");
    $self->{socket}->close();
}

sub port {
    my $self = shift;
    my $port = shift;
    my $ret = $self->{'port'};
    if ($port) {
	$self->{'port'} = $port;
    }
    return $ret;
}

sub ip {
    my $self = shift;
    my $ip = shift;
    my $ret = $self->{ip};
    if ($ip) {
	$self->{ip} = $ip;
    }
    return $ret;
}

sub voice {
    my $self = shift;
    my $voice = shift;
    my $ret = $self->{voice};
    if ($voice) {
	$self->{voice} = $voice;
        $self->sendraw("set self voice $voice\r\n");
    }
    return $ret;
}

sub rate {
    my $self = shift;
    my $rate = shift;
    my $ret = $self->{rate};
    if ($rate) {
	$self->{rate} = $rate;
        $self->sendraw("set self rate $rate\r\n");
    }
    return $ret;
}

sub volume {
    my $self = shift;
    my $volume = shift;
    my $ret = $self->{volume};
    if ($volume) {
	$self->{volume} = $volume;
        $self->sendraw("set self volume $volume\r\n");
    }
    return $ret;
}

sub pitch {
    my $self = shift;
    my $pitch = shift;
    my $ret = $self->{pitch};
    if ($pitch) {
	$self->{pitch} = $pitch;
        $self->sendraw("set self pitch $pitch\r\n");
    }
    return $ret;
}

sub lang {
    my $self = shift;
    my $lang = shift;
    my $ret = $self->{lang};
    if ($lang) {
	$self->{lang} = $lang;
        $self->sendraw("set self language $lang\r\n");
    }
    return $ret;
}

sub config_voice { # voice, lang, rate, volume, pitch
    my $self = shift;
    my $voice = shift;
    my $lang = shift;
    my $rate = shift;
    my $vol = shift;
    my $pitch = shift;
    $self->voice($voice);
    $self->rate($rate);
    $self->volume($vol);
    $self->pitch($pitch);
    $self->lang($lang);
}

sub msg {
    my $self = shift;
    my $msg = $self->{msg};
    $self->{msg} = "";
    return $msg;
}


sub say {
    my $self = shift;
    my $text = shift;
    $self->sendraw("speak\r\n$text\r\n.\r\n");
}

sub cancel {
    my $self = shift;
    $self->sendraw("cancel self\r\n");
}

sub get_voices {
    my $self = shift;
    my @voice_lst = qw(MALE1 MALE2 MALE3 FEMALE1 FEMALE2 FEMALE3 CHILD_MALE CHILD_FEMALE);
    return \@voice_lst;
}

sub sendraw {
    my $self = shift;
    my $raw = shift;
    if ($self->{socket}->connected()) {
        $self->msg();
	$self->{socket}->print($raw);
	while (my $ln = $self->{socket}->getline()) {
	    $self->{msg} .= $ln;
	}
    } else {
	$self->{msg} = "Error no socket connection.\n";
	print $self->{msg};
    }
}


1;

__END__