2 * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
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
21 #include "libavutil/opt.h"
26 enum PulsatorModes { SINE, TRIANGLE, SQUARE, SAWUP, SAWDOWN, NB_MODES };
27 enum PulsatorTimings { UNIT_BPM, UNIT_MS, UNIT_HZ, NB_TIMINGS };
29 typedef struct SimpleLFO {
39 typedef struct AudioPulsatorContext {
54 } AudioPulsatorContext;
56 #define OFFSET(x) offsetof(AudioPulsatorContext, x)
57 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59 static const AVOption apulsator_options[] = {
60 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
61 { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
62 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=SINE}, SINE, NB_MODES-1, FLAGS, "mode" },
63 { "sine", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SINE}, 0, 0, FLAGS, "mode" },
64 { "triangle", NULL, 0, AV_OPT_TYPE_CONST, {.i64=TRIANGLE},0, 0, FLAGS, "mode" },
65 { "square", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SQUARE}, 0, 0, FLAGS, "mode" },
66 { "sawup", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWUP}, 0, 0, FLAGS, "mode" },
67 { "sawdown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWDOWN}, 0, 0, FLAGS, "mode" },
68 { "amount", "set modulation", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
69 { "offset_l", "set offset L", OFFSET(offset_l), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
70 { "offset_r", "set offset R", OFFSET(offset_r), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
71 { "width", "set pulse width", OFFSET(pwidth), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 2, FLAGS },
72 { "timing", "set timing", OFFSET(timing), AV_OPT_TYPE_INT, {.i64=2}, 0, NB_TIMINGS-1, FLAGS, "timing" },
73 { "bpm", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_BPM}, 0, 0, FLAGS, "timing" },
74 { "ms", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_MS}, 0, 0, FLAGS, "timing" },
75 { "hz", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_HZ}, 0, 0, FLAGS, "timing" },
76 { "bpm", "set BPM", OFFSET(bpm), AV_OPT_TYPE_DOUBLE, {.dbl=120}, 30, 300, FLAGS },
77 { "ms", "set ms", OFFSET(ms), AV_OPT_TYPE_INT, {.i64=500}, 10, 2000, FLAGS },
78 { "hz", "set frequency", OFFSET(hz), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0.01, 100, FLAGS },
82 AVFILTER_DEFINE_CLASS(apulsator);
84 static void lfo_advance(SimpleLFO *lfo, unsigned count)
86 lfo->phase = fabs(lfo->phase + count * lfo->freq / lfo->srate);
88 lfo->phase = fmod(lfo->phase, 1);
91 static double lfo_get_value(SimpleLFO *lfo)
93 double phs = FFMIN(100, lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
101 val = sin(phs * 2 * M_PI);
105 val = (phs - 0.75) * 4 - 1;
112 val = phs < 0.5 ? -1 : +1;
122 return val * lfo->amount;
125 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
127 AVFilterContext *ctx = inlink->dst;
128 AVFilterLink *outlink = ctx->outputs[0];
129 AudioPulsatorContext *s = ctx->priv;
130 const double *src = (const double *)in->data[0];
131 const int nb_samples = in->nb_samples;
132 const double level_out = s->level_out;
133 const double level_in = s->level_in;
134 const double amount = s->amount;
139 if (av_frame_is_writable(in)) {
142 out = ff_get_audio_buffer(inlink, in->nb_samples);
145 return AVERROR(ENOMEM);
147 av_frame_copy_props(out, in);
149 dst = (double *)out->data[0];
151 for (n = 0; n < nb_samples; n++) {
154 double inL = src[0] * level_in;
155 double inR = src[1] * level_in;
159 procL *= lfo_get_value(&s->lfoL) * 0.5 + amount / 2;
160 procR *= lfo_get_value(&s->lfoR) * 0.5 + amount / 2;
162 outL = procL + inL * (1 - amount);
163 outR = procR + inR * (1 - amount);
171 lfo_advance(&s->lfoL, 1);
172 lfo_advance(&s->lfoR, 1);
181 return ff_filter_frame(outlink, out);
184 static int query_formats(AVFilterContext *ctx)
186 AVFilterChannelLayouts *layout = NULL;
187 AVFilterFormats *formats = NULL;
190 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
191 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
192 (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
193 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
196 formats = ff_all_samplerates();
197 return ff_set_common_samplerates(ctx, formats);
200 static int config_input(AVFilterLink *inlink)
202 AVFilterContext *ctx = inlink->dst;
203 AudioPulsatorContext *s = ctx->priv;
207 case UNIT_BPM: freq = s->bpm / 60; break;
208 case UNIT_MS: freq = 1 / (s->ms / 1000.); break;
209 case UNIT_HZ: freq = s->hz; break;
214 s->lfoL.mode = s->mode;
215 s->lfoR.mode = s->mode;
216 s->lfoL.offset = s->offset_l;
217 s->lfoR.offset = s->offset_r;
218 s->lfoL.srate = inlink->sample_rate;
219 s->lfoR.srate = inlink->sample_rate;
220 s->lfoL.amount = s->amount;
221 s->lfoR.amount = s->amount;
222 s->lfoL.pwidth = s->pwidth;
223 s->lfoR.pwidth = s->pwidth;
228 static const AVFilterPad inputs[] = {
231 .type = AVMEDIA_TYPE_AUDIO,
232 .config_props = config_input,
233 .filter_frame = filter_frame,
238 static const AVFilterPad outputs[] = {
241 .type = AVMEDIA_TYPE_AUDIO,
246 AVFilter ff_af_apulsator = {
248 .description = NULL_IF_CONFIG_SMALL("Audio pulsator."),
249 .priv_size = sizeof(AudioPulsatorContext),
250 .priv_class = &apulsator_class,
251 .query_formats = query_formats,