174 lines
6.2 KiB
JavaScript
174 lines
6.2 KiB
JavaScript
/*
|
|
* Author: Liu Ninggang
|
|
* File Created: Monday, 27th September 2021 10:28:13 am
|
|
* Last Modified: Wednesday, 29th September 2021 5:36:21 pm
|
|
* Modified By: Liu Ninggang
|
|
* Copyright (c) 2021 TP-LINK
|
|
*/
|
|
|
|
/* eslint-disable */
|
|
let inputBuffer;
|
|
let outputBuffer;
|
|
let finalInputBuffer;
|
|
let finalOutputBuffer;
|
|
let has_init = 0;
|
|
let isG726 = true;
|
|
var Module = {};
|
|
let audioDeviceDataBuffer;
|
|
let sendDeviceBuffer;
|
|
let has_audio_init = 0;
|
|
let lastAACSampleRate = 0;
|
|
let has_post_audio_process_init = 0;
|
|
let lastAudioDecodeSampleRate = 0;
|
|
|
|
postMessage({ hasInstalled: true });
|
|
|
|
this.onmessage = function (event) {
|
|
let { cmd } = event.data;
|
|
let { isStop, decoderType } = event.data;
|
|
if (isStop) {
|
|
if (has_post_audio_process_init || has_audio_init) {
|
|
Module._tp_audio_deinit();
|
|
}
|
|
return;
|
|
}
|
|
if (cmd === 'init') {
|
|
let { scripts, wasmBinary } = event.data;
|
|
Module.wasmBinary = wasmBinary;
|
|
self.importScripts(...scripts);
|
|
return;
|
|
}
|
|
if (!Module['asm']) return;
|
|
|
|
if (!has_init) {
|
|
has_init = 1;
|
|
audioDeviceDataBuffer = Module._malloc(1024 * 2);
|
|
inputBuffer = Module._malloc(1024 * 4);
|
|
outputBuffer = Module._malloc(1024 * 4);
|
|
finalOutputBuffer = Module._malloc(1024 * 4);
|
|
postMessage({ hasInit: true });
|
|
return;
|
|
}
|
|
if (cmd === 'flush') {
|
|
postMessage({ flushed: true });
|
|
return;
|
|
}
|
|
let { dts, pts, timestamp, seq, type, samplerate, bitCount, mode } = event.data;
|
|
if (decoderType !== '_decodePCM') {
|
|
let newSampleRate = samplerate || 8000;
|
|
if (lastAudioDecodeSampleRate != newSampleRate) {
|
|
if (has_post_audio_process_init) {
|
|
Module._tp_audio_deinit();
|
|
}
|
|
has_post_audio_process_init = 1;
|
|
// _tp_audio_init(sample_rate,
|
|
// ns_enable, ns_mode, ns_intensity, ns_threshold, ns_twice_process_enable, ns_gain,
|
|
// echo_intensity, aec_gain,
|
|
// tp_ao_agc_enable, ao_target_level, ao_none_motor_compression_gain)
|
|
let initRet = Module._tp_audio_init(newSampleRate,
|
|
1, 1, 10, 30000, 0, 1,
|
|
4, 1,
|
|
0, -1, 12);
|
|
if (initRet !== 0) {
|
|
has_post_audio_process_init = 0;
|
|
Module._tp_audio_deinit();
|
|
}
|
|
lastAudioDecodeSampleRate = newSampleRate;
|
|
}
|
|
}
|
|
let param = {
|
|
pts: pts,
|
|
dts: dts,
|
|
timestamp: timestamp,
|
|
seq: seq,
|
|
type: type,
|
|
samplerate: samplerate,
|
|
bitCount: bitCount
|
|
};
|
|
const chunk = event.data.data;
|
|
Module.HEAPU8.set(chunk, inputBuffer);
|
|
let chunk_num;
|
|
if (decoderType === '_decodeG726') {
|
|
if (isG726) {
|
|
Module._initG726State(0, bitCount);
|
|
isG726 = false;
|
|
}
|
|
chunk_num = Module._decodeG726(0, inputBuffer, chunk.length, outputBuffer);
|
|
if (chunk_num === 1) {
|
|
let getLength = ((chunk.length << 4) / bitCount) >>> 1;
|
|
let procRet = -1;
|
|
if (has_post_audio_process_init) {
|
|
procRet = Module._tp_audio_process(outputBuffer, null, inputBuffer, getLength * 2);
|
|
}
|
|
let resultPCM = procRet !== 0 ? outputBuffer : inputBuffer;
|
|
let finalOutArray = Module.HEAP16.subarray(resultPCM >> 1, (resultPCM + getLength * 2) >> 1);
|
|
finalOutArray = new Int16Array(finalOutArray);
|
|
postMessage({
|
|
data: finalOutArray.buffer,
|
|
...param
|
|
});
|
|
}
|
|
} else if (decoderType === '_decodeAAC') {
|
|
if (lastAACSampleRate <= 0) {
|
|
lastAACSampleRate = samplerate;
|
|
}
|
|
if (lastAACSampleRate != samplerate) {
|
|
Module._destroy_AAC_decoder();
|
|
}
|
|
lastAACSampleRate = samplerate;
|
|
var pcmLen = Module._decodeAAC(outputBuffer, inputBuffer, chunk.length);
|
|
|
|
if (pcmLen >= 0) {
|
|
let procRet = -1;
|
|
if (has_post_audio_process_init) {
|
|
procRet = Module._tp_audio_process(outputBuffer, null, inputBuffer, pcmLen * 2);
|
|
}
|
|
let resultPCM = procRet !== 0 ? outputBuffer : inputBuffer;
|
|
let finalOutArray = Module.HEAP16.subarray(resultPCM >> 1, (resultPCM + pcmLen * 2) >> 1);
|
|
finalOutArray = new Int16Array(finalOutArray);
|
|
postMessage({
|
|
data: finalOutArray.buffer,
|
|
...param
|
|
});
|
|
}
|
|
} else if (decoderType === '_decodePCM') {
|
|
if (!has_audio_init) {
|
|
has_audio_init = 1;
|
|
// _tp_audio_init(sample_rate,
|
|
// ns_enable, ns_mode, ns_intensity, ns_threshold, ns_twice_process_enable, ns_gain,
|
|
// echo_intensity, aec_gain,
|
|
// tp_ao_agc_enable, ao_target_level, ao_none_motor_compression_gain)
|
|
Module._tp_audio_init(samplerate || 8000,
|
|
1, 1, 35, 30000, 0, 1,
|
|
4, 1,
|
|
0, -1, 12);
|
|
}
|
|
Module._tp_audio_process(inputBuffer, null, outputBuffer, chunk.length, 0);
|
|
chunk_num = Module._encodeG711a(inputBuffer, outputBuffer, chunk.length / 2);
|
|
if (chunk_num === 1) {
|
|
// 播放数据
|
|
let finalOutArray = Module.HEAPU8.subarray(inputBuffer, inputBuffer + chunk.length / 2);
|
|
finalOutArray = new Uint8Array(finalOutArray);
|
|
postMessage({
|
|
data: finalOutArray.buffer,
|
|
...param
|
|
});
|
|
}
|
|
} else {
|
|
chunk_num = Module[decoderType](outputBuffer, inputBuffer, chunk.length);
|
|
if (chunk_num === 1) {
|
|
let procRet = -1;
|
|
if (has_post_audio_process_init) {
|
|
procRet = Module._tp_audio_process(outputBuffer, null, inputBuffer, chunk.length * 2);
|
|
}
|
|
let resultPCM = procRet !== 0 ? outputBuffer : inputBuffer;
|
|
// 播放数据
|
|
let finalOutArray = Module.HEAP16.subarray(resultPCM >> 1, (resultPCM + chunk.length * 2) >> 1);
|
|
finalOutArray = new Int16Array(finalOutArray);
|
|
postMessage({
|
|
data: finalOutArray.buffer,
|
|
...param
|
|
});
|
|
}
|
|
}
|
|
}; |