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/avassert.h"
22 #include "libavutil/opt.h"
27 enum PulsatorModes { SINE, TRIANGLE, SQUARE, SAWUP, SAWDOWN, NB_MODES };
28 enum PulsatorTimings { UNIT_BPM, UNIT_MS, UNIT_HZ, NB_TIMINGS };
30 typedef struct SimpleLFO {
40 typedef struct AudioPulsatorContext {
55 } AudioPulsatorContext;
57 #define OFFSET(x) offsetof(AudioPulsatorContext, x)
58 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
60 static const AVOption apulsator_options[] = {
61 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
62 { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
63 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=SINE}, SINE, NB_MODES-1, FLAGS, "mode" },
64 { "sine", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SINE}, 0, 0, FLAGS, "mode" },
65 { "triangle", NULL, 0, AV_OPT_TYPE_CONST, {.i64=TRIANGLE},0, 0, FLAGS, "mode" },
66 { "square", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SQUARE}, 0, 0, FLAGS, "mode" },
67 { "sawup", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWUP}, 0, 0, FLAGS, "mode" },
68 { "sawdown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWDOWN}, 0, 0, FLAGS, "mode" },
69 { "amount", "set modulation", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
70 { "offset_l", "set offset L", OFFSET(offset_l), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
71 { "offset_r", "set offset R", OFFSET(offset_r), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
72 { "width", "set pulse width", OFFSET(pwidth), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 2, FLAGS },
73 { "timing", "set timing", OFFSET(timing), AV_OPT_TYPE_INT, {.i64=2}, 0, NB_TIMINGS-1, FLAGS, "timing" },
74 { "bpm", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_BPM}, 0, 0, FLAGS, "timing" },
75 { "ms", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_MS}, 0, 0, FLAGS, "timing" },
76 { "hz", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_HZ}, 0, 0, FLAGS, "timing" },
77 { "bpm", "set BPM", OFFSET(bpm), AV_OPT_TYPE_DOUBLE, {.dbl=120}, 30, 300, FLAGS },
78 { "ms", "set ms", OFFSET(ms), AV_OPT_TYPE_INT, {.i64=500}, 10, 2000, FLAGS },
79 { "hz", "set frequency", OFFSET(hertz), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0.01, 100, FLAGS },
83 AVFILTER_DEFINE_CLASS(apulsator);
85 static void lfo_advance(SimpleLFO *lfo, unsigned count)
87 lfo->phase = fabs(lfo->phase + count * lfo->freq / lfo->srate);
89 lfo->phase = fmod(lfo->phase, 1);
92 static double lfo_get_value(SimpleLFO *lfo)
94 double phs = FFMIN(100, lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
102 val = sin(phs * 2 * M_PI);
106 val = (phs - 0.75) * 4 - 1;
113 val = phs < 0.5 ? -1 : +1;
121 default: av_assert0(0);
124 return val * lfo->amount;
127 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
129 AVFilterContext *ctx = inlink->dst;
130 AVFilterLink *outlink = ctx->outputs[0];
131 AudioPulsatorContext *s = ctx->priv;
132 const double *src = (const double *)in->data[0];
133 const int nb_samples = in->nb_samples;
134 const double level_out = s->level_out;
135 const double level_in = s->level_in;
136 const double amount = s->amount;
141 if (av_frame_is_writable(in)) {
144 out = ff_get_audio_buffer(inlink, in->nb_samples);
147 return AVERROR(ENOMEM);
149 av_frame_copy_props(out, in);
151 dst = (double *)out->data[0];
153 for (n = 0; n < nb_samples; n++) {
156 double inL = src[0] * level_in;
157 double inR = src[1] * level_in;
161 procL *= lfo_get_value(&s->lfoL) * 0.5 + amount / 2;
162 procR *= lfo_get_value(&s->lfoR) * 0.5 + amount / 2;
164 outL = procL + inL * (1 - amount);
165 outR = procR + inR * (1 - amount);
173 lfo_advance(&s->lfoL, 1);
174 lfo_advance(&s->lfoR, 1);
183 return ff_filter_frame(outlink, out);
186 static int query_formats(AVFilterContext *ctx)
188 AVFilterChannelLayouts *layout = NULL;
189 AVFilterFormats *formats = NULL;
192 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
193 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
194 (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
195 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
198 formats = ff_all_samplerates();
199 return ff_set_common_samplerates(ctx, formats);
202 static int config_input(AVFilterLink *inlink)
204 AVFilterContext *ctx = inlink->dst;
205 AudioPulsatorContext *s = ctx->priv;
209 case UNIT_BPM: freq = s->bpm / 60; break;
210 case UNIT_MS: freq = 1 / (s->ms / 1000.); break;
211 case UNIT_HZ: freq = s->hertz; break;
212 default: av_assert0(0);
217 s->lfoL.mode = s->mode;
218 s->lfoR.mode = s->mode;
219 s->lfoL.offset = s->offset_l;
220 s->lfoR.offset = s->offset_r;
221 s->lfoL.srate = inlink->sample_rate;
222 s->lfoR.srate = inlink->sample_rate;
223 s->lfoL.amount = s->amount;
224 s->lfoR.amount = s->amount;
225 s->lfoL.pwidth = s->pwidth;
226 s->lfoR.pwidth = s->pwidth;
231 static const AVFilterPad inputs[] = {
234 .type = AVMEDIA_TYPE_AUDIO,
235 .config_props = config_input,
236 .filter_frame = filter_frame,
241 static const AVFilterPad outputs[] = {
244 .type = AVMEDIA_TYPE_AUDIO,
249 const AVFilter ff_af_apulsator = {
251 .description = NULL_IF_CONFIG_SMALL("Audio pulsator."),
252 .priv_size = sizeof(AudioPulsatorContext),
253 .priv_class = &apulsator_class,
254 .query_formats = query_formats,