Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Enabled SSL by default, code cleanup.

  • Loading branch information...
commit 444c8125f3ae6911dcf26b790ca455b6558e3dce 1 parent 44770e5
Lefteris Zafiris authored

Showing 3 changed files with 19 additions and 42 deletions. Show diff stats Hide diff stats

  1. +10 10 README
  2. +8 6 samples/speech-recog-cli.pl
  3. +1 26 speech-recog.agi
20 README
@@ -11,11 +11,15 @@ Requirements
11 11 ------------
12 12 Perl The Perl Programming Language
13 13 perl-libwww The World-Wide Web library for Perl
  14 +IO-Socket-SSL Perl module that implements an interface to SSL sockets.
14 15 flac Free Lossless Audio Codec
15 16 Internet access in order to contact google and get the speech data.
16 17
17   -If you plan to use SSL you will need to install the 'IO-Socket-SSL' Perl module
18   -that implements an interface to SSL sockets.
  18 +** Optional/Highly experimental **
  19 +speex patent-free audio compression format designed for speech.
  20 + works only with patched speex encoder that supports
  21 + MIME "x-speex-with-header-byte"
  22 + https://github.com/zaf/Speex-with-header-bytes
19 23
20 24 ------------
21 25 Installation
@@ -122,14 +126,10 @@ Zulu
122 126 -----------------------
123 127 Security Considerations
124 128 -----------------------
125   -This script contacts google's servers in order send the recorded voice data and get back
126   -the resulted text. If you are using voice recognition for sensitive data or you want to
127   -make sure that no 3rd party can eavesdrop your communication with the google service it is
128   -advised to enable the use of SSL in the script. This will encrypt all the traffic between
129   -your pbx and google servers making it much safer.
130   -To enable the use of SSL set the variable '$use_ssl' to 1 in the script. Keep in mind that
131   -security comes with a price, the initialisation of the SSL session adds a small delay in
132   -communication (usually under 1 sec).
  129 +This script contacts googles' servers in order send the recorded voice data and get back
  130 +the resulted text. The script uses SSL by default to encrypt all the traffic between
  131 +your pbx and google servers so no 3rd party can eavesdrop your communication, but your
  132 +voice data will be available to Google under a not yet defined policy.
133 133
134 134 -------
135 135 License
14 samples/speech-recog-cli.pl
@@ -24,6 +24,7 @@
24 24 use Getopt::Std;
25 25 use File::Basename;
26 26 use LWP::UserAgent;
  27 +use LWP::ConnCache;
27 28
28 29 my %options;
29 30 my $filetype;
@@ -46,13 +47,14 @@
46 47
47 48 $ua->agent("Mozilla/5.0 (X11; Linux) AppleWebKit/535.2 (KHTML, like Gecko)");
48 49 $ua->env_proxy;
  50 +$ua->conn_cache(LWP::ConnCache->new());
49 51 $ua->timeout(20);
50 52
51 53 # send each sound file to google and get the recognition results #
52 54 foreach my $file (@ARGV) {
53 55 my ($filename, $dir, $ext) = fileparse($file, qr/\.[^.]*/);
54 56 if ($ext ne ".flac" && $ext ne ".spx" && $ext ne ".wav") {
55   - say_msg("Unsupported filetype: $ext\n");
  57 + say_msg("Unsupported filetype: $ext");
56 58 ++$error;
57 59 next;
58 60 }
@@ -72,7 +74,7 @@
72 74 $audio = do { local $/; <$fh> };
73 75 close($fh);
74 76 } else {
75   - say_msg("Cant read file $file.\n");
  77 + say_msg("Cant read file $file");
76 78 ++$error;
77 79 next;
78 80 }
@@ -83,7 +85,7 @@
83 85 Content => "$audio",
84 86 );
85 87 if (!$response->is_success) {
86   - say_msg("Failed to get data for file:$file.\n");
  88 + say_msg("Failed to get data for file:$file");
87 89 ++$error;
88 90 next;
89 91 }
@@ -177,7 +179,7 @@ sub encode_flac {
177 179 UNLINK => 1,
178 180 );
179 181 if (system($flac, "-8", "-f", "--totally-silent", "-o", "$tmpname", "$file")) {
180   - say_msg("$flac failed to encode file.");
  182 + say_msg("$flac failed to encode file");
181 183 return -1;
182 184 }
183 185 return $tmpname;
@@ -185,8 +187,8 @@ sub encode_flac {
185 187
186 188 sub say_msg {
187 189 # Print messages to user if 'quiet' flag is not set #
188   - my $message = shift;
189   - warn "$0: $message" if (!defined $options{q});
  190 + my @message = @_;
  191 + warn @message if (!defined $options{q});
190 192 return;
191 193 }
192 194
27 speech-recog.agi
@@ -82,7 +82,7 @@ my $pro_filter = 0;
82 82 my $use_speex = 0;
83 83
84 84 # Use SSL #
85   -my $use_ssl = 0;
  85 +my $use_ssl = 1;
86 86
87 87 # Verbose debugging messages #
88 88 my $debug = 0;
@@ -112,8 +112,6 @@ my $tmpdir = "/tmp";
112 112 my $host = "www.google.com/speech-api/v1/recognize";
113 113 my $flac = `/usr/bin/which flac`;
114 114 my $speex = `/usr/bin/which speexenc`;
115   -# ugly hardcoded path is ugly, make sure to change this #
116   -#my $speex = "/home/zaf/bin/speexenc";
117 115
118 116 # Store AGI input #
119 117 while (<STDIN>) {
@@ -125,11 +123,6 @@ while (<STDIN>) {
125 123
126 124 $name = " -- $AGI{request}:";
127 125
128   -# if ($debug) {
129   -# warn "$name AGI Environment Dump:\n";
130   -# warn "$name $_ = $AGI{$_}\n" foreach (sort keys %AGI);
131   -# }
132   -
133 126 # Abort if required programs not found. #
134 127 if ($use_speex) {
135 128 die "$name speexenc is missing. Aborting.\n" if (!$speex);
@@ -228,10 +221,6 @@ if ($debug) {
228 221 "Encoding format: ", ($use_speex) ? "speex" : "flac", "\n",
229 222 "$name Languge: $language, SSL: ", ($use_ssl) ? "yes, " : "no, ",
230 223 "$silence, Interrupt keys: $intkey\n";
231   -# warn "$name Playing back recorded file.\n";
232   -# print "STREAM FILE $tmpname \"\"\n";
233   -# @result = checkresponse();
234   -# warn "$name Failed to play file\n" if ($result[0] == -1);
235 224 }
236 225
237 226 # Encode sound data #
@@ -267,20 +256,6 @@ if ($uaresponse->content =~ /^\{"status":(\d*),"id":"(.*)","hypotheses":\[(.*)\]
267 256 $response{status} = "$1";
268 257 $response{id} = "$2";
269 258 warn "$name Error reading audio file\n" if ($response{status} != 0);
270   -# # Save voice samples for debugging purposes #
271   -# if ($use_speex && $debug) {
272   -# if ($response{status} == 0) {
273   -# move("$tmpname.spx", "$tmpname-success.spx");
274   -# } else {
275   -# move("$tmpname.spx", "$tmpname-fail.spx");
276   -# }
277   -# } elsif (!$use_speex && $debug) {
278   -# if ($response{status} == 0) {
279   -# move("$tmpname.flac", "$tmpname-success.flac");
280   -# } else {
281   -# move("$tmpname.flac", "$tmpname-fail.flac");
282   -# }
283   -# }
284 259
285 260 if ($3 =~ /^\{"utterance":"(.*)","confidence":(.*)\}/) {
286 261 $response{utterance} = "$1";

0 comments on commit 444c812

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