Skip to content
Browse files

Cosmetic changes, small fixes

  • Loading branch information...
1 parent 999c6c5 commit 644c2a0d35f497e103e94840c8d68174797fb38a @zaf committed Mar 10, 2012
Showing with 54 additions and 51 deletions.
  1. +25 −22 samples/speech-recog-cli.pl
  2. +8 −8 samples/wolfram/wolfram.agi
  3. +21 −21 speech-recog.agi
View
47 samples/speech-recog-cli.pl
@@ -49,18 +49,18 @@
# send each sound file to google and get the recognition results #
foreach my $file (@ARGV) {
- my($filename, $dir, $ext) = fileparse($file, qr/\.[^.]*/);
+ my ($filename, $dir, $ext) = fileparse($file, qr/\.[^.]*/);
if ($ext ne ".flac" && $ext ne ".spx" && $ext ne ".wav") {
say_msg("Unsupported filetype: $ext\n");
++$error;
next;
}
if ($ext eq ".flac") {
- $filetype = "x-flac";
+ $filetype = "x-flac";
} elsif ($ext eq ".spx") {
- $filetype = "x-speex-with-header-byte";
+ $filetype = "x-speex-with-header-byte";
} elsif ($ext eq ".wav") {
- $filetype = "x-flac";
+ $filetype = "x-flac";
if (($file = encode_flac($file)) eq '-1') {
++$error;
next;
@@ -146,10 +146,10 @@ sub parse_options {
if (defined $options{s}) {
# Set up connection type #
$url = "https://" . $host;
- $ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
+ $ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
} else {
$url = "http://" . $host;
- $ua = LWP::UserAgent->new;
+ $ua = LWP::UserAgent->new;
}
# set profanity filter #
$pro_filter = 2 if (defined $options{f});
@@ -169,8 +169,9 @@ sub encode_flac {
}
chomp($flac);
- my ($fh, $tmpname) = tempfile("recg_XXXXXX",
- DIR => $tmpdir,
+ my ($fh, $tmpname) = tempfile(
+ "recg_XXXXXX",
+ DIR => $tmpdir,
SUFFIX => '.flac',
UNLINK => 1,
);
@@ -183,24 +184,26 @@ sub encode_flac {
sub say_msg {
# Print messages to user if 'quiet' flag is not set #
- print @_, "\n" if (!defined $options{q});
+ my $message = shift;
+ print "$message\n" if (!defined $options{q});
+ return;
}
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 (default 'en-US')\n",
- " -o <type> specify the type of output fomratting\n",
- " detailed print detailed info like confidence and return status (default)\n",
- " compact print only the recognized utterance\n",
- " raw raw JSON output\n",
- " -r <rate> specify the audio sample rate in Hz (deafult 8000)\n",
- " -n <number> specify the maximum number of results (default 1)\n",
- " -f filter out profanities\n",
- " -s use SSL to encrypt web trafic\n",
- " -q don't print any error messages or warnings\n",
- " -h this help message\n\n";
+ "Usage: $0 [options] [file(s)]\n\n",
+ "Supported options:\n",
+ " -l <lang> specify the language to use (default 'en-US')\n",
+ " -o <type> specify the type of output fomratting\n",
+ " detailed print detailed info like confidence and return status (default)\n",
+ " compact print only the recognized utterance\n",
+ " raw raw JSON output\n",
+ " -r <rate> specify the audio sample rate in Hz (deafult 8000)\n",
+ " -n <number> specify the maximum number of results (default 1)\n",
+ " -f filter out profanities\n",
+ " -s use SSL to encrypt web trafic\n",
+ " -q don't print any error messages or warnings\n",
+ " -h this help message\n\n";
exit(1);
}
View
16 samples/wolfram/wolfram.agi
@@ -71,7 +71,7 @@ if (!length($AGI{arg_1})) {
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);
+warn "$name Questions is: $AGI{arg_1}\n" if($debug);
$question = escape($AGI{arg_1});
if ($use_ssl) {
@@ -96,7 +96,7 @@ if (!$ua_response->is_success) {
$w_reply = XMLin($ua_response->content);
if ($w_reply->{success} eq 'false') {
- print STDERR "$name Wolfram returned no answer.\n" if ($debug);
+ warn "$name Wolfram returned no answer.\n" if ($debug);
return_var("I don't know how to answer that.");
exit;
}
@@ -142,10 +142,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);
+ warn "$name Failed to get any answer.\n" if ($debug);
return_var("Failed to get any answer.");
} else {
- print STDERR "$name Answer is:$answer\n" if ($debug);
+ warn "$name Answer is:$answer\n" if ($debug);
return_var($answer);
}
exit;
@@ -167,7 +167,7 @@ sub add_answer {
# Store answers in channel variable. #
sub return_var {
my $text = shift;
- print STDERR "$name Setting $var_name to $text\n" if ($debug);
+ warn "$name Setting $var_name to $text\n" if ($debug);
print "SET VARIABLE \"$var_name\" \"$text\"\n";
checkresponse();
}
@@ -180,14 +180,14 @@ sub checkresponse {
if ($input =~ /^200/) {
$input =~ /result=(-?\d+)\s?(.*)$/;
if (!length($1)) {
- print STDERR "$name Command failed: $input\n";
+ warn "$name Command failed: $input\n";
@values = ("-1");
} else {
- print STDERR "$name Command returned: $input\n" if ($debug);
+ warn "$name Command returned: $input\n" if ($debug);
@values = ("$1", "$2");
}
} else {
- print STDERR "$name Unexpected result: $input\n";
+ warn "$name Unexpected result: $input\n";
@values = ("-1");
}
return @values;
View
42 speech-recog.agi
@@ -66,10 +66,10 @@ $| = 1;
my $language = "en-US";
# Default max silence timeout #
-my $timeout = 2;
+my $timeout = 2;
# Default interrupt key #
-my $intkey = "#";
+my $intkey = "#";
# Input audio sample rate #
my $samplerate = 8000;
@@ -123,19 +123,19 @@ while (<STDIN>) {
$name = " -- $AGI{request}:";
# if ($debug) {
-# print STDERR "$name AGI Environment Dump:\n";
-# print STDERR "$name $_ = $AGI{$_}\n" foreach (sort keys %AGI);
+# warn "$name AGI Environment Dump:\n";
+# warn "$name $_ = $AGI{$_}\n" foreach (sort keys %AGI);
# }
# Abort if required programs not found. #
if ($use_speex) {
die "$name speexenc is missing. Aborting.\n" if (!$speex);
chomp($speex);
- print STDERR "$name Found speexenc in: $speex\n" if ($debug);
+ warn "$name Found speexenc in: $speex\n" if ($debug);
} else {
die "$name flac is missing. Aborting.\n" if (!$flac);
chomp($flac);
- print STDERR "$name Found flac in: $flac\n" if ($debug);
+ warn "$name Found flac in: $flac\n" if ($debug);
}
# Setting recording file format according to sample rate. #
@@ -173,11 +173,11 @@ if (length($AGI{arg_4})) {
}
# Answer channel if not already answered #
-print STDERR "$name Checking channel status.\n" if ($debug);
+warn "$name Checking channel status.\n" if ($debug);
print "CHANNEL STATUS\n";
@result = checkresponse();
if ($result[0] == 4) {
- print STDERR "$name Answering channel.\n" if ($debug);
+ warn "$name Answering channel.\n" if ($debug);
print "ANSWER\n";
checkresponse();
}
@@ -189,7 +189,7 @@ if ($result[0] == 4) {
id => -1,
confidence => -1,
);
-print STDERR "$name Clearing channel variables.\n" if ($debug);
+warn "$name Clearing channel variables.\n" if ($debug);
foreach (keys %response) {
print "SET VARIABLE \"$_\" \"$response{$_}\"\n";
checkresponse();
@@ -206,14 +206,14 @@ print "RECORD FILE $tmpname $format \"$intkey\" \"-1\" $beep \"$silence\"\n";
die "$name Failed to record file, aborting...\n" if ($result[0] == -1);
if ($debug) {
- print STDERR "$name Recording Format: $format, Rate: $samplerate Hz, ",
+ warn "$name Recording Format: $format, Rate: $samplerate Hz, ",
"Encoding format: ", ($use_speex) ? "speex" : "flac", "\n",
"$name Languge: $language, SSL: ", ($use_ssl) ? "yes, " : "no, ",
"$silence, Interrupt keys: $intkey\n";
- print STDERR "$name Playing back recorded file.\n";
+ warn "$name Playing back recorded file.\n";
print "STREAM FILE $tmpname \"\"\n";
@result = checkresponse();
- print STDERR "$name Failed to play file\n" if ($result[0] == -1);
+ warn "$name Failed to play file\n" if ($result[0] == -1);
}
if ($use_speex) {
@@ -239,10 +239,10 @@ close($fh);
if ($use_ssl) {
$url = "https://" . $host;
- $ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
+ $ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
} else {
$url = "http://" . $host;
- $ua = LWP::UserAgent->new;
+ $ua = LWP::UserAgent->new;
}
$ua->agent("Mozilla/5.0 (X11; Linux) AppleWebKit/535.2 (KHTML, like Gecko)");
@@ -257,7 +257,7 @@ die "$name Unable to get speech data.\n" if (!$uaresponse->is_success);
if ($uaresponse->content =~ /^\{"status":(\d*),"id":"(.*)","hypotheses":\[(.*)\]\}$/) {
$response{status} = "$1";
$response{id} = "$2";
- print STDERR "$name Error reading audio file\n" if ($response{status} != 0);
+ warn "$name Error reading audio file\n" if ($response{status} != 0);
# # Save voice samples for debugging purposes #
# if ($use_speex && $debug) {
# if ($response{status} == 0) {
@@ -280,10 +280,10 @@ if ($uaresponse->content =~ /^\{"status":(\d*),"id":"(.*)","hypotheses":\[(.*)\]
}
}
-print STDERR "$name The response was:\n", $uaresponse->content if ($debug);
+warn "$name The response was:\n", $uaresponse->content if ($debug);
foreach (keys %response) {
- print STDERR "$name Setting variable: $_ = $response{$_}\n" if ($debug);
+ warn "$name Setting variable: $_ = $response{$_}\n" if ($debug);
print "SET VARIABLE \"$_\" \"$response{$_}\"\n";
checkresponse();
}
@@ -297,14 +297,14 @@ sub checkresponse {
if ($input =~ /^200/) {
$input =~ /result=(-?\d+)\s?(.*)$/;
if (!length($1)) {
- print STDERR "$name Command failed: $input\n";
+ warn "$name Command failed: $input\n";
@values = ("-1");
} else {
- print STDERR "$name Command returned: $input\n" if ($debug);
+ warn "$name Command returned: $input\n" if ($debug);
@values = ("$1", "$2");
}
} else {
- print STDERR "$name Unexpected result: $input\n";
+ warn "$name Unexpected result: $input\n";
@values = ("-1");
}
return @values;
@@ -316,7 +316,7 @@ sub int_handler {
END {
if ($tmpname) {
- print STDERR "$name Cleaning temp files.\n" if ($debug);
+ warn "$name Cleaning temp files.\n" if ($debug);
unlink glob "$tmpname.*";
}
}

0 comments on commit 644c2a0

Please sign in to comment.
Something went wrong with that request. Please try again.