FFmpeg
Loading...
Searching...
No Matches
asrc_flite.c
Go to the documentation of this file.
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>
28#include "libavutil/avstring.h"
30#include "libavutil/file.h"
31#include "libavutil/mem.h"
32#include "libavutil/opt.h"
33#include "libavutil/thread.h"
34#include "avfilter.h"
35#include "filters.h"
36#include "audio.h"
37#include "formats.h"
38
39typedef struct FliteContext {
40 const AVClass *class;
41 char *voice_str;
42 char *textfile;
43 char *text;
44 char *text_p;
50 cst_voice *voice;
51 cst_audio_streaming_info *asi;
54 int frame_nb_samples; ///< number of samples per frame
56
57#define OFFSET(x) offsetof(FliteContext, x)
58#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59
60static const AVOption flite_options[] = {
61 { "list_voices", "list voices and exit", OFFSET(list_voices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
62 { "nb_samples", "set number of samples per frame", OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
63 { "n", "set number of samples per frame", OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
64 { "text", "set text to speak", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
65 { "textfile", "set filename of the text to speak", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
66 { "v", "set voice", OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, 0, 0, FLAGS },
67 { "voice", "set voice", OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, 0, 0, FLAGS },
68 { NULL }
69};
70
72
74
75static int flite_inited = 0;
76
77/* declare functions for all the supported voices */
78#define DECLARE_REGISTER_VOICE_FN(name) \
79 cst_voice *register_cmu_us_## name(const char *); \
80 void unregister_cmu_us_## name(cst_voice *)
86
88 const char *name;
89 cst_voice * (*register_fn)(const char *);
90 void (*unregister_fn)(cst_voice *);
91 cst_voice *voice;
92 unsigned usage_count;
93};
94
95#define MAKE_VOICE_STRUCTURE(voice_name) { \
96 .name = #voice_name, \
97 .register_fn = register_cmu_us_ ## voice_name, \
98 .unregister_fn = unregister_cmu_us_ ## voice_name, \
99}
107
108static void list_voices(void *log_ctx, const char *sep)
109{
111 for (i = 0; i < n; i++)
112 av_log(log_ctx, AV_LOG_INFO, "%s%s",
113 voice_entries[i].name, i < (n-1) ? sep : "\n");
114}
115
116static int select_voice(struct voice_entry **entry_ret, const char *voice_name, void *log_ctx)
117{
118 int i;
119
120 for (i = 0; i < FF_ARRAY_ELEMS(voice_entries); i++) {
121 struct voice_entry *entry = &voice_entries[i];
122 if (!strcmp(entry->name, voice_name)) {
123 cst_voice *voice;
125 if (!entry->voice)
126 entry->voice = entry->register_fn(NULL);
127 voice = entry->voice;
128 if (voice)
129 entry->usage_count++;
131 if (!voice) {
132 av_log(log_ctx, AV_LOG_ERROR,
133 "Could not register voice '%s'\n", voice_name);
134 return AVERROR_UNKNOWN;
135 }
136 *entry_ret = entry;
137 return 0;
138 }
139 }
140
141 av_log(log_ctx, AV_LOG_ERROR, "Could not find voice '%s'\n", voice_name);
142 av_log(log_ctx, AV_LOG_INFO, "Choose between the voices: ");
143 list_voices(log_ctx, ", ");
144
145 return AVERROR(EINVAL);
146}
147
148static int audio_stream_chunk_by_word(const cst_wave *wave, int start, int size,
149 int last, cst_audio_streaming_info *asi)
150{
151 FliteContext *flite = asi->userdata;
152 void *const ptr[8] = { &wave->samples[start] };
153
154 flite->nb_channels = wave->num_channels;
155 flite->sample_rate = wave->sample_rate;
156 if (!flite->fifo) {
158 if (!flite->fifo)
159 return CST_AUDIO_STREAM_STOP;
160 }
161
162 av_audio_fifo_write(flite->fifo, ptr, size);
163
164 return CST_AUDIO_STREAM_CONT;
165}
166
168{
169 FliteContext *flite = ctx->priv;
170 int ret = 0;
171 char *text;
172
173 if (flite->list_voices) {
174 list_voices(ctx, "\n");
175 return AVERROR_EXIT;
176 }
177
179 if (!flite_inited) {
180 if ((ret = flite_init()) >= 0)
181 flite_inited = 1;
182 }
184 if (ret < 0) {
185 av_log(ctx, AV_LOG_ERROR, "flite initialization failed\n");
186 return AVERROR_EXTERNAL;
187 }
188
189 if ((ret = select_voice(&flite->voice_entry, flite->voice_str, ctx)) < 0)
190 return ret;
191 flite->voice = flite->voice_entry->voice;
192
193 if (flite->textfile && flite->text) {
195 "Both text and textfile options set: only one must be specified\n");
196 return AVERROR(EINVAL);
197 }
198
199 if (flite->textfile) {
200 uint8_t *textbuf;
201 size_t textbuf_size;
202
203 if ((ret = av_file_map(flite->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
205 "The text file '%s' could not be read: %s\n",
206 flite->textfile, av_err2str(ret));
207 return ret;
208 }
209
210 if (!(flite->text = av_malloc(textbuf_size+1))) {
211 av_file_unmap(textbuf, textbuf_size);
212 return AVERROR(ENOMEM);
213 }
214 memcpy(flite->text, textbuf, textbuf_size);
215 flite->text[textbuf_size] = 0;
216 av_file_unmap(textbuf, textbuf_size);
217 }
218
219 if (!flite->text) {
221 "No speech text specified, specify the 'text' or 'textfile' option\n");
222 return AVERROR(EINVAL);
223 }
224
225 flite->asi = new_audio_streaming_info();
226 if (!flite->asi)
227 return AVERROR_BUG;
228
229 flite->asi->asc = audio_stream_chunk_by_word;
230 flite->asi->userdata = flite;
231 feat_set(flite->voice->features, "streaming_info", audio_streaming_info_val(flite->asi));
232
233 flite->text_p = flite->text;
234 if (!(text = av_strtok(flite->text_p, "\n", &flite->text_saveptr)))
235 return AVERROR(EINVAL);
236 flite->text_p = NULL;
237
238 flite_text_to_speech(text, flite->voice, "none");
239
240 return 0;
241}
242
244{
245 FliteContext *flite = ctx->priv;
246
247 if (flite->voice_entry) {
249 if (!--flite->voice_entry->usage_count) {
250 flite->voice_entry->unregister_fn(flite->voice);
251 flite->voice_entry->voice = NULL;
252 }
254 }
255 av_audio_fifo_free(flite->fifo);
256}
257
259 AVFilterFormatsConfig **cfg_in,
260 AVFilterFormatsConfig **cfg_out)
261{
262 const FliteContext *flite = ctx->priv;
263
264 static const enum AVSampleFormat formats[] = {
267 };
268 int sample_rates[] = { flite->sample_rate, -1 };
270 { .nb_channels = 0 },
271 };
272
273 int ret;
274
276
278 if (ret < 0)
279 return ret;
280
281 ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, formats);
282 if (ret < 0)
283 return ret;
284
286 if (ret < 0)
287 return ret;
288
289 return 0;
290}
291
292static int config_props(AVFilterLink *outlink)
293{
294 AVFilterContext *ctx = outlink->src;
295 FliteContext *flite = ctx->priv;
296
297 outlink->sample_rate = flite->sample_rate;
298 outlink->time_base = (AVRational){1, flite->sample_rate};
299
300 av_log(ctx, AV_LOG_VERBOSE, "voice:%s fmt:%s sample_rate:%d\n",
301 flite->voice_str,
302 av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
303
304 return 0;
305}
306
308{
309 AVFilterLink *outlink = ctx->outputs[0];
310 FliteContext *flite = ctx->priv;
311 AVFrame *samplesref;
312 int nb_samples;
313
314 if (!ff_outlink_frame_wanted(outlink))
315 return FFERROR_NOT_READY;
316
317 nb_samples = FFMIN(av_audio_fifo_size(flite->fifo), flite->frame_nb_samples);
318 if (!nb_samples) {
319 char *text;
320
321 if (!(text = av_strtok(flite->text_p, "\n", &flite->text_saveptr))) {
322 ff_outlink_set_status(outlink, AVERROR_EOF, flite->pts);
323 return 0;
324 }
325
326 flite_text_to_speech(text, flite->voice, "none");
328 return 0;
329 }
330
331 samplesref = ff_get_audio_buffer(outlink, nb_samples);
332 if (!samplesref)
333 return AVERROR(ENOMEM);
334
335 av_audio_fifo_read(flite->fifo, (void **)samplesref->extended_data,
336 nb_samples);
337
338 samplesref->pts = flite->pts;
339 samplesref->sample_rate = flite->sample_rate;
340 flite->pts += nb_samples;
341
342 return ff_filter_frame(outlink, samplesref);
343}
344
345static const AVFilterPad flite_outputs[] = {
346 {
347 .name = "default",
348 .type = AVMEDIA_TYPE_AUDIO,
349 .config_props = config_props,
350 },
351};
352
354 .p.name = "flite",
355 .p.description = NULL_IF_CONFIG_SMALL("Synthesize voice from text using libflite."),
356 .p.priv_class = &flite_class,
357 .init = init,
358 .uninit = uninit,
359 .priv_size = sizeof(FliteContext),
363};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
const FFFilter ff_asrc_flite
Definition asrc_flite.c:353
#define entry
static AVFormatContext * ctx
static int config_props(AVFilterLink *outlink)
Definition asrc_flite.c:292
static int flite_inited
Definition asrc_flite.c:75
static void list_voices(void *log_ctx, const char *sep)
Definition asrc_flite.c:108
#define DECLARE_REGISTER_VOICE_FN(name)
Definition asrc_flite.c:78
static struct voice_entry voice_entries[]
Definition asrc_flite.c:100
#define MAKE_VOICE_STRUCTURE(voice_name)
Definition asrc_flite.c:95
static int audio_stream_chunk_by_word(const cst_wave *wave, int start, int size, int last, cst_audio_streaming_info *asi)
Definition asrc_flite.c:148
static const AVOption flite_options[]
Definition asrc_flite.c:60
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition asrc_flite.c:258
static int select_voice(struct voice_entry **entry_ret, const char *voice_name, void *log_ctx)
Definition asrc_flite.c:116
static int activate(AVFilterContext *ctx)
Definition asrc_flite.c:307
static av_cold void uninit(AVFilterContext *ctx)
Definition asrc_flite.c:243
#define OFFSET(x)
Definition asrc_flite.c:57
static AVMutex flite_mutex
Definition asrc_flite.c:73
static const AVFilterPad flite_outputs[]
Definition asrc_flite.c:345
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
Audio FIFO Buffer.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1690
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:229
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static const int sample_rates[]
Definition dcaenc.h:34
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
Misc file utilities.
int ff_set_common_samplerates_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *samplerates)
Definition formats.c:1050
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition formats.c:1026
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition audio_fifo.c:62
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
Definition audio_fifo.c:119
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
Definition audio_fifo.c:175
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition audio_fifo.c:48
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition audio_fifo.c:222
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition samplefmt.c:51
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:629
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition file.c:142
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition file.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define AV_MUTEX_INITIALIZER
Definition thread.h:185
#define AVMutex
Definition thread.h:184
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition os2threads.h:119
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition os2threads.h:132
const char * name
Definition qsvenc.c:142
formats
Definition signature.h:47
#define FF_ARRAY_ELEMS(a)
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
Context for an Audio FIFO Buffer.
Definition audio_fifo.c:37
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
int sample_rate
Sample rate of the audio data.
Definition frame.h:635
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
char * text_saveptr
Definition asrc_flite.c:45
char * text_p
Definition asrc_flite.c:44
int64_t pts
Definition asrc_flite.c:53
AVAudioFifo * fifo
Definition asrc_flite.c:48
cst_voice * voice
Definition asrc_flite.c:50
char * text
Definition asrc_flite.c:43
struct voice_entry * voice_entry
Definition asrc_flite.c:52
cst_audio_streaming_info * asi
Definition asrc_flite.c:51
char * textfile
Definition asrc_flite.c:42
char * voice_str
Definition asrc_flite.c:41
int frame_nb_samples
number of samples per frame
Definition asrc_flite.c:54
Definition asrc_flite.c:87
unsigned usage_count
Definition asrc_flite.c:92
cst_voice * voice
Definition asrc_flite.c:91
void(* unregister_fn)(cst_voice *)
Definition asrc_flite.c:90
const char * name
Definition asrc_flite.c:88
#define av_log(a,...)
int size