1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
| // Initialize ONNX model
int initialize_onnx_model(RNNoiseContext* ctx, const char* model_path) {
// Get ONNX Runtime API
const OrtApiBase* api_base = OrtGetApiBase();
if (!api_base) {
fprintf(stderr, "Error getting ONNX Runtime API base\n");
return -1;
}
ctx->api = api_base->GetApi(ORT_API_VERSION);
if (!ctx->api) {
fprintf(stderr, "Error getting ONNX Runtime API\n");
return -1;
}
// Initialize ONNX Runtime environment
OrtStatus* status = ctx->api->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "RNNoiseONNX", &ctx->env);
if (status != NULL) {
fprintf(stderr, "Error creating ONNX Runtime environment\n");
return -1;
}
// Create session options
status = ctx->api->CreateSessionOptions(&ctx->session_options);
if (status != NULL) {
fprintf(stderr, "Error creating session options\n");
return -1;
}
// Set session options
status = ctx->api->SetIntraOpNumThreads(ctx->session_options, 1);
if (status != NULL) {
fprintf(stderr, "Error setting intra-op threads\n");
return -1;
}
status = ctx->api->SetSessionGraphOptimizationLevel(ctx->session_options, ORT_ENABLE_EXTENDED);
if (status != NULL) {
fprintf(stderr, "Error setting optimization level\n");
return -1;
}
// Create session
status = ctx->api->CreateSession(ctx->env, model_path, ctx->session_options, &ctx->session);
if (status != NULL) {
fprintf(stderr, "Error creating ONNX session\n");
return -1;
}
// Get allocator
status = ctx->api->GetAllocatorWithDefaultOptions(&ctx->allocator);
if (status != NULL) {
fprintf(stderr, "Error getting allocator\n");
return -1;
}
// Get input/output names
size_t num_input_nodes, num_output_nodes;
status = ctx->api->SessionGetInputCount(ctx->session, &num_input_nodes);
if (status != NULL) {
fprintf(stderr, "Error getting input count\n");
return -1;
}
status = ctx->api->SessionGetOutputCount(ctx->session, &num_output_nodes);
if (status != NULL) {
fprintf(stderr, "Error getting output count\n");
return -1;
}
printf("ONNX Model Info:\n");
printf(" Input nodes: %zu\n", num_input_nodes);
printf(" Output nodes: %zu\n", num_output_nodes);
// Detect model type: 4 inputs + 5 outputs = model with GRU states
ctx->has_gru_states = (num_input_nodes == 4 && num_output_nodes == 5);
if (ctx->has_gru_states) {
printf(" Model type: WITH GRU state inputs/outputs\n");
// Get all input names
status = ctx->api->SessionGetInputName(ctx->session, 0, ctx->allocator, &ctx->input_name);
if (status != NULL) {
fprintf(stderr, "Error getting features input name\n");
return -1;
}
status = ctx->api->SessionGetInputName(ctx->session, 1, ctx->allocator, &ctx->input_name_vad_state);
if (status != NULL) {
fprintf(stderr, "Error getting VAD state input name\n");
return -1;
}
status = ctx->api->SessionGetInputName(ctx->session, 2, ctx->allocator, &ctx->input_name_noise_state);
if (status != NULL) {
fprintf(stderr, "Error getting noise state input name\n");
return -1;
}
status = ctx->api->SessionGetInputName(ctx->session, 3, ctx->allocator, &ctx->input_name_denoise_state);
if (status != NULL) {
fprintf(stderr, "Error getting denoise state input name\n");
return -1;
}
// Get all output names
status = ctx->api->SessionGetOutputName(ctx->session, 0, ctx->allocator, &ctx->output_name_denoise);
if (status != NULL) {
fprintf(stderr, "Error getting denoise output name\n");
return -1;
}
status = ctx->api->SessionGetOutputName(ctx->session, 1, ctx->allocator, &ctx->output_name_vad);
if (status != NULL) {
fprintf(stderr, "Error getting VAD output name\n");
return -1;
}
status = ctx->api->SessionGetOutputName(ctx->session, 2, ctx->allocator, &ctx->output_name_vad_state);
if (status != NULL) {
fprintf(stderr, "Error getting VAD state output name\n");
return -1;
}
status = ctx->api->SessionGetOutputName(ctx->session, 3, ctx->allocator, &ctx->output_name_noise_state);
if (status != NULL) {
fprintf(stderr, "Error getting noise state output name\n");
return -1;
}
status = ctx->api->SessionGetOutputName(ctx->session, 4, ctx->allocator, &ctx->output_name_denoise_state);
if (status != NULL) {
fprintf(stderr, "Error getting denoise state output name\n");
return -1;
}
printf(" Inputs:\n");
printf(" [0] %s (features)\n", ctx->input_name);
printf(" [1] %s (VAD GRU state)\n", ctx->input_name_vad_state);
printf(" [2] %s (noise GRU state)\n", ctx->input_name_noise_state);
printf(" [3] %s (denoise GRU state)\n", ctx->input_name_denoise_state);
printf(" Outputs:\n");
printf(" [0] %s (denoise)\n", ctx->output_name_denoise);
printf(" [1] %s (VAD)\n", ctx->output_name_vad);
printf(" [2] %s (VAD GRU state)\n", ctx->output_name_vad_state);
printf(" [3] %s (noise GRU state)\n", ctx->output_name_noise_state);
printf(" [4] %s (denoise GRU state)\n", ctx->output_name_denoise_state);
} else {
printf(" Model type: Standard (without GRU state ports)\n");
// Get input name (standard model)
status = ctx->api->SessionGetInputName(ctx->session, 0, ctx->allocator, &ctx->input_name);
if (status != NULL) {
fprintf(stderr, "Error getting input name\n");
return -1;
}
// Get output names (standard model)
status = ctx->api->SessionGetOutputName(ctx->session, 0, ctx->allocator, &ctx->output_name_denoise);
if (status != NULL) {
fprintf(stderr, "Error getting denoise output name\n");
return -1;
}
status = ctx->api->SessionGetOutputName(ctx->session, 1, ctx->allocator, &ctx->output_name_vad);
if (status != NULL) {
fprintf(stderr, "Error getting VAD output name\n");
return -1;
}
printf(" Input: %s\n", ctx->input_name);
printf(" Output denoise: %s\n", ctx->output_name_denoise);
printf(" Output VAD: %s\n", ctx->output_name_vad);
}
// Allocate buffers
ctx->input_buffer = (float*)malloc(FRAME_SIZE * sizeof(float));
ctx->output_buffer = (float*)malloc(FRAME_SIZE * sizeof(float));
if (!ctx->input_buffer || !ctx->output_buffer) {
fprintf(stderr, "Error: Memory allocation failed\n");
return -1;
}
// Initialize RNNoise state for feature extraction
ctx->denoise_state = rnnoise_create(NULL);
if (!ctx->denoise_state) {
fprintf(stderr, "Error: Failed to create RNNoise state\n");
return -1;
}
rnnoise_init(ctx->denoise_state, NULL);
// Initialize biquad filter memory
ctx->mem_hp_x[0] = 0.0f;
ctx->mem_hp_x[1] = 0.0f;
// Initialize processing buffers
memset(ctx->X, 0, sizeof(ctx->X));
memset(ctx->P, 0, sizeof(ctx->P));
memset(ctx->Ex, 0, sizeof(ctx->Ex));
memset(ctx->Ep, 0, sizeof(ctx->Ep));
memset(ctx->Exp, 0, sizeof(ctx->Exp));
memset(ctx->lastg, 0, sizeof(ctx->lastg));
memset(ctx->synthesis_mem, 0, sizeof(ctx->synthesis_mem));
// Initialize frame count
ctx->frame_count = 0;
// Initialize GRU states if model supports it
initialize_gru_states(ctx);
printf("ONNX model loaded successfully: %s\n", model_path);
return 0;
}
// Initialize GRU states
void initialize_gru_states(RNNoiseContext* ctx) {
memset(ctx->vad_gru_state, 0, sizeof(ctx->vad_gru_state));
memset(ctx->noise_gru_state, 0, sizeof(ctx->noise_gru_state));
memset(ctx->denoise_gru_state, 0, sizeof(ctx->denoise_gru_state));
ctx->gru_states_initialized = 0;
}
// ONNX inference with external state management
int onnx_inference_with_states(RNNoiseContext* ctx, const float* features, float* gains, float* vad) {
// Prepare separate input tensors for features and GRU states
float features_data[42];
float vad_state_data[24];
float noise_state_data[48];
float denoise_state_data[96];
// Copy features
memcpy(features_data, features, 42 * sizeof(float));
// Copy GRU states (use saved states for next frame)
memcpy(vad_state_data, ctx->vad_gru_state, 24 * sizeof(float));
memcpy(noise_state_data, ctx->noise_gru_state, 48 * sizeof(float));
memcpy(denoise_state_data, ctx->denoise_gru_state, 96 * sizeof(float));
// Create input tensors
const int64_t features_shape[] = {1, 1, 42};
const int64_t vad_state_shape[] = {1, 24};
const int64_t noise_state_shape[] = {1, 48};
const int64_t denoise_state_shape[] = {1, 96};
OrtMemoryInfo* memory_info;
OrtStatus* status = ctx->api->CreateCpuMemoryInfo(OrtArenaAllocator, OrtMemTypeDefault, &memory_info);
if (status != NULL) {
fprintf(stderr, "Error creating memory info\n");
return -1;
}
// Create input tensors
OrtValue* features_tensor = NULL;
OrtValue* vad_state_tensor = NULL;
OrtValue* noise_state_tensor = NULL;
OrtValue* denoise_state_tensor = NULL;
status = ctx->api->CreateTensorWithDataAsOrtValue(
memory_info, features_data, 42 * sizeof(float),
features_shape, 3, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &features_tensor);
if (status != NULL) {
fprintf(stderr, "Error creating features tensor\n");
ctx->api->ReleaseMemoryInfo(memory_info);
return -1;
}
status = ctx->api->CreateTensorWithDataAsOrtValue(
memory_info, vad_state_data, 24 * sizeof(float),
vad_state_shape, 2, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &vad_state_tensor);
if (status != NULL) {
fprintf(stderr, "Error creating VAD state tensor\n");
ctx->api->ReleaseValue(features_tensor);
ctx->api->ReleaseMemoryInfo(memory_info);
return -1;
}
status = ctx->api->CreateTensorWithDataAsOrtValue(
memory_info, noise_state_data, 48 * sizeof(float),
noise_state_shape, 2, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &noise_state_tensor);
if (status != NULL) {
fprintf(stderr, "Error creating noise state tensor\n");
ctx->api->ReleaseValue(features_tensor);
ctx->api->ReleaseValue(vad_state_tensor);
ctx->api->ReleaseMemoryInfo(memory_info);
return -1;
}
status = ctx->api->CreateTensorWithDataAsOrtValue(
memory_info, denoise_state_data, 96 * sizeof(float),
denoise_state_shape, 2, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &denoise_state_tensor);
if (status != NULL) {
fprintf(stderr, "Error creating denoise state tensor\n");
ctx->api->ReleaseValue(features_tensor);
ctx->api->ReleaseValue(vad_state_tensor);
ctx->api->ReleaseValue(noise_state_tensor);
ctx->api->ReleaseMemoryInfo(memory_info);
return -1;
}
// Prepare input names and tensors
const char* input_names[] = {ctx->input_name, ctx->input_name_vad_state,
ctx->input_name_noise_state, ctx->input_name_denoise_state};
OrtValue* input_tensors[] = {features_tensor, vad_state_tensor, noise_state_tensor, denoise_state_tensor};
// Prepare output names
const char* output_names[] = {ctx->output_name_denoise, ctx->output_name_vad,
ctx->output_name_vad_state, ctx->output_name_noise_state,
ctx->output_name_denoise_state};
OrtValue* output_tensors[5] = {NULL, NULL, NULL, NULL, NULL};
// Run inference
status = ctx->api->Run(ctx->session, NULL, input_names, (const OrtValue* const*)input_tensors, 4,
output_names, 5, output_tensors);
if (status != NULL) {
fprintf(stderr, "Error running inference\n");
ctx->api->ReleaseValue(features_tensor);
ctx->api->ReleaseValue(vad_state_tensor);
ctx->api->ReleaseValue(noise_state_tensor);
ctx->api->ReleaseValue(denoise_state_tensor);
ctx->api->ReleaseMemoryInfo(memory_info);
return -1;
}
// Get output data
float* denoise_output = NULL;
float* vad_output = NULL;
float* updated_vad_state = NULL;
float* updated_noise_state = NULL;
float* updated_denoise_state = NULL;
status = ctx->api->GetTensorMutableData(output_tensors[0], (void**)&denoise_output);
if (status != NULL) {
fprintf(stderr, "Error getting denoise output data\n");
goto cleanup;
}
status = ctx->api->GetTensorMutableData(output_tensors[1], (void**)&vad_output);
if (status != NULL) {
fprintf(stderr, "Error getting VAD output data\n");
goto cleanup;
}
status = ctx->api->GetTensorMutableData(output_tensors[2], (void**)&updated_vad_state);
if (status != NULL) {
fprintf(stderr, "Error getting updated VAD state data\n");
goto cleanup;
}
status = ctx->api->GetTensorMutableData(output_tensors[3], (void**)&updated_noise_state);
if (status != NULL) {
fprintf(stderr, "Error getting updated noise state data\n");
goto cleanup;
}
status = ctx->api->GetTensorMutableData(output_tensors[4], (void**)&updated_denoise_state);
if (status != NULL) {
fprintf(stderr, "Error getting updated denoise state data\n");
goto cleanup;
}
// Store results
memcpy(gains, denoise_output, NB_BANDS * sizeof(float));
*vad = vad_output[0];
// Update GRU states with the outputs from the model (for next frame)
memcpy(ctx->vad_gru_state, updated_vad_state, 24 * sizeof(float));
memcpy(ctx->noise_gru_state, updated_noise_state, 48 * sizeof(float));
memcpy(ctx->denoise_gru_state, updated_denoise_state, 96 * sizeof(float));
ctx->gru_states_initialized = 1;
cleanup:
// Cleanup
ctx->api->ReleaseValue(features_tensor);
ctx->api->ReleaseValue(vad_state_tensor);
ctx->api->ReleaseValue(noise_state_tensor);
ctx->api->ReleaseValue(denoise_state_tensor);
for (int i = 0; i < 5; i++) {
if (output_tensors[i]) {
ctx->api->ReleaseValue(output_tensors[i]);
}
}
ctx->api->ReleaseMemoryInfo(memory_info);
return 0;
}
|