[vlc-devel] [PATCH 2/2] audio_filter: add RNNoise denoiser filter

Tristan Matthews tmatth at videolan.org
Fri Jul 26 07:19:13 CEST 2019


---
 NEWS                             |   3 +
 configure.ac                     |  15 +++++
 modules/audio_filter/Makefile.am |   7 ++
 modules/audio_filter/rnnoise.c   | 109 +++++++++++++++++++++++++++++++
 po/POTFILES.in                   |   1 +
 5 files changed, 135 insertions(+)
 create mode 100644 modules/audio_filter/rnnoise.c

diff --git a/NEWS b/NEWS
index 0f4f6d74c0..e364df7abe 100644
--- a/NEWS
+++ b/NEWS
@@ -71,6 +71,9 @@ Video output:
  * Remove RealRTSP plugin
  * Remove Real demuxer plugin
 
+Audio filter:
+ * Add RNNoise recurrent neural network denoiser
+
 Video filter:
  * Update yadif
  * Remove remote OSD plugin
diff --git a/configure.ac b/configure.ac
index ac818d8b83..c0f1f09dcb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2901,6 +2901,21 @@ AS_IF([test "${enable_speex}" != "no"], [
 ])
 AM_CONDITIONAL([HAVE_SPEEXDSP], [test "$have_speexdsp" = "yes"])
 
+AC_ARG_ENABLE([rnnoise],
+  AS_HELP_STRING([--enable-rnnoise],
+    [RNNoise denoiser filter (default auto)]))
+have_rnnoise="no"
+AS_IF([test "${enable_rnnoise}" != "no"], [
+  PKG_CHECK_MODULES([RNNOISE], [rnnoise], [
+    have_rnnoise="yes"
+  ], [
+    AS_IF([test "x${enable_rnnoise}" != "x"], [
+      AC_MSG_ERROR([$RNNOISE_PKG_ERRORS.])
+    ])
+  ])
+])
+AM_CONDITIONAL([HAVE_RNNOISE], [test "${have_rnnoise}" = "yes"])
+
 dnl
 dnl  Opus plugin
 dnl
diff --git a/modules/audio_filter/Makefile.am b/modules/audio_filter/Makefile.am
index 309074c75b..a73ec822d1 100644
--- a/modules/audio_filter/Makefile.am
+++ b/modules/audio_filter/Makefile.am
@@ -137,3 +137,10 @@ libspeex_resampler_plugin_la_LIBADD = $(SPEEXDSP_LIBS)
 if HAVE_SPEEXDSP
 audio_filter_LTLIBRARIES += libspeex_resampler_plugin.la
 endif
+
+librnnoise_plugin_la_SOURCES = audio_filter/rnnoise.c
+librnnoise_plugin_la_CFLAGS = $(AM_CFLAGS) $(RNNOISE_CFLAGS)
+librnnoise_plugin_la_LIBADD = $(RNNOISE_LIBS) $(LIBM)
+if HAVE_RNNOISE
+audio_filter_LTLIBRARIES += librnnoise_plugin.la
+endif
diff --git a/modules/audio_filter/rnnoise.c b/modules/audio_filter/rnnoise.c
new file mode 100644
index 0000000000..ddf18870e7
--- /dev/null
+++ b/modules/audio_filter/rnnoise.c
@@ -0,0 +1,109 @@
+/*****************************************************************************
+ * rnnoise.c : Recurrent neural network for audio noise reduction
+ *****************************************************************************
+ * Copyright © 2019 Tristan Matthews
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_aout.h>
+#include <vlc_filter.h>
+#include <vlc_plugin.h>
+
+#include <rnnoise.h>
+
+static int Open (vlc_object_t *);
+static void Close (vlc_object_t *);
+
+typedef struct
+{
+    DenoiseState *p_st;
+    bool b_first;
+} filter_sys_t;
+
+vlc_module_begin ()
+    set_shortname (N_("RNNoise"))
+    set_description (N_("RNNoise filter"))
+    set_category (CAT_AUDIO)
+    set_subcategory (SUBCAT_AUDIO_AFILTER)
+    set_capability ("audio filter", 0)
+    set_callbacks( Open, Close )
+vlc_module_end ()
+
+static block_t *Process (filter_t *, block_t *);
+
+static int Open (vlc_object_t *obj)
+{
+    filter_t *p_filter = (filter_t *)obj;
+
+    filter_sys_t *p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
+    if( unlikely(!p_sys) )
+        return VLC_ENOMEM;
+
+    p_sys->b_first = true;
+
+    p_sys->p_st = rnnoise_create(NULL);
+    if( unlikely(!p_sys->p_st) ) {
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
+
+    p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
+    aout_FormatPrepare(&p_filter->fmt_in.audio);
+    p_filter->fmt_out.audio = p_filter->fmt_in.audio;
+    p_filter->pf_audio_filter = Process;
+    return VLC_SUCCESS;
+}
+
+static void Close( vlc_object_t *obj )
+{
+    filter_t *p_filter  = (filter_t *)obj;
+    filter_sys_t *p_sys = p_filter->p_sys;
+    if( p_sys->p_st )
+        rnnoise_destroy( p_sys->p_st );
+    free( p_sys );
+}
+
+#define FRAME_SIZE 480
+
+static block_t *Process (filter_t *p_filter, block_t *p_block)
+{
+    filter_sys_t *p_sys = p_filter->p_sys;
+    float tmp[FRAME_SIZE];
+    float *p_buffer = (float *)p_block->p_buffer;
+    for ( int i_nb_samples = p_block->i_nb_samples; i_nb_samples > 0; i_nb_samples -= FRAME_SIZE )
+    {
+        /* rnnoise process blocks of 480 samples, and expects input to be in the 32768 scale. */
+        for (unsigned i = 0; i < FRAME_SIZE; i++) tmp[i] = p_buffer[i] * 32768;
+
+        rnnoise_process_frame(p_sys->p_st, tmp, tmp);
+
+        if( !p_sys->b_first )
+        {
+            for (unsigned i = 0; i < FRAME_SIZE; i++) p_buffer[i] = tmp[i] / 32768.;
+        }
+        else
+        {
+            p_sys->b_first = false;
+        }
+        p_buffer += FRAME_SIZE;
+    }
+    return p_block;
+}
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 1c2a5298a8..49b89067f4 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -245,6 +245,7 @@ modules/audio_filter/resampler/soxr.c
 modules/audio_filter/resampler/speex.c
 modules/audio_filter/resampler/src.c
 modules/audio_filter/resampler/ugly.c
+modules/audio_filter/rnnoise.c
 modules/audio_filter/scaletempo.c
 modules/audio_filter/spatializer/spatializer.cpp
 modules/audio_filter/stereo_widen.c
-- 
2.20.1



More information about the vlc-devel mailing list