-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost.js
320 lines (284 loc) · 10.3 KB
/
post.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright (C) 2023 Yahweasel
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
var aec3 = {
create: Module.cwrap(
"WebRtcAec3_create", "number", ["number", "number", "number"]
),
free: Module.cwrap("WebRtcAec3_free", null, ["number"]),
setAudioBufferDelay: Module.cwrap(
"WebRtcAec3_setAudioBufferDelay", null, ["number", "number"]
),
abNumFrames: Module.cwrap(
"WebRtcAudioBuffer_num_frames", "number", ["number"]
),
abChannels: Module.cwrap(
"WebRtcAudioBuffer_channels", "number", ["number"]
),
abSplitIntoFrequencyBands: Module.cwrap(
"WebRtcAudioBuffer_splitIntoFrequencyBands", null, ["number"]
),
abMergeFrequencyBands: Module.cwrap(
"WebRtcAudioBuffer_mergeFrequencyBands", null, ["number"]
),
abCopyIn: Module.cwrap(
"WebRtcAudioBuffer_copyIn", null, [
"number", "number", "number", "number"
]
),
abCopyOut: Module.cwrap(
"WebRtcAudioBuffer_copyOut", null, [
"number", "number", "number", "number"
]
),
mkRenderBuffer: Module.cwrap(
"WebRtcAec3_mkRenderBuffer", "number", [
"number", "number", "number", "number", "number", "number", "number"
]
),
mkCaptureBuffer: Module.cwrap(
"WebRtcAec3_mkCaptureBuffer", "number", [
"number", "number", "number", "number", "number", "number", "number"
]
),
analyzeRender: Module.cwrap("WebRtcAec3_analyzeRender", null, ["number"]),
analyzeCapture: Module.cwrap("WebRtcAec3_analyzeCapture", null, ["number"]),
processCapture: Module.cwrap(
"WebRtcAec3_processCapture", null, ["number", "number"]
),
};
(function() {
var buffers = [
"renderIn", "captureIn",
"render", "capture",
"renderOut", "captureOut"
];
for (var bi = 0; bi < buffers.length; bi++) {
var bn = buffers[bi] + "Buffer";
aec3[bn] = Module.cwrap("WebRtcAec3_" + bn, "number", ["number"]);
}
})();
Module.AEC3 = function(sampleRate, renderNumChannels, captureNumChannels) {
/**
* Remember metadata.
*/
this.sampleRate = sampleRate;
this.renderNumChannels = renderNumChannels;
this.captureNumChannels = captureNumChannels;
// Pointer to the instance itself
var ptr = this._instance = aec3.create(
sampleRate, renderNumChannels, captureNumChannels
);
// Render buffer
this._renderBuf = {
sampleRateIn: 0,
sampleRateOut: 0
};
this._assertBuf(
this._renderBuf,
sampleRate, renderNumChannels,
sampleRate, renderNumChannels,
aec3.mkRenderBuffer, aec3.renderBuffer, aec3.renderOutBuffer
);
// Capture buffer
this._captureBuf = {
sampleRateIn: 0,
sampleRateOut: 0
};
this._assertBuf(
this._captureBuf,
sampleRate, captureNumChannels,
sampleRate, captureNumChannels,
aec3.mkCaptureBuffer, aec3.captureBuffer, aec3.captureOutBuffer
);
};
Object.assign(Module.AEC3.prototype, {
free: function() {
aec3.free(this._instance);
},
setAudioBufferDelay: function(to) {
aec3.setAudioBufferDelay(this._instance, to);
},
// Assert that a buffer is as needed
_assertBuf: function(
buf,
sampleRateOut, channelsOut,
sampleRateIn, channelsIn,
inCtor, bufGetter, outGetter
) {
if (buf.sampleRateOut !== sampleRateOut ||
buf.sampleRateIn !== sampleRateIn ||
buf.out.length !== channelsOut ||
buf.inp.length !== channelsIn) {
var ptr = buf.inpPtr = inCtor(
this._instance,
sampleRateIn, channelsIn,
this.sampleRate, channelsOut,
sampleRateOut, channelsOut
);
buf.inp = this._floatPtrPtr(
aec3.abChannels(ptr), channelsIn, aec3.abNumFrames(ptr)
);
ptr = buf.bufPtr = bufGetter(this._instance);
buf.buf = this._floatPtrPtr(
aec3.abChannels(ptr), channelsOut, aec3.abNumFrames(ptr)
);
ptr = buf.outPtr = outGetter(this._instance);
buf.out = this._floatPtrPtr(
aec3.abChannels(ptr), channelsOut, aec3.abNumFrames(ptr)
);
buf.sampleRateIn = sampleRateIn;
buf.sampleRateOut = sampleRateOut;
buf.pos = 0;
}
return buf;
},
_floatPtrPtr: function(floatPtrPtr, floatPtrArrSz, floatArrSz) {
var floatPtrArr = new Uint32Array(
Module.HEAPU8.buffer, floatPtrPtr, floatPtrArrSz
);
return Array.from(floatPtrArr).map(function(ptr) {
return new Float32Array(Module.HEAPU8.buffer, ptr, floatArrSz);
});
},
// Perform an action one buffer at a time
_bufAtATime: function(buf, data, act) {
var bufPos = buf.pos;
var dataPos = 0;
while (true) {
var bufRem = buf.inp[0].length - bufPos;
var dataRem = data[0].length - dataPos;
// Copy in some data
if (dataRem >= bufRem) {
// We can fill an entire buffer
for (var ch = 0; ch < data.length; ch++) {
buf.inp[ch].set(
data[ch].subarray(dataPos, dataPos + bufRem),
bufPos
);
}
// And act
aec3.abCopyIn(
buf.bufPtr, buf.inpPtr, buf.sampleRateIn, buf.inp.length
);
act();
bufPos = 0;
dataPos += bufRem;
} else if (dataRem) {
// Leave some overflow
for (var ch = 0; ch < data.length; ch++) {
buf.inp[ch].set(
data[ch].subarray(dataPos), bufPos
);
}
bufPos += dataRem;
break;
} else break;
}
buf.pos = bufPos;
},
/**
* Analyze this render data. Will process as much as can be eagerly.
* @param data Data to analyze.
* @param opts Options.
*/
analyze: function(data, opts) {
var self = this;
opts = opts || {};
var buf = this._renderBuf;
this._assertBuf(
buf,
opts.sampleRateOut || this.sampleRate, this.renderNumChannels,
opts.sampleRateIn || this.sampleRate, data.length,
aec3.mkRenderBuffer, aec3.renderBuffer, aec3.renderOutBuffer
);
this._bufAtATime(
this._renderBuf, data, function() {
aec3.abSplitIntoFrequencyBands(buf.bufPtr);
aec3.analyzeRender(self._instance);
}
);
},
/**
* Get the length of the output data given this input data. That is, if you
* process this data now, how many samples will the output have?
* @param data Data that will be processed.
* @param opts Options. Must be the same as will be used by `process`.
*/
processSize: function(data, opts) {
opts = opts || {};
var inSampleRate = opts.sampleRateIn || this.sampleRate;
var inFrameSize = ~~(inSampleRate / 100);
var outSampleRate = opts.sampleRateOut || this.sampleRate;
var outFrameSize = ~~(outSampleRate / 100);
var samples = data[0].length;
var buf = this._captureBuf;
if (buf.sampleRateIn === inSampleRate &&
buf.sampleRateOut === outSampleRate &&
buf.inp.length === data.length) {
// Any overflowed data will be included
samples += buf.pos;
}
var frames = ~~(samples / inFrameSize);
return frames * outFrameSize;
},
/**
* Process this data, i.e., remove echo. The processed data is deposited
* into a buffer that the user must provide (as `out`). The output must be
* an array of Float32Arrays: the outer array (array of channels) must be
* `captureNumChannels` in length, and each Float32Array must have the
* length given by `processSize` or more.
* @param out Output for processed data.
* @param data Data to process.
* @param opts Opts.
*/
process: function(out, data, opts) {
var self = this;
opts = opts || {};
this._assertBuf(
this._captureBuf,
opts.sampleRateOut || this.sampleRate, this.captureNumChannels,
opts.sampleRateIn || this.sampleRate, data.length,
aec3.mkCaptureBuffer, aec3.captureBuffer, aec3.captureOutBuffer
);
var buf = this._captureBuf;
var ret = [];
var outIdx = 0;
this._bufAtATime(
buf, data, function() {
if (opts.pre) {
// Get the separated data before processing
aec3.abCopyOut(
buf.outPtr, buf.bufPtr, buf.sampleRateOut,
buf.out.length
);
for (var c = 0; c < buf.out.length; c++)
opts.pre[c].set(buf.out[c], outIdx);
}
// Process with AEC3, then suppress
aec3.abSplitIntoFrequencyBands(buf.bufPtr);
aec3.analyzeCapture(self._instance);
aec3.processCapture(self._instance, 0);
aec3.abMergeFrequencyBands(buf.bufPtr);
// And copy out
aec3.abCopyOut(
buf.outPtr, buf.bufPtr, buf.sampleRateOut, buf.out.length
);
for (var c = 0; c < buf.out.length; c++)
out[c].set(buf.out[c], outIdx);
outIdx += buf.out[0].length;
}
);
}
});