1 /* Copyright (c) 2018 Gregor Richards */
2 /*
3    Redistribution and use in source and binary forms, with or without
4    modification, are permitted provided that the following conditions
5    are met:
6 
7    - Redistributions of source code must retain the above copyright
8    notice, this list of conditions and the following disclaimer.
9 
10    - Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 
14    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
18    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 
35 #include "rnn.h"
36 #include "rnn_data.h"
37 #include "rnnoise-nu.h"
38 
39 /* Although these values are the same as in rnn.h, we make them separate to
40  * avoid accidentally burning internal values into a file format */
41 #define F_ACTIVATION_TANH       0
42 #define F_ACTIVATION_SIGMOID    1
43 #define F_ACTIVATION_RELU       2
44 
rnnoise_model_from_file(FILE * f)45 RNNModel *rnnoise_model_from_file(FILE *f)
46 {
47     int i, in;
48 
49     if (fscanf(f, "rnnoise-nu model file version %d\n", &in) != 1 || in != 1)
50         return NULL;
51 
52     RNNModel *ret = calloc(1, sizeof(RNNModel));
53     if (!ret)
54         return NULL;
55 
56 #define ALLOC_LAYER(type, name) \
57     type *name; \
58     name = calloc(1, sizeof(type)); \
59     if (!name) { \
60         rnnoise_model_free(ret); \
61         return NULL; \
62     } \
63     ret->name = name
64 
65     ALLOC_LAYER(DenseLayer, input_dense);
66     ALLOC_LAYER(GRULayer, vad_gru);
67     ALLOC_LAYER(GRULayer, noise_gru);
68     ALLOC_LAYER(GRULayer, denoise_gru);
69     ALLOC_LAYER(DenseLayer, denoise_output);
70     ALLOC_LAYER(DenseLayer, vad_output);
71 
72 #define INPUT_VAL(name) do { \
73     if (fscanf(f, "%d", &in) != 1 || in < 0 || in > 128) { \
74         rnnoise_model_free(ret); \
75         return NULL; \
76     } \
77     name = in; \
78     } while (0)
79 
80 #define INPUT_ACTIVATION(name) do { \
81     int activation; \
82     INPUT_VAL(activation); \
83     switch (activation) { \
84         case F_ACTIVATION_SIGMOID: \
85             name = ACTIVATION_SIGMOID; \
86             break; \
87         case F_ACTIVATION_RELU: \
88             name = ACTIVATION_RELU; \
89             break; \
90         default: \
91             name = ACTIVATION_TANH; \
92     } \
93     } while (0)
94 
95 #define INPUT_ARRAY(name, len) do { \
96     rnn_weight *values = malloc((len) * sizeof(rnn_weight)); \
97     if (!values) { \
98         rnnoise_model_free(ret); \
99         return NULL; \
100     } \
101     name = values; \
102     for (i = 0; i < (len); i++) { \
103         if (fscanf(f, "%d", &in) != 1) { \
104             rnnoise_model_free(ret); \
105             return NULL; \
106         } \
107         values[i] = in; \
108     } \
109     } while (0)
110 
111 #define INPUT_DENSE(name) do { \
112     INPUT_VAL(name->nb_inputs); \
113     INPUT_VAL(name->nb_neurons); \
114     INPUT_ACTIVATION(name->activation); \
115     INPUT_ARRAY(name->input_weights, name->nb_inputs * name->nb_neurons); \
116     INPUT_ARRAY(name->bias, name->nb_neurons); \
117     } while (0)
118 
119 #define INPUT_GRU(name) do { \
120     INPUT_VAL(name->nb_inputs); \
121     INPUT_VAL(name->nb_neurons); \
122     INPUT_ACTIVATION(name->activation); \
123     INPUT_ARRAY(name->input_weights, name->nb_inputs * name->nb_neurons * 3); \
124     INPUT_ARRAY(name->recurrent_weights, name->nb_neurons * name->nb_neurons * 3); \
125     INPUT_ARRAY(name->bias, name->nb_neurons * 3); \
126     } while (0)
127 
128     INPUT_DENSE(input_dense);
129     INPUT_GRU(vad_gru);
130     INPUT_GRU(noise_gru);
131     INPUT_GRU(denoise_gru);
132     INPUT_DENSE(denoise_output);
133     INPUT_DENSE(vad_output);
134 
135     return ret;
136 }
137 
rnnoise_model_free(RNNModel * model)138 void rnnoise_model_free(RNNModel *model)
139 {
140 #define FREE_MAYBE(ptr) do { if (ptr) free(ptr); } while (0)
141 #define FREE_DENSE(name) do { \
142     if (model->name) { \
143         free((void *) model->name->input_weights); \
144         free((void *) model->name->bias); \
145         free((void *) model->name); \
146     } \
147     } while (0)
148 #define FREE_GRU(name) do { \
149     if (model->name) { \
150         free((void *) model->name->input_weights); \
151         free((void *) model->name->recurrent_weights); \
152         free((void *) model->name->bias); \
153         free((void *) model->name); \
154     } \
155     } while (0)
156 
157     if (!model)
158         return;
159     FREE_DENSE(input_dense);
160     FREE_GRU(vad_gru);
161     FREE_GRU(noise_gru);
162     FREE_GRU(denoise_gru);
163     FREE_DENSE(denoise_output);
164     FREE_DENSE(vad_output);
165     free(model);
166 }
167