Skip to content

Commit 231b9c0

Browse files
GregorRjmvalin
authored andcommitted
Modularizing the RNN model
1 parent 91ef401 commit 231b9c0

File tree

7 files changed

+88
-47
lines changed

7 files changed

+88
-47
lines changed

examples/rnnoise_demo.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright (c) 2017 Mozilla */
1+
/* Copyright (c) 2018 Gregor Richards
2+
* Copyright (c) 2017 Mozilla */
23
/*
34
Redistribution and use in source and binary forms, with or without
45
modification, are permitted provided that the following conditions
@@ -35,7 +36,7 @@ int main(int argc, char **argv) {
3536
float x[FRAME_SIZE];
3637
FILE *f1, *fout;
3738
DenoiseState *st;
38-
st = rnnoise_create();
39+
st = rnnoise_create(NULL);
3940
if (argc!=3) {
4041
fprintf(stderr, "usage: %s <noisy speech> <output denoised>\n", argv[0]);
4142
return 1;

include/rnnoise.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright (c) 2017 Mozilla */
1+
/* Copyright (c) 2018 Gregor Richards
2+
* Copyright (c) 2017 Mozilla */
23
/*
34
Redistribution and use in source and binary forms, with or without
45
modification, are permitted provided that the following conditions
@@ -40,12 +41,13 @@
4041

4142

4243
typedef struct DenoiseState DenoiseState;
44+
typedef struct RNNModel RNNModel;
4345

4446
RNNOISE_EXPORT int rnnoise_get_size();
4547

46-
RNNOISE_EXPORT int rnnoise_init(DenoiseState *st);
48+
RNNOISE_EXPORT int rnnoise_init(DenoiseState *st, RNNModel *model);
4749

48-
RNNOISE_EXPORT DenoiseState *rnnoise_create();
50+
RNNOISE_EXPORT DenoiseState *rnnoise_create(RNNModel *model);
4951

5052
RNNOISE_EXPORT void rnnoise_destroy(DenoiseState *st);
5153

src/denoise.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright (c) 2017 Mozilla */
1+
/* Copyright (c) 2018 Gregor Richards
2+
* Copyright (c) 2017 Mozilla */
23
/*
34
Redistribution and use in source and binary forms, with or without
45
modification, are permitted provided that the following conditions
@@ -70,6 +71,11 @@
7071
#define TRAINING 0
7172
#endif
7273

74+
75+
/* The built-in model, used if no file is given as input */
76+
extern const struct RNNModel rnnoise_model_orig;
77+
78+
7379
static const opus_int16 eband5ms[] = {
7480
/*0 200 400 600 800 1k 1.2 1.4 1.6 2k 2.4 2.8 3.2 4k 4.8 5.6 6.8 8k 9.6 12k 15.6 20k*/
7581
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100
@@ -284,19 +290,29 @@ int rnnoise_get_size() {
284290
return sizeof(DenoiseState);
285291
}
286292

287-
int rnnoise_init(DenoiseState *st) {
293+
int rnnoise_init(DenoiseState *st, RNNModel *model) {
288294
memset(st, 0, sizeof(*st));
295+
if (model)
296+
st->rnn.model = model;
297+
else
298+
st->rnn.model = &rnnoise_model_orig;
299+
st->rnn.vad_gru_state = calloc(sizeof(float), st->rnn.model->vad_gru_size);
300+
st->rnn.noise_gru_state = calloc(sizeof(float), st->rnn.model->noise_gru_size);
301+
st->rnn.denoise_gru_state = calloc(sizeof(float), st->rnn.model->denoise_gru_size);
289302
return 0;
290303
}
291304

292-
DenoiseState *rnnoise_create() {
305+
DenoiseState *rnnoise_create(RNNModel *model) {
293306
DenoiseState *st;
294307
st = malloc(rnnoise_get_size());
295-
rnnoise_init(st);
308+
rnnoise_init(st, model);
296309
return st;
297310
}
298311

299312
void rnnoise_destroy(DenoiseState *st) {
313+
free(st->rnn.vad_gru_state);
314+
free(st->rnn.noise_gru_state);
315+
free(st->rnn.denoise_gru_state);
300316
free(st);
301317
}
302318

@@ -542,9 +558,9 @@ int main(int argc, char **argv) {
542558
DenoiseState *st;
543559
DenoiseState *noise_state;
544560
DenoiseState *noisy;
545-
st = rnnoise_create();
546-
noise_state = rnnoise_create();
547-
noisy = rnnoise_create();
561+
st = rnnoise_create(NULL);
562+
noise_state = rnnoise_create(NULL);
563+
noisy = rnnoise_create(NULL);
548564
if (argc!=4) {
549565
fprintf(stderr, "usage: %s <speech> <noise> <output denoised>\n", argv[0]);
550566
return 1;

src/rnn.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,17 @@ void compute_rnn(RNNState *rnn, float *gains, float *vad, const float *input) {
162162
float dense_out[MAX_NEURONS];
163163
float noise_input[MAX_NEURONS*3];
164164
float denoise_input[MAX_NEURONS*3];
165-
compute_dense(&input_dense, dense_out, input);
166-
compute_gru(&vad_gru, rnn->vad_gru_state, dense_out);
167-
compute_dense(&vad_output, vad, rnn->vad_gru_state);
168-
for (i=0;i<INPUT_DENSE_SIZE;i++) noise_input[i] = dense_out[i];
169-
for (i=0;i<VAD_GRU_SIZE;i++) noise_input[i+INPUT_DENSE_SIZE] = rnn->vad_gru_state[i];
170-
for (i=0;i<INPUT_SIZE;i++) noise_input[i+INPUT_DENSE_SIZE+VAD_GRU_SIZE] = input[i];
171-
compute_gru(&noise_gru, rnn->noise_gru_state, noise_input);
165+
compute_dense(rnn->model->input_dense, dense_out, input);
166+
compute_gru(rnn->model->vad_gru, rnn->vad_gru_state, dense_out);
167+
compute_dense(rnn->model->vad_output, vad, rnn->vad_gru_state);
168+
for (i=0;i<rnn->model->input_dense_size;i++) noise_input[i] = dense_out[i];
169+
for (i=0;i<rnn->model->vad_gru_size;i++) noise_input[i+rnn->model->input_dense_size] = rnn->vad_gru_state[i];
170+
for (i=0;i<INPUT_SIZE;i++) noise_input[i+rnn->model->input_dense_size+rnn->model->vad_gru_size] = input[i];
171+
compute_gru(rnn->model->noise_gru, rnn->noise_gru_state, noise_input);
172172

173-
for (i=0;i<VAD_GRU_SIZE;i++) denoise_input[i] = rnn->vad_gru_state[i];
174-
for (i=0;i<NOISE_GRU_SIZE;i++) denoise_input[i+VAD_GRU_SIZE] = rnn->noise_gru_state[i];
175-
for (i=0;i<INPUT_SIZE;i++) denoise_input[i+VAD_GRU_SIZE+NOISE_GRU_SIZE] = input[i];
176-
compute_gru(&denoise_gru, rnn->denoise_gru_state, denoise_input);
177-
compute_dense(&denoise_output, gains, rnn->denoise_gru_state);
173+
for (i=0;i<rnn->model->vad_gru_size;i++) denoise_input[i] = rnn->vad_gru_state[i];
174+
for (i=0;i<rnn->model->noise_gru_size;i++) denoise_input[i+rnn->model->vad_gru_size] = rnn->noise_gru_state[i];
175+
for (i=0;i<INPUT_SIZE;i++) denoise_input[i+rnn->model->vad_gru_size+rnn->model->noise_gru_size] = input[i];
176+
compute_gru(rnn->model->denoise_gru, rnn->denoise_gru_state, denoise_input);
177+
compute_dense(rnn->model->denoise_output, gains, rnn->denoise_gru_state);
178178
}

src/rnn.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct {
5656
int activation;
5757
} GRULayer;
5858

59+
typedef struct RNNModel RNNModel;
5960
typedef struct RNNState RNNState;
6061

6162
void compute_dense(const DenseLayer *layer, float *output, const float *input);

src/rnn_data.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#endif
66

77
#include "rnn.h"
8+
#include "rnn_data.h"
89

910
static const rnn_weight input_dense_weights[1008] = {
1011
-10, 0, -3, 1, -8, -6, 3, -13,
@@ -141,7 +142,7 @@ static const rnn_weight input_dense_bias[24] = {
141142
-126, 28, 127, 125, -30, 127, -89, -20
142143
};
143144

144-
const DenseLayer input_dense = {
145+
static const DenseLayer input_dense = {
145146
input_dense_bias,
146147
input_dense_weights,
147148
42, 24, ACTIVATION_TANH
@@ -597,7 +598,7 @@ static const rnn_weight vad_gru_bias[72] = {
597598
-29, 127, 34, -66, 49, 53, 27, 62
598599
};
599600

600-
const GRULayer vad_gru = {
601+
static const GRULayer vad_gru = {
601602
vad_gru_bias,
602603
vad_gru_weights,
603604
vad_gru_recurrent_weights,
@@ -3115,7 +3116,7 @@ static const rnn_weight noise_gru_bias[144] = {
31153116
-23, -64, 31, 86, -50, 2, -38, 7
31163117
};
31173118

3118-
const GRULayer noise_gru = {
3119+
static const GRULayer noise_gru = {
31193120
noise_gru_bias,
31203121
noise_gru_weights,
31213122
noise_gru_recurrent_weights,
@@ -10727,7 +10728,7 @@ static const rnn_weight denoise_gru_bias[288] = {
1072710728
-21, 25, 18, -58, 25, 126, -84, 127
1072810729
};
1072910730

10730-
const GRULayer denoise_gru = {
10731+
static const GRULayer denoise_gru = {
1073110732
denoise_gru_bias,
1073210733
denoise_gru_weights,
1073310734
denoise_gru_recurrent_weights,
@@ -11007,7 +11008,7 @@ static const rnn_weight denoise_output_bias[22] = {
1100711008
-126, -105, -53, -49, -18, -9
1100811009
};
1100911010

11010-
const DenseLayer denoise_output = {
11011+
static const DenseLayer denoise_output = {
1101111012
denoise_output_bias,
1101211013
denoise_output_weights,
1101311014
96, 22, ACTIVATION_SIGMOID
@@ -11023,9 +11024,28 @@ static const rnn_weight vad_output_bias[1] = {
1102311024
-50
1102411025
};
1102511026

11026-
const DenseLayer vad_output = {
11027+
static const DenseLayer vad_output = {
1102711028
vad_output_bias,
1102811029
vad_output_weights,
1102911030
24, 1, ACTIVATION_SIGMOID
1103011031
};
1103111032

11033+
const struct RNNModel rnnoise_model_orig = {
11034+
24,
11035+
&input_dense,
11036+
11037+
24,
11038+
&vad_gru,
11039+
11040+
48,
11041+
&noise_gru,
11042+
11043+
96,
11044+
&denoise_gru,
11045+
11046+
22,
11047+
&denoise_output,
11048+
11049+
1,
11050+
&vad_output
11051+
};

src/rnn_data.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
/*This file is automatically generated from a Keras model*/
2-
31
#ifndef RNN_DATA_H
42
#define RNN_DATA_H
53

64
#include "rnn.h"
75

8-
#define INPUT_DENSE_SIZE 24
9-
extern const DenseLayer input_dense;
6+
struct RNNModel {
7+
int input_dense_size;
8+
DenseLayer *input_dense;
109

11-
#define VAD_GRU_SIZE 24
12-
extern const GRULayer vad_gru;
10+
int vad_gru_size;
11+
GRULayer *vad_gru;
1312

14-
#define NOISE_GRU_SIZE 48
15-
extern const GRULayer noise_gru;
13+
int noise_gru_size;
14+
GRULayer *noise_gru;
1615

17-
#define DENOISE_GRU_SIZE 96
18-
extern const GRULayer denoise_gru;
16+
int denoise_gru_size;
17+
GRULayer *denoise_gru;
1918

20-
#define DENOISE_OUTPUT_SIZE 22
21-
extern const DenseLayer denoise_output;
19+
int denoise_output_size;
20+
DenseLayer *denoise_output;
2221

23-
#define VAD_OUTPUT_SIZE 1
24-
extern const DenseLayer vad_output;
22+
int vad_output_size;
23+
DenseLayer *vad_output;
24+
};
2525

2626
struct RNNState {
27-
float vad_gru_state[VAD_GRU_SIZE];
28-
float noise_gru_state[NOISE_GRU_SIZE];
29-
float denoise_gru_state[DENOISE_GRU_SIZE];
27+
const RNNModel *model;
28+
float *vad_gru_state;
29+
float *noise_gru_state;
30+
float *denoise_gru_state;
3031
};
3132

3233

0 commit comments

Comments
 (0)