[FFmpeg-devel] [PATCH 2/2] lavfi: add sine audio source.

Stefano Sabatini stefasab at gmail.com
Sun Mar 17 20:24:43 CET 2013


On date Saturday 2013-03-16 16:44:06 +0100, Nicolas George encoded:
> 
> Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
> ---
>  Changelog                |    1 +
>  doc/filters.texi         |   51 +++++++++++
>  libavfilter/Makefile     |    1 +
>  libavfilter/allfilters.c |    1 +
>  libavfilter/asrc_sine.c  |  228 ++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/version.h    |    2 +-
>  6 files changed, 283 insertions(+), 1 deletion(-)
>  create mode 100644 libavfilter/asrc_sine.c
> 
> diff --git a/Changelog b/Changelog
> index 516b293..958634b 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -8,6 +8,7 @@ version <next>:
>    or vice versa
>  - support for Monkey's Audio versions from 3.93
>  - perms and aperms filters
> +- sine audio filter source
>  
>  
>  version 1.2:
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 0dbec8e..de16a2f 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -1653,6 +1653,57 @@ ffplay -f lavfi flite=text='No more be grieved for which that thou hast done.'
>  For more information about libflite, check:
>  @url{http://www.speech.cs.cmu.edu/flite/}
>  
> + at section sine
> +
> +Generate an audio signal made of a sine wave with amplitude 1/8.
> +
> +The audio signal is bit-exact.
> +
> +It accepts a list of options in the form of @var{key}=@var{value} pairs
> +separated by ":". If the option name is omitted, the first option is the
> +frequency and the second option is the beep factor.
> +
> +The supported options are:
> +
> + at table @option
> +
> + at item frequency, f
> +Set the carrier frequency. Default is 440 Hz.
> +
> + at item beep_factor, b
> +Enable a periodic beep every second with frequency @var{beep_factor} times
> +the carrier frequency. Default is 0, meaning the beep is disabled.
> +
> + at item sample_rate, s
> +Specify the sample rate, default is 44100.
> +
> + at item duration, d
> +Specify the duration.

... of the generated audio stream.

> +
> + at item samples_per_frame
> +Set the number of samples per output frame, default is 1024.
> + at end table
> +
> + at subsection Examples
> +
> + at itemize
> +
> + at item
> +Generate a simple 440 Hz sine wave:
> + at example
> +sine
> + at end example
> +
> + at item
> +Generate a 220 Hz sine wave with a 880 Hz beep each second, for 5 seconds:
> + at example
> +sine=220:4:d=5
> +sine=f=220:b=4:d=5
> +sine=frequency=220:beep_factor=4:duration=5
> + at end example
> +
> + at end itemize
> +
>  @c man end AUDIO SOURCES
>  
>  @chapter Audio Sinks
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 2758c8e..b73569e 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -90,6 +90,7 @@ OBJS-$(CONFIG_VOLUMEDETECT_FILTER)           += af_volumedetect.o
>  OBJS-$(CONFIG_AEVALSRC_FILTER)               += asrc_aevalsrc.o
>  OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
>  OBJS-$(CONFIG_FLITE_FILTER)                  += asrc_flite.o
> +OBJS-$(CONFIG_SINE_FILTER)                   += asrc_sine.o
>  
>  OBJS-$(CONFIG_ANULLSINK_FILTER)              += asink_anullsink.o
>  
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 086e6c9..45a67e5 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -86,6 +86,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER(AEVALSRC,       aevalsrc,       asrc);
>      REGISTER_FILTER(ANULLSRC,       anullsrc,       asrc);
>      REGISTER_FILTER(FLITE,          flite,          asrc);
> +    REGISTER_FILTER(SINE,           sine,           asrc);
>  
>      REGISTER_FILTER(ANULLSINK,      anullsink,      asink);
>  
> diff --git a/libavfilter/asrc_sine.c b/libavfilter/asrc_sine.c
> new file mode 100644
> index 0000000..f2dcae9
> --- /dev/null
> +++ b/libavfilter/asrc_sine.c
> @@ -0,0 +1,228 @@
> +/*
> + * Copyright (c) 2013 Nicolas George
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public License
> + * as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public License
> + * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "libavutil/avassert.h"
> +#include "libavutil/channel_layout.h"
> +#include "libavutil/opt.h"
> +#include "audio.h"
> +#include "avfilter.h"
> +#include "internal.h"
> +
> +typedef struct {
> +    const AVClass *class;
> +    double frequency;
> +    double beep_factor;
> +    int samples_per_frame;
> +    int sample_rate;
> +    int64_t duration;
> +    int16_t *sin;
> +    int64_t pts;

> +    uint32_t phi;
> +    uint32_t dphi;

doxy?

> +    unsigned beep_period;
> +    unsigned beep_index;
> +    unsigned beep_length;

> +    uint32_t phi_beep;
> +    uint32_t dphi_beep;

doxy?

> +} SineContext;
> +
> +#define CONTEXT SineContext
> +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
> +

> +#define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
> +    { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type,          \
> +      { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
> +
> +#define OPT_INT(name, field, def, min, max, descr, ...) \
> +    OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
> +
> +#define OPT_DBL(name, field, def, min, max, descr, ...) \
> +    OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__)
> +
> +#define OPT_DUR(name, field, def, min, max, descr, ...) \
> +    OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__)

a bit overkill? But we may move the helpers to an internal header.

> +
> +static const AVOption sine_options[]= {

nit+++: sine_options[]_= {

[...]
-- 
FFmpeg = Fostering and Fantastic Marvellous Peaceful EnGine


More information about the ffmpeg-devel mailing list