forked from webmachinelearning/webnn-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processer.js
65 lines (56 loc) · 1.88 KB
/
processer.js
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
'use strict';
export class Processer {
constructor(audioContext, audioElement, frames) {
this.audioContext = audioContext;
this.audioElement = audioElement;
this.frames = frames;
this.framesize = 480;
this.featureSize = 42;
this.gainsSize = 22;
}
async getAudioPCMData() {
const request = new Request(this.audioElement.src);
const response = await fetch(request);
const audioFileData = await response.arrayBuffer();
const audioDecodeData =
await this.audioContext.decodeAudioData(audioFileData);
const audioPCMData = audioDecodeData.getChannelData(0);
return audioPCMData;
}
preProcessing(pcm) {
const pcmLength = this.framesize * this.frames;
const featuresLength = this.featureSize * this.frames;
const pcmPtr = Module._malloc(4 * pcmLength);
for (let i = 0; i < pcmLength; i++) {
Module.HEAPF32[pcmPtr / 4 + i] = pcm[i];
}
const getFeatures = Module.cwrap(
'pre_processing', 'number', ['number', 'number'],
);
const featuresPtr = getFeatures(pcmPtr, this.frames);
const features = [];
for (let i = 0; i < featuresLength; i++) {
features[i] = Module.HEAPF32[(featuresPtr >> 2) + i];
}
Module._free(pcmPtr, featuresPtr);
return features;
}
postProcessing(gains) {
const audioLength = this.framesize * this.frames;
const gainsLength = this.gainsSize * this.frames;
const gainsPtr = Module._malloc(4 * gainsLength);
for (let i = 0; i < gainsLength; i++) {
Module.HEAPF32[gainsPtr / 4 + i] = gains[i];
}
const getAudio = Module.cwrap(
'post_processing', 'number', ['number', 'number'],
);
const audioPtr = getAudio(gainsPtr, this.frames);
const audio = [];
for (let i = 0; i < audioLength; i++) {
audio[i] = Module.HEAPF32[(audioPtr >> 2) + i];
}
Module._free(gainsPtr, audioPtr);
return audio;
}
}