FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/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;
44  cst_voice *voice;
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 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 
53 static const AVOption flite_options[] = {
54  { "list_voices", "list voices and exit", OFFSET(list_voices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
55  { "nb_samples", "set number of samples per frame", OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
56  { "n", "set number of samples per frame", OFFSET(frame_nb_samples), AV_OPT_TYPE_INT, {.i64=512}, 0, INT_MAX, FLAGS },
57  { "text", "set text to speak", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
58  { "textfile", "set filename of the text to speak", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
59  { "v", "set voice", OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, CHAR_MIN, CHAR_MAX, FLAGS },
60  { "voice", "set voice", OFFSET(voice_str), AV_OPT_TYPE_STRING, {.str="kal"}, CHAR_MIN, CHAR_MAX, FLAGS },
61  { NULL }
62 };
63 
65 
66 static volatile int flite_inited = 0;
67 
68 /* declare functions for all the supported voices */
69 #define DECLARE_REGISTER_VOICE_FN(name) \
70  cst_voice *register_cmu_us_## name(const char *); \
71  void unregister_cmu_us_## name(cst_voice *);
77 
78 struct voice_entry {
79  const char *name;
80  cst_voice * (*register_fn)(const char *);
81  void (*unregister_fn)(cst_voice *);
82  cst_voice *voice;
83  unsigned usage_count;
84 } voice_entry;
85 
86 #define MAKE_VOICE_STRUCTURE(voice_name) { \
87  .name = #voice_name, \
88  .register_fn = register_cmu_us_ ## voice_name, \
89  .unregister_fn = unregister_cmu_us_ ## voice_name, \
90 }
91 static struct voice_entry voice_entries[] = {
94  MAKE_VOICE_STRUCTURE(kal16),
97 };
98 
99 static void list_voices(void *log_ctx, const char *sep)
100 {
101  int i, n = FF_ARRAY_ELEMS(voice_entries);
102  for (i = 0; i < n; i++)
103  av_log(log_ctx, AV_LOG_INFO, "%s%s",
104  voice_entries[i].name, i < (n-1) ? sep : "\n");
105 }
106 
107 static int select_voice(struct voice_entry **entry_ret, const char *voice_name, void *log_ctx)
108 {
109  int i;
110 
111  for (i = 0; i < FF_ARRAY_ELEMS(voice_entries); i++) {
112  struct voice_entry *entry = &voice_entries[i];
113  if (!strcmp(entry->name, voice_name)) {
114  if (!entry->voice)
115  entry->voice = entry->register_fn(NULL);
116  if (!entry->voice) {
117  av_log(log_ctx, AV_LOG_ERROR,
118  "Could not register voice '%s'\n", voice_name);
119  return AVERROR_UNKNOWN;
120  }
121  entry->usage_count++;
122  *entry_ret = entry;
123  return 0;
124  }
125  }
126 
127  av_log(log_ctx, AV_LOG_ERROR, "Could not find voice '%s'\n", voice_name);
128  av_log(log_ctx, AV_LOG_INFO, "Choose between the voices: ");
129  list_voices(log_ctx, ", ");
130 
131  return AVERROR(EINVAL);
132 }
133 
134 static av_cold int init(AVFilterContext *ctx, const char *args)
135 {
136  FliteContext *flite = ctx->priv;
137  int ret = 0;
138 
139  flite->class = &flite_class;
140  av_opt_set_defaults(flite);
141 
142  if ((ret = av_set_options_string(flite, args, "=", ":")) < 0)
143  return ret;
144 
145  if (flite->list_voices) {
146  list_voices(ctx, "\n");
147  return AVERROR_EXIT;
148  }
149 
150  if (!flite_inited) {
151  if (flite_init() < 0) {
152  av_log(ctx, AV_LOG_ERROR, "flite initialization failed\n");
153  return AVERROR_UNKNOWN;
154  }
155  flite_inited++;
156  }
157 
158  if ((ret = select_voice(&flite->voice_entry, flite->voice_str, ctx)) < 0)
159  return ret;
160  flite->voice = flite->voice_entry->voice;
161 
162  if (flite->textfile && flite->text) {
163  av_log(ctx, AV_LOG_ERROR,
164  "Both text and textfile options set: only one must be specified\n");
165  return AVERROR(EINVAL);
166  }
167 
168  if (flite->textfile) {
169  uint8_t *textbuf;
170  size_t textbuf_size;
171 
172  if ((ret = av_file_map(flite->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
173  av_log(ctx, AV_LOG_ERROR,
174  "The text file '%s' could not be read: %s\n",
175  flite->textfile, av_err2str(ret));
176  return ret;
177  }
178 
179  if (!(flite->text = av_malloc(textbuf_size+1)))
180  return AVERROR(ENOMEM);
181  memcpy(flite->text, textbuf, textbuf_size);
182  flite->text[textbuf_size] = 0;
183  av_file_unmap(textbuf, textbuf_size);
184  }
185 
186  if (!flite->text) {
187  av_log(ctx, AV_LOG_ERROR,
188  "No speech text specified, specify the 'text' or 'textfile' option\n");
189  return AVERROR(EINVAL);
190  }
191 
192  /* synth all the file data in block */
193  flite->wave = flite_text_to_wave(flite->text, flite->voice);
194  flite->wave_samples = flite->wave->samples;
195  flite->wave_nb_samples = flite->wave->num_samples;
196  return 0;
197 }
198 
199 static av_cold void uninit(AVFilterContext *ctx)
200 {
201  FliteContext *flite = ctx->priv;
202 
203  av_opt_free(flite);
204 
205  if (!--flite->voice_entry->usage_count)
206  flite->voice_entry->unregister_fn(flite->voice);
207  flite->voice = NULL;
208  flite->voice_entry = NULL;
209  delete_wave(flite->wave);
210  flite->wave = NULL;
211 }
212 
214 {
215  FliteContext *flite = ctx->priv;
216 
217  AVFilterChannelLayouts *chlayouts = NULL;
218  int64_t chlayout = av_get_default_channel_layout(flite->wave->num_channels);
219  AVFilterFormats *sample_formats = NULL;
220  AVFilterFormats *sample_rates = NULL;
221 
222  ff_add_channel_layout(&chlayouts, chlayout);
223  ff_set_common_channel_layouts(ctx, chlayouts);
224  ff_add_format(&sample_formats, AV_SAMPLE_FMT_S16);
225  ff_set_common_formats(ctx, sample_formats);
226  ff_add_format(&sample_rates, flite->wave->sample_rate);
227  ff_set_common_samplerates (ctx, sample_rates);
228 
229  return 0;
230 }
231 
232 static int config_props(AVFilterLink *outlink)
233 {
234  AVFilterContext *ctx = outlink->src;
235  FliteContext *flite = ctx->priv;
236 
237  outlink->sample_rate = flite->wave->sample_rate;
238  outlink->time_base = (AVRational){1, flite->wave->sample_rate};
239 
240  av_log(ctx, AV_LOG_VERBOSE, "voice:%s fmt:%s sample_rate:%d\n",
241  flite->voice_str,
242  av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
243  return 0;
244 }
245 
246 static int request_frame(AVFilterLink *outlink)
247 {
248  AVFilterBufferRef *samplesref;
249  FliteContext *flite = outlink->src->priv;
250  int nb_samples = FFMIN(flite->wave_nb_samples, flite->frame_nb_samples);
251 
252  if (!nb_samples)
253  return AVERROR_EOF;
254 
255  samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
256  if (!samplesref)
257  return AVERROR(ENOMEM);
258 
259  memcpy(samplesref->data[0], flite->wave_samples,
260  nb_samples * flite->wave->num_channels * 2);
261  samplesref->pts = flite->pts;
262  samplesref->pos = -1;
263  samplesref->audio->sample_rate = flite->wave->sample_rate;
264  flite->pts += nb_samples;
265  flite->wave_samples += nb_samples * flite->wave->num_channels;
266  flite->wave_nb_samples -= nb_samples;
267 
268  return ff_filter_frame(outlink, samplesref);
269 }
270 
271 static const AVFilterPad flite_outputs[] = {
272  {
273  .name = "default",
274  .type = AVMEDIA_TYPE_AUDIO,
275  .config_props = config_props,
276  .request_frame = request_frame,
277  },
278  { NULL }
279 };
280 
282  .name = "flite",
283  .description = NULL_IF_CONFIG_SMALL("Synthesize voice from text using libflite."),
284  .query_formats = query_formats,
285  .init = init,
286  .uninit = uninit,
287  .priv_size = sizeof(FliteContext),
288  .inputs = NULL,
289  .outputs = flite_outputs,
290  .priv_class = &flite_class,
291 };