From patchwork Sat Mar 14 12:16:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ashutosh Pradhan X-Patchwork-Id: 18190 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id BDF0D449E18 for ; Sat, 14 Mar 2020 14:16:36 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8D03B68B492; Sat, 14 Mar 2020 14:16:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 414A568AABF for ; Sat, 14 Mar 2020 14:16:29 +0200 (EET) Received: by mail-pg1-f172.google.com with SMTP id x7so6646538pgh.5 for ; Sat, 14 Mar 2020 05:16:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CGdFw+9IIGYp5sBSdZFlSQ0vTXNQvSx6hHjmLFBeoO8=; b=Lms3qIE88JzL4D8Y+1zEWlpS3EkcEtC8eX3SCJCR6teycvlRV6EpL+FZ2sF4jHz9VR O/yOGxNXMy9ah3OQiCZiCUTmHL52eRPBMwb8luaGNq4Cmqv6CWaaGQetoVDXJbgRDdwq +bZL+D6ciQ5R+V1VsMcTWyPhMoJWr5jvgmCpaisqcL2SoVvQMP8AyNGcixbpEmPwFMR4 RgVATQBrUkG8MHBerxbRzXG+XNwwRKF9/TcZ+ei+ejWSIvlO81/VH5AzaCqiP4mW2x3W wVvglYIpHhb/k109uj81vV/dZp4H/7t7eXHViKzU4/OzNWU7bTGZVjOMdd0iNJhN1ZYS /Oeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CGdFw+9IIGYp5sBSdZFlSQ0vTXNQvSx6hHjmLFBeoO8=; b=hLx++mJ5XpWkmpns/qYNbh+L3rkr8YkR1nfqEF8Vf7UsXiLZtrMOEmbOTe7EV0EExA lmOV9rvfKUKcaUrQtyD2pOpgaZY7lnsM79YHARir6TBhozuyI36pod7FUGFCWKBpF9/J JhLh9YqTpazuLRm8ls8q9LurpISlk77X6kRyOszkWV6/yhmYJ/qWaCb9g4xNmH8l4YW/ Gh3d/9dfzlbEf7CX6DM3XOPSLfm7AE3I83byuGjql9E7k15Vyt8seCVCjUjtzZd7t/Bd +Dg2U5V8T/6DTypy67aMfyVCNq25R3Xr/wU3uvcr/T2A9EF1vZ7nMV0U5x+MVqyFIDON fx/Q== X-Gm-Message-State: ANhLgQ1Xc1kRyVNsPQQBcd6kAfV7c4v7KPDxZE0kppTXbMluw3+My3QK J0oSm/D6MwiI8WYdVsrB/VJvhzF2 X-Google-Smtp-Source: ADFU+vtkCFkYBYxLK+UvELLBTXcfE9PVV/sOFP1TGoC7f1hRuOE3ZW/dyt0TgqeIC4l+HriLl8sBWg== X-Received: by 2002:a62:1513:: with SMTP id 19mr18660947pfv.85.1584188186648; Sat, 14 Mar 2020 05:16:26 -0700 (PDT) Received: from localhost.localdomain ([223.176.47.235]) by smtp.gmail.com with ESMTPSA id c9sm13652602pjr.47.2020.03.14.05.16.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Mar 2020 05:16:25 -0700 (PDT) From: apk120 To: ffmpeg-devel@ffmpeg.org Date: Sat, 14 Mar 2020 17:46:00 +0530 Message-Id: <20200314121600.25697-1-ashutoshp012345@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: Subject: [FFmpeg-devel] GSoC 2020:Audio tones source filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: ashutoshp012345@gmail.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Audio tones source filter to generate random audio tones using fluidsynth diff --git a/Makefile b/Makefile index 45a22b0cb3..44cf1b5710 100644 --- a/Makefile +++ b/Makefile @@ -111,7 +111,7 @@ else endif %$(PROGSSUF)_g$(EXESUF): $(FF_DEP_LIBS) - $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $(OBJS-$*) $(FF_EXTRALIBS) + $(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $(OBJS-$*) $(FF_EXTRALIBS) -lfluidsynth VERSION_SH = $(SRC_PATH)/ffbuild/version.sh GIT_LOG = $(SRC_PATH)/.git/logs/HEAD 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..e3703f5e4b --- /dev/null +++ b/libavfilter/asrc_atone.c @@ -0,0 +1,244 @@ +/* + * 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 +#include +#include +#include +#include +#include + +#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 + int changerate; ///< get the frequency 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_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_INT("Tone Change interval",changerate, 20, 1, INT_MAX, "set the random tone change interval",), + {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()); + + 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 % (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, +}; + + + +