summaryrefslogtreecommitdiffstats
path: root/src/vox.c
blob: 7fec432cb4512d213aa97f9ec046ae93d0efb707 (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
#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->flags &= ~XAS_VOX_ACTIVE;

    return 0;

error_waitpid:
    return -1;
}

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

    int pid;

    char sample_rate[40];

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

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

    (void)vox_stop(vox);

    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) {
        int fd;

        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, args) < 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->flags |= 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:
    return -1;
}

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

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

    size_t i;

    if (!(vox->flags & XAS_VOX_ACTIVE)) {
        memset(samples, '\0', count * vox->sample_size);

        return count;
    }

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

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

    readcount = readlen / vox->sample_size;

    for (i=readcount; i<count-readcount; i++) {
        samples[i] = 0;
    }

    return count;

error_read:
    return -1;
}

xas_vox *xas_vox_new(const char *text2wave_path,
                         size_t sample_size,
                         size_t sample_rate,
                         size_t buffer_size,
                         void *ctx) {
    xas_vox *vox;

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

    vox->text2wave_path = text2wave_path;
    vox->sample_size    = sample_size;
    vox->sample_rate    = sample_rate;
    vox->buffer_size    = buffer_size;
    vox->ctx            = ctx;
    vox->flags          = XAS_VOX_IDLE;

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

    return vox;

error_malloc_vox:
    return NULL;
}

void xas_vox_destroy(xas_vox *vox) {
    free(vox);
}

int xas_vox_stop(xas_vox *vox) {
    return vox_stop(vox);
}

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 (output->sample_size != vox->sample_size
     || output->sample_rate != vox->sample_rate) {
        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_vsayf(xas_vox *vox, const char *format, va_list args) {
    if (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", message);
}

int xas_vox_active(xas_vox *vox) {
    return vox->flags & XAS_VOX_ACTIVE;
}

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,
                                         XAS_AUDIO_STREAM_MONO,
                                         vox->sample_size,
                                         vox->sample_rate,
                                         vox->buffer_size);
}