Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements from my branch #39

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ librnnoise_la_SOURCES = \
src/denoise.c \
src/rnn.c \
src/rnn_data.c \
src/rnn_reader.c \
src/pitch.c \
src/kiss_fft.c \
src/celt_lpc.c
Expand All @@ -35,7 +36,7 @@ noinst_PROGRAMS = examples/rnnoise_demo
endif

examples_rnnoise_demo_SOURCES = examples/rnnoise_demo.c
examples_rnnoise_demo_LDADD = librnnoise.la
examples_rnnoise_demo_LDADD = librnnoise.la $(LIBM)

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = rnnoise.pc
Expand Down
4 changes: 2 additions & 2 deletions README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RNNoise is a noise suppression library based on a recurrent neural network
RNNoise is a noise suppression library based on a recurrent neural network.

To compile, just type:
% ./autogen.sh
Expand All @@ -12,6 +12,6 @@ While it is meant to be used as a library, a simple command-line tool is
provided as an example. It operates on RAW 16-bit (machine endian) mono
PCM files sampled at 48 kHz. It can be used as:

./examples/rnnoise_demo input.pcm output.pcm
./examples/rnnoise_demo <number of channels> <maximum attenuation> [model file] < input.raw > output.raw

The output is also a 16-bit raw PCM file.
11 changes: 11 additions & 0 deletions TRAINING
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(1) cd src ; ./compile.sh

(2) ./denoise_training signal.raw noise.raw count > training.f32

(note the matrix size and replace 500000 87 below)

(3) cd training ; ./bin2hdf5.py ../src/training.f32 500000 87 training.h5

(4) ./rnn_train.py

(5) ./dump_rnn.py weights.hdf5 ../src/rnn_data.c ../src/rnn_data.h
85 changes: 66 additions & 19 deletions examples/rnnoise_demo.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (c) 2017 Mozilla */
/* Copyright (c) 2018 Gregor Richards
* Copyright (c) 2017 Mozilla */
/*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
Expand All @@ -24,36 +25,82 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <math.h>
#include <stdio.h>
#include "rnnoise.h"
#include <stdlib.h>
#include <sys/types.h>
#include "rnnoise.h"

#define FRAME_SIZE 480

int main(int argc, char **argv) {
int i;
int i, ci;
int first = 1;
int channels;
float x[FRAME_SIZE];
FILE *f1, *fout;
DenoiseState *st;
st = rnnoise_create();
if (argc!=3) {
fprintf(stderr, "usage: %s <noisy speech> <output denoised>\n", argv[0]);
short *tmp;
RNNModel *model = NULL;
DenoiseState **sts;
float max_attenuation;
if (argc < 3) {
fprintf(stderr, "usage: %s <channels> <max attenuation dB> [model file]\n", argv[0]);
return 1;
}

channels = atoi(argv[1]);
if (channels < 1) channels = 1;
max_attenuation = pow(10, -atof(argv[2])/10);

if (argc >= 4) {
FILE *model_file = fopen(argv[3], "r");
if (!model_file) {
perror(argv[3]);
return 1;
}
model = rnnoise_model_from_file(model_file);
fprintf(stderr, "\n\n\n%p\n\n\n", model);
if (!model) {
perror(argv[3]);
return 1;
}
fclose(model_file);
}

sts = malloc(channels * sizeof(DenoiseState *));
if (!sts) {
perror("malloc");
return 1;
}
f1 = fopen(argv[1], "r");
fout = fopen(argv[2], "w");
tmp = malloc(channels * FRAME_SIZE * sizeof(short));
if (!tmp) {
perror("malloc");
return 1;
}
for (i = 0; i < channels; i++) {
sts[i] = rnnoise_create(model);
rnnoise_set_param(sts[i], RNNOISE_PARAM_MAX_ATTENUATION, max_attenuation);
}

while (1) {
short tmp[FRAME_SIZE];
fread(tmp, sizeof(short), FRAME_SIZE, f1);
if (feof(f1)) break;
for (i=0;i<FRAME_SIZE;i++) x[i] = tmp[i];
rnnoise_process_frame(st, x, x);
for (i=0;i<FRAME_SIZE;i++) tmp[i] = x[i];
if (!first) fwrite(tmp, sizeof(short), FRAME_SIZE, fout);
fread(tmp, sizeof(short), channels * FRAME_SIZE, stdin);
if (feof(stdin)) break;

for (ci = 0; ci < channels; ci++) {
for (i=0;i<FRAME_SIZE;i++) x[i] = tmp[i*channels+ci];
rnnoise_process_frame(sts[ci], x, x);
for (i=0;i<FRAME_SIZE;i++) tmp[i*channels+ci] = x[i];
}

if (!first) fwrite(tmp, sizeof(short), channels * FRAME_SIZE, stdout);
first = 0;
}
rnnoise_destroy(st);
fclose(f1);
fclose(fout);

for (i = 0; i < channels; i++)
rnnoise_destroy(sts[i]);
free(tmp);
free(sts);
if (model)
rnnoise_model_free(model);
return 0;
}
26 changes: 22 additions & 4 deletions include/rnnoise.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (c) 2017 Mozilla */
/* Copyright (c) 2018 Gregor Richards
* Copyright (c) 2017 Mozilla */
/*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
Expand All @@ -24,6 +25,12 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef RNNOISE_H
#define RNNOISE_H 1

#include <stdio.h>


#ifndef RNNOISE_EXPORT
# if defined(WIN32)
# if defined(RNNOISE_BUILD) && defined(DLL_EXPORT)
Expand All @@ -38,15 +45,26 @@
# endif
#endif


typedef struct DenoiseState DenoiseState;
typedef struct RNNModel RNNModel;

RNNOISE_EXPORT int rnnoise_get_size();

RNNOISE_EXPORT int rnnoise_init(DenoiseState *st);
RNNOISE_EXPORT int rnnoise_init(DenoiseState *st, RNNModel *model);

RNNOISE_EXPORT DenoiseState *rnnoise_create();
RNNOISE_EXPORT DenoiseState *rnnoise_create(RNNModel *model);

RNNOISE_EXPORT void rnnoise_destroy(DenoiseState *st);

RNNOISE_EXPORT float rnnoise_process_frame(DenoiseState *st, float *out, const float *in);

RNNOISE_EXPORT RNNModel *rnnoise_model_from_file(FILE *f);

RNNOISE_EXPORT void rnnoise_model_free(RNNModel *model);

/* Parameters to a denoise state */
#define RNNOISE_PARAM_MAX_ATTENUATION 1

RNNOISE_EXPORT void rnnoise_set_param(DenoiseState *st, int param, float value);

#endif
Loading