Skip to content

Commit

Permalink
Improved cli app, small fixes
Browse files Browse the repository at this point in the history
CLI app can now accept flac, speex or wav files as input.
Small fixes in other scripts.
  • Loading branch information
zaf committed Mar 4, 2012
1 parent 8dd27fc commit 9d10199
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 39 deletions.
106 changes: 91 additions & 15 deletions samples/speech-recog-cli.pl
Expand Up @@ -9,7 +9,7 @@
# the GNU General Public License Version 2. See the COPYING file # the GNU General Public License Version 2. See the COPYING file
# at the top of the source tree. # at the top of the source tree.
# #
# The script takes as input flac files at 8kHz and returns the following values: # The script takes as input flac, speex or wav files at 8kHz and returns the following values:
# status : Return status. 0 means success, non zero values indicating different errors. # status : Return status. 0 means success, non zero values indicating different errors.
# id : Some id string that googles engine returns, not very useful(?). # id : Some id string that googles engine returns, not very useful(?).
# utterance : The generated text string. # utterance : The generated text string.
Expand All @@ -20,24 +20,55 @@


use strict; use strict;
use warnings; use warnings;
use File::Temp qw(tempfile);
use Getopt::Std;
use File::Basename;
use LWP::UserAgent; use LWP::UserAgent;


if (!@ARGV || $ARGV[0] eq '-h' || $ARGV[0] eq '--help') { my %options;
print "Speech recognition using google voice.\n\n"; my $filetype;
print "Usage: $0 [FILES]\n\n";
exit;
}

my $url = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium"; my $url = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium";
my $samplerate = 8000; my $samplerate = 8000;
#my $filetype = "x-speex-with-header-byte";
my $filetype = "x-flac";
my $language = "en-US"; my $language = "en-US";
my $results = 1; my $results = 1;

getopts('l:r:hq', \%options);

VERSION_MESSAGE() if (defined $options{h} || !@ARGV);

if (defined $options{l}) {
# check if language setting is valid #
if ($options{l} =~ /^[a-z]{2}(-[a-zA-Z]{2,6})?$/) {
$language = $options{l};
} else {
say_msg("Invalid language setting. Using default.");
}
}

if (defined $options{r}) {
# set number or results #
$results = $options{r} if ($options{r} =~ /%d+/);
}

my @file_list = @ARGV; my @file_list = @ARGV;


foreach my $file (@file_list) { foreach my $file (@file_list) {
print "Openning $file\n"; my($filename, $dir, $ext) = fileparse($file, qr/\.[^.]*/);
if ($ext ne ".flac" && $ext ne ".spx" && $ext ne ".wav") {
say_msg("Unsupported filetype: $ext");
exit 1;
}

if ($ext eq ".flac") {
$filetype = "x-flac";
} elsif ($ext eq ".spx") {
$filetype = "x-speex-with-header-byte";
} elsif ($ext eq ".wav") {
$filetype = "x-flac";
$file = encode_flac($file);
}

say_msg("Openning $file");
open(my $fh, "<", "$file") or die "Cant read file: $!"; open(my $fh, "<", "$file") or die "Cant read file: $!";
my $audio = do { local $/; <$fh> }; my $audio = do { local $/; <$fh> };
close($fh); close($fh);
Expand All @@ -54,15 +85,60 @@
if ($response->content =~ /^\{"status":(\d*),"id":"(.*)","hypotheses":\[(.*)\]\}$/) { if ($response->content =~ /^\{"status":(\d*),"id":"(.*)","hypotheses":\[(.*)\]\}$/) {
$response{status} = "$1"; $response{status} = "$1";
$response{id} = "$2"; $response{id} = "$2";
if ($response{status} == 5) { if ($response{status} != 0) {
print "Error reading audio file\n"; say_msg("Error reading audio file");
} }
if ($3 =~ /^\{"utterance":"(.*)","confidence":(.*)\}/) { if ($3 =~ /^\{"utterance":"(.*)","confidence":(.*)\}/) {
$response{utterance} = "$1"; $response{utterance} = "$1";
$response{confidence} = "$2"; $response{confidence} = "$2";
} }
} }
printf "%-10s : %s\n", $_, $response{$_} foreach (keys %response); if (!defined $options{q}) {
#print $response->content; printf "%-10s : %s\n", $_, $response{$_} foreach (keys %response);
} else {
print "$response{utterance}\n";
}
print $response->content;
}
exit 0;

sub encode_flac {
# Encode file to flac and return the filename #
my $file = shift;
my $tmpdir = "/tmp";
my $flac = `/usr/bin/which flac`;

if (!$flac) {
say_msg("flac encoder is missing. Aborting.");
exit 1;
}
chomp($flac);

my ($fh, $tmpname) = tempfile("recg_XXXXXX",
DIR => $tmpdir,
SUFFIX => '.flac',
UNLINK => 1,
);
if (system($flac, "-8", "-f", "--totally-silent", "-o", "$tmpname", "$file")) {
say_msg("$flac failed to encode file.");
exit 1;
}
return $tmpname;
}

sub say_msg {
# Print messages to user if 'quiet' flag is not set #
print @_, "\n" if (!defined $options{q});
}

sub VERSION_MESSAGE {
# Help message #
print "Speech recognition using google voice API.\n\n",
"Usage: $0 [options] [file(s)]\n\n",
"Supported options:\n",
" -l <lang> specify the language to use, defaults to 'en-US' (English)\n",
" -r <number> specify the number of results\n",
" -q Return only recognised utterance. Don't print any messages or warnings\n",
" -h this help message\n\n";
exit 1;
} }
exit;
36 changes: 18 additions & 18 deletions samples/wolfram/wolfram.agi
Expand Up @@ -63,12 +63,12 @@ while (<STDIN>) {
$name = " -- $AGI{request}:"; $name = " -- $AGI{request}:";


if (!$app_id) { if (!$app_id) {
&return_var("You must have an Application ID from WolframAlpha to use this script."); return_var("You must have an Application ID from WolframAlpha to use this script.");
die "$name You must have an Application ID from WolframAlpha to use this script.\n"; die "$name You must have an Application ID from WolframAlpha to use this script.\n";
} }


if (!length($AGI{arg_1})) { if (!length($AGI{arg_1})) {
&return_var("No text passed to Wolfram."); return_var("No text passed to Wolfram.");
die "$name No text passed, Aborting.\n"; die "$name No text passed, Aborting.\n";
} }
print STDERR "$name Questions is: $AGI{arg_1}\n" if($debug); print STDERR "$name Questions is: $AGI{arg_1}\n" if($debug);
Expand All @@ -90,40 +90,40 @@ my $ua_request = HTTP::Request->new(
); );
my $ua_response = $ua->request($ua_request); my $ua_response = $ua->request($ua_request);
if (!$ua_response->is_success) { if (!$ua_response->is_success) {
&return_var("Failed to contact Wolfram server."); return_var("Failed to contact Wolfram server.");
die "$name Failed to contact server."; die "$name Failed to contact server.";
} }


$w_reply = XMLin($ua_response->content); $w_reply = XMLin($ua_response->content);
if ($w_reply->{success} eq 'false') { if ($w_reply->{success} eq 'false') {
print STDERR "$name Wolfram returned no answer.\n" if ($debug); print STDERR "$name Wolfram returned no answer.\n" if ($debug);
&return_var("I don't know how to answer that."); return_var("I don't know how to answer that.");
exit; exit;
} }


# Trying to locate answers in predifined fields in Wolframs output. # # Trying to locate answers in predifined fields in Wolframs output. #
foreach (keys %{$w_reply->{pod}}) { foreach (keys %{$w_reply->{pod}}) {
if (/subpod/) { if (/subpod/) {
&add_answer("$w_reply->{pod}{$_}{plaintext}\n"); add_answer("$w_reply->{pod}{$_}{plaintext}\n");
$results++; $results++;
last; last;
} elsif (/Result|Value/) { } elsif (/Result|Value/) {
eval{ &add_answer("$w_reply->{pod}{$_}{subpod}{plaintext}\n"); }; eval{ add_answer("$w_reply->{pod}{$_}{subpod}{plaintext}\n"); };
eval{ &add_answer("$w_reply->{pod}{$_}{subpod}[0]{plaintext}\n"); }; eval{ add_answer("$w_reply->{pod}{$_}{subpod}[0]{plaintext}\n"); };
eval{ &add_answer("$w_reply->{pod}{$_}{subpod}[1]{plaintext}\n"); }; eval{ add_answer("$w_reply->{pod}{$_}{subpod}[1]{plaintext}\n"); };
$results++; $results++;
last; last;
} elsif (/Definition:WordData|Basic:ChemicalData|ComparisonAsLength|Comparison| } elsif (/Definition:WordData|Basic:ChemicalData|ComparisonAsLength|Comparison|
BasicInformation|NotableFacts:PeopleData|Basic|Properties/x BasicInformation|NotableFacts:PeopleData|Basic|Properties/x
) { ) {
&add_answer("$w_reply->{pod}{$_}{subpod}{plaintext}\n"); add_answer("$w_reply->{pod}{$_}{subpod}{plaintext}\n");
$results++; $results++;
} elsif (/WeatherForecast:WeatherData/) { } elsif (/WeatherForecast:WeatherData/) {
&add_answer("$w_reply->{pod}{$_}{subpod}[0]{title} ", add_answer("$w_reply->{pod}{$_}{subpod}[0]{title} ",
"$w_reply->{pod}{$_}{subpod}[0]{plaintext}\n"); "$w_reply->{pod}{$_}{subpod}[0]{plaintext}\n");
&add_answer("$w_reply->{pod}{$_}{subpod}[1]{title} ", add_answer("$w_reply->{pod}{$_}{subpod}[1]{title} ",
"$w_reply->{pod}{$_}{subpod}[1]{plaintext}\n"); "$w_reply->{pod}{$_}{subpod}[1]{plaintext}\n");
eval{ &add_answer("$w_reply->{pod}{$_}{subpod}[2]{title} ", eval{ add_answer("$w_reply->{pod}{$_}{subpod}[2]{title} ",
"$w_reply->{pod}{$_}{subpod}[2]{plaintext}\n"); }; "$w_reply->{pod}{$_}{subpod}[2]{plaintext}\n"); };
$results++; $results++;
last; last;
Expand All @@ -133,20 +133,20 @@ foreach (keys %{$w_reply->{pod}}) {
# If the above did't work search for answers all available fields. # # If the above did't work search for answers all available fields. #
if (!$results) { if (!$results) {
foreach (keys %{$w_reply->{pod}}) { foreach (keys %{$w_reply->{pod}}) {
eval{ &add_answer("$_: $w_reply->{pod}{$_}{subpod}{plaintext}\n"); }; eval{ add_answer("$_: $w_reply->{pod}{$_}{subpod}{plaintext}\n"); };
eval{ &add_answer("$_: $w_reply->{pod}{$_}{subpod}[0]{plaintext}\n"); }; eval{ add_answer("$_: $w_reply->{pod}{$_}{subpod}[0]{plaintext}\n"); };
eval{ &add_answer("$_: $w_reply->{pod}{$_}{subpod}[1]{plaintext}\n"); }; eval{ add_answer("$_: $w_reply->{pod}{$_}{subpod}[1]{plaintext}\n"); };
$results++; $results++;
} }
} }


# In case we were not able to locate any answers. # # In case we were not able to locate any answers. #
if (!$results) { if (!$results) {
print STDERR "$name Failed to get any answer.\n" if ($debug); print STDERR "$name Failed to get any answer.\n" if ($debug);
&return_var("Failed to get any answer."); return_var("Failed to get any answer.");
} else { } else {
print STDERR "$name Answer is:$answer\n" if ($debug); print STDERR "$name Answer is:$answer\n" if ($debug);
&return_var($answer); return_var($answer);
} }
exit; exit;


Expand All @@ -169,7 +169,7 @@ sub return_var {
my $text = shift; my $text = shift;
print STDERR "$name Setting $var_name to $text\n" if ($debug); print STDERR "$name Setting $var_name to $text\n" if ($debug);
print "SET VARIABLE \"$var_name\" \"$text\"\n"; print "SET VARIABLE \"$var_name\" \"$text\"\n";
&checkresponse(); checkresponse();
} }


sub checkresponse { sub checkresponse {
Expand Down
12 changes: 6 additions & 6 deletions speech-recog.agi
Expand Up @@ -169,11 +169,11 @@ if (length($AGI{arg_4})) {
# Answer channel if not already answered # # Answer channel if not already answered #
print STDERR "$name Checking channel status.\n" if ($debug); print STDERR "$name Checking channel status.\n" if ($debug);
print "CHANNEL STATUS\n"; print "CHANNEL STATUS\n";
@result = &checkresponse(); @result = checkresponse();
if ($result[0] == 4) { if ($result[0] == 4) {
print STDERR "$name Answering channel.\n" if ($debug); print STDERR "$name Answering channel.\n" if ($debug);
print "ANSWER\n"; print "ANSWER\n";
&checkresponse(); checkresponse();
} }


# Reset variables. # # Reset variables. #
Expand All @@ -186,7 +186,7 @@ if ($result[0] == 4) {
print STDERR "$name Clearing channel variables.\n" if ($debug); print STDERR "$name Clearing channel variables.\n" if ($debug);
foreach (keys %response) { foreach (keys %response) {
print "SET VARIABLE \"$_\" \"$response{$_}\"\n"; print "SET VARIABLE \"$_\" \"$response{$_}\"\n";
&checkresponse(); checkresponse();
} }


# Hnadle interrupts # # Hnadle interrupts #
Expand All @@ -196,7 +196,7 @@ $SIG{'HUP'} = \&int_handler;
# Record file # # Record file #
($fh, $tmpname) = tempfile("stt_XXXXXX", DIR => $tmpdir, UNLINK => 1); ($fh, $tmpname) = tempfile("stt_XXXXXX", DIR => $tmpdir, UNLINK => 1);
print "RECORD FILE $tmpname $format \"$intkey\" \"-1\" $beep \"$silence\"\n"; print "RECORD FILE $tmpname $format \"$intkey\" \"-1\" $beep \"$silence\"\n";
@result = &checkresponse(); @result = checkresponse();
die "$name Failed to record file, aborting...\n" if ($result[0] == -1); die "$name Failed to record file, aborting...\n" if ($result[0] == -1);


if ($debug) { if ($debug) {
Expand All @@ -206,7 +206,7 @@ if ($debug) {
"$silence, Interrupt keys: $intkey\n"; "$silence, Interrupt keys: $intkey\n";
print STDERR "$name Playing back recorded file.\n"; print STDERR "$name Playing back recorded file.\n";
print "STREAM FILE $tmpname \"\"\n"; print "STREAM FILE $tmpname \"\"\n";
@result = &checkresponse(); @result = checkresponse();
print STDERR "$name Failed to play file\n" if ($result[0] == -1); print STDERR "$name Failed to play file\n" if ($result[0] == -1);
} }


Expand Down Expand Up @@ -279,7 +279,7 @@ print STDERR "$name The response was:\n", $uaresponse->content if ($debug);
foreach (keys %response) { foreach (keys %response) {
print STDERR "$name Setting variable: $_ = $response{$_}\n" if ($debug); print STDERR "$name Setting variable: $_ = $response{$_}\n" if ($debug);
print "SET VARIABLE \"$_\" \"$response{$_}\"\n"; print "SET VARIABLE \"$_\" \"$response{$_}\"\n";
&checkresponse(); checkresponse();
} }
exit; exit;


Expand Down

0 comments on commit 9d10199

Please sign in to comment.