Browse code

avfilter: add anoisesrc

Signed-off-by: Kyle Swanson <k@ylo.ph>
Signed-off-by: Paul B Mahol <onemda@gmail.com>

Kyle Swanson authored on 2015/11/08 20:39:37
Showing 6 changed files
... ...
@@ -32,6 +32,7 @@ version <next>:
32 32
 - Interplay ACM demuxer and audio decoder
33 33
 - XMA1 & XMA2 decoder
34 34
 - realtime filter
35
+- anoisesrc audio filter source
35 36
 
36 37
 
37 38
 version 2.8:
... ...
@@ -3157,6 +3157,46 @@ ffplay -f lavfi flite=text='No more be grieved for which that thou hast done.'
3157 3157
 For more information about libflite, check:
3158 3158
 @url{http://www.speech.cs.cmu.edu/flite/}
3159 3159
 
3160
+@section anoisesrc
3161
+
3162
+Generate a noise audio signal.
3163
+
3164
+The filter accepts the following options:
3165
+
3166
+@table @option
3167
+@item sample_rate, r
3168
+Specify the sample rate. Default value is 48000 Hz.
3169
+
3170
+@item amplitude, a
3171
+Specify the amplitude (0.0 - 1.0) of the generated audio stream. Default value
3172
+is 1.0.
3173
+
3174
+@item duration, d
3175
+Specify the duration of the generated audio stream. Not specifying this option
3176
+results in noise with an infinite length.
3177
+
3178
+@item color, colour, c
3179
+Specify the color of noise. Available noise colors are white, pink, and brown.
3180
+Default color is white.
3181
+
3182
+@item seed, s
3183
+Specify a value used to seed the PRNG.
3184
+
3185
+@item nb_samples, n
3186
+Set the number of samples per each output frame, default is 1024.
3187
+@end table
3188
+
3189
+@subsection Examples
3190
+
3191
+@itemize
3192
+
3193
+@item
3194
+Generate 60 seconds of pink noise, with a 44.1 kHz sampling rate and an amplitude of 0.5:
3195
+@example
3196
+anoisesrc=d=60:c=pink:r=44100:a=0.5
3197
+@end example
3198
+@end itemize
3199
+
3160 3200
 @section sine
3161 3201
 
3162 3202
 Generate an audio signal made of a sine wave with amplitude 1/8.
... ...
@@ -91,6 +91,7 @@ OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
91 91
 OBJS-$(CONFIG_VOLUMEDETECT_FILTER)           += af_volumedetect.o
92 92
 
93 93
 OBJS-$(CONFIG_AEVALSRC_FILTER)               += aeval.o
94
+OBJS-$(CONFIG_ANOISESRC_FILTER)              += asrc_anoisesrc.o
94 95
 OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
95 96
 OBJS-$(CONFIG_FLITE_FILTER)                  += asrc_flite.o
96 97
 OBJS-$(CONFIG_SINE_FILTER)                   += asrc_sine.o
... ...
@@ -113,6 +113,7 @@ void avfilter_register_all(void)
113 113
     REGISTER_FILTER(VOLUMEDETECT,   volumedetect,   af);
114 114
 
115 115
     REGISTER_FILTER(AEVALSRC,       aevalsrc,       asrc);
116
+    REGISTER_FILTER(ANOISESRC,      anoisesrc,      asrc);
116 117
     REGISTER_FILTER(ANULLSRC,       anullsrc,       asrc);
117 118
     REGISTER_FILTER(FLITE,          flite,          asrc);
118 119
     REGISTER_FILTER(SINE,           sine,           asrc);
119 120
new file mode 100644
... ...
@@ -0,0 +1,207 @@
0
+/*
1
+ * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
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 License
7
+ * 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
13
+ * GNU Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/opt.h"
21
+#include "audio.h"
22
+#include "avfilter.h"
23
+#include "internal.h"
24
+#include "libavutil/lfg.h"
25
+#include "libavutil/random_seed.h"
26
+
27
+typedef struct {
28
+    const AVClass *class;
29
+    int sample_rate;
30
+    double amplitude;
31
+    int64_t duration;
32
+    int64_t color;
33
+    int64_t seed;
34
+    int nb_samples;
35
+
36
+    int64_t pts;
37
+    int infinite;
38
+    double (*filter)(double white, double *buf);
39
+    double buf[7];
40
+    AVLFG c;
41
+} ANoiseSrcContext;
42
+
43
+#define OFFSET(x) offsetof(ANoiseSrcContext, x)
44
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
45
+
46
+static const AVOption anoisesrc_options[] = {
47
+    { "sample_rate",  "set sample rate",  OFFSET(sample_rate),  AV_OPT_TYPE_INT,       {.i64 = 48000},  15,  INT_MAX,    FLAGS },
48
+    { "r",            "set sample rate",  OFFSET(sample_rate),  AV_OPT_TYPE_INT,       {.i64 = 48000},  15,  INT_MAX,    FLAGS },
49
+    { "amplitude",    "set amplitude",    OFFSET(amplitude),    AV_OPT_TYPE_DOUBLE,    {.dbl = 1.},     0.,  1.,         FLAGS },
50
+    { "a",            "set amplitude",    OFFSET(amplitude),    AV_OPT_TYPE_DOUBLE,    {.dbl = 1.},     0.,  1.,         FLAGS },
51
+    { "duration",     "set duration",     OFFSET(duration),     AV_OPT_TYPE_DURATION,  {.i64 =  0},      0,  INT64_MAX,  FLAGS },
52
+    { "d",            "set duration",     OFFSET(duration),     AV_OPT_TYPE_DURATION,  {.i64 =  0},      0,  INT64_MAX,  FLAGS },
53
+    { "color",        "set noise color",  OFFSET(color),        AV_OPT_TYPE_INT,       {.i64 =  1},      0,  2,          FLAGS, "color" },
54
+    { "colour",       "set noise color",  OFFSET(color),        AV_OPT_TYPE_INT,       {.i64 =  1},      0,  2,          FLAGS, "color" },
55
+    { "c",            "set noise color",  OFFSET(color),        AV_OPT_TYPE_INT,       {.i64 =  0},      0,  2,          FLAGS, "color" },
56
+    {     "white",    0,                  0,                    AV_OPT_TYPE_CONST,     {.i64 =  0},      0,  0,          FLAGS, "color" },
57
+    {     "pink",     0,                  0,                    AV_OPT_TYPE_CONST,     {.i64 =  1},      0,  0,          FLAGS, "color" },
58
+    {     "brown",    0,                  0,                    AV_OPT_TYPE_CONST,     {.i64 =  2},      0,  0,          FLAGS, "color" },
59
+    { "seed",         "set random seed",  OFFSET(seed),         AV_OPT_TYPE_INT64,     {.i64 = -1},     -1,  UINT_MAX,   FLAGS },
60
+    { "s",            "set random seed",  OFFSET(seed),         AV_OPT_TYPE_INT64,     {.i64 = -1},     -1,  UINT_MAX,   FLAGS },
61
+    { "nb_samples",   "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
62
+    { "n",            "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
63
+    {NULL}
64
+};
65
+
66
+AVFILTER_DEFINE_CLASS(anoisesrc);
67
+
68
+static av_cold int query_formats(AVFilterContext *ctx)
69
+{
70
+    ANoiseSrcContext *s = ctx->priv;
71
+    static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
72
+    int sample_rates[] = { s->sample_rate, -1 };
73
+    static const enum AVSampleFormat sample_fmts[] = {
74
+        AV_SAMPLE_FMT_DBL,
75
+        AV_SAMPLE_FMT_NONE
76
+    };
77
+
78
+    AVFilterFormats *formats;
79
+    AVFilterChannelLayouts *layouts;
80
+    int ret;
81
+
82
+    formats = ff_make_format_list(sample_fmts);
83
+    if (!formats)
84
+        return AVERROR(ENOMEM);
85
+    ret = ff_set_common_formats (ctx, formats);
86
+    if (ret < 0)
87
+        return ret;
88
+
89
+    layouts = avfilter_make_format64_list(chlayouts);
90
+    if (!layouts)
91
+        return AVERROR(ENOMEM);
92
+    ret = ff_set_common_channel_layouts(ctx, layouts);
93
+    if (ret < 0)
94
+        return ret;
95
+
96
+    formats = ff_make_format_list(sample_rates);
97
+    if (!formats)
98
+        return AVERROR(ENOMEM);
99
+    return ff_set_common_samplerates(ctx, formats);
100
+}
101
+
102
+static double white_filter(double white, double *buf)
103
+{
104
+    return white;
105
+};
106
+
107
+static double pink_filter(double white, double *buf)
108
+{
109
+    double pink;
110
+
111
+    /* http://www.musicdsp.org/files/pink.txt */
112
+    buf[0] = 0.99886 * buf[0] + white * 0.0555179;
113
+    buf[1] = 0.99332 * buf[1] + white * 0.0750759;
114
+    buf[2] = 0.96900 * buf[2] + white * 0.1538520;
115
+    buf[3] = 0.86650 * buf[3] + white * 0.3104856;
116
+    buf[4] = 0.55000 * buf[4] + white * 0.5329522;
117
+    buf[5] = -0.7616 * buf[5] - white * 0.0168980;
118
+    pink = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362;
119
+    buf[6] = white * 0.115926;
120
+    return pink * 0.11;
121
+}
122
+
123
+static double brown_filter(double white, double *buf)
124
+{
125
+    double brown;
126
+
127
+    brown = ((0.02 * white) + buf[0]) / 1.02;
128
+    buf[0] = brown;
129
+    return brown * 3.5;
130
+}
131
+
132
+static av_cold int config_props(AVFilterLink *outlink)
133
+{
134
+    AVFilterContext *ctx = outlink->src;
135
+    ANoiseSrcContext *s = ctx->priv;
136
+
137
+    if (s->seed == -1)
138
+        s->seed = av_get_random_seed();
139
+    av_lfg_init(&s->c, s->seed);
140
+
141
+    if (s->duration == 0)
142
+        s->infinite = 1;
143
+    s->duration = av_rescale(s->duration, s->sample_rate, AV_TIME_BASE);
144
+
145
+    switch (s->color) {
146
+    case 0: s->filter = white_filter; break;
147
+    case 1: s->filter = pink_filter;  break;
148
+    case 2: s->filter = brown_filter; break;
149
+    }
150
+
151
+    return 0;
152
+}
153
+
154
+static int request_frame(AVFilterLink *outlink)
155
+{
156
+    AVFilterContext *ctx = outlink->src;
157
+    ANoiseSrcContext *s = ctx->priv;
158
+    AVFrame *frame;
159
+    int nb_samples, i;
160
+    double *dst;
161
+
162
+    if (!s->infinite && s->duration <= 0) {
163
+        return AVERROR_EOF;
164
+    } else if (!s->infinite && s->duration < s->nb_samples) {
165
+        nb_samples = s->duration;
166
+    } else {
167
+        nb_samples = s->nb_samples;
168
+    }
169
+
170
+    if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
171
+        return AVERROR(ENOMEM);
172
+
173
+    dst = (double *)frame->data[0];
174
+    for (i = 0; i < nb_samples; i++) {
175
+        double white;
176
+        white = s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1);
177
+        dst[i] = s->filter(white, s->buf);
178
+    }
179
+
180
+    if (!s->infinite)
181
+        s->duration -= nb_samples;
182
+
183
+    frame->pts = s->pts;
184
+    s->pts    += nb_samples;
185
+    return ff_filter_frame(outlink, frame);
186
+}
187
+
188
+static const AVFilterPad anoisesrc_outputs[] = {
189
+    {
190
+        .name          = "default",
191
+        .type          = AVMEDIA_TYPE_AUDIO,
192
+        .request_frame = request_frame,
193
+        .config_props  = config_props,
194
+    },
195
+    { NULL }
196
+};
197
+
198
+AVFilter ff_asrc_anoisesrc = {
199
+    .name          = "anoisesrc",
200
+    .description   = NULL_IF_CONFIG_SMALL("Generate a noise audio signal."),
201
+    .query_formats = query_formats,
202
+    .priv_size     = sizeof(ANoiseSrcContext),
203
+    .inputs        = NULL,
204
+    .outputs       = anoisesrc_outputs,
205
+    .priv_class    = &anoisesrc_class,
206
+};
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  14
34
-#define LIBAVFILTER_VERSION_MICRO 101
33
+#define LIBAVFILTER_VERSION_MINOR  15
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \