summaryrefslogtreecommitdiffstats
path: root/src/vox.c
blob: 141f555f38e6405452f16037cb6cae1a85a27320 (plain)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sysexits.h>
#include <sys/wait.h>
#include <errno.h>

#include <xas/riff.h>
#include <xas/vox.h>

static int vox_stop(xas_vox *vox) {
    if (vox->stdin >= 0) {
        (void)close(vox->stdin);

        vox->stdin = -1;
    }

    if (vox->stdout >= 0) {
        (void)close(vox->stdout);

        vox->stdout = -1;
    }

    if (vox->in) {
        (void)fclose(vox->in);

        vox->in = NULL;
    }

    if (vox->pid) {
        int status;

        if (waitpid(vox->pid, &status, 0) < 0) {
            goto error_waitpid;
        }

        vox->pid = -1;
    }

    vox->state = XAS_VOX_IDLE;

    return 0;

error_waitpid:
    return -1;
}

static char **args_concat(int argn1, char **args1,
                          int argn2, char **args2,
                          int *total) {
    char **ret;

    int count = argn1 + argn2,
        i, o;

    if ((ret = malloc((count + 1) * sizeof(char *))) == NULL) {
        goto error_malloc;
    }

    for (i=0, o=0; i<argn1; i++, o++) {
        ret[o] = args1[i];
    }

    for (i=0; i<argn2; i++, o++) {
        ret[o] = args2[i];
    }

    *total = count;
    ret[o] = NULL;

    return ret;

error_malloc:
    return NULL;
}

static int vox_start(xas_vox *vox) {
    int pipe_stdin[2],
        pipe_stdout[2];

    int pid;

    char sample_rate[40];

    if (vox->state == XAS_VOX_ACTIVE) {
        (void)vox_stop(vox);
    }

    if (vox->tmpfh) {
        if (fflush(vox->tmpfh) < 0) {
            goto error_fflush;
        }
    }

    if (pipe(pipe_stdin) < 0) {
        goto error_pipe_stdin;
    }

    if (pipe(pipe_stdout) < 0) {
        goto error_pipe_stdout;
    }

    if ((pid = fork()) < 0) {
        goto error_fork;
    }

    if (pid == 0) {
        char *args[] = {
            (char *)vox->text2wave_path,
            "-F",
            sample_rate,
            "-",
            "-o",
            "-",
            NULL,
            NULL,
            NULL
        };

        int argn = 6;

        int argc;
        char **argv;

        int fd;

        snprintf(sample_rate, sizeof(sample_rate)-1, "%zu", vox->format.sample_rate);

        if (vox->tmpfile[0] && vox->tmpfd > 0) {
            args[argn++] = "-eval";
            args[argn++] = vox->tmpfile;
        }

        if ((argv = args_concat(argn, args, vox->argn, vox->args, &argc)) == NULL) {
            goto error_child;
        }

        if ((fd = open("/dev/null", O_WRONLY)) < 0) {
            goto error_child;
        }

        close(pipe_stdin[1]);
        close(pipe_stdout[0]);

        if (dup2(pipe_stdin[0], 0) < 0) {
            goto error_child;
        }

        if (dup2(pipe_stdout[1], 1) < 0) {
            goto error_child;
        }

        if (dup2(fd, 2) < 0) {
            goto error_child;
        }

        if (execv(vox->text2wave_path, argv) < 0) {
            goto error_child;
        }

error_child:
        exit(EX_OSERR);
    }

    close(pipe_stdin[0]);
    close(pipe_stdout[1]);

    vox->pid    = pid;
    vox->stdin  = pipe_stdin[1];
    vox->stdout = pipe_stdout[0];
    vox->in     = fdopen(pipe_stdin[1], "w");
    vox->state  = XAS_VOX_ACTIVE;

    return 0;

error_fork:
    (void)close(pipe_stdout[1]);
    (void)close(pipe_stdout[0]);

error_pipe_stdout:
    (void)close(pipe_stdin[1]);
    (void)close(pipe_stdin[0]);

error_pipe_stdin:
error_fflush:
    return -1;
}

static void vox_cleanup(xas_vox *vox) {
    (void)vox_stop(vox);
}

static ssize_t vox_fill(xas_vox *vox,
                        int16_t *samples,
                        size_t count) {
    ssize_t readlen,
            readcount;

    if (vox->state != XAS_VOX_ACTIVE) {
        return 0;
    }

    if ((readlen = read(vox->stdout,
                        samples,
                        count * vox->format.sample_size)) < 0) {
        goto error_read;
    }

    if (readlen == 0) {
        vox_stop(vox);
    }

    readcount = readlen / vox->format.sample_size;

    if (readcount > 0 && vox->gain != 1.0f) {
        xas_audio_apply_gain(vox->format,
                               samples,
                               vox->gain,
                               0,
                               readcount);
    }

    return readcount;

error_read:
    return -1;
}

static int tmpfile_open(xas_vox *vox) {
    if (vox->tmpfile[0] && vox->tmpfd > 0) {
        return 0;
    }

    memset(vox->tmpfile, '\0', sizeof(vox->tmpfile));

    memcpy(vox->tmpfile,
           XAS_VOX_SETTINGS_TMP_PATH,
           strlen(XAS_VOX_SETTINGS_TMP_PATH));

    if ((vox->tmpfd = mkstemp(vox->tmpfile)) < 0) {
        goto error_mkstemp;
    }

    if ((vox->tmpfh = fdopen(vox->tmpfd, "w")) == NULL) {
        goto error_fdopen;
    }

    return 0;

error_fdopen:
    close(vox->tmpfd);
    unlink(vox->tmpfile);

error_mkstemp:
    return -1;
}

static void tmpfile_close(xas_vox *vox) {
    fclose(vox->tmpfh);
    (void)close(vox->tmpfd);
    unlink(vox->tmpfile);

    memset(vox->tmpfile, '\0', sizeof(vox->tmpfile));
    vox->tmpfd = -1;
    vox->tmpfh = NULL;
}

static int set_gain(xas_vox *vox, float gain) {
    xas_vox_set_gain(vox, gain);

    return 0;
}

xas_vox *xas_vox_new_args(xas_audio_format format,
                              size_t buffer_size,
                              const char *text2wave_path,
                              int argn,
                              char **args) {
    xas_vox *vox;

    if ((vox = malloc(sizeof(*vox))) == NULL) {
        goto error_malloc_vox;
    }

    vox->obj.start      = (xas_object_start_callback)xas_vox_generate;
    vox->obj.stop       = (xas_object_stop_callback)xas_vox_stop;
    vox->obj.set_gain   = (xas_object_set_gain_callback)set_gain;
    vox->obj.stream_new = (xas_object_stream_new_callback)xas_vox_stream_new;
    vox->obj.destroy    = (xas_object_destroy_callback)xas_vox_destroy;

    vox->text2wave_path = text2wave_path;

    vox->format.channels    = XAS_AUDIO_MONO;
    vox->format.sample_size = format.sample_size;
    vox->format.sample_rate = format.sample_rate;

    vox->buffer_size = buffer_size;
    vox->gain        = XAS_VOX_DEFAULT_GAIN;
    vox->state       = XAS_VOX_IDLE;
    vox->pid         = -1;
    vox->stdin       = -1;
    vox->stdout      = -1;
    vox->in          = NULL;

    vox->argn = argn;
    vox->args = args;

    memset(vox->tmpfile, '\0', sizeof(vox->tmpfile));
    vox->tmpfd = -1;
    vox->tmpfh = NULL;

    return vox;

error_malloc_vox:
    return NULL;
}

xas_vox *xas_vox_new(xas_audio_format format,
                         size_t buffer_size,
                         const char *text2wave_path) {
    return xas_vox_new_args(format,
                              buffer_size,
                              text2wave_path,
                              0,
                              NULL);
}

void xas_vox_destroy(xas_vox *vox) {
    if (vox->tmpfile[0] && vox->tmpfd > 0) {
        tmpfile_close(vox);
    }

    free(vox);
}

void xas_vox_set_gain(xas_vox *vox, float gain) {
    vox->gain = gain;
}

int xas_vox_set_voice(xas_vox *vox, const char *voice) {
    if (tmpfile_open(vox) < 0) {
        goto error_tmpfile_open;
    }

    if (fprintf(vox->tmpfh, "(%s)\n", voice) < 0) {
        goto error_fprintf;
    }

    return 0;

error_fprintf:
error_tmpfile_open:
    return -1;
}

static int set_parameter_va(xas_vox *vox,
                            const char *name,
                            const char *format,
                            ...) {
    va_list args;

    if (tmpfile_open(vox) < 0) {
        goto error_tmpfile_open;
    }

    va_start(args, format);

    if (fprintf(vox->tmpfh, "(Parameter.set '%s ", name) < 0) {
        goto error_fprintf;
    }

    if (vfprintf(vox->tmpfh, format, args) < 0) {
        goto error_fprintf;
    }

    if (fprintf(vox->tmpfh, ")\n") < 0) {
        goto error_fprintf;
    }

    va_end(args);

    return 0;

error_fprintf:
    va_end(args);

error_tmpfile_open:
    return -1;
}

int xas_vox_set_parameter(xas_vox *vox,
                            const char *name,
                            const char *value) {
    return set_parameter_va(vox, name, "%s", value);
}

int xas_vox_set_parameter_str(xas_vox *vox,
                                const char *name,
                                const char *value) {
    return set_parameter_va(vox, name, "\"%s\"", value);
}

int xas_vox_set_parameter_float(xas_vox *vox,
                                  const char *name,
                                  float value) {
    return set_parameter_va(vox, name, "%f", value);
}

int xas_vox_set_parameter_int(xas_vox *vox,
                                const char *name,
                                int value) {
    return set_parameter_va(vox, name, "%d", value);
}

int xas_vox_active(xas_vox *vox) {
    return vox->state == XAS_VOX_ACTIVE;
}

int xas_vox_generate(xas_vox *vox) {
    xas_audio_stream *output;

    (void)fflush(vox->in);
    (void)fclose(vox->in);
    (void)close(vox->stdin);

    vox->stdin = -1;
    vox->in    = NULL;

    if ((output = xas_riff_open_fd(vox->stdout)) == NULL) {
        goto error_riff_open_fd;
    }

    if (!xas_audio_format_eq(output->format, vox->format)) {
        errno = EINVAL;

        goto error_invalid_stream;
    }

    xas_audio_stream_destroy(output);

    return 0;

error_invalid_stream:
error_riff_open_fd:
    return -1;
}

int xas_vox_stop(xas_vox *vox) {
    if (vox->state == XAS_VOX_ACTIVE) {
        return vox_stop(vox);
    }

    return 0;
}

int xas_vox_vsayf(xas_vox *vox, const char *format, va_list args) {
    if (vox->state == XAS_VOX_IDLE && vox_start(vox) < 0) {
        goto error_vox_start;
    }

    return vfprintf(vox->in, format, args);

error_vox_start:
    return -1;
}

int xas_vox_sayf(xas_vox *vox, const char *format, ...) {
    int ret;
    va_list args;

    va_start(args, format);
    ret = xas_vox_vsayf(vox, format, args);
    va_end(args);

    return ret;
}

int xas_vox_say(xas_vox *vox, const char *message) {
    return xas_vox_sayf(vox, "%s\n", message);
}

xas_audio_stream *xas_vox_stream_new(xas_vox *vox) {
    return xas_audio_stream_new_source((xas_audio_fill)vox_fill,
                                         (xas_audio_cleanup)vox_cleanup,
                                         vox->format,
                                         vox->buffer_size,
                                         vox);
}