diff mbox series

[FFmpeg-devel] GSoC 2020 Audio Tones Source Filter

Message ID 20200316080458.8466-1-ashutoshp012345@gmail.com
State New
Headers show
Series [FFmpeg-devel] GSoC 2020 Audio Tones Source Filter
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Ashutosh Pradhan March 16, 2020, 8:04 a.m. UTC
Generate random legato tones using fluidsynth api.
diff mbox series

Patch

diff --git a/Changelog b/Changelog
index d1572553a5..5ddd2484b0 100644
--- a/Changelog
+++ b/Changelog
@@ -48,6 +48,7 @@  version <next>:
 - AMQP 0-9-1 protocol (RabbitMQ)
 - Vulkan support
 - avgblur_vulkan, overlay_vulkan, scale_vulkan and chromaber_vulkan filters
+- atone filter
 
 
 version 4.2:
diff --git a/configure b/configure
index 6ceb0c7af4..2ec7e377f3 100755
--- a/configure
+++ b/configure
@@ -233,6 +233,7 @@  External library support:
                            and libraw1394 [no]
   --enable-libfdk-aac      enable AAC de/encoding via libfdk-aac [no]
   --enable-libflite        enable flite (voice synthesis) support via libflite [no]
+  --enable-libfluidsynth   enable libfluidsynth support for fluidsynth [no]
   --enable-libfontconfig   enable libfontconfig, useful for drawtext filter [no]
   --enable-libfreetype     enable libfreetype, needed for drawtext filter [no]
   --enable-libfribidi      enable libfribidi, improves drawtext filter [no]
@@ -1770,6 +1771,7 @@  EXTERNAL_LIBRARY_LIST="
     libdc1394
     libdrm
     libflite
+    libfluidsynth
     libfontconfig
     libfreetype
     libfribidi
@@ -3465,6 +3467,7 @@  asr_filter_deps="pocketsphinx"
 ass_filter_deps="libass"
 atempo_filter_deps="avcodec"
 atempo_filter_select="rdft"
+atone_filter_deps="libfluidsynth"
 avgblur_opencl_filter_deps="opencl"
 avgblur_vulkan_filter_deps="vulkan libglslang"
 azmq_filter_deps="libzmq"
@@ -6270,6 +6273,7 @@  enabled libfdk_aac        && { check_pkg_config libfdk_aac fdk-aac "fdk-aac/aace
                                  warn "using libfdk without pkg-config"; } }
 flite_extralibs="-lflite_cmu_time_awb -lflite_cmu_us_awb -lflite_cmu_us_kal -lflite_cmu_us_kal16 -lflite_cmu_us_rms -lflite_cmu_us_slt -lflite_usenglish -lflite_cmulex -lflite"
 enabled libflite          && require libflite "flite/flite.h" flite_init $flite_extralibs
+enabled libfluidsynth     && require_pkg_config libfluidsynth fluidsynth "fluidsynth.h" fluid_log
 enabled fontconfig        && enable libfontconfig
 enabled libfontconfig     && require_pkg_config libfontconfig fontconfig "fontconfig/fontconfig.h" FcInit
 enabled libfreetype       && require_pkg_config libfreetype freetype2 "ft2build.h FT_FREETYPE_H" FT_Init_FreeType
diff --git a/doc/filters.texi b/doc/filters.texi
index 328e984e92..4985b29149 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -6050,6 +6050,51 @@  anoisesrc=d=60:c=pink:r=44100:a=0.5
 @end example
 @end itemize
 
+@section atone
+
+Generate random legato tones using Fluidsynth API.
+To compile filter configure ffmpeg with @code{--enable-libfluidsynth} 
+
+The filter accepts the following options:
+
+@table @option
+@item sample_rate, r
+Specify the sample rate. Default value is 44100 Hz.
+
+@item sfont
+Specify the location of soundfont file. Default value is 
+"/usr/share/sounds/sf2/FluidR3_GM.sf2"(for linux).
+
+@item duration, d
+Specify the duration of the generated audio stream. Not specifying this option
+results in playing tones for infinite length.
+
+@item velocity, v
+Specify the velocity of key press. Default value is 80.
+
+@item MIDI_channel
+Specify a the MIDI channel number between(0 to 16) to play tones. Default is 0.
+
+@item tone_change_interval, t
+Specify a the time interval between successive tones in seconds. Default is 0.25s.
+
+@item samples_per_frame
+Set the number of samples per each output frame. Default is 1024.
+@end table
+
+@subsection Examples
+
+@itemize
+
+@item
+Generate 10 seconds of random tones, with a key velocity of 100, midi channel 1
+and an tone change interval of 0.1s:
+@example
+atone=d=10:MIDI_channel=1:v=100:t=0.1:sfont="example.sf2"
+atone=duration=10:MIDI_channel=1:velocity=100:tone_change_interval=0.1:sfont="example.sf2"
+@end example
+@end itemize
+
 @section hilbert
 
 Generate odd-tap Hilbert transform FIR coefficients.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 750412da6b..b1f0c4be35 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -151,6 +151,7 @@  OBJS-$(CONFIG_FLITE_FILTER)                  += asrc_flite.o
 OBJS-$(CONFIG_HILBERT_FILTER)                += asrc_hilbert.o
 OBJS-$(CONFIG_SINC_FILTER)                   += asrc_sinc.o
 OBJS-$(CONFIG_SINE_FILTER)                   += asrc_sine.o
+OBJS-$(CONFIG_ATONE_FILTER)                  += asrc_atone.o
 
 OBJS-$(CONFIG_ANULLSINK_FILTER)              += asink_anullsink.o
 
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 501e5d041b..4d3efc7e15 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -145,6 +145,7 @@  extern AVFilter ff_asrc_flite;
 extern AVFilter ff_asrc_hilbert;
 extern AVFilter ff_asrc_sinc;
 extern AVFilter ff_asrc_sine;
+extern AVFilter ff_asrc_atone;
 
 extern AVFilter ff_asink_anullsink;
 
diff --git a/libavfilter/asrc_atone.c b/libavfilter/asrc_atone.c
new file mode 100644
index 0000000000..5588e2a3f5
--- /dev/null
+++ b/libavfilter/asrc_atone.c
@@ -0,0 +1,250 @@ 
+/*
+ * 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 <float.h>
+#include <stdio.h>
+#include <string.h>
+#include <fluidsynth.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/eval.h"
+#include "libavutil/opt.h"
+#include "libavutil/lfg.h"
+#include "libavutil/random_seed.h"
+#include "audio.h"
+#include "avfilter.h"
+#include "internal.h"
+
+typedef struct AtoneContext
+{
+    const AVClass* class;
+    int64_t duration;
+    int nb_samples;
+    int sample_rate;
+    int64_t pts;
+    int infinite;
+
+    fluid_settings_t* settings;
+    fluid_synth_t* synth;
+    char* sfont;                      ///< soundfont file
+    int sfont_id;
+    int midi_chan;                   ///< midi channel number
+    int velocity;                   ///< velocity of key
+    double changerate;              ///< get the time interval of changing tones
+    float* left;                   ///< store the left buffer
+    float* right;                  ///< store the right buffer
+    AVLFG key;                  
+}AtoneContext;
+
+#define CONTEXT AtoneContext
+#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__)
+
+#define OPT_STR(name, field, def, min, max, descr, ...) \
+    OPT_GENERIC(name, field, def, min, max, descr, STRING, str, __VA_ARGS__)
+
+static const AVOption atone_options[] = {
+    OPT_INT("velocity",          velocity,       80,                                          0, INT_MAX,             "set the velocity of key press",),
+    OPT_INT("v",                 velocity,       80,                                          0, INT_MAX,             "set the velocity of key press",),
+    OPT_INT("sample_rate",       sample_rate,    44100,                                       1, INT_MAX,             "set the sample rate",),
+    OPT_INT("r",                 sample_rate,    44100,                                       1, INT_MAX,             "set the sample rate",),
+    OPT_DUR("duration",          duration,       0,                                           0, INT64_MAX,           "set the audio duration",),
+    OPT_DUR("d",                 duration,       0,                                           0, INT64_MAX,           "set the audio duration",),
+    OPT_STR("sfont",             sfont,          "/usr/share/sounds/sf2/FluidR3_GM.sf2",      0, 0,                   "set the soundfont file",),
+    OPT_INT("samples_per_frame", nb_samples,     1024,                                        0, INT_MAX,             "set the number of samples per frame",),
+    OPT_INT("MIDI_channel",      midi_chan,      0,                                           0, 16,                  "set the MIDI Channel",),
+    OPT_DBL("tone_change_interval",changerate,   0.25,                                         0, DBL_MAX,             "set the random tone change time in seconds",),
+    OPT_DBL("t",                 changerate,     0.25,                                         0, DBL_MAX,             "set the random tone change time in seconds",),
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(atone);
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    AtoneContext *s = ctx->priv;
+    /*Initialise the fluidsynth settings object followed by synthesizer*/
+    s->settings = new_fluid_settings();
+    if (s->settings== NULL){
+        av_log(s, AV_LOG_ERROR, "Failed to create the fluidsynth settings");
+        return AVERROR_EXTERNAL;
+    }
+    
+    s->synth = new_fluid_synth(s->settings);
+    if (s->synth== NULL){
+        av_log(s, AV_LOG_ERROR, "Failed to create the fluidsynth synth");
+        return AVERROR_EXTERNAL;
+    }
+
+    s->sfont_id= fluid_synth_sfload(s->synth, s->sfont, 1);
+    if(s->sfont_id== FLUID_FAILED){
+        av_log(s, AV_LOG_ERROR, "Loading the Soundfont Failed");
+        return AVERROR_EXTERNAL;
+    }
+
+    if (!(s->left = av_malloc(sizeof(*s->left)*(s->nb_samples))))
+        return AVERROR(ENOMEM);
+
+    if (!(s->right = av_malloc(sizeof(*s->right)*(s->nb_samples))))
+        return AVERROR(ENOMEM);
+
+    av_lfg_init(&s->key, av_get_random_seed());
+    s->changerate= s->changerate*s->sample_rate/s->nb_samples;
+    if (s->changerate<1.0)
+        s->changerate= 1.0;
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    AtoneContext *s = ctx->priv;
+
+    delete_fluid_synth(s->synth);
+    delete_fluid_settings(s->settings);
+    av_freep(&s->left);
+    av_freep(&s->right);
+}
+
+static av_cold int config_props(AVFilterLink *outlink)
+{
+    AtoneContext *s = outlink->src->priv;
+
+    if (s->duration==0)
+        s->infinite=1;
+    s->duration = av_rescale(s->duration, s->sample_rate, AV_TIME_BASE);
+    return 0;
+}
+
+static void gen_random_tone_samples(AtoneContext* s, int buf_size, int key){
+    
+    fluid_synth_noteon(s->synth, s->midi_chan, key, s->velocity);
+    fluid_synth_write_float(s->synth, buf_size, s->left, 0, 1, s->right, 0, 1);
+    fluid_synth_noteoff(s->synth, s->midi_chan, key);
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AtoneContext *s = ctx->priv;
+    AVFrame *frame;
+    int nb_samples, i;
+    unsigned key;
+    float *sample;
+    
+    if (!s->infinite && s->duration <= 0) {
+        return AVERROR_EOF;
+    } else if (!s->infinite && s->duration < s->nb_samples) {
+        nb_samples = s->duration;
+    } else {
+        nb_samples = s->nb_samples;
+    }
+
+    if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
+        return AVERROR(ENOMEM);
+    
+      
+    if (s->pts % ((int)(s->changerate)*nb_samples) == 0){
+       key= av_lfg_get(&s->key);
+    }
+
+    gen_random_tone_samples(s, nb_samples, key%128);
+    sample = (float *)frame->data[0];
+    for (i = 0; i < nb_samples; i++) {
+        sample[2*i]= s->left[i];
+        sample[2*i+1]= s->right[i];
+    }
+
+    if (!s->infinite)
+        s->duration -= nb_samples;
+
+    frame->pts = s->pts;
+    s->pts    += nb_samples;
+    return ff_filter_frame(outlink, frame);
+}
+
+static av_cold int query_formats(AVFilterContext *ctx)
+{
+    AtoneContext *s = ctx->priv;
+    static const int64_t chlayouts[] = { AV_CH_LAYOUT_STEREO, -1 };
+    int sample_rates[] = { s->sample_rate, -1 };
+    static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE};
+    AVFilterFormats *formats;
+    AVFilterChannelLayouts *layouts;
+    int ret;
+
+    formats = ff_make_format_list(sample_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    ret = ff_set_common_formats (ctx, formats);
+    if (ret < 0)
+        return ret;
+
+    layouts = avfilter_make_format64_list(chlayouts);
+    if (!layouts)
+        return AVERROR(ENOMEM);
+    ret = ff_set_common_channel_layouts(ctx, layouts);
+    if (ret < 0)
+        return ret;
+
+    formats = ff_make_format_list(sample_rates);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    return ff_set_common_samplerates(ctx, formats);
+}
+
+
+static const AVFilterPad atone_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_AUDIO,
+        .request_frame = request_frame,
+        .config_props  = config_props,
+    },
+    { NULL }
+};
+
+AVFilter ff_asrc_atone = {
+    .name          = "atone",
+    .description   = NULL_IF_CONFIG_SMALL("Generate Random Tones."),
+    .query_formats = query_formats,
+    .init          = init,
+    .uninit        = uninit,
+    .priv_size     = sizeof(AtoneContext),
+    .inputs        = NULL,
+    .outputs       = atone_outputs,
+    .priv_class    = &atone_class,
+};
+
+
+
+
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 7b41018be7..4c4e8afe2d 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@ 
 #include "libavutil/version.h"
 
 #define LIBAVFILTER_VERSION_MAJOR   7
-#define LIBAVFILTER_VERSION_MINOR  77
+#define LIBAVFILTER_VERSION_MINOR  78
 #define LIBAVFILTER_VERSION_MICRO 100