-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp_picotts.c
318 lines (265 loc) · 8.33 KB
/
app_picotts.c
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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2012 Ramon Martinez
*
* Ramon Martinez <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* 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.
*/
/*! \file
*
* \brief Say text to the user, using PicoTTS TTS engine.
*
* \author\verbatim Ramon Martinez <[email protected]> based on works of Lefteris Zafiris <[email protected]>\endverbatim
*
* \extref PicoTTS text to speech Synthesis System
*
* \ingroup applications
*/
/*** MODULEINFO
<defaultenabled>no</defaultenabled>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 00 $")
#include <stdio.h>
#include <string.h>
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/app.h"
#include "asterisk/utils.h"
#include "asterisk/strings.h"
#define AST_MODULE "PicoTTS"
#define FLITE_CONFIG "picotts.conf"
#define MAXLEN 2048
#define DEF_RATE 8000
#define DEF_VOICE "es-ES"
#define DEF_DIR "/tmp"
static char *app = "PicoTTS";
static char *synopsis = "Say text to the user, using PicoTTS TTS engine";
static char *descrip =
" PicoTTS(text[,intkeys][,language]): This will invoke the PicoTTS TTS engine, send a text string,\n"
"get back the resulting waveform and play it to the user, allowing any given interrupt\n"
"keys to immediately terminate and return the value, or 'any' to allow any number back.\n";
static int target_sample_rate;
static int usecache;
static const char *cachedir;
static const char *voice_name;
static struct ast_config *cfg;
static struct ast_flags config_flags = { 0 };
static int read_config(void)
{
const char *temp;
/* set default values */
target_sample_rate = DEF_RATE;
usecache = 0;
cachedir = DEF_DIR;
voice_name = DEF_VOICE;
cfg = ast_config_load(FLITE_CONFIG, config_flags);
if (!cfg || cfg == CONFIG_STATUS_FILEINVALID)
{
ast_log(LOG_WARNING,
"PicoTTS: Unable to read config file %s. Using default settings\n",
FLITE_CONFIG);
}
else
{
if ((temp = ast_variable_retrieve(cfg, "general", "usecache")))
usecache = ast_true(temp);
if ((temp = ast_variable_retrieve(cfg, "general", "cachedir")))
cachedir = temp;
if ((temp = ast_variable_retrieve(cfg, "general", "voice")))
voice_name = temp;
if ((temp = ast_variable_retrieve(cfg, "general", "samplerate")))
target_sample_rate = atoi(temp);
}
if (target_sample_rate != 8000 && target_sample_rate != 16000)
{
ast_log(LOG_WARNING, "PicoTTS: Unsupported sample rate: %d. Falling back to %d\n",
target_sample_rate, DEF_RATE);
target_sample_rate = DEF_RATE;
}
return 0;
}
static int picotts_text_to_wave(const char *filedata, const char *language, const char *texttospeech)
{
int res_tts = 0;
char temp[2048];
sprintf(temp, "pico2wave -w %s -l %s '%s'", filedata, language, texttospeech);
res_tts = system(temp);
ast_log(LOG_WARNING, "PicoTTS: command %s, code %d.\n", temp, res_tts);
return(0);
}
static int delete_wave(const char *filedata)
{
return unlink(filedata);
}
static int picotts_exec(struct ast_channel *chan, const char *data)
{
int res = 0;
char *mydata;
int writecache = 0;
char MD5_name[33] = "";
int sample_rate;
char cachefile[MAXLEN] = "";
char tmp_name[20];
char raw_tmp_name[26];
char rawpico_tmp_name[26];
int voice;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(text);
AST_APP_ARG(interrupt);
AST_APP_ARG(lang);
);
if (ast_strlen_zero(data))
{
ast_log(LOG_ERROR, "PicoTTS requires t least an argument (text)\n");
return -1;
}
mydata = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, mydata);
if (args.interrupt && !strcasecmp(args.interrupt, "any"))
args.interrupt = AST_DIGIT_ANY;
args.text = ast_strip_quoted(args.text, "\"", "\"");
args.lang = ast_strip_quoted(args.lang, "\"", "\"");
if (ast_strlen_zero(args.text))
{
ast_log(LOG_WARNING, "PicoTTS: No text passed for synthesis.\n");
return res;
}
if (ast_strlen_zero(args.lang))
{
ast_log(LOG_WARNING, "PicoTTS: language is default: %s.\n", voice_name);
}
else
{
voice_name = args.lang;
}
ast_debug(1, "PicoTTS:\nText passed: %s\nInterrupt key(s): %s\nVoice: %s\nRate: %d\n",
args.text, args.interrupt, voice_name, target_sample_rate);
/*Cache mechanism */
if (usecache)
{
ast_md5_hash(MD5_name, args.text);
if (strlen(cachedir) + strlen(MD5_name) + 6 <= MAXLEN)
{
ast_debug(1, "PicoTTS: Activating cache mechanism...\n");
snprintf(cachefile, sizeof(cachefile), "%s/%s", cachedir, MD5_name);
if (ast_fileexists(cachefile, NULL, NULL) <= 0)
{
ast_debug(1, "PicoTTS: Cache file does not yet exist.\n");
writecache = 1;
}
else
{
ast_debug(1, "PicoTTS: Cache file exists.\n");
if (chan->_state != AST_STATE_UP)
ast_answer(chan);
res = ast_streamfile(chan, cachefile, chan->language);
if (res)
{
ast_log(LOG_ERROR, "PicoTTS: ast_streamfile from cache failed on %s\n",
chan->name);
}
else
{
res = ast_waitstream(chan, args.interrupt);
ast_stopstream(chan);
return res;
}
}
}
}
/* Create temp filenames */
snprintf(tmp_name, sizeof(tmp_name), "/tmp/picotts_%li", ast_random() % 99999999);
if (target_sample_rate == 8000)
snprintf(raw_tmp_name, sizeof(raw_tmp_name), "%s.wav", tmp_name);
if (target_sample_rate == 16000)
{
snprintf(raw_tmp_name, sizeof(raw_tmp_name), "%s.gsm", tmp_name);
snprintf(rawpico_tmp_name, sizeof(raw_tmp_name), "%s.wav", tmp_name);
}
/* Invoke PicoTTS */
//picotts_init();
if (strcmp(voice_name, "en-US") == 0)
voice = 1;
else if (strcmp(voice_name, "en-GB") == 0)
voice = 2;
else if (strcmp(voice_name, "de-DE") == 0)
voice = 3;
else if (strcmp(voice_name, "es-ES") == 0)
voice = 4;
else if (strcmp(voice_name, "fr-FR") == 0)
voice = 5;
else if (strcmp(voice_name, "it-IT") == 0)
voice = 6;
else
{
ast_log(LOG_WARNING, "PicoTTS: Unsupported voice %s. Using default voice.\n",
voice_name);
voice = 1;
voice_name = "en-US";
}
res = picotts_text_to_wave(rawpico_tmp_name, voice_name, args.text);
char temp[1024];
sprintf(temp, "sox -v 0.8 %s -r 8000 -c1 %s resample -ql", rawpico_tmp_name, raw_tmp_name);
system(temp);
unlink(rawpico_tmp_name);
sample_rate = 16000;
if (res == -1)
{
ast_log(LOG_ERROR, "PicoTTS: failed to write file %s , code %d\n", raw_tmp_name, res);
return res;
}
if (writecache)
{
ast_debug(1, "PicoTTS: Saving cache file %s\n", cachefile);
ast_filecopy(tmp_name, cachefile, NULL);
}
if (chan->_state != AST_STATE_UP)
ast_answer(chan);
res = ast_streamfile(chan, tmp_name, chan->language);
if (res)
{
ast_log(LOG_ERROR, "PicoTTS: ast_streamfile failed on %s\n", chan->name);
}
else
{
res = ast_waitstream(chan, args.interrupt);
ast_stopstream(chan);
}
ast_filedelete(tmp_name, NULL);
return res;
}
static int reload(void)
{
ast_config_destroy(cfg);
read_config();
return 0;
}
static int unload_module(void)
{
ast_config_destroy(cfg);
return ast_unregister_application(app);
}
static int load_module(void)
{
read_config();
return ast_register_application(app, picotts_exec, synopsis, descrip) ?
AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PicoTTS TTS Interface",
.load = load_module,
.unload = unload_module,
.reload = reload,
);