]> git.sesse.net Git - ffmpeg/blob - libavfilter/asrc_flite.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / asrc_flite.c
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * flite voice synth source
24  */
25
26 #include <flite/flite.h>
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/file.h"
29 #include "libavutil/opt.h"
30 #include "avfilter.h"
31 #include "audio.h"
32 #include "formats.h"
33 #include "internal.h"
34
35 typedef struct {
36     const AVClass *class;
37     char *voice_str;
38     char *textfile;
39     char *text;
40     cst_wave *wave;
41     int16_t *wave_samples;
42     int      wave_nb_samples;
43     int list_voices;
44     cst_voice *voice;
45     struct voice_entry *voice_entry;
46     int64_t pts;
47     int frame_nb_samples; ///< number of samples per frame
48 } FliteContext;
49
50 #define OFFSET(x) offsetof(FliteContext, x)
51
52 static const AVOption flite_options[] = {
53     { "list_voices", "list voices and exit",              OFFSET(list_voices), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
54     { "nb_samples",  "set number of samples per frame",   OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.dbl=512}, 0, INT_MAX },
55     { "n",           "set number of samples per frame",   OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.dbl=512}, 0, INT_MAX },
56     { "text",        "set text to speak",                 OFFSET(text),      AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
57     { "textfile",    "set filename of the text to speak", OFFSET(textfile),  AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
58     { "v",           "set voice",                         OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, CHAR_MIN, CHAR_MAX },
59     { "voice",       "set voice",                         OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, CHAR_MIN, CHAR_MAX },
60     { NULL }
61 };
62
63 AVFILTER_DEFINE_CLASS(flite);
64
65 static volatile int flite_inited = 0;
66
67 /* declare functions for all the supported voices */
68 #define DECLARE_REGISTER_VOICE_FN(name) \
69     cst_voice *register_cmu_us_## name(const char *); \
70     void     unregister_cmu_us_## name(cst_voice *);
71 DECLARE_REGISTER_VOICE_FN(awb);
72 DECLARE_REGISTER_VOICE_FN(kal);
73 DECLARE_REGISTER_VOICE_FN(kal16);
74 DECLARE_REGISTER_VOICE_FN(rms);
75 DECLARE_REGISTER_VOICE_FN(slt);
76
77 struct voice_entry {
78     const char *name;
79     cst_voice * (*register_fn)(const char *);
80     void (*unregister_fn)(cst_voice *);
81     cst_voice *voice;
82     unsigned usage_count;
83 } voice_entry;
84
85 #define MAKE_VOICE_STRUCTURE(voice_name) {             \
86     .name          =                      #voice_name, \
87     .register_fn   =   register_cmu_us_ ## voice_name, \
88     .unregister_fn = unregister_cmu_us_ ## voice_name, \
89 }
90 static struct voice_entry voice_entries[] = {
91     MAKE_VOICE_STRUCTURE(awb),
92     MAKE_VOICE_STRUCTURE(kal),
93     MAKE_VOICE_STRUCTURE(kal16),
94     MAKE_VOICE_STRUCTURE(rms),
95     MAKE_VOICE_STRUCTURE(slt),
96 };
97
98 static void list_voices(void *log_ctx, const char *sep)
99 {
100     int i, n = FF_ARRAY_ELEMS(voice_entries);
101     for (i = 0; i < n; i++)
102         av_log(log_ctx, AV_LOG_INFO, "%s%s",
103                voice_entries[i].name, i < (n-1) ? sep : "\n");
104 }
105
106 static int select_voice(struct voice_entry **entry_ret, const char *voice_name, void *log_ctx)
107 {
108     int i;
109
110     for (i = 0; i < FF_ARRAY_ELEMS(voice_entries); i++) {
111         struct voice_entry *entry = &voice_entries[i];
112         if (!strcmp(entry->name, voice_name)) {
113             if (!entry->voice)
114                 entry->voice = entry->register_fn(NULL);
115             if (!entry->voice) {
116                 av_log(log_ctx, AV_LOG_ERROR,
117                        "Could not register voice '%s'\n", voice_name);
118                 return AVERROR_UNKNOWN;
119             }
120             entry->usage_count++;
121             *entry_ret = entry;
122             return 0;
123         }
124     }
125
126     av_log(log_ctx, AV_LOG_ERROR, "Could not find voice '%s'\n", voice_name);
127     av_log(log_ctx, AV_LOG_INFO, "Choose between the voices: ");
128     list_voices(log_ctx, ", ");
129
130     return AVERROR(EINVAL);
131 }
132
133 static av_cold int init(AVFilterContext *ctx, const char *args)
134 {
135     FliteContext *flite = ctx->priv;
136     int ret = 0;
137
138     flite->class = &flite_class;
139     av_opt_set_defaults(flite);
140
141     if ((ret = av_set_options_string(flite, args, "=", ":")) < 0) {
142         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
143         return ret;
144     }
145
146     if (flite->list_voices) {
147         list_voices(ctx, "\n");
148         return AVERROR_EXIT;
149     }
150
151     if (!flite_inited) {
152         if (flite_init() < 0) {
153             av_log(ctx, AV_LOG_ERROR, "flite initialization failed\n");
154             return AVERROR_UNKNOWN;
155         }
156         flite_inited++;
157     }
158
159     if ((ret = select_voice(&flite->voice_entry, flite->voice_str, ctx)) < 0)
160         return ret;
161     flite->voice = flite->voice_entry->voice;
162
163     if (flite->textfile && flite->text) {
164         av_log(ctx, AV_LOG_ERROR,
165                "Both text and textfile options set: only one must be specified\n");
166         return AVERROR(EINVAL);
167     }
168
169     if (flite->textfile) {
170         uint8_t *textbuf;
171         size_t textbuf_size;
172
173         if ((ret = av_file_map(flite->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
174             av_log(ctx, AV_LOG_ERROR,
175                    "The text file '%s' could not be read: %s\n",
176                    flite->textfile, av_err2str(ret));
177             return ret;
178         }
179
180         if (!(flite->text = av_malloc(textbuf_size+1)))
181             return AVERROR(ENOMEM);
182         memcpy(flite->text, textbuf, textbuf_size);
183         flite->text[textbuf_size] = 0;
184         av_file_unmap(textbuf, textbuf_size);
185     }
186
187     if (!flite->text) {
188         av_log(ctx, AV_LOG_ERROR,
189                "No speech text specified, specify the 'text' or 'textfile' option\n");
190         return AVERROR(EINVAL);
191     }
192
193     /* synth all the file data in block */
194     flite->wave = flite_text_to_wave(flite->text, flite->voice);
195     flite->wave_samples    = flite->wave->samples;
196     flite->wave_nb_samples = flite->wave->num_samples;
197     return 0;
198 }
199
200 static av_cold void uninit(AVFilterContext *ctx)
201 {
202     FliteContext *flite = ctx->priv;
203
204     av_opt_free(flite);
205
206     if (!--flite->voice_entry->usage_count)
207         flite->voice_entry->unregister_fn(flite->voice);
208     flite->voice = NULL;
209     flite->voice_entry = NULL;
210     delete_wave(flite->wave);
211     flite->wave = NULL;
212 }
213
214 static int query_formats(AVFilterContext *ctx)
215 {
216     FliteContext *flite = ctx->priv;
217
218     AVFilterChannelLayouts *chlayouts = NULL;
219     int64_t chlayout = av_get_default_channel_layout(flite->wave->num_channels);
220     AVFilterFormats *sample_formats = NULL;
221     AVFilterFormats *sample_rates = NULL;
222
223     ff_add_channel_layout(&chlayouts, chlayout);
224     ff_set_common_channel_layouts(ctx, chlayouts);
225     ff_add_format(&sample_formats, AV_SAMPLE_FMT_S16);
226     ff_set_common_formats(ctx, sample_formats);
227     ff_add_format(&sample_rates, flite->wave->sample_rate);
228     ff_set_common_samplerates (ctx, sample_rates);
229
230     return 0;
231 }
232
233 static int config_props(AVFilterLink *outlink)
234 {
235     AVFilterContext *ctx = outlink->src;
236     FliteContext *flite = ctx->priv;
237
238     outlink->sample_rate = flite->wave->sample_rate;
239     outlink->time_base = (AVRational){1, flite->wave->sample_rate};
240
241     av_log(ctx, AV_LOG_VERBOSE, "voice:%s fmt:%s sample_rate:%d\n",
242            flite->voice_str,
243            av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
244     return 0;
245 }
246
247 static int request_frame(AVFilterLink *outlink)
248 {
249     AVFilterBufferRef *samplesref;
250     FliteContext *flite = outlink->src->priv;
251     int nb_samples = FFMIN(flite->wave_nb_samples, flite->frame_nb_samples);
252
253     if (!nb_samples)
254         return AVERROR_EOF;
255
256     samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
257     if (!samplesref)
258         return AVERROR(ENOMEM);
259
260     memcpy(samplesref->data[0], flite->wave_samples,
261            nb_samples * flite->wave->num_channels * 2);
262     samplesref->pts = flite->pts;
263     samplesref->pos = -1;
264     samplesref->audio->sample_rate = flite->wave->sample_rate;
265     flite->pts += nb_samples;
266     flite->wave_samples += nb_samples * flite->wave->num_channels;
267     flite->wave_nb_samples -= nb_samples;
268
269     return ff_filter_samples(outlink, samplesref);
270 }
271
272 AVFilter avfilter_asrc_flite = {
273     .name        = "flite",
274     .description = NULL_IF_CONFIG_SMALL("Synthesize voice from text using libflite."),
275     .query_formats = query_formats,
276     .init        = init,
277     .uninit      = uninit,
278     .priv_size   = sizeof(FliteContext),
279
280     .inputs = (const AVFilterPad[]) {{ .name = NULL}},
281
282     .outputs = (const AVFilterPad[]) {
283         {
284             .name = "default",
285             .type = AVMEDIA_TYPE_AUDIO,
286             .config_props = config_props,
287             .request_frame = request_frame,
288         },
289         { .name = NULL }
290     },
291 };