You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于ToneJS结合MediaStream仅提取变调后音频流的技术咨询

ToneJS结合MediaStream仅提取变调后音频流的技术咨询

Hey there! Let's break down why you're hearing a mix of the original and pitch-shifted audio with a delay, and fix it so only the processed audio gets sent to remote users.

First, diagnose the core issue

The most likely culprit here is that your original microphone track is still being transmitted alongside the pitch-shifted one, or the audio is leaking through an unprocessed path. The delay you hear comes from the buffering that Tone.js's PitchShift uses to handle pitch manipulation—if the original audio takes a lower-latency route to the remote user, it arrives first, followed by the delayed processed signal, creating that mixed echo effect.

Fix steps with modified code

Here's how to adjust your implementation to ensure only the pitch-shifted audio is sent:

  1. Use Tone.js's native MediaStreamSource
    Instead of creating a native Web Audio MediaStreamSource, use Tone.js's wrapper to ensure better compatibility with the rest of the Tone processing chain.

  2. Double-check you're only sending the processed track
    Make sure the remote users are receiving only the track from dstNode.stream, and that you haven't accidentally included the original localMicrophoneTrack in the stream you're sending.

  3. Simplify your connection chain
    Tone.js supports chained connections, which makes it easier to verify your signal flow has no leaks.

Modified code example:

// Initialize Tone Context
const audioContext = new Tone.Context();
Tone.setContext(audioContext);

// Use Tone's MediaStreamSource instead of native Web Audio's
const mediaStreamSource = new Tone.MediaStreamSource(
  new MediaStream([localMicrophoneTrack.getMediaStreamTrack()])
);

const gainNode = new Tone.Gain();
const pitchShift = new Tone.PitchShift({ windowSize: 0.03, pitch: 6 });
const dstNode = audioContext.createMediaStreamDestination();

// Chain the connections cleanly
mediaStreamSource.connect(gainNode).connect(pitchShift).connect(dstNode);

// Grab only the processed audio track
const processedTrack = dstNode.stream.getAudioTracks()[0];

Additional checks to eliminate leaks

  • Disable original track usage: If you were using localMicrophoneTrack for local preview or another stream, stop that track or remove it from any active MediaStreams being sent.
  • Activate the audio context: Browsers require user interaction (like a button click) to activate audio contexts. Ensure you've triggered this before processing starts—if the context is suspended, your processing chain might not work, and the original audio could slip through.
  • Verify no extra connections: Check if the original MediaStreamSource (or microphone track) is connected to any other nodes or destinations that might be sending audio to the remote user.

That should resolve the mixed audio issue and ensure only the pitch-shifted track is transmitted.

备注:内容来源于stack exchange,提问作者Nazarii Shvets

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 10:22:58