2 * Copyright (c) 2012 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * flite voice synth source
26 #include <flite/flite.h>
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/file.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/thread.h"
36 typedef struct FliteContext {
42 int16_t *wave_samples;
46 struct voice_entry *voice_entry;
48 int frame_nb_samples; ///< number of samples per frame
51 #define OFFSET(x) offsetof(FliteContext, x)
52 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54 static const AVOption flite_options[] = {
55 { "list_voices", "list voices and exit", OFFSET(list_voices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
56 { "nb_samples", "set number of samples per frame", OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
57 { "n", "set number of samples per frame", OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
58 { "text", "set text to speak", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
59 { "textfile", "set filename of the text to speak", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
60 { "v", "set voice", OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, 0, 0, FLAGS },
61 { "voice", "set voice", OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, 0, 0, FLAGS },
65 AVFILTER_DEFINE_CLASS(flite);
67 static AVMutex flite_mutex = AV_MUTEX_INITIALIZER;
69 static int flite_inited = 0;
71 /* declare functions for all the supported voices */
72 #define DECLARE_REGISTER_VOICE_FN(name) \
73 cst_voice *register_cmu_us_## name(const char *); \
74 void unregister_cmu_us_## name(cst_voice *)
75 DECLARE_REGISTER_VOICE_FN(awb);
76 DECLARE_REGISTER_VOICE_FN(kal);
77 DECLARE_REGISTER_VOICE_FN(kal16);
78 DECLARE_REGISTER_VOICE_FN(rms);
79 DECLARE_REGISTER_VOICE_FN(slt);
83 cst_voice * (*register_fn)(const char *);
84 void (*unregister_fn)(cst_voice *);
89 #define MAKE_VOICE_STRUCTURE(voice_name) { \
90 .name = #voice_name, \
91 .register_fn = register_cmu_us_ ## voice_name, \
92 .unregister_fn = unregister_cmu_us_ ## voice_name, \
94 static struct voice_entry voice_entries[] = {
95 MAKE_VOICE_STRUCTURE(awb),
96 MAKE_VOICE_STRUCTURE(kal),
97 MAKE_VOICE_STRUCTURE(kal16),
98 MAKE_VOICE_STRUCTURE(rms),
99 MAKE_VOICE_STRUCTURE(slt),
102 static void list_voices(void *log_ctx, const char *sep)
104 int i, n = FF_ARRAY_ELEMS(voice_entries);
105 for (i = 0; i < n; i++)
106 av_log(log_ctx, AV_LOG_INFO, "%s%s",
107 voice_entries[i].name, i < (n-1) ? sep : "\n");
110 static int select_voice(struct voice_entry **entry_ret, const char *voice_name, void *log_ctx)
114 for (i = 0; i < FF_ARRAY_ELEMS(voice_entries); i++) {
115 struct voice_entry *entry = &voice_entries[i];
116 if (!strcmp(entry->name, voice_name)) {
118 pthread_mutex_lock(&flite_mutex);
120 entry->voice = entry->register_fn(NULL);
121 voice = entry->voice;
123 entry->usage_count++;
124 pthread_mutex_unlock(&flite_mutex);
126 av_log(log_ctx, AV_LOG_ERROR,
127 "Could not register voice '%s'\n", voice_name);
128 return AVERROR_UNKNOWN;
135 av_log(log_ctx, AV_LOG_ERROR, "Could not find voice '%s'\n", voice_name);
136 av_log(log_ctx, AV_LOG_INFO, "Choose between the voices: ");
137 list_voices(log_ctx, ", ");
139 return AVERROR(EINVAL);
142 static av_cold int init(AVFilterContext *ctx)
144 FliteContext *flite = ctx->priv;
147 if (flite->list_voices) {
148 list_voices(ctx, "\n");
152 pthread_mutex_lock(&flite_mutex);
154 if ((ret = flite_init()) >= 0)
157 pthread_mutex_unlock(&flite_mutex);
159 av_log(ctx, AV_LOG_ERROR, "flite initialization failed\n");
160 return AVERROR_EXTERNAL;
163 if ((ret = select_voice(&flite->voice_entry, flite->voice_str, ctx)) < 0)
165 flite->voice = flite->voice_entry->voice;
167 if (flite->textfile && flite->text) {
168 av_log(ctx, AV_LOG_ERROR,
169 "Both text and textfile options set: only one must be specified\n");
170 return AVERROR(EINVAL);
173 if (flite->textfile) {
177 if ((ret = av_file_map(flite->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
178 av_log(ctx, AV_LOG_ERROR,
179 "The text file '%s' could not be read: %s\n",
180 flite->textfile, av_err2str(ret));
184 if (!(flite->text = av_malloc(textbuf_size+1))) {
185 av_file_unmap(textbuf, textbuf_size);
186 return AVERROR(ENOMEM);
188 memcpy(flite->text, textbuf, textbuf_size);
189 flite->text[textbuf_size] = 0;
190 av_file_unmap(textbuf, textbuf_size);
194 av_log(ctx, AV_LOG_ERROR,
195 "No speech text specified, specify the 'text' or 'textfile' option\n");
196 return AVERROR(EINVAL);
199 /* synth all the file data in block */
200 flite->wave = flite_text_to_wave(flite->text, flite->voice);
201 flite->wave_samples = flite->wave->samples;
202 flite->wave_nb_samples = flite->wave->num_samples;
206 static av_cold void uninit(AVFilterContext *ctx)
208 FliteContext *flite = ctx->priv;
210 if (flite->voice_entry) {
211 pthread_mutex_lock(&flite_mutex);
212 if (!--flite->voice_entry->usage_count) {
213 flite->voice_entry->unregister_fn(flite->voice);
214 flite->voice_entry->voice = NULL;
216 pthread_mutex_unlock(&flite_mutex);
218 delete_wave(flite->wave);
222 static int query_formats(AVFilterContext *ctx)
224 FliteContext *flite = ctx->priv;
227 AVFilterChannelLayouts *chlayouts = NULL;
228 AVFilterFormats *sample_formats = NULL;
229 AVFilterFormats *sample_rates = NULL;
230 AVChannelLayout chlayout = { 0 };
232 av_channel_layout_default(&chlayout, flite->wave->num_channels);
234 if ((ret = ff_add_channel_layout (&chlayouts , &chlayout )) < 0 ||
235 (ret = ff_set_common_channel_layouts (ctx , chlayouts )) < 0 ||
236 (ret = ff_add_format (&sample_formats, AV_SAMPLE_FMT_S16 )) < 0 ||
237 (ret = ff_set_common_formats (ctx , sample_formats )) < 0 ||
238 (ret = ff_add_format (&sample_rates , flite->wave->sample_rate)) < 0 ||
239 (ret = ff_set_common_samplerates (ctx , sample_rates )) < 0)
245 static int config_props(AVFilterLink *outlink)
247 AVFilterContext *ctx = outlink->src;
248 FliteContext *flite = ctx->priv;
250 outlink->sample_rate = flite->wave->sample_rate;
251 outlink->time_base = (AVRational){1, flite->wave->sample_rate};
253 av_log(ctx, AV_LOG_VERBOSE, "voice:%s fmt:%s sample_rate:%d\n",
255 av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
259 static int request_frame(AVFilterLink *outlink)
262 FliteContext *flite = outlink->src->priv;
263 int nb_samples = FFMIN(flite->wave_nb_samples, flite->frame_nb_samples);
268 samplesref = ff_get_audio_buffer(outlink, nb_samples);
270 return AVERROR(ENOMEM);
272 memcpy(samplesref->data[0], flite->wave_samples,
273 nb_samples * flite->wave->num_channels * 2);
274 samplesref->pts = flite->pts;
276 FF_DISABLE_DEPRECATION_WARNINGS
277 samplesref->pkt_pos = -1;
278 FF_ENABLE_DEPRECATION_WARNINGS
280 samplesref->sample_rate = flite->wave->sample_rate;
281 flite->pts += nb_samples;
282 flite->wave_samples += nb_samples * flite->wave->num_channels;
283 flite->wave_nb_samples -= nb_samples;
285 return ff_filter_frame(outlink, samplesref);
288 static const AVFilterPad flite_outputs[] = {
291 .type = AVMEDIA_TYPE_AUDIO,
292 .config_props = config_props,
293 .request_frame = request_frame,
297 const AVFilter ff_asrc_flite = {
299 .description = NULL_IF_CONFIG_SMALL("Synthesize voice from text using libflite."),
302 .priv_size = sizeof(FliteContext),
304 FILTER_OUTPUTS(flite_outputs),
305 FILTER_QUERY_FUNC(query_formats),
306 .priv_class = &flite_class,