#include #include #include #include #include #include #include #include #include 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; istate == XAS_VOX_ACTIVE) { (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) { char *args[] = { (char *)vox->text2wave_path, "-F", sample_rate, "-", "-o", "-", NULL }; int argc; char **argv; int fd; snprintf(sample_rate, sizeof(sample_rate)-1, "%zu", vox->format.sample_rate); if ((argv = args_concat(6, 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: 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; if (vox->state != XAS_VOX_ACTIVE) { xas_audio_zero(vox->format, samples, 0, count); return count; } 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; xas_audio_zero(vox->format, samples, readcount, count-readcount); return count; 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; } 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->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->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; 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); } 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; } int xas_vox_set_parameter(xas_vox *vox, const char *name, const char *value) { if (tmpfile_open(vox) < 0) { goto error_tmpfile_open; } if (fprintf(vox->tmpfh, "(Parameter.set '%s %s)\n", name, value) < 0) { goto error_fprintf; } return 0; error_fprintf: error_tmpfile_open: return -1; } int xas_vox_stop(xas_vox *vox) { if (vox->state == XAS_VOX_ACTIVE) { return vox_stop(vox); } return 0; } 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 (output->format.sample_size != vox->format.sample_size || output->format.sample_rate != vox->format.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->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", 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); }