summaryrefslogtreecommitdiffstats
path: root/src/vox.c
blob: 502d79b5b30152bd333653d5ea3291cf59979a2b (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
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#include <xas/vox.h>

static void voice_cleanup(xas_vox_voice *voice, xas_audio_stream *stream) {
    int status;

    if (voice->flags & XAS_VOX_VOICE_ACTIVE) {
        if (voice->pid >= 0) {
            (void)waitpid(voice->pid, &status, 0);
        }

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

static ssize_t voice_fill(xas_vox_voice *voice,
                          int16_t *samples,
                          size_t count,
                          xas_audio_stream *stream) {
    size_t i;

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

    return i;
}

xas_audio_stream *xas_vox_new(const char *text2wave_path,
                                  size_t sample_size,
                                  size_t sample_rate,
                                  size_t buffer_size,
                                  void *ctx) {
    xas_audio_stream *stream;
    xas_vox_voice *voice;

    if ((voice = malloc(sizeof(*voice))) == NULL) {
        goto error_malloc_voice;
    }

    voice->text2wave_path = text2wave_path;
    voice->sample_size    = sample_size;
    voice->sample_rate    = sample_rate;
    voice->ctx            = ctx;
    voice->flags          = XAS_VOX_VOICE_IDLE;

    voice->pid    = -1;
    voice->stdout = -1;

    if ((stream = xas_audio_stream_new_source((xas_audio_fill)voice_fill,
                                                (xas_audio_cleanup)voice_cleanup,
                                                voice,
                                                sample_size,
                                                sample_rate,
                                                XAS_AUDIO_STREAM_MONO,
                                                buffer_size)) == NULL) {
        goto error_audio_stream_new_source;
    }

    return stream;

error_audio_stream_new_source:
    free(voice);

error_malloc_voice:
    return NULL;
}