Browse code

avfilter: add audio pulsator filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2015/11/29 03:50:32
Showing 6 changed files
... ...
@@ -37,6 +37,7 @@ version <next>:
37 37
 - compensationdelay filter
38 38
 - acompressor filter
39 39
 - support encoding 16-bit RLE SGI images
40
+- apulsator filter
40 41
 
41 42
 
42 43
 version 2.8:
... ...
@@ -1030,6 +1030,63 @@ It accepts the following values:
1030 1030
 @end table
1031 1031
 @end table
1032 1032
 
1033
+@section apulsator
1034
+
1035
+Audio pulsator is something between an autopanner and a tremolo.
1036
+But it can produce funny stereo effects as well. Pulsator changes the volume
1037
+of the left and right channel based on a LFO (low frequency oscillator) with
1038
+different waveforms and shifted phases.
1039
+This filter have the ability to define an offset between left and right
1040
+channel. An offset of 0 means that both LFO shapes match each other.
1041
+The left and right channel are altered equally - a conventional tremolo.
1042
+An offset of 50% means that the shape of the right channel is exactly shifted
1043
+in phase (or moved backwards about half of the frequency) - pulsator acts as
1044
+an autopanner. At 1 both curves match again. Every setting in between moves the
1045
+phase shift gapless between all stages and produces some "bypassing" sounds with
1046
+sine and triangle waveforms. The more you set the offset near 1 (starting from
1047
+the 0.5) the faster the signal passes from the left to the right speaker.
1048
+
1049
+The filter accepts the following options:
1050
+
1051
+@table @option
1052
+@item level_in
1053
+Set input gain. By default it is 1. Range is [0.015625 - 64].
1054
+
1055
+@item level_out
1056
+Set output gain. By default it is 1. Range is [0.015625 - 64].
1057
+
1058
+@item mode
1059
+Set waveform shape the LFO will use. Can be one of: sine, triangle, square,
1060
+sawup or sawdown. Default is sine.
1061
+
1062
+@item amount
1063
+Set modulation. Define how much of original signal is affected by the LFO.
1064
+
1065
+@item offset_l
1066
+Set left channel offset. Default is 0. Allowed range is [0 - 1].
1067
+
1068
+@item offset_r
1069
+Set right channel offset. Default is 0.5. Allowed range is [0 - 1].
1070
+
1071
+@item width
1072
+Set pulse width. Default is 1. Allowed range is [0 - 2].
1073
+
1074
+@item timing
1075
+Set possible timing mode. Can be one of: bpm, ms or hz. Default is hz.
1076
+
1077
+@item bpm
1078
+Set bpm. Default is 120. Allowed range is [30 - 300]. Only used if timing
1079
+is set to bpm.
1080
+
1081
+@item ms
1082
+Set ms. Default is 500. Allowed range is [10 - 2000]. Only used if timing
1083
+is set to ms.
1084
+
1085
+@item hz
1086
+Set frequency in Hz. Default is 2. Allowed range is [0.01 - 100]. Only used
1087
+if timing is set to hz.
1088
+@end table
1089
+
1033 1090
 @anchor{aresample}
1034 1091
 @section aresample
1035 1092
 
... ...
@@ -40,6 +40,7 @@ OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
40 40
 OBJS-$(CONFIG_APAD_FILTER)                   += af_apad.o
41 41
 OBJS-$(CONFIG_APERMS_FILTER)                 += f_perms.o
42 42
 OBJS-$(CONFIG_APHASER_FILTER)                += af_aphaser.o generate_wave_table.o
43
+OBJS-$(CONFIG_APULSATOR_FILTER)              += af_apulsator.o
43 44
 OBJS-$(CONFIG_AREALTIME_FILTER)              += f_realtime.o
44 45
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
45 46
 OBJS-$(CONFIG_AREVERSE_FILTER)               += f_reverse.o
46 47
new file mode 100644
... ...
@@ -0,0 +1,254 @@
0
+/*
1
+ * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/opt.h"
21
+#include "avfilter.h"
22
+#include "internal.h"
23
+#include "audio.h"
24
+
25
+enum PulsatorModes { SINE, TRIANGLE, SQUARE, SAWUP, SAWDOWN, NB_MODES };
26
+enum PulsatorTimings { UNIT_BPM, UNIT_MS, UNIT_HZ, NB_TIMINGS };
27
+
28
+typedef struct SimpleLFO {
29
+    double phase;
30
+    double freq;
31
+    double offset;
32
+    double amount;
33
+    double pwidth;
34
+    int mode;
35
+    int srate;
36
+} SimpleLFO;
37
+
38
+typedef struct AudioPulsatorContext {
39
+    const AVClass *class;
40
+    int mode;
41
+    double level_in;
42
+    double level_out;
43
+    double amount;
44
+    double offset_l;
45
+    double offset_r;
46
+    double pwidth;
47
+    double bpm;
48
+    double hz;
49
+    int ms;
50
+    int timing;
51
+
52
+    SimpleLFO lfoL, lfoR;
53
+} AudioPulsatorContext;
54
+
55
+#define OFFSET(x) offsetof(AudioPulsatorContext, x)
56
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57
+
58
+static const AVOption apulsator_options[] = {
59
+    { "level_in",   "set input gain", OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
60
+    { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
61
+    { "mode",             "set mode", OFFSET(mode),      AV_OPT_TYPE_INT,    {.i64=SINE}, SINE,   NB_MODES-1, FLAGS, "mode" },
62
+    {   "sine",                 NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=SINE},    0,            0, FLAGS, "mode" },
63
+    {   "triangle",             NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=TRIANGLE},0,            0, FLAGS, "mode" },
64
+    {   "square",               NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=SQUARE},  0,            0, FLAGS, "mode" },
65
+    {   "sawup",                NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=SAWUP},   0,            0, FLAGS, "mode" },
66
+    {   "sawdown",              NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=SAWDOWN}, 0,            0, FLAGS, "mode" },
67
+    { "amount",     "set modulation", OFFSET(amount),    AV_OPT_TYPE_DOUBLE, {.dbl=1},       0,            1, FLAGS },
68
+    { "offset_l",     "set offset L", OFFSET(offset_l),  AV_OPT_TYPE_DOUBLE, {.dbl=0},       0,            1, FLAGS },
69
+    { "offset_r",     "set offset R", OFFSET(offset_r),  AV_OPT_TYPE_DOUBLE, {.dbl=.5},      0,            1, FLAGS },
70
+    { "width",     "set pulse width", OFFSET(pwidth),    AV_OPT_TYPE_DOUBLE, {.dbl=1},       0,            2, FLAGS },
71
+    { "timing",         "set timing", OFFSET(timing),    AV_OPT_TYPE_INT,    {.i64=2},       0, NB_TIMINGS-1, FLAGS, "timing" },
72
+    {   "bpm",                  NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=UNIT_BPM},  0,          0, FLAGS, "timing" },
73
+    {   "ms",                   NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=UNIT_MS},   0,          0, FLAGS, "timing" },
74
+    {   "hz",                   NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=UNIT_HZ},   0,          0, FLAGS, "timing" },
75
+    { "bpm",               "set BPM", OFFSET(bpm),       AV_OPT_TYPE_DOUBLE, {.dbl=120},    30,          300, FLAGS },
76
+    { "ms",                 "set ms", OFFSET(ms),        AV_OPT_TYPE_INT,    {.i64=500},    10,         2000, FLAGS },
77
+    { "hz",          "set frequency", OFFSET(hz),        AV_OPT_TYPE_DOUBLE, {.dbl=2},    0.01,          100, FLAGS },
78
+    { NULL }
79
+};
80
+
81
+AVFILTER_DEFINE_CLASS(apulsator);
82
+
83
+static void lfo_advance(SimpleLFO *lfo, unsigned count)
84
+{
85
+    lfo->phase = fabs(lfo->phase + count * lfo->freq / lfo->srate);
86
+    if (lfo->phase >= 1)
87
+        lfo->phase = fmod(lfo->phase, 1);
88
+}
89
+
90
+static double lfo_get_value(SimpleLFO *lfo)
91
+{
92
+    double phs = FFMIN(100, lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
93
+    double val;
94
+
95
+    if (phs > 1)
96
+        phs = fmod(phs, 1.);
97
+
98
+    switch (lfo->mode) {
99
+    case SINE:
100
+        val = sin(phs * 2 * M_PI);
101
+        break;
102
+    case TRIANGLE:
103
+        if (phs > 0.75)
104
+            val = (phs - 0.75) * 4 - 1;
105
+        else if (phs > 0.25)
106
+            val = -4 * phs + 2;
107
+        else
108
+            val = phs * 4;
109
+        break;
110
+    case SQUARE:
111
+        val = phs < 0.5 ? -1 : +1;
112
+        break;
113
+    case SAWUP:
114
+        val = phs * 2 - 1;
115
+        break;
116
+    case SAWDOWN:
117
+        val = 1 - phs * 2;
118
+        break;
119
+    }
120
+
121
+    return val * lfo->amount;
122
+}
123
+
124
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
125
+{
126
+    AVFilterContext *ctx = inlink->dst;
127
+    AVFilterLink *outlink = ctx->outputs[0];
128
+    AudioPulsatorContext *s = ctx->priv;
129
+    const double *src = (const double *)in->data[0];
130
+    const int nb_samples = in->nb_samples;
131
+    const double level_out = s->level_out;
132
+    const double level_in = s->level_in;
133
+    const double amount = s->amount;
134
+    AVFrame *out;
135
+    double *dst;
136
+    int n;
137
+
138
+    if (av_frame_is_writable(in)) {
139
+        out = in;
140
+    } else {
141
+        out = ff_get_audio_buffer(inlink, in->nb_samples);
142
+        if (!out) {
143
+            av_frame_free(&in);
144
+            return AVERROR(ENOMEM);
145
+        }
146
+        av_frame_copy_props(out, in);
147
+    }
148
+    dst = (double *)out->data[0];
149
+
150
+    for (n = 0; n < nb_samples; n++) {
151
+        double outL;
152
+        double outR;
153
+        double inL = src[0] * level_in;
154
+        double inR = src[1] * level_in;
155
+        double procL = inL;
156
+        double procR = inR;
157
+
158
+        procL *= lfo_get_value(&s->lfoL) * 0.5 + amount / 2;
159
+        procR *= lfo_get_value(&s->lfoR) * 0.5 + amount / 2;
160
+
161
+        outL = procL + inL * (1 - amount);
162
+        outR = procR + inR * (1 - amount);
163
+
164
+        outL *= level_out;
165
+        outR *= level_out;
166
+
167
+        dst[0] = outL;
168
+        dst[1] = outR;
169
+
170
+        lfo_advance(&s->lfoL, 1);
171
+        lfo_advance(&s->lfoR, 1);
172
+
173
+        dst += 2;
174
+        src += 2;
175
+    }
176
+
177
+    if (in != out)
178
+        av_frame_free(&in);
179
+
180
+    return ff_filter_frame(outlink, out);
181
+}
182
+
183
+static int query_formats(AVFilterContext *ctx)
184
+{
185
+    AVFilterChannelLayouts *layout = NULL;
186
+    AVFilterFormats *formats = NULL;
187
+    int ret;
188
+
189
+    if ((ret = ff_add_format                 (&formats, AV_SAMPLE_FMT_DBL  )) < 0 ||
190
+        (ret = ff_set_common_formats         (ctx     , formats            )) < 0 ||
191
+        (ret = ff_add_channel_layout         (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
192
+        (ret = ff_set_common_channel_layouts (ctx     , layout             )) < 0)
193
+        return ret;
194
+
195
+    formats = ff_all_samplerates();
196
+    return ff_set_common_samplerates(ctx, formats);
197
+}
198
+
199
+static int config_input(AVFilterLink *inlink)
200
+{
201
+    AVFilterContext *ctx = inlink->dst;
202
+    AudioPulsatorContext *s = ctx->priv;
203
+    double freq;
204
+
205
+    switch (s->timing) {
206
+    case UNIT_BPM:  freq = s->bpm / 60;         break;
207
+    case UNIT_MS:   freq = 1 / (s->ms / 1000.); break;
208
+    case UNIT_HZ:   freq = s->hz;               break;
209
+    }
210
+
211
+    s->lfoL.freq   = freq;
212
+    s->lfoR.freq   = freq;
213
+    s->lfoL.mode   = s->mode;
214
+    s->lfoR.mode   = s->mode;
215
+    s->lfoL.offset = s->offset_l;
216
+    s->lfoR.offset = s->offset_r;
217
+    s->lfoL.srate  = inlink->sample_rate;
218
+    s->lfoR.srate  = inlink->sample_rate;
219
+    s->lfoL.amount = s->amount;
220
+    s->lfoR.amount = s->amount;
221
+    s->lfoL.pwidth = s->pwidth;
222
+    s->lfoR.pwidth = s->pwidth;
223
+
224
+    return 0;
225
+}
226
+
227
+static const AVFilterPad inputs[] = {
228
+    {
229
+        .name         = "default",
230
+        .type         = AVMEDIA_TYPE_AUDIO,
231
+        .config_props = config_input,
232
+        .filter_frame = filter_frame,
233
+    },
234
+    { NULL }
235
+};
236
+
237
+static const AVFilterPad outputs[] = {
238
+    {
239
+        .name = "default",
240
+        .type = AVMEDIA_TYPE_AUDIO,
241
+    },
242
+    { NULL }
243
+};
244
+
245
+AVFilter ff_af_apulsator = {
246
+    .name          = "apulsator",
247
+    .description   = NULL_IF_CONFIG_SMALL("Audio pulsator."),
248
+    .priv_size     = sizeof(AudioPulsatorContext),
249
+    .priv_class    = &apulsator_class,
250
+    .query_formats = query_formats,
251
+    .inputs        = inputs,
252
+    .outputs       = outputs,
253
+};
... ...
@@ -62,6 +62,7 @@ void avfilter_register_all(void)
62 62
     REGISTER_FILTER(APAD,           apad,           af);
63 63
     REGISTER_FILTER(APERMS,         aperms,         af);
64 64
     REGISTER_FILTER(APHASER,        aphaser,        af);
65
+    REGISTER_FILTER(APULSATOR,      apulsator,      af);
65 66
     REGISTER_FILTER(AREALTIME,      arealtime,      af);
66 67
     REGISTER_FILTER(ARESAMPLE,      aresample,      af);
67 68
     REGISTER_FILTER(AREVERSE,       areverse,       af);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  17
33
+#define LIBAVFILTER_VERSION_MINOR  18
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \