1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Michael Jerris <mike@jerris.com>
28  * Paul D. Tinsley <pdt at jackhammer.org>
29  * Christopher M. Rienzo <chris@rienzo.com>
30  *
31  *
32  * switch_core_speech.c -- Main Core Library (speech functions)
33  *
34  */
35 
36 #include <switch.h>
37 #include "private/switch_core_pvt.h"
38 
switch_core_speech_open(switch_speech_handle_t * sh,const char * module_name,const char * voice_name,unsigned int rate,unsigned int interval,unsigned int channels,switch_speech_flag_t * flags,switch_memory_pool_t * pool)39 SWITCH_DECLARE(switch_status_t) switch_core_speech_open(switch_speech_handle_t *sh,
40 														const char *module_name,
41 														const char *voice_name,
42 														unsigned int rate, unsigned int interval, unsigned int channels,
43 														switch_speech_flag_t *flags, switch_memory_pool_t *pool)
44 {
45 	switch_status_t status;
46 	char buf[256] = "";
47 	char *param = NULL;
48 
49 	if (!sh || !flags || zstr(module_name)) {
50 		return SWITCH_STATUS_FALSE;
51 	}
52 
53 	if (strchr(module_name, ':')) {
54 		switch_set_string(buf, module_name);
55 		if ((param = strchr(buf, ':'))) {
56 			*param++ = '\0';
57 			module_name = buf;
58 		}
59 	}
60 
61 	if ((sh->speech_interface = switch_loadable_module_get_speech_interface(module_name)) == 0) {
62 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid speech module [%s]!\n", module_name);
63 		return SWITCH_STATUS_GENERR;
64 	}
65 
66 	sh->flags = *flags;
67 	if (pool) {
68 		sh->memory_pool = pool;
69 	} else {
70 		if ((status = switch_core_new_memory_pool(&sh->memory_pool)) != SWITCH_STATUS_SUCCESS) {
71 			UNPROTECT_INTERFACE(sh->speech_interface);
72 			return status;
73 		}
74 		switch_set_flag(sh, SWITCH_SPEECH_FLAG_FREE_POOL);
75 	}
76 
77 	sh->engine = switch_core_strdup(sh->memory_pool, module_name);
78 	if (param) {
79 		sh->param = switch_core_strdup(sh->memory_pool, param);
80 	}
81 
82 	sh->rate = rate;
83 	sh->name = switch_core_strdup(sh->memory_pool, module_name);
84 	sh->samples = switch_samples_per_packet(rate, interval);
85 	sh->samplerate = rate;
86 	sh->native_rate = rate;
87 	sh->channels = channels;
88 	sh->real_channels = 1;
89 
90 	if ((status = sh->speech_interface->speech_open(sh, voice_name, rate, channels, flags)) == SWITCH_STATUS_SUCCESS) {
91 		switch_set_flag(sh, SWITCH_SPEECH_FLAG_OPEN);
92 	} else {
93 		UNPROTECT_INTERFACE(sh->speech_interface);
94 	}
95 
96 	return status;
97 }
98 
switch_core_speech_feed_tts(switch_speech_handle_t * sh,const char * text,switch_speech_flag_t * flags)99 SWITCH_DECLARE(switch_status_t) switch_core_speech_feed_tts(switch_speech_handle_t *sh, const char *text, switch_speech_flag_t *flags)
100 {
101 	switch_status_t status = SWITCH_STATUS_SUCCESS;
102 	char *param_string = NULL;
103 	char *data = NULL;
104 	char *ltext = NULL;
105 
106 	switch_assert(sh != NULL);
107 
108 	if (zstr(text)) {
109 		status = SWITCH_STATUS_FALSE;
110 		goto done;
111 	}
112 
113 	/* Set TTS parameters from params in the text string
114 	 * Params are defined as follows {name1=val1,name2=val2,name3=val3}text to speak
115 	 */
116 	ltext = strdup(text);
117 	data = ltext;
118 
119 	/* strip leading spaces */
120 	while (data && *data == ' ') {
121 		data++;
122 	}
123 	if (zstr(data)) {
124 		status = SWITCH_STATUS_FALSE;
125 		goto done;
126 	}
127 
128 	/* extract params */
129 	if (*data == '{') {
130 		param_string = data + 1;
131 		data = switch_find_end_paren(data, '{', '}');
132 		if (zstr(data)) {
133 			status = SWITCH_STATUS_FALSE;
134 			goto done;
135 		} else {
136 			*data = '\0';
137 			data++;
138 		}
139 	}
140 
141 	/* set TTS params */
142 	if (!zstr(param_string)) {
143 		char *param[256] = { 0 };
144 		int i;
145 		int argc = switch_separate_string(param_string, ',', param, (sizeof(param) / sizeof(param[0])));
146 		for (i = 0; i < argc && param[i]; ++i) {
147 			char *param_pair[2] = { 0 };
148 			if (switch_separate_string(param[i], '=', param_pair, (sizeof(param_pair) / sizeof(param_pair[0]))) == 2) {
149 				switch_core_speech_text_param_tts(sh, param_pair[0], param_pair[1]);
150 			}
151 		}
152 	}
153 
154 	status = sh->speech_interface->speech_feed_tts(sh, data, flags);
155 
156   done:
157 
158 	switch_safe_free(ltext);
159 	return status;
160 }
161 
switch_core_speech_flush_tts(switch_speech_handle_t * sh)162 SWITCH_DECLARE(void) switch_core_speech_flush_tts(switch_speech_handle_t *sh)
163 {
164 	switch_assert(sh != NULL);
165 
166 	if (sh->speech_interface->speech_flush_tts) {
167 		sh->speech_interface->speech_flush_tts(sh);
168 	}
169 }
170 
switch_core_speech_text_param_tts(switch_speech_handle_t * sh,char * param,const char * val)171 SWITCH_DECLARE(void) switch_core_speech_text_param_tts(switch_speech_handle_t *sh, char *param, const char *val)
172 {
173 	switch_assert(sh != NULL);
174 
175 	if (sh->speech_interface->speech_text_param_tts) {
176 		sh->speech_interface->speech_text_param_tts(sh, param, val);
177 	}
178 }
179 
switch_core_speech_numeric_param_tts(switch_speech_handle_t * sh,char * param,int val)180 SWITCH_DECLARE(void) switch_core_speech_numeric_param_tts(switch_speech_handle_t *sh, char *param, int val)
181 {
182 	switch_assert(sh != NULL);
183 
184 	if (sh->speech_interface->speech_numeric_param_tts) {
185 		sh->speech_interface->speech_numeric_param_tts(sh, param, val);
186 	}
187 }
188 
switch_core_speech_float_param_tts(switch_speech_handle_t * sh,char * param,double val)189 SWITCH_DECLARE(void) switch_core_speech_float_param_tts(switch_speech_handle_t *sh, char *param, double val)
190 {
191 	switch_assert(sh != NULL);
192 
193 	if (sh->speech_interface->speech_float_param_tts) {
194 		sh->speech_interface->speech_float_param_tts(sh, param, val);
195 	}
196 }
197 
switch_core_speech_read_tts(switch_speech_handle_t * sh,void * data,switch_size_t * datalen,switch_speech_flag_t * flags)198 SWITCH_DECLARE(switch_status_t) switch_core_speech_read_tts(switch_speech_handle_t *sh, void *data, switch_size_t *datalen, switch_speech_flag_t *flags)
199 {
200 	switch_status_t status;
201 	switch_size_t want, orig_len = *datalen;
202 
203 	switch_assert(sh != NULL);
204 
205 	want = *datalen;
206 
207   top:
208 
209 	if (sh->buffer && (switch_buffer_inuse(sh->buffer) >= orig_len || switch_test_flag(sh, SWITCH_SPEECH_FLAG_DONE))) {
210 		if ((*datalen = switch_buffer_read(sh->buffer, data, orig_len))) {
211 			status = SWITCH_STATUS_SUCCESS;
212 			goto done;
213 		}
214 	}
215 
216 	if (switch_test_flag(sh, SWITCH_SPEECH_FLAG_DONE)) {
217 		switch_clear_flag(sh, SWITCH_SPEECH_FLAG_DONE);
218 		*datalen = 0;
219 		return SWITCH_STATUS_BREAK;
220 	}
221 
222   more:
223 
224 	*datalen = orig_len / sh->channels;
225 
226 	if ((status = sh->speech_interface->speech_read_tts(sh, data, datalen, flags)) != SWITCH_STATUS_SUCCESS) {
227 		switch_set_flag(sh, SWITCH_SPEECH_FLAG_DONE);
228 		goto top;
229 	}
230 
231 	if (sh->native_rate && sh->samplerate && sh->native_rate != sh->samplerate) {
232 		if (!sh->resampler) {
233 			if (switch_resample_create(&sh->resampler,
234 									   sh->native_rate, sh->samplerate, (uint32_t) orig_len / sh->channels, SWITCH_RESAMPLE_QUALITY, 1) != SWITCH_STATUS_SUCCESS) {
235 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Unable to create resampler!\n");
236 				return SWITCH_STATUS_GENERR;
237 			}
238 		}
239 
240 		switch_resample_process(sh->resampler, data, (uint32_t)(*datalen / 2));
241 		if (sh->resampler->to_len < want / 2 || sh->resampler->to_len > orig_len / 2) {
242 			if (!sh->buffer) {
243 				int factor = sh->resampler->to_len * sh->samplerate / 1000;
244 				switch_buffer_create_dynamic(&sh->buffer, factor, factor, 0);
245 				switch_assert(sh->buffer);
246 			}
247 			if (!sh->dbuf || sh->dbuflen < sh->resampler->to_len * 2) {
248 				sh->dbuflen = sh->resampler->to_len * 2;
249 				sh->dbuf = switch_core_alloc(sh->memory_pool, sh->dbuflen);
250 			}
251 			switch_assert(sh->resampler->to_len <= sh->dbuflen);
252 
253 			memcpy((int16_t *) sh->dbuf, sh->resampler->to, sh->resampler->to_len * 2);
254 			switch_buffer_write(sh->buffer, sh->dbuf, sh->resampler->to_len * 2);
255 
256 			if (switch_buffer_inuse(sh->buffer) < want) {
257 				*datalen = want;
258 				goto more;
259 			}
260 			*datalen = switch_buffer_read(sh->buffer, data, orig_len);
261 			status = SWITCH_STATUS_SUCCESS;
262 		} else {
263 			memcpy(data, sh->resampler->to, sh->resampler->to_len * 2);
264 			*datalen = sh->resampler->to_len * 2;
265 			status = SWITCH_STATUS_SUCCESS;
266 		}
267 	}
268 
269 
270  done:
271 
272 	if (sh->channels != sh->real_channels) {
273 		uint32_t rlen = *datalen / 2;
274 		switch_mux_channels((int16_t *) data, rlen, 1, sh->channels);
275 		*datalen = rlen * 2 * sh->channels;
276 	}
277 
278 	return status;
279 
280 }
281 
282 
switch_core_speech_close(switch_speech_handle_t * sh,switch_speech_flag_t * flags)283 SWITCH_DECLARE(switch_status_t) switch_core_speech_close(switch_speech_handle_t *sh, switch_speech_flag_t *flags)
284 {
285 	switch_status_t status = sh->speech_interface->speech_close(sh, flags);
286 
287 	if (!switch_test_flag(sh, SWITCH_SPEECH_FLAG_OPEN)) {
288 		return SWITCH_STATUS_FALSE;
289 	}
290 
291 	if (sh->buffer) {
292 		switch_buffer_destroy(&sh->buffer);
293 	}
294 
295 	switch_resample_destroy(&sh->resampler);
296 
297 	UNPROTECT_INTERFACE(sh->speech_interface);
298 
299 	if (switch_test_flag(sh, SWITCH_SPEECH_FLAG_FREE_POOL)) {
300 		switch_core_destroy_memory_pool(&sh->memory_pool);
301 	}
302 
303 	switch_clear_flag(sh, SWITCH_SPEECH_FLAG_OPEN);
304 
305 	return status;
306 }
307 
308 /* For Emacs:
309  * Local Variables:
310  * mode:c
311  * indent-tabs-mode:t
312  * tab-width:4
313  * c-basic-offset:4
314  * End:
315  * For VIM:
316  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
317  */
318