File size: 3,059 Bytes
842ef7c
 
38a9df7
 
 
 
 
 
842ef7c
 
 
38a9df7
842ef7c
38a9df7
 
 
 
 
 
 
 
 
 
 
 
 
842ef7c
 
 
 
 
 
 
 
 
 
 
 
 
38a9df7
842ef7c
 
 
 
 
 
 
 
 
 
 
38a9df7
842ef7c
 
 
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
"""Real-time streaming engine: 80-sample (5 ms) blocks in, 80-sample blocks out, 10 ms algorithmic latency.



Needs only numpy + onnxruntime (no PyTorch). Each call: shift the block into a 512-sample (32 ms) history, apply the

long analysis window, FFT, run one GRU step (state kept between calls), mask, inverse FFT, apply the short synthesis

window, and overlap-add. The emitted block is the PREVIOUS 5 ms of audio, now complete, so input -> output delay is

2 x 80 samples = 10 ms. Same math as model.analysis/synthesis in the training code (parity-checked).

    eng = StreamEngine(); y_block = eng.process(x_block)   # x_block: float32[80] at 16000 Hz

    y = process_file(x)                                     # whole mono 16000 Hz signal, time-aligned output

"""
import os, numpy as np, onnxruntime as ort

SR, K, M, D_ZEROS = 16000, 512, 80, 21          # sample rate, analysis length, hop, leading zeros
ONNX = os.path.join(os.path.dirname(os.path.abspath(__file__)), "call_denoiser_v4.onnx")


def windows():
    """Asymmetric analysis/synthesis pair (Wang et al. 2021): analysis * synthesis = Hann(2M) on the last 2M samples."""
    def hann(n): return 0.5 - 0.5 * np.cos(2 * np.pi * np.arange(n) / n)
    L = 2 * (K - M - D_ZEROS); hL, h2M = hann(L), hann(2 * M)
    wa = np.zeros(K); ws = np.zeros(K)
    wa[D_ZEROS:K - M] = np.sqrt(hL[: K - M - D_ZEROS])
    wa[K - M:] = np.sqrt(h2M[M:])
    seg = slice(K - 2 * M, K - M)
    ws[seg] = h2M[:M] / np.maximum(wa[seg], 1e-12)
    ws[K - M:] = np.sqrt(h2M[M:])
    return wa, ws


class StreamEngine:
    def __init__(self, path=ONNX, layers=2, hidden=256, mask_floor=0.0):
        so = ort.SessionOptions(); so.intra_op_num_threads = 1; so.inter_op_num_threads = 1
        self.s = ort.InferenceSession(path, so, providers=["CPUExecutionProvider"])
        wa, ws = windows(); self.wa = wa.astype(np.float32); self.ws = ws[K - 2 * M:].astype(np.float32)
        self.h = np.zeros((layers, 1, hidden), np.float32); self.floor = mask_floor
        self.inbuf = np.zeros(K, np.float32); self.tail = np.zeros(M, np.float32)

    def reset(self):
        self.h[:] = 0; self.inbuf[:] = 0; self.tail[:] = 0

    def process(self, x):
        self.inbuf[:-M] = self.inbuf[M:]; self.inbuf[-M:] = x
        S = np.fft.rfft(self.inbuf * self.wa)
        m, self.h = self.s.run(None, {"mag": np.abs(S).astype(np.float32)[None, None], "h": self.h})
        m = np.maximum(m[0, 0], self.floor)
        seg = np.fft.irfft(S * m, K)[K - 2 * M:].astype(np.float32) * self.ws
        out = self.tail + seg[:M]; self.tail = seg[M:].copy()
        return out


def process_file(x, **kw):
    """Whole signal through the streaming engine, re-aligned to the input (the engine itself runs 10 ms late)."""
    e = StreamEngine(**kw); x = np.asarray(x, np.float32); n = len(x)
    xp = np.concatenate([x, np.zeros(2 * M, np.float32)]); xp = xp[: (len(xp) // M) * M]
    y = np.concatenate([e.process(xp[i:i + M]) for i in range(0, len(xp), M)])
    return y[M: M + n]