-
Notifications
You must be signed in to change notification settings - Fork 31
/
resampler.ts
52 lines (48 loc) · 1.35 KB
/
resampler.ts
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
///<reference path="typings/emscripten.d.ts" />
/// <reference path="speex_resampler.ts" />
class ResamplingWorker {
worker: Worker;
resampler: SpeexResampler = null;
constructor(worker: Worker) {
this.worker = worker;
this.worker.onmessage = (e: MessageEvent) => {
this.setup(<any>e.data);
};
}
setup(config: any) {
try {
this.resampler = new SpeexResampler(
config.channels,
config.in_sampling_rate,
config.out_sampling_rate,
config.quality || 5
);
this.worker.postMessage({
status: 0
});
this.worker.onmessage = (e: MessageEvent) => {
this.process(<Float32Array>e.data.samples);
};
} catch (e) {
this.worker.postMessage({
status: -1,
reason: e
});
}
}
process(input: Float32Array) {
try {
var ret = new Float32Array(this.resampler.process(input));
this.worker.postMessage({
status: 0,
result: ret,
}, [ret.buffer]);
} catch (e) {
this.worker.postMessage({
status: -1,
reason: e
});
}
}
}
new ResamplingWorker(this);