-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtflite-c-win.cpp
282 lines (227 loc) · 8.15 KB
/
tflite-c-win.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cstring>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#define DR_WAV_IMPLEMENTATION
#include <tensorflow\lite\c\c_api.h>
#ifndef MIN
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#endif
#include "pocketfft_hdronly.h"
#include <iostream>
#include "sin.h"
#include <cmath>
#define block_len 512
#define block_shift 256
#define fft_out_size (block_len / 2 + 1)
#define BLOCK_SIZE 256
#define gru_size 128 * 32 * 2
using namespace pocketfft;
using namespace std;
typedef complex<double> cpx_type;
typedef unsigned char BYTE;
using namespace std;
typedef std::complex<double> Complex;
struct trg_engine {
float in_buffer[block_len] = { 0 };
float out_buffer[block_len] = { 0 };
float states_1[gru_size] = { 0 };
TfLiteTensor* input_details_1[2];
const TfLiteTensor* output_details_1[4];
TfLiteInterpreter* interpreter_dpcrn;
TfLiteModel* model_dpcrn;
};
void audio_denoise(char* in_file, char* out_file);
#define S16_INPUT_RAW
void f32_16khz_to_s16_16khz(float* in, short* out, int count)
{
for (int i = 0; i < count / BLOCK_SIZE; i++)
{
for (int j = 0; j < BLOCK_SIZE; j++)
out[j] = in[j] * 32767.f;
in += BLOCK_SIZE;
out += BLOCK_SIZE;
}
}
void calc_mag_phase(vector<cpx_type> fft_res, float* inp, int count)
{
for (int i = 0; i < count; i++)
{
inp[i * 3] = fft_res[i].real();
inp[i * 3 + 1] = fft_res[i].imag();
inp[i * 3 + 2] = 2 * log(sqrtf(fft_res[i].real() * fft_res[i].real() + fft_res[i].imag() * fft_res[i].imag()));
}
}
void tflite_create(trg_engine* engine)
{
engine->model_dpcrn = TfLiteModelCreateFromFile("model/dpcrn_quant.tflite");
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
TfLiteInterpreterOptionsSetNumThreads(options, 1);
engine->interpreter_dpcrn = TfLiteInterpreterCreate(engine->model_dpcrn, options);
if (engine->interpreter_dpcrn == nullptr) {
printf("Failed to create interpreter");
return;
}
if (TfLiteInterpreterAllocateTensors(engine->interpreter_dpcrn) != kTfLiteOk) {
printf("Failed to allocate tensors!");
return;
}
engine->input_details_1[0] = TfLiteInterpreterGetInputTensor(engine->interpreter_dpcrn, 0);
engine->input_details_1[1] = TfLiteInterpreterGetInputTensor(engine->interpreter_dpcrn, 1);
engine->output_details_1[0] = TfLiteInterpreterGetOutputTensor(engine->interpreter_dpcrn, 0);
engine->output_details_1[1] = TfLiteInterpreterGetOutputTensor(engine->interpreter_dpcrn, 1);
engine->output_details_1[2] = TfLiteInterpreterGetOutputTensor(engine->interpreter_dpcrn, 2);
engine->output_details_1[3] = TfLiteInterpreterGetOutputTensor(engine->interpreter_dpcrn, 3);
}
void tflite_destroy(trg_engine* engine)
{
TfLiteModelDelete(engine->model_dpcrn);
}
void tflite_infer(trg_engine* engine)
{
float inp[(block_len / 2 + 1) * 3] = { 0 };
float estimated_block[block_len];
double fft_in[block_len];
vector<cpx_type> fft_res(block_len);
shape_t shape;
shape.push_back(block_len);
shape_t axes;
axes.push_back(0);
stride_t stridel, strideo;
strideo.push_back(sizeof(cpx_type));
stridel.push_back(sizeof(double));
for (int i = 0; i < block_len; i++)
{
fft_in[i] = engine->in_buffer[i] * win_sin[i];
}
r2c(shape, stridel, strideo, axes, FORWARD, fft_in, fft_res.data(), 1.0);
calc_mag_phase(fft_res, inp, fft_out_size);
memcpy(engine->input_details_1[0]->data.f, inp, fft_out_size * 3 * sizeof(float));
memcpy(engine->input_details_1[1]->data.f, engine->states_1, gru_size * sizeof(float));
if (TfLiteInterpreterInvoke(engine->interpreter_dpcrn) != kTfLiteOk) {
printf("Error invoking detection model");
}
float* out_mask = engine->output_details_1[0]->data.f;
float* out_cos = engine->output_details_1[1]->data.f;
float* out_sin = engine->output_details_1[2]->data.f;
memcpy(engine->states_1, engine->output_details_1[3]->data.f, gru_size * sizeof(float));
for (int i = 0; i < fft_out_size; i++) {
fft_res[i] = complex<double>{ fft_res[i].real() * out_mask[i] * out_cos[i] - fft_res[i].imag() * out_mask[i] * out_sin[i], fft_res[i].real() * out_mask[i] * out_sin[i] + fft_res[i].imag() * out_mask[i] * out_cos[i] };
}
c2r(shape, strideo, stridel, axes, BACKWARD, fft_res.data(), fft_in, 1.0);
for (int i = 0; i < block_len; i++)
estimated_block[i] = (fft_in[i] / block_len) * win_sin[i];
memmove(engine->out_buffer, engine->out_buffer + block_shift, (block_len - block_shift) * sizeof(float));
memset(engine->out_buffer + (block_len - block_shift), 0, block_shift * sizeof(float));
for (int i = 0; i < block_len; i++)
engine->out_buffer[i] += estimated_block[i];
}
void trg_denoise(trg_engine* engine, float* samples, float* out, int sampleCount)
{
int num_blocks = sampleCount / block_shift;
for (int idx = 0; idx < num_blocks; idx++)
{
memmove(engine->in_buffer, engine->in_buffer + block_shift, (block_len - block_shift) * sizeof(float));
memcpy(engine->in_buffer + (block_len - block_shift), samples, block_shift * sizeof(float));
tflite_infer(engine);
memcpy(out, engine->out_buffer, block_shift * sizeof(float));
samples += block_shift;
out += block_shift;
}
}
void s16_16khz_to_f32_16khz(short* in, float* out, int count)
{
for (int j = 0; j < count; j++)
out[j] = in[j] / 32767.f;
}
void floatTobytes(float* data, BYTE* bytes, int dataLength)
{
int i;
size_t length = sizeof(float) * dataLength;
BYTE* pdata = (BYTE*)data;
for (i = 0; i < length; i++)
{
bytes[i] = *pdata++;
}
return;
}
float bytesToFloat(BYTE* bytes)
{
return *((float*)bytes);
}
void shortToByte(short* data, BYTE* bytes, int dataLength)
{
for (int i = 0; i < dataLength; i++) {
bytes[i * 2] = (BYTE)(0xff & data[i]);
bytes[i * 2 + 1] = (BYTE)((0xff00 & data[i]) >> 8);
}
return;
}
short bytesToShort(BYTE* bytes)
{
short addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
return addr;
}
void ByteToChar(BYTE* bytes, char* chars, unsigned int count) {
for (unsigned int i = 0; i < count; i++)
chars[i] = (char)bytes[i];
}
void audio_denoise(char* in_file, char* out_file, trg_engine* eng1)
{
uint32_t sampleRate = 16000;
uint64_t inSampleCount = 0;
#ifdef S16_INPUT_RAW
inSampleCount = BLOCK_SIZE * 2;
short inBuffer_s16_16k[BLOCK_SIZE];
FILE* fp = fopen(in_file, "rb");
FILE* fpf_out = fopen(out_file, "wb");
if (!fp)
{
printf("Please change input file path.\n");
return;
}
while (!feof(fp))
{
BYTE* inBuffer_byte_16k = (BYTE*)malloc(inSampleCount * sizeof(BYTE));
fread(inBuffer_byte_16k, inSampleCount, 1, fp);
for (int i = 0, j = 0; i < inSampleCount; i = i + 2)
{
inBuffer_s16_16k[j] = bytesToShort(inBuffer_byte_16k);
inBuffer_byte_16k = inBuffer_byte_16k + 2;
j++;
}
float f32_sample[BLOCK_SIZE];
float outBuffer_f32_16khz[BLOCK_SIZE];
short out_s16_16khz[BLOCK_SIZE];
BYTE out_bytes[BLOCK_SIZE * 2];
char out_chars[BLOCK_SIZE * 2];
s16_16khz_to_f32_16khz(inBuffer_s16_16k, f32_sample, BLOCK_SIZE);
trg_denoise(eng1, f32_sample, outBuffer_f32_16khz, BLOCK_SIZE);
f32_16khz_to_s16_16khz(outBuffer_f32_16khz, out_s16_16khz, BLOCK_SIZE);
shortToByte(out_s16_16khz, out_bytes, BLOCK_SIZE);
fwrite(out_bytes, BLOCK_SIZE * 2, 1, fpf_out);
ByteToChar(out_bytes, out_chars, BLOCK_SIZE * 2);
inBuffer_byte_16k++;
}
tflite_destroy(eng1);
fclose(fp);
#else
float* inBuffer = wavRead_scalar(in_file, &sampleRate, &inSampleCount);
#endif
}
int main() {
printf("audio denoise by dpcrn_tflite model.\n");
char* in_file = (char*)"440C020A_mix.wav";
char* out_file = (char*)"440C020A_mix.pcm";
trg_engine eng1;
tflite_create(&eng1);
audio_denoise(in_file, out_file, &eng1);
printf("press any key to exit.\n");
return 0;
}