js中pcm怎么转g711a编码?

在vue项目里面通过navigator.mediaDevices.getUserMedia({ audio: true }).then((stream)采集到的音频,怎么转成G711A编码?网上找的都是java、C这些语言的,要么就是node写的客户端等等,没有看到关于纯js的


回答:

const audioContext = new AudioContext();

const source = audioContext.createMediaStreamSource(stream);

const buffer = await audioContext.decodeAudioData(await stream.getAudioTracks()[0].getBuffer());

const g711a = audioContext.createScriptProcessor(1024, 1, 1);

g711a.onaudioprocess = (event) => {

const input = event.inputBuffer.getChannelData(0);

const output = new Float32Array(input.length);

for (let i = 0; i < input.length; i++) {

const sample = input[i];

if (sample < -1) {

sample = -1;

} else if (sample > 1) {

sample = 1;

}

output[i] = sample * 255;

}

event.outputBuffer.copyToChannel(output, 0);

};

source.connect(g711a);

g711a.connect(audioContext.destination);

Web Audio
https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Audio_AP...

不知道是否可以

以上是 js中pcm怎么转g711a编码? 的全部内容, 来源链接: utcz.com/p/934979.html

回到顶部