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.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 
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     ret->name ## _size = name->nb_neurons; \
115     INPUT_ACTIVATION(name->activation); \
116     INPUT_ARRAY(name->input_weights, name->nb_inputs * name->nb_neurons); \
117     INPUT_ARRAY(name->bias, name->nb_neurons); \
118     } while (0)
119 
120 #define INPUT_GRU(name) do { \
121     INPUT_VAL(name->nb_inputs); \
122     INPUT_VAL(name->nb_neurons); \
123     ret->name ## _size = name->nb_neurons; \
124     INPUT_ACTIVATION(name->activation); \
125     INPUT_ARRAY(name->input_weights, name->nb_inputs * name->nb_neurons * 3); \
126     INPUT_ARRAY(name->recurrent_weights, name->nb_neurons * name->nb_neurons * 3); \
127     INPUT_ARRAY(name->bias, name->nb_neurons * 3); \
128     } while (0)
129 
130     INPUT_DENSE(input_dense);
131     INPUT_GRU(vad_gru);
132     INPUT_GRU(noise_gru);
133     INPUT_GRU(denoise_gru);
134     INPUT_DENSE(denoise_output);
135     INPUT_DENSE(vad_output);
136 
137     return ret;
138 }
139 
140 void rnnoise_model_free(RNNModel *model)
141 {
142 #define FREE_MAYBE(ptr) do { if (ptr) free(ptr); } while (0)
143 #define FREE_DENSE(name) do { \
144     if (model->name) { \
145         free((void *) model->name->input_weights); \
146         free((void *) model->name->bias); \
147         free((void *) model->name); \
148     } \
149     } while (0)
150 #define FREE_GRU(name) do { \
151     if (model->name) { \
152         free((void *) model->name->input_weights); \
153         free((void *) model->name->recurrent_weights); \
154         free((void *) model->name->bias); \
155         free((void *) model->name); \
156     } \
157     } while (0)
158 
159     if (!model)
160         return;
161     FREE_DENSE(input_dense);
162     FREE_GRU(vad_gru);
163     FREE_GRU(noise_gru);
164     FREE_GRU(denoise_gru);
165     FREE_DENSE(denoise_output);
166     FREE_DENSE(vad_output);
167     free(model);
168 }
169