-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathworklet.c
59 lines (53 loc) · 1.57 KB
/
worklet.c
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
#include <emscripten.h>
#include <stdlib.h>
#include <string.h>
#include <rnnoise.h>
#define FRAME_SIZE 480
#define MAX_FRAME_SIZE 16384
static const float scale = -INT16_MIN;
static float buffer[MAX_FRAME_SIZE * 2], vad_prob;
static size_t input, processed, output, buffering, latency;
static DenoiseState *state = NULL;
float *EMSCRIPTEN_KEEPALIVE getInput()
{
// Shifts
if (output && input > MAX_FRAME_SIZE)
{
memmove(buffer, &buffer[output], sizeof(float) * (input -= output));
processed -= output;
output = 0;
}
return &buffer[input];
}
float EMSCRIPTEN_KEEPALIVE getVadProb() { return vad_prob; }
float *EMSCRIPTEN_KEEPALIVE pipe(size_t length)
{
// Increases latency
if (length > buffering)
latency = buffering = (FRAME_SIZE / length + (FRAME_SIZE % length ? 1 : 0)) * length;
// Scales input
for (size_t end = input + length; input < end; ++input)
buffer[input] *= scale;
// Processes
while (processed + FRAME_SIZE <= input)
{
vad_prob = rnnoise_process_frame(state, &buffer[processed], &buffer[processed]);
processed += FRAME_SIZE;
}
// Buffers
if (output + latency > processed)
return NULL;
latency = length;
size_t o = output;
// Scales output
for (size_t end = output + length; output < end; ++output)
buffer[output] /= scale;
return &buffer[o];
}
void EMSCRIPTEN_KEEPALIVE reset()
{
if (state)
rnnoise_destroy(state);
vad_prob = latency = buffering = output = processed = input = 0;
state = rnnoise_create(NULL);
}