forked from tangaza/Common
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sound.pm
253 lines (170 loc) · 5.99 KB
/
Sound.pm
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
#
# Common: Shared library for voice-based applications.
#
# Copyright (C) 2010 Nokia Corporation.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Billy Odero, Jonathan Ledlie
package Nokia::Common::Sound;
use Exporter;
@ISA = ('Exporter');
@EXPORT = ('msg', 'init_sound', 'get_language_name');
use Digest::MD5 qw(md5_hex);
use File::Basename;
use strict;
use warnings;
=head1 NAME
Nokia::Common::Sound - Audio manager
=head1 DESCRIPTION
Manages all voice input
=head1 METHODS
=cut
######################################################################
# TODO As messages get solidified and recorded properly, put them in here.
my %msgs = ();
# TODO make this smarter so that it is not an absolute path
#my $snd_dir = '/home/mosoko/mosoko/trunk/sounds/';
#my $snd_dir_matt = "$snd_dir/matt/";
#my $snd_dir_charles = "$snd_dir/charles/";
#my $ast_snd_dir = '/var/lib/asterisk/sounds/';
my %language_id2name = ();
######################################################################
=head2 init_sound
Initializes parameters necessary for processing sound files
=cut
sub init_sound {
my $self = shift;
my $rs = $self->{server}{schema}->resultset('Languages');
while (my $language = $rs->next) {
$language_id2name{$language->id} = $language->name;
}
}
######################################################################
=head2 get_language_name
Returns the name of the language based on it's language id
=over 4
=item Args:
$language_id: The id of language whose name is being requested
=back
=cut
sub get_language_name {
my ($self, $language_id) = @_;
if (defined ($language_id2name{$language_id})) {
return $language_id2name{$language_id};
}
die ("No language for $language_id");
}
######################################################################
=head2 msg
Uses the supplied C<$words> parameter to loop through the file recordings
in the filesystem and returns the full system filepath to be played back
=over 4
=item Args:
$words: The name of the speech file to be played
=back
=cut
sub msg {
my ($self, $words) = @_;
if (!defined ($self) || !defined($words)) {
my @func = (caller(1));
if (!defined ($self)) {
print STDERR ("self not def\n");
}
if (!defined($words)) {
print STDERR ("words not def\n");
}
die ("bad call to msg from $func[3] line $func[2]");
}
$self->log (4, "language= ".$self->{user}->{language});
$self->log (4, "words= ".$words);
my $lang_words = $self->{user}->{language}.'/'.$words;
if (defined($msgs{$lang_words})) {
$self->log (4, "found msg in hash: $lang_words");
return $msgs{$lang_words};
}
die("sound_path not defined. ".
"Check if sound_path parameter is defined ".
"in the agi file") if (!defined($self->get_property('sound_path')));
$self->log (4, "searching directories for: $lang_words");
foreach my $snd_dir (split(/:/,$self->get_property('sound_path'))) {
# if the sound file exists, add it to the msgs hash
# (we won't check for it again)
$self->log (4, "looking for ".
"$snd_dir"."$lang_words.sln");
# first look for it with a language prefix
if (-e "$snd_dir"."$lang_words.sln" ||
-e "$snd_dir"."$lang_words.gsm" ||
-e "$snd_dir"."$lang_words.wav" ) {
$self->log (4, "sound (lang) found $lang_words in $snd_dir");
$msgs{$lang_words} = $snd_dir.$lang_words;
return $msgs{$lang_words};
}
$self->log (4, "looking for ".
"$snd_dir"."$words.sln");
if (-e "$snd_dir"."$words.sln" ||
-e "$snd_dir"."$words.gsm" ||
-e "$snd_dir"."$words.wav" ) {
$self->log (4, "sound (no lang) found $words in $snd_dir");
$msgs{$lang_words} = $snd_dir.$words;
return $msgs{$lang_words};
}
}
# if it doesn't exist, take the input words and
# make a text-to-speech prompt
$self->log (2, "tts $words");
$words =~ s/_/ /g;
$words =~ s/-/ /g;
$msgs{$lang_words} = &getTTSFilename($self, $words);
return $msgs{$lang_words};
}
######################################################################
# location of the wave file cache and working directory
my $SOUNDDIR = "/var/lib/asterisk/festivalcache/";
# festival text2wave location
my $T2WDIR= "/usr/bin/";
################################################################################
# sub getTTSFilename
# http://search.cpan.org/src/JAMESGOL/asterisk-perl-0.10/examples/directory.agi
################################################################################
#
=head2 getTTSFilename
If the file to be played-back does not exist this method generates a TTS wav file
that will be played back instead
=over 4
=item Args:
$text: The text to be converted from text to speech
=back
=cut
sub getTTSFilename {
my ($self, $text) = @_;
my $hash = md5_hex($text);
my $wavefile = "$SOUNDDIR"."tts-diirectory-$hash.wav";
unless( -f $wavefile ) {
$self->log (1, "missing sound: $text");
open( fileOUT, ">$SOUNDDIR"."say-text-$hash.txt" );
print fileOUT "$text";
close( fileOUT );
my $execf=$T2WDIR."text2wave $SOUNDDIR"."say-text-$hash.txt -F 8000 -o $wavefile";
system( $execf );
unlink( $SOUNDDIR."say-text-$hash.txt" );
}
return "$SOUNDDIR".basename($wavefile,".wav");
} # sub getTTSFilename
######################################################################
=head1 AUTHORS
Billy Odero, Jonathan Ledlie
Copyright (C) 2010 Nokia Corporation.
=cut
1;