forked from zaf/asterisk-mstts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mstts-tiny.agi
executable file
·379 lines (340 loc) · 10.1 KB
/
mstts-tiny.agi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env perl
#
# AGI script that uses MS Translator API for speech synthesis.
#
# Copyright (C) 2012 - 2014, Lefteris Zafiris <[email protected]>
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the COPYING file
# at the top of the source tree.
#
# -----
# Usage
# -----
# agi(mstts.agi,"text",[language],[intkey],[speed]): This will invoke the Microsoft TTS
# engine, render the text string to speech and play it back to the user.
# If 'intkey' is set the script will wait for user input. Any given interrupt keys will
# cause the playback to immediately terminate and the dialplan to proceed to the
# matching extension (this is mainly for use in IVR, see README for examples).
# If 'speed' is set the speech rate is altered by that factor.
#
# The script contacts Microsoft Translate TTS service in order to get the voice data
# which then stores in a local cache (by default /tmp/) for future use.
#
# In order to use this script you have to subscribe to the Microsoft
# Translator API on Azure Marketplace and register your application:
# https://datamarket.azure.com/developer/applications/
#
# Parameters like default language, sample rate, caching and cache dir
# can be set up by altering the following variables:
# Default langeuage: $lang
# Sample rate: $samplerate
# Speed factor: $speed
# Chace: $usecache
# Chache directory: $cachedir
# SoX Version: $sox_ver
#
# Tiny version drops the use of mpg123 and requires MP3 format support in sox.
# It also makes the assumption that sox is in /usr/bin/ in order
# to skip detection and speed things up.
#
use warnings;
use strict;
use Encode qw(decode encode);
use File::Temp qw(tempfile);
use File::Copy qw(move);
use File::Path qw(mkpath);
use Digest::MD5 qw(md5_hex);
use URI::Escape;
use HTTP::Tiny;
$| = 1;
# ----------------------------------- #
# User defined parameters: #
# ----------------------------------- #
# Your Azure Marketplace credentials. #
my $clientid = "";
my $clientsecret = "";
# Default language #
my $lang = "en";
# Output speed factor #
my $speed = 1;
# Use of cache mechanism #
my $usecache = 1;
# Cache directory path #
my $cachedir = "/tmp";
# Output audio sample rate #
# Leave blank to auto-detect #
my $samplerate = "8000";
# SoX Version #
# Leave blank to auto-detect #
my $sox_ver = "14";
# Verbose debugging messages #
my $debug = 0;
# ----------------------------------- #
my %AGI;
my @text;
my $fh;
my $tmpname;
my $filename;
my $fexten;
my @result;
my $atoken;
my $http;
my $sox = "/usr/bin/sox";
my $format = "audio/mp3";
my $quality = "MaxQuality";
my $intkey = "";
my $tmpdir = "/tmp";
my $maxlen = 4096;
my $timeout = 10;
my $url = "https://api.microsofttranslator.com/V2/Http.svc";
# Store AGI input #
($AGI{arg_1}, $AGI{arg_2}, $AGI{arg_3}, $AGI{arg_4}) = @ARGV;
while (<STDIN>) {
chomp;
last if (!length);
$AGI{$1} = $2 if (/^agi_(\w+)\:\s+(.*)$/);
}
my $name = " -- $AGI{request}:";
# Sanitising input #
$AGI{arg_1} = decode('utf8', $AGI{arg_1});
for ($AGI{arg_1}) {
s/[\\|*~<>^\(\)\[\]\{\}[:cntrl:]]/ /g;
s/\s+/ /g;
s/^\s|\s$//g;
fatal_log("No text passed for synthesis.") if (!length);
$_ .= "." unless (/^.+[.,?!:;]$/);
@text = /.{1,1000}[.,?!:;]|.{1,1000}\s/g;
}
# Setting language, interrupt keys and speed rate #
if (length($AGI{arg_2})) {
if ($AGI{arg_2} =~ /[a-z]{2}(-[a-z]{2,3})?/) {
$lang = $AGI{arg_2};
} else {
fatal_log("Invalid language setting.");
}
}
if (length($AGI{arg_3})) {
$intkey = "0123456789#*" if ($AGI{arg_3} eq "any");
$intkey = $AGI{arg_3} if ($AGI{arg_3} =~ /^[0-9*#]+$/);
}
if (length($AGI{arg_4})) {
$speed = $AGI{arg_4} if ($AGI{arg_4} =~ /^\d+(\.\d+)?$/);
}
# Check cache path size: dir length + md5 + file extension #
if ($usecache) {
if ((length($cachedir) + 32 + 6) < $maxlen) {
mkpath("$cachedir") unless (-d "$cachedir");
} else {
console_log("Cache path size exceeds limit. Disabling cache.");
$usecache = 0;
}
}
# Answer channel if not already answered #
print "CHANNEL STATUS\n";
@result = checkresponse();
if ($result[0] == 4) {
print "ANSWER\n";
@result = checkresponse();
if ($result[0] != 0) {
fatal_log("Failed to answer channel.");
}
}
# Setting filename extension according to sample rate. #
if (!$samplerate) { ($fexten, $samplerate) = detect_format(); }
elsif ($samplerate == 12000) { $fexten = "sln12"; }
elsif ($samplerate == 16000) { $fexten = "sln16"; }
elsif ($samplerate == 32000) { $fexten = "sln32"; }
elsif ($samplerate == 44100) { $fexten = "sln44"; }
elsif ($samplerate == 48000) { $fexten = "sln48"; }
else { ($fexten, $samplerate) = ("sln", 8000); }
# Synthesize speech using MS Translator API #
foreach my $line (@text) {
$line = encode('utf8', $line);
$line =~ s/^\s+|\s+$//g;
next if (length($line) == 0);
if ($debug) {
console_log(
"Text passed for synthesis: $line, Language: $lang",
"Interrupt keys: $intkey, Sample rate: $samplerate, Speed: $speed",
"Caching: $usecache, Cache dir: $cachedir"
);
}
if ($usecache) {
$filename = md5_hex("$line.$lang.$speed");
# Stream file from cache if it exists #
if (-r "$cachedir/$filename.$fexten") {
console_log("File already in cache.") if ($debug);
my $res = playback("$cachedir/$filename", $intkey);
last if ($res > 0);
die if ($res < 0);
next;
}
}
# Initialise User angent #
if (!$http) {
$http = HTTP::Tiny->new(
agent => 'Asterisk MS TTS module',
timeout => $timeout,
verify_SSL => 1,
);
}
# Get access token #
$atoken = get_access_token() if (!$atoken);
fatal_log("No API key found. Aborting.") if (!$atoken);
$line = uri_escape($line);
# Hnadle interrupts #
$SIG{'INT'} = \&int_handler;
$SIG{'HUP'} = \&int_handler;
($fh, $tmpname) = tempfile("mstts_XXXXXXXX", DIR => $tmpdir, UNLINK => 1);
my $response = $http->mirror(
"$url/Speak?text=$line&language=$lang&format=$format&options=$quality&appid=$atoken",
$tmpname,
);
fatal_log("Failed to fetch file.") unless ($response->{success});
my @soxargs = get_sox_args($tmpname, "$tmpname.$fexten");
system(@soxargs) == 0 or fatal_log("$sox failed: $?");
# Playback and save file in cache #
my $res = playback($tmpname, $intkey);
die if ($res < 0);
if ($usecache) {
console_log("Saving file $filename to cache") if ($debug);
move("$tmpname.$fexten", "$cachedir/$filename.$fexten");
} else {
unlink "$tmpname.$fexten";
}
last if ($res > 0);
}
exit;
sub get_access_token {
# Obtaining an Access Token #
my $token;
my $expire;
my $fh;
my $tfile;
my $spool = $ENV{'AST_SPOOL_DIR'};
if ($spool) {
$tfile = $spool . "/tmp/mstts.token";
}
if ($tfile) {
if (!-f $tfile) {
open($fh, ">", $tfile);
} elsif (open($fh, "+<", $tfile)) {
console_log("Reading access token from file.") if ($debug);
while (<$fh>) {
/^expire:(\d+)$/ and $expire = $1;
/^token:(.+)$/ and $token = $1;
}
}
}
if (!$token or time > $expire - 5) {
$token = '';
console_log("Getting a new access token.") if ($debug);
my $form_data = [
client_id => $clientid,
client_secret => $clientsecret,
scope => 'http://api.microsofttranslator.com',
grant_type => 'client_credentials',
];
my $response = $http->post_form(
"https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/",
$form_data,
);
if ($response->{success} and $response->{content} =~ /^\{"token_type":".+?","access_token":"(.+?)","expires_in":"(\d+?)","scope":".+?"\}$/) {
$token = uri_escape("Bearer $1");
$expire = time + $2;
if ($fh) {
seek($fh, 0,0);
print $fh "expire:$expire\n";
print $fh "token:$token\n";
truncate($fh, tell($fh));
}
} else {
console_log("Failed to get Access Token.") if ($debug);
}
}
close $fh if($fh);
return $token;
}
sub checkresponse {
my $input = <STDIN>;
my @values;
chomp $input;
if ($input =~ /^200 result=(-?\d+)\s?(.*)$/) {
@values = ("$1", "$2");
} else {
$input .= <STDIN> if ($input =~ /^520-Invalid/);
warn "$name Unexpected result: $input\n";
@values = (-1, -1);
}
return @values;
}
sub playback {
my ($file, $keys) = @_;
my @response;
print "STREAM FILE $file \"$keys\"\n";
@response = checkresponse();
if ($response[0] >= 32 && chr($response[0]) =~ /[\w*#]/) {
console_log("Got digit ", chr($response[0])) if ($debug);
print "SET EXTENSION ", chr($response[0]), "\n";
checkresponse();
print "SET PRIORITY 1\n";
checkresponse();
} elsif ($response[0] == -1) {
console_log("Failed to play $file");
}
return $response[0];
}
sub detect_format {
# Detect the sound format used #
my @format;
print "GET FULL VARIABLE \${CHANNEL(audionativeformat)}\n";
my @reply = checkresponse();
for ($reply[1]) {
if (/(silk|sln)12/) { @format = ("sln12", 12000); }
elsif (/(speex|slin|silk)16|g722|siren7/) { @format = ("sln16", 16000); }
elsif (/(speex|slin|celt)32|siren14/) { @format = ("sln32", 32000); }
elsif (/(celt|slin)44/) { @format = ("sln44", 44100); }
elsif (/(celt|slin)48/) { @format = ("sln48", 48000); }
else { @format = ("sln", 8000); }
}
return @format;
}
sub get_sox_args {
my ($source_file, $dest_file) = @_;
# Detect sox version #
if (!$sox_ver) {
$sox_ver = (system("$sox --version > /dev/null 2>&1") == 0) ? 14 : 12;
console_log("Found sox version $sox_ver in: $sox") if ($debug);
}
# Set the appropiate sox cli arguments #
my @soxargs = ($sox, "-t", "mp3", $source_file, "-q", "-r", $samplerate, "-t", "raw", $dest_file);
if ($speed != 1) {
if ($sox_ver >= 14) {
push(@soxargs, ("tempo", "-s", $speed));
} else {
push(@soxargs, ("stretch", 1/$speed, "80"));
}
}
return @soxargs;
}
sub console_log {
foreach my $message (@_) {
warn "$name $message\n";
print "NOOP \"$name $message\"\n";
checkresponse();
}
}
sub fatal_log {
console_log(@_);
die;
}
sub int_handler {
die "$name Interrupt signal received, terminating...\n";
}
END {
if ($tmpname) {
warn "$name Cleaning temp files.\n" if ($debug);
unlink glob "$tmpname*";
}
}