gpt4 book ai didi

javascript - WebRTC 传输高音频流采样率

转载 作者:行者123 更新时间:2023-12-02 04:23:32 24 4
gpt4 key购买 nike

给定两个客户端之间的 WebRTC PeerConnection,一个客户端正在尝试向另一个客户端发送音频 MediaStream。
如果此 MediaStream 是 440hz 的振荡器 - 一切正常。音频非常清脆,传输正确。
但是,如果音频为 20000hz,则音频非常嘈杂且噼啪作响 - 我希望什么也听不到,但我却听到了很多噪音。
我相信这可能是连接中发送的采样率问题,也许它没有像我预期的那样以 48000samples/秒的速度发送音频。
有没有办法提高采样率?
这是重现该问题的 fiddle :
https://jsfiddle.net/mb3c5gw1/9/
包含可视化器的最小复制代码:

<button id="btn">start</button>
<canvas id="canvas"></canvas>

<script>class OscilloMeter{constructor(a){this.ctx=a.getContext("2d")}listen(a,b){function c(){g.getByteTimeDomainData(j),d.clearRect(0,0,e,f),d.beginPath();let a=0;for(let c=0;c<h;c++){const e=j[c]/128;var b=e*f/2;d.lineTo(a,b),a+=k}d.lineTo(canvas.width,canvas.height/2),d.stroke(),requestAnimationFrame(c)}const d=this.ctx,e=d.canvas.width,f=d.canvas.height,g=b.createAnalyser(),h=g.fftSize=256,j=new Uint8Array(h),k=e/h;d.lineWidth=2,a.connect(g),c()}}</script>
btn.onclick = e => {

const ctx = new AudioContext();

const source = ctx.createMediaStreamDestination();

const oscillator = ctx.createOscillator();

oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(20000, ctx.currentTime); // value in hertz
oscillator.connect(source);
oscillator.start();



// a visual cue of AudioNode out (uses an AnalyserNode)
const meter = new OscilloMeter(canvas);

const pc1 = new RTCPeerConnection(),
pc2 = new RTCPeerConnection();

pc2.ontrack = ({
track
}) => {
const endStream = new MediaStream([track]);
const src = ctx.createMediaStreamSource(endStream);

const audio = new Audio();
audio.srcObject = endStream;
meter.listen(src, ctx);
audio.play()
};

pc1.onicecandidate = e => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = e => pc1.addIceCandidate(e.candidate);
pc1.oniceconnectionstatechange = e => console.log(pc1.iceConnectionState);
pc1.onnegotiationneeded = async e => {
try {
await pc1.setLocalDescription(await pc1.createOffer());
await pc2.setRemoteDescription(pc1.localDescription);
await pc2.setLocalDescription(await pc2.createAnswer());
await pc1.setRemoteDescription(pc2.localDescription);
} catch (e) {
console.error(e);
}
}


const stream = source.stream;
pc1.addTrack(stream.getAudioTracks()[0], stream);

};

最佳答案

在 webrtc 演示中环顾四周,我发现:https://webrtc.github.io/samples/src/content/peerconnection/audio/在示例中,它们显示了一个下拉列表,您可以在其中设置音频编解码器。我认为这是您的解决方案。

关于javascript - WebRTC 传输高音频流采样率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63844317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com