Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved cli app, small fixes

CLI app can now accept flac, speex or wav files as input.
Small fixes in other scripts.
  • Loading branch information...
commit 9d10199449e5b2ba153c4b4c73bb9280ab2aba66 1 parent 8dd27fc
@zaf authored
View
106 samples/speech-recog-cli.pl
@@ -9,7 +9,7 @@
# the GNU General Public License Version 2. See the COPYING file
# 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.
# id : Some id string that googles engine returns, not very useful(?).
# utterance : The generated text string.
@@ -20,24 +20,55 @@
use strict;
use warnings;
+use File::Temp qw(tempfile);
+use Getopt::Std;
+use File::Basename;
use LWP::UserAgent;
-if (!@ARGV || $ARGV[0] eq '-h' || $ARGV[0] eq '--help') {
- print "Speech recognition using google voice.\n\n";
- print "Usage: $0 [FILES]\n\n";
- exit;
-}
-
+my %options;
+my $filetype;
my $url = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium";
my $samplerate = 8000;
-#my $filetype = "x-speex-with-header-byte";
-my $filetype = "x-flac";
my $language = "en-US";
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;
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: $!";
my $audio = do { local $/; <$fh> };
close($fh);
@@ -54,15 +85,60 @@
if ($response->content =~ /^\{"status":(\d*),"id":"(.*)","hypotheses":\[(.*)\]\}$/) {
$response{status} = "$1";
$response{id} = "$2";
- if ($response{status} == 5) {
- print "Error reading audio file\n";
+ if ($response{status} != 0) {
+ say_msg("Error reading audio file");
}
if ($3 =~ /^\{"utterance":"(.*)","confidence":(.*)\}/) {
$response{utterance} = "$1";
$response{confidence} = "$2";
}
}
- printf "%-10s : %s\n", $_, $response{$_} foreach (keys %response);
- #print $response->content;
+ if (!defined $options{q}) {
+ 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;
View
36 samples/wolfram/wolfram.agi
@@ -63,12 +63,12 @@ while (<STDIN>) {
$name = " -- $AGI{request}:";
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";
}
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";
}
print STDERR "$name Questions is: $AGI{arg_1}\n" if($debug);
@@ -90,40 +90,40 @@ my $ua_request = HTTP::Request->new(
);
my $ua_response = $ua->request($ua_request);
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.";
}
$w_reply = XMLin($ua_response->content);
if ($w_reply->{success} eq 'false') {
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;
}
# Trying to locate answers in predifined fields in Wolframs output. #
foreach (keys %{$w_reply->{pod}}) {
if (/subpod/) {
- &add_answer("$w_reply->{pod}{$_}{plaintext}\n");
+ add_answer("$w_reply->{pod}{$_}{plaintext}\n");
$results++;
last;
} elsif (/Result|Value/) {
- 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}[1]{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}[1]{plaintext}\n"); };
$results++;
last;
} elsif (/Definition:WordData|Basic:ChemicalData|ComparisonAsLength|Comparison|
BasicInformation|NotableFacts:PeopleData|Basic|Properties/x
) {
- &add_answer("$w_reply->{pod}{$_}{subpod}{plaintext}\n");
+ add_answer("$w_reply->{pod}{$_}{subpod}{plaintext}\n");
$results++;
} 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");
- &add_answer("$w_reply->{pod}{$_}{subpod}[1]{title} ",
+ add_answer("$w_reply->{pod}{$_}{subpod}[1]{title} ",
"$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"); };
$results++;
last;
@@ -133,9 +133,9 @@ foreach (keys %{$w_reply->{pod}}) {
# If the above did't work search for answers all available fields. #
if (!$results) {
foreach (keys %{$w_reply->{pod}}) {
- 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}[1]{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}[1]{plaintext}\n"); };
$results++;
}
}
@@ -143,10 +143,10 @@ if (!$results) {
# In case we were not able to locate any answers. #
if (!$results) {
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 {
print STDERR "$name Answer is:$answer\n" if ($debug);
- &return_var($answer);
+ return_var($answer);
}
exit;
@@ -169,7 +169,7 @@ sub return_var {
my $text = shift;
print STDERR "$name Setting $var_name to $text\n" if ($debug);
print "SET VARIABLE \"$var_name\" \"$text\"\n";
- &checkresponse();
+ checkresponse();
}
sub checkresponse {
View
12 speech-recog.agi
@@ -169,11 +169,11 @@ if (length($AGI{arg_4})) {
# Answer channel if not already answered #
print STDERR "$name Checking channel status.\n" if ($debug);
print "CHANNEL STATUS\n";
-@result = &checkresponse();
+@result = checkresponse();
if ($result[0] == 4) {
print STDERR "$name Answering channel.\n" if ($debug);
print "ANSWER\n";
- &checkresponse();
+ checkresponse();
}
# Reset variables. #
@@ -186,7 +186,7 @@ if ($result[0] == 4) {
print STDERR "$name Clearing channel variables.\n" if ($debug);
foreach (keys %response) {
print "SET VARIABLE \"$_\" \"$response{$_}\"\n";
- &checkresponse();
+ checkresponse();
}
# Hnadle interrupts #
@@ -196,7 +196,7 @@ $SIG{'HUP'} = \&int_handler;
# Record file #
($fh, $tmpname) = tempfile("stt_XXXXXX", DIR => $tmpdir, UNLINK => 1);
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);
if ($debug) {
@@ -206,7 +206,7 @@ if ($debug) {
"$silence, Interrupt keys: $intkey\n";
print STDERR "$name Playing back recorded file.\n";
print "STREAM FILE $tmpname \"\"\n";
- @result = &checkresponse();
+ @result = checkresponse();
print STDERR "$name Failed to play file\n" if ($result[0] == -1);
}
@@ -279,7 +279,7 @@ print STDERR "$name The response was:\n", $uaresponse->content if ($debug);
foreach (keys %response) {
print STDERR "$name Setting variable: $_ = $response{$_}\n" if ($debug);
print "SET VARIABLE \"$_\" \"$response{$_}\"\n";
- &checkresponse();
+ checkresponse();
}
exit;
Please sign in to comment.
Something went wrong with that request. Please try again.