Skip to content

Commit c7845a3

Browse files
committed
Reviving model loading based on Opus code
1 parent f4700a6 commit c7845a3

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

Makefile.am

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,22 @@ librnnoise_la_LIBADD = $(DEPS_LIBS) $(lrintf_lib) $(LIBM)
5454
librnnoise_la_LDFLAGS = -no-undefined \
5555
-version-info @OP_LT_CURRENT@:@OP_LT_REVISION@:@OP_LT_AGE@
5656

57-
noinst_PROGRAMS = dump_features
57+
noinst_PROGRAMS = dump_features dump_weights_blob
5858
if OP_ENABLE_EXAMPLES
5959
noinst_PROGRAMS += examples/rnnoise_demo
6060
endif
6161

6262
examples_rnnoise_demo_SOURCES = examples/rnnoise_demo.c
6363
examples_rnnoise_demo_LDADD = librnnoise.la
6464

65-
dump_features_SOURCES = src/dump_features.c src/denoise.c src/pitch.c src/celt_lpc.c src/kiss_fft.c src/rnnoise_data.c src/parse_lpcnet_weights.c src/rnnoise_tables.c
65+
dump_features_SOURCES = src/dump_features.c src/denoise.c src/pitch.c src/celt_lpc.c src/kiss_fft.c src/parse_lpcnet_weights.c src/rnnoise_tables.c
6666
dump_features_LDADD = $(LIBM)
6767
dump_features_CFLAGS = $(AM_CFLAGS) -DTRAINING
6868

69+
dump_weights_blob_SOURCES = src/write_weights.c
70+
dump_weights_blob_LDADD = $(LIBM)
71+
dump_weights_blob_CFLAGS = $(AM_CFLAGS) -DDUMP_BINARY_WEIGHTS
72+
6973
pkgconfigdir = $(libdir)/pkgconfig
7074
pkgconfig_DATA = rnnoise.pc
7175

examples/rnnoise_demo.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ int main(int argc, char **argv) {
3636
float x[FRAME_SIZE];
3737
FILE *f1, *fout;
3838
DenoiseState *st;
39+
#ifdef USE_WEIGHTS_FILE
40+
FILE *model_file = fopen("weights_blob.bin", "r");
41+
RNNModel *model = rnnoise_model_from_file(model_file);
42+
st = rnnoise_create(model);
43+
#else
3944
st = rnnoise_create(NULL);
45+
#endif
46+
4047
if (argc!=3) {
4148
fprintf(stderr, "usage: %s <noisy speech> <output denoised>\n", argv[0]);
4249
return 1;
@@ -56,5 +63,9 @@ int main(int argc, char **argv) {
5663
rnnoise_destroy(st);
5764
fclose(f1);
5865
fclose(fout);
66+
#ifdef USE_WEIGHTS_FILE
67+
fclose(model_file);
68+
rnnoise_model_free(model);
69+
#endif
5970
return 0;
6071
}

src/denoise.c

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050
#endif
5151

5252

53-
/* The built-in model, used if no file is given as input */
54-
extern const struct RNNModel rnnoise_model_orig;
55-
5653
/* ERB bandwidths going in reverse from 20 kHz and then replacing the 700 and 800
5754
with just 750 because having 32 bands is convenient for the DNN.
5855
B(1)=400;
@@ -226,6 +223,33 @@ static void apply_window(float *x) {
226223
}
227224
}
228225

226+
struct RNNModel {
227+
unsigned char *blob;
228+
int blob_len;
229+
};
230+
231+
RNNModel *rnnoise_model_from_file(FILE *f) {
232+
RNNModel *model;
233+
model = malloc(sizeof(*model));
234+
235+
fseek(f, 0, SEEK_END);
236+
model->blob_len = ftell(f);
237+
fseek(f, 0, SEEK_SET);
238+
239+
model->blob = malloc(model->blob_len);
240+
if (fread(model->blob, model->blob_len, 1, f) != 1)
241+
{
242+
rnnoise_model_free(model);
243+
return NULL;
244+
}
245+
return model;
246+
}
247+
248+
void rnnoise_model_free(RNNModel *model) {
249+
free(model->blob);
250+
free(model);
251+
}
252+
229253
int rnnoise_get_size() {
230254
return sizeof(DenoiseState);
231255
}
@@ -234,21 +258,41 @@ int rnnoise_get_frame_size() {
234258
return FRAME_SIZE;
235259
}
236260

237-
extern const WeightArray rnnoise_arrays[];
238261
int rnnoise_init(DenoiseState *st, RNNModel *model) {
239262
memset(st, 0, sizeof(*st));
240-
init_rnnoise(&st->model, rnnoise_arrays);
241263
#if !TRAINING
242-
st->arch = rnn_select_arch();
264+
if (model != NULL) {
265+
WeightArray *list;
266+
int ret = 1;
267+
parse_weights(&list, model->blob, model->blob_len);
268+
if (list != NULL) {
269+
ret = init_rnnoise(&st->model, list);
270+
opus_free(list);
271+
}
272+
if (ret != 0) return -1;
273+
}
274+
#ifndef USE_WEIGHTS_FILE
275+
else {
276+
int ret = init_rnnoise(&st->model, rnnoise_arrays);
277+
if (ret != 0) return -1;
278+
}
243279
#endif
280+
st->arch = rnn_select_arch();
281+
#else
244282
(void)model;
283+
#endif
245284
return 0;
246285
}
247286

248287
DenoiseState *rnnoise_create(RNNModel *model) {
288+
int ret;
249289
DenoiseState *st;
250290
st = malloc(rnnoise_get_size());
251-
rnnoise_init(st, model);
291+
ret = rnnoise_init(st, model);
292+
if (ret != 0) {
293+
free(st);
294+
return NULL;
295+
}
252296
return st;
253297
}
254298

src/denoise.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include "rnnoise.h"
2828
#include "kiss_fft.h"
29-
29+
#include "nnet.h"
3030

3131
#define FRAME_SIZE 480
3232
#define WINDOW_SIZE (2*FRAME_SIZE)
@@ -40,6 +40,7 @@
4040
#define PITCH_FRAME_SIZE 960
4141
#define PITCH_BUF_SIZE (PITCH_MAX_PERIOD+PITCH_FRAME_SIZE)
4242

43+
extern const WeightArray rnnoise_arrays[];
4344

4445
extern const int eband20ms[];
4546

0 commit comments

Comments
 (0)