aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/asrc_flite.c
blob: 1bbd329177fcdd4fe33e78cfc4547d8085d45f15 (plain) (blame)
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
/*
 * Copyright (c) 2012 Stefano Sabatini
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * flite voice synth source
 */

#include <flite/flite.h>
#include "libavutil/audio_fifo.h"
#include "libavutil/avstring.h"
#include "libavutil/channel_layout.h"
#include "libavutil/file.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/thread.h"
#include "avfilter.h"
#include "filters.h"
#include "audio.h"
#include "formats.h"

typedef struct FliteContext {
    const AVClass *class;
    char *voice_str;
    char *textfile;
    char *text;
    char *text_p;
    char *text_saveptr;
    int nb_channels;
    int sample_rate;
    AVAudioFifo *fifo;
    int list_voices;
    cst_voice *voice;
    cst_audio_streaming_info *asi;
    struct voice_entry *voice_entry;
    int64_t pts;
    int frame_nb_samples; ///< number of samples per frame
} FliteContext;

#define OFFSET(x) offsetof(FliteContext, x)
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM

static const AVOption flite_options[] = {
    { "list_voices", "list voices and exit",              OFFSET(list_voices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
    { "nb_samples",  "set number of samples per frame",   OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
    { "n",           "set number of samples per frame",   OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
    { "text",        "set text to speak",                 OFFSET(text),      AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
    { "textfile",    "set filename of the text to speak", OFFSET(textfile),  AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
    { "v",           "set voice",                         OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, 0, 0, FLAGS },
    { "voice",       "set voice",                         OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, 0, 0, FLAGS },
    { NULL }
};

AVFILTER_DEFINE_CLASS(flite);

static AVMutex flite_mutex = AV_MUTEX_INITIALIZER;

static int flite_inited = 0;

/* declare functions for all the supported voices */
#define DECLARE_REGISTER_VOICE_FN(name) \
    cst_voice *register_cmu_us_## name(const char *); \
    void     unregister_cmu_us_## name(cst_voice *)
DECLARE_REGISTER_VOICE_FN(awb);
DECLARE_REGISTER_VOICE_FN(kal);
DECLARE_REGISTER_VOICE_FN(kal16);
DECLARE_REGISTER_VOICE_FN(rms);
DECLARE_REGISTER_VOICE_FN(slt);

struct voice_entry {
    const char *name;
    cst_voice * (*register_fn)(const char *);
    void (*unregister_fn)(cst_voice *);
    cst_voice *voice;
    unsigned usage_count;
};

#define MAKE_VOICE_STRUCTURE(voice_name) {             \
    .name          =                      #voice_name, \
    .register_fn   =   register_cmu_us_ ## voice_name, \
    .unregister_fn = unregister_cmu_us_ ## voice_name, \
}
static struct voice_entry voice_entries[] = {
    MAKE_VOICE_STRUCTURE(awb),
    MAKE_VOICE_STRUCTURE(kal),
    MAKE_VOICE_STRUCTURE(kal16),
    MAKE_VOICE_STRUCTURE(rms),
    MAKE_VOICE_STRUCTURE(slt),
};

static void list_voices(void *log_ctx, const char *sep)
{
    int i, n = FF_ARRAY_ELEMS(voice_entries);
    for (i = 0; i < n; i++)
        av_log(log_ctx, AV_LOG_INFO, "%s%s",
               voice_entries[i].name, i < (n-1) ? sep : "\n");
}

static int select_voice(struct voice_entry **entry_ret, const char *voice_name, void *log_ctx)
{
    int i;

    for (i = 0; i < FF_ARRAY_ELEMS(voice_entries); i++) {
        struct voice_entry *entry = &voice_entries[i];
        if (!strcmp(entry->name, voice_name)) {
            cst_voice *voice;
            pthread_mutex_lock(&flite_mutex);
            if (!entry->voice)
                entry->voice = entry->register_fn(NULL);
            voice = entry->voice;
            if (voice)
                entry->usage_count++;
            pthread_mutex_unlock(&flite_mutex);
            if (!voice) {
                av_log(log_ctx, AV_LOG_ERROR,
                       "Could not register voice '%s'\n", voice_name);
                return AVERROR_UNKNOWN;
            }
            *entry_ret = entry;
            return 0;
        }
    }

    av_log(log_ctx, AV_LOG_ERROR, "Could not find voice '%s'\n", voice_name);
    av_log(log_ctx, AV_LOG_INFO, "Choose between the voices: ");
    list_voices(log_ctx, ", ");

    return AVERROR(EINVAL);
}

static int audio_stream_chunk_by_word(const cst_wave *wave, int start, int size,
                                      int last, cst_audio_streaming_info *asi)
{
    FliteContext *flite = asi->userdata;
    void *const ptr[8] = { &wave->samples[start] };

    flite->nb_channels = wave->num_channels;
    flite->sample_rate = wave->sample_rate;
    if (!flite->fifo) {
        flite->fifo = av_audio_fifo_alloc(AV_SAMPLE_FMT_S16, flite->nb_channels, size);
        if (!flite->fifo)
            return CST_AUDIO_STREAM_STOP;
    }

    av_audio_fifo_write(flite->fifo, ptr, size);

    return CST_AUDIO_STREAM_CONT;
}

static av_cold int init(AVFilterContext *ctx)
{
    FliteContext *flite = ctx->priv;
    int ret = 0;
    char *text;

    if (flite->list_voices) {
        list_voices(ctx, "\n");
        return AVERROR_EXIT;
    }

    pthread_mutex_lock(&flite_mutex);
    if (!flite_inited) {
        if ((ret = flite_init()) >= 0)
            flite_inited = 1;
    }
    pthread_mutex_unlock(&flite_mutex);
    if (ret < 0) {
        av_log(ctx, AV_LOG_ERROR, "flite initialization failed\n");
        return AVERROR_EXTERNAL;
    }

    if ((ret = select_voice(&flite->voice_entry, flite->voice_str, ctx)) < 0)
        return ret;
    flite->voice = flite->voice_entry->voice;

    if (flite->textfile && flite->text) {
        av_log(ctx, AV_LOG_ERROR,
               "Both text and textfile options set: only one must be specified\n");
        return AVERROR(EINVAL);
    }

    if (flite->textfile) {
        uint8_t *textbuf;
        size_t textbuf_size;

        if ((ret = av_file_map(flite->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
            av_log(ctx, AV_LOG_ERROR,
                   "The text file '%s' could not be read: %s\n",
                   flite->textfile, av_err2str(ret));
            return ret;
        }

        if (!(flite->text = av_malloc(textbuf_size+1))) {
            av_file_unmap(textbuf, textbuf_size);
            return AVERROR(ENOMEM);
        }
        memcpy(flite->text, textbuf, textbuf_size);
        flite->text[textbuf_size] = 0;
        av_file_unmap(textbuf, textbuf_size);
    }

    if (!flite->text) {
        av_log(ctx, AV_LOG_ERROR,
               "No speech text specified, specify the 'text' or 'textfile' option\n");
        return AVERROR(EINVAL);
    }

    flite->asi = new_audio_streaming_info();
    if (!flite->asi)
        return AVERROR_BUG;

    flite->asi->asc = audio_stream_chunk_by_word;
    flite->asi->userdata = flite;
    feat_set(flite->voice->features, "streaming_info", audio_streaming_info_val(flite->asi));

    flite->text_p = flite->text;
    if (!(text = av_strtok(flite->text_p, "\n", &flite->text_saveptr)))
        return AVERROR(EINVAL);
    flite->text_p = NULL;

    flite_text_to_speech(text, flite->voice, "none");

    return 0;
}

static av_cold void uninit(AVFilterContext *ctx)
{
    FliteContext *flite = ctx->priv;

    if (flite->voice_entry) {
        pthread_mutex_lock(&flite_mutex);
        if (!--flite->voice_entry->usage_count) {
            flite->voice_entry->unregister_fn(flite->voice);
            flite->voice_entry->voice = NULL;
        }
        pthread_mutex_unlock(&flite_mutex);
    }
    av_audio_fifo_free(flite->fifo);
}

static int query_formats(const AVFilterContext *ctx,
                         AVFilterFormatsConfig **cfg_in,
                         AVFilterFormatsConfig **cfg_out)
{
    const FliteContext *flite = ctx->priv;

    static const enum AVSampleFormat formats[] = {
        AV_SAMPLE_FMT_S16,
        AV_SAMPLE_FMT_NONE,
    };
    int sample_rates[] = { flite->sample_rate, -1 };
    AVChannelLayout layouts[2] = {
        { .nb_channels = 0 },
    };

    int ret;

    av_channel_layout_default(&layouts[0], flite->nb_channels);

    ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, layouts);
    if (ret < 0)
        return ret;

    ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, formats);
    if (ret < 0)
        return ret;

    ret = ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, sample_rates);
    if (ret < 0)
        return ret;

    return 0;
}

static int config_props(AVFilterLink *outlink)
{
    AVFilterContext *ctx = outlink->src;
    FliteContext *flite = ctx->priv;

    outlink->sample_rate = flite->sample_rate;
    outlink->time_base = (AVRational){1, flite->sample_rate};

    av_log(ctx, AV_LOG_VERBOSE, "voice:%s fmt:%s sample_rate:%d\n",
           flite->voice_str,
           av_get_sample_fmt_name(outlink->format), outlink->sample_rate);

    return 0;
}

static int activate(AVFilterContext *ctx)
{
    AVFilterLink *outlink = ctx->outputs[0];
    FliteContext *flite = ctx->priv;
    AVFrame *samplesref;
    int nb_samples;

    if (!ff_outlink_frame_wanted(outlink))
        return FFERROR_NOT_READY;

    nb_samples = FFMIN(av_audio_fifo_size(flite->fifo), flite->frame_nb_samples);
    if (!nb_samples) {
        char *text;

        if (!(text = av_strtok(flite->text_p, "\n", &flite->text_saveptr))) {
            ff_outlink_set_status(outlink, AVERROR_EOF, flite->pts);
            return 0;
        }

        flite_text_to_speech(text, flite->voice, "none");
        ff_filter_set_ready(ctx, 100);
        return 0;
    }

    samplesref = ff_get_audio_buffer(outlink, nb_samples);
    if (!samplesref)
        return AVERROR(ENOMEM);

    av_audio_fifo_read(flite->fifo, (void **)samplesref->extended_data,
                       nb_samples);

    samplesref->pts = flite->pts;
    samplesref->sample_rate = flite->sample_rate;
    flite->pts += nb_samples;

    return ff_filter_frame(outlink, samplesref);
}

static const AVFilterPad flite_outputs[] = {
    {
        .name          = "default",
        .type          = AVMEDIA_TYPE_AUDIO,
        .config_props  = config_props,
    },
};

const AVFilter ff_asrc_flite = {
    .name          = "flite",
    .description   = NULL_IF_CONFIG_SMALL("Synthesize voice from text using libflite."),
    .init          = init,
    .uninit        = uninit,
    .priv_size     = sizeof(FliteContext),
    .activate      = activate,
    .inputs        = NULL,
    FILTER_OUTPUTS(flite_outputs),
    FILTER_QUERY_FUNC2(query_formats),
    .priv_class    = &flite_class,
};