Debian Codegraph @

src/clients/say/say.c

raw
1
2 /*
3 * say.c - Super-simple Speech Dispatcher client
4 *
5 * Copyright (C) 2001, 2002, 2003, 2007 Brailcom, o.p.s.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 *
20 * $Id: say.c,v 1.16 2007-05-03 09:43:12 hanke Exp $
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <assert.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <semaphore.h>
35 #include <errno.h>
36 #include <getopt.h>
37
38 /*
39 * Since this client is built as part of the Speech Dispatcher source
40 * tree, we must include speechd_types.h directly.
41 * Clients built outside the speech dispatcher source tree should not do
42 * this.
43 */
44 #include <speechd_types.h>
45 #include <libspeechd.h>
46
47 #include "options.h"
48 #include <i18n.h>
49 #include <locale.h>
50
51 #define MAX_LINELEN 16384
52
53 sem_t *semaphore;
54
55 /* Callback for Speech Dispatcher notifications */
56 void end_of_speech(size_t msg_id, size_t client_id, SPDNotificationType type)
57 {
58 sem_post(semaphore);
59 }
60
61 void index_mark(size_t msg_id, size_t client_id, SPDNotificationType type, char *index_mark)
62 {
63 if (type == SPD_EVENT_INDEX_MARK)
64 fprintf(stderr, "reached mark '%s'\n", index_mark);
65 }
66
67 int main(int argc, char **argv)
68 {
69 SPDConnection *conn;
70 SPDPriority spd_priority;
71 int err;
72 char *error;
73 int msg_arg_required = 0;
74 int ret;
75 int option_ret;
76 char *line;
77
78 /* initialize i18n support */
79 i18n_init();
80
81 rate = -101;
82 pitch = -101;
83 pitch_range = -101;
84 volume = -101;
85 language = NULL;
86 voice_type = NULL;
87 punctuation_mode = NULL;
88 spelling = -2;
89 ssml_mode = SPD_DATA_TEXT;
90 wait_till_end = 0;
91 stop_previous = 0;
92 cancel_previous = 0;
93 list_synthesis_voices = 0;
94 list_output_modules = 0;
95 synthesis_voice = NULL;
96 pipe_mode = 0;
97 priority = NULL;
98 application_name = NULL;
99 connection_name = NULL;
100
101 option_ret = options_parse(argc, argv);
102
103 /* Check if the text to say or options are specified in the argument */
104 msg_arg_required = (pipe_mode != 1) && (stop_previous != 1)
105 && (cancel_previous != 1) && (list_synthesis_voices != 1)
106 && (list_output_modules != 1) && (sound_icon == NULL);
107 if ((optind >= argc) && msg_arg_required) {
108 options_print_help(argv);
109 return 1;
110 }
111
112 /* Open a connection to Speech Dispatcher */
113 conn = spd_open2(application_name ? application_name : "spd-say",
114 connection_name ? connection_name : "main",
115 NULL, SPD_MODE_THREADED, NULL, 1, &error);
116 if (conn == NULL) {
117 fprintf(stderr, "Failed to connect to Speech Dispatcher:\n%s\n",
118 error);
119 exit(1);
120 }
121
122 if (stop_previous)
123 spd_stop_all(conn);
124 if (cancel_previous)
125 spd_cancel_all(conn);
126
127 /* Set the desired parameters */
128
129 if (language != NULL) {
130 if (spd_set_language(conn, language))
131 printf("Invalid language!\n");
132 } else {
133 char *locale = strdup(setlocale(LC_MESSAGES, NULL));
134 char *dot = strchr(locale, '.');
135 if (dot)
136 *dot = 0;
137 char *at = strchr(locale, '@');
138 if (at)
139 *at = 0;
140 char *underscore = strchr(locale, '_');
141 if (underscore)
142 *underscore = '-';
143 if (spd_set_language(conn, locale))
144 printf("Invalid language %s!\n", locale);
145 free(locale);
146 }
147
148 if (output_module != NULL)
149 if (spd_set_output_module(conn, output_module))
150 printf("Invalid output module!\n");
151
152 if (list_output_modules) {
153 char **list;
154 int i;
155
156 list = spd_list_modules(conn);
157
158 if (list != NULL) {
159 printf("OUTPUT MODULES\n");
160 for (i = 0; list[i]; i++) {
161 printf("%s\n", list[i]);
162 }
163 } else {
164 printf("Output modules not found.\n");
165 }
166
167 free_spd_modules(list);
168 }
169
170 if (voice_type != NULL) {
171 if (!strcmp(voice_type, "male1")) {
172 if (spd_set_voice_type(conn, SPD_MALE1))
173 printf("Can't set this voice!\n");
174 } else if (!strcmp(voice_type, "male2")) {
175 if (spd_set_voice_type(conn, SPD_MALE2))
176 printf("Can't set this voice!\n");
177 } else if (!strcmp(voice_type, "male3")) {
178 if (spd_set_voice_type(conn, SPD_MALE3))
179 printf("Can't set this voice!\n");
180 } else if (!strcmp(voice_type, "female1")) {
181 if (spd_set_voice_type(conn, SPD_FEMALE1))
182 printf("Can't set this voice!\n");
183 } else if (!strcmp(voice_type, "female2")) {
184 if (spd_set_voice_type(conn, SPD_FEMALE2))
185 printf("Can't set this voice!\n");
186 } else if (!strcmp(voice_type, "female3")) {
187 if (spd_set_voice_type(conn, SPD_FEMALE3))
188 printf("Can't set this voice!\n");
189 } else if (!strcmp(voice_type, "child_male")) {
190 if (spd_set_voice_type(conn, SPD_CHILD_MALE))
191 printf("Can't set this voice!\n");
192 } else if (!strcmp(voice_type, "child_female")) {
193 if (spd_set_voice_type(conn, SPD_CHILD_FEMALE))
194 printf("Can't set this voice!\n");
195 } else {
196 printf("Invalid voice\n");
197 }
198 }
199
200 if (list_synthesis_voices) {
201 SPDVoice **list;
202 int i;
203
204 list = spd_list_synthesis_voices2(conn, language, NULL);
205 if (list != NULL) {
206 printf("%25s%25s%25s\n", "NAME", "LANGUAGE", "VARIANT");
207 for (i = 0; list[i]; i++) {
208 printf("%25s%25s%25s\n", list[i]->name,
209 list[i]->language, list[i]->variant);
210 }
211 free_spd_voices(list);
212 } else {
213 printf("Failed to get voice list.\n");
214 }
215 }
216
217 if (synthesis_voice != NULL)
218 if (spd_set_synthesis_voice(conn, synthesis_voice))
219 printf("Failed to set synthesis voice!\n");
220
221 if (ssml_mode == SPD_DATA_SSML)
222 if (spd_set_data_mode(conn, ssml_mode))
223 printf("Failed to set SSML mode.\n");
224
225 if (rate != -101)
226 if (spd_set_voice_rate(conn, rate))
227 printf("Invalid rate!\n");
228
229 if (pitch != -101)
230 if (spd_set_voice_pitch(conn, pitch))
231 printf("Invalid pitch!\n");
232
233 if (pitch_range != -101)
234 if (spd_set_voice_pitch_range(conn, pitch_range))
235 printf("Invalid pitch range!\n");
236
237 if (volume != -101)
238 if (spd_set_volume(conn, volume))
239 printf("Invalid volume!\n");
240
241 if (spelling == 1)
242 if (spd_set_spelling(conn, SPD_SPELL_ON))
243 printf("Can't set spelling to on!\n");
244
245 if (punctuation_mode != NULL) {
246 if (!strcmp(punctuation_mode, "none")) {
247 if (spd_set_punctuation(conn, SPD_PUNCT_NONE))
248 printf("Can't set this punctuation mode!\n");
249 } else if (!strcmp(punctuation_mode, "some")) {
250 if (spd_set_punctuation(conn, SPD_PUNCT_SOME))
251 printf("Can't set this punctuation mode!\n");
252 } else if (!strcmp(punctuation_mode, "most")) {
253 if (spd_set_punctuation(conn, SPD_PUNCT_MOST))
254 printf("Can't set this punctuation mode!\n");
255 } else if (!strcmp(punctuation_mode, "all")) {
256 if (spd_set_punctuation(conn, SPD_PUNCT_ALL))
257 printf("Can't set this punctuation mode!\n");
258 } else {
259 printf("Invalid punctuation mode.\n");
260 }
261 }
262
263 /* Set default priority... */
264 if (1 == pipe_mode)
265 spd_priority = SPD_MESSAGE;
266 else
267 spd_priority = SPD_TEXT;
268 /* ...and look if it wasn't overriden */
269 if (priority != NULL) {
270 if (!strcmp(priority, "important"))
271 spd_priority = SPD_IMPORTANT;
272 else if (!strcmp(priority, "message"))
273 spd_priority = SPD_MESSAGE;
274 else if (!strcmp(priority, "text"))
275 spd_priority = SPD_TEXT;
276 else if (!strcmp(priority, "notification"))
277 spd_priority = SPD_NOTIFICATION;
278 else if (!strcmp(priority, "progress"))
279 spd_priority = SPD_PROGRESS;
280 else {
281 printf("Invalid priority.\n");
282 }
283 }
284
285 if (sound_icon != NULL)
286 if (spd_sound_icon(conn, spd_priority, sound_icon))
287 printf("Invalid sound_icon!\n");
288
289 if (wait_till_end) {
290 char name[64];
291 snprintf(name, sizeof(name), "/speechd-say-%d", getpid());
292 semaphore = sem_open(name, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0);
293 if (semaphore == SEM_FAILED) {
294 fprintf(stderr, "Can't initialize semaphore: %s",
295 strerror(errno));
296 return 0;
297 }
298 sem_unlink(name);
299
300 /* Notify when the message is canceled or the speech terminates */
301 conn->callback_end = end_of_speech;
302 conn->callback_cancel = end_of_speech;
303 conn->callback_im = index_mark;
304 spd_set_notification_on(conn, SPD_END);
305 spd_set_notification_on(conn, SPD_CANCEL);
306 spd_set_notification_on(conn, SPD_INDEX_MARKS);
307 }
308
309 if (character) {
310 /* Speak just characters */
311 if (optind < argc) {
312 err =
313 spd_char(conn, spd_priority, argv[optind]);
314
315 if (err == -1) {
316 fprintf(stderr,
317 "Speech Dispatcher failed to say character\n");
318 exit(1);
319 }
320
321 /* Wait till the callback is called */
322 if (wait_till_end)
323 sem_wait(semaphore);
324 }
325 }
326 else if (key) {
327 /* Speak just keys */
328 if (optind < argc) {
329 err =
330 spd_key(conn, spd_priority, argv[optind]);
331
332 if (err == -1) {
333 fprintf(stderr,
334 "Speech Dispatcher failed to say key\n");
335 exit(1);
336 }
337
338 /* Wait till the callback is called */
339 if (wait_till_end)
340 sem_wait(semaphore);
341 }
342 }
343 else if (pipe_mode == 1) {
344 /* In pipe mode, read from stdin, write to stdout, and also to Speech Dispatcher. */
345 line = (char *)malloc(MAX_LINELEN);
346 while (NULL != fgets(line, MAX_LINELEN, stdin)) {
347 fputs(line, stdout);
348 if (0 == strncmp(line, "!-!", 3)) {
349 /* Remove EOL */
350 line[strlen(line) - 1] = 0;
351 spd_execute_command(conn, line + 3);
352 } else {
353 spd_say(conn, spd_priority, line);
354 if (wait_till_end)
355 sem_wait(semaphore);
356 }
357 }
358 free(line);
359
360 } else {
361 /* Say the message with priority "text" */
362 /* Or do nothing in case of -C or -S with no message. */
363 if (optind < argc) {
364 err =
365 spd_sayf(conn, spd_priority, "%s", (char *)argv[optind]);
366 if (err == -1) {
367 fprintf(stderr,
368 "Speech Dispatcher failed to say message\n");
369 exit(1);
370 }
371
372 /* Wait till the callback is called */
373 if (wait_till_end)
374 sem_wait(semaphore);
375 }
376 }
377
378 /* Close the connection */
379 spd_close(conn);
380
381 return 0;
382 }