【问题标题】:How to connect Web Audio API to Tone.js?如何将 Web Audio API 连接到 Tone.js?
【发布时间】:2020-02-28 19:05:46
【问题描述】:

我正在做一个 在线音频播放器,所以我想在我的应用中集成 Pitch Shifter,该应用可在 Tone js 上使用但不在 Web Audio API...

所以我的想法是将 Tonejs Pitch Shifter 连接到 Web Audio API 的 audioContext

有什么办法吗?

这是我的参考代码

var audioCtx = new (window.AudioContext || window.webkitAudioContext);

var mediaElem = document.querySelector('audio');

var stream = audioCtx.createMediaElementSource(mediaElem);

var gainNode = audioCtx.createGain();

stream.connect(gainNode);

// tone js

var context = new Tone.Context(audioCtx); // Which is Mentioned in Tonejs Docs!

var pitchShift = new Tone.PitchShift().toMaster();

pitchShift.connect(gainNode);

// Gives Error!
gainNode.connect(audioCtx.destination);

【问题讨论】:

    标签: javascript node.js audio web-audio-api tone.js


    【解决方案1】:

    我猜你想实现这样的信号流:

    mediaElement > gainNode > pitchShift > destination
    

    要确保 Tone.js 使用相同的 AudioContext,您可以使用 Tone 对象上的设置器来分配它。这需要在使用 Tone.js 执行任何其他操作之前完成。

    Tone.context = context;
    

    Tone.js 还导出了一个帮助器,可用于将原生 AudioNodes 连接到 Tone.js 提供的节点。

    Tone.connect(gainNode, pitchShift);
    

    我稍微修改了您的示例代码以合并更改。

    var audioCtx = new (window.AudioContext || window.webkitAudioContext);
    var mediaElem = document.querySelector('audio');
    var stream = audioCtx.createMediaElementSource(mediaElem);
    var gainNode = audioCtx.createGain();
    
    // This a normal connection between to native AudioNodes.
    stream.connect(gainNode);
    
    // Set the context used by Tone.js
    Tone.context = audioCtx;
    
    var pitchShift = new Tone.PitchShift();
    
    // Use the Tone.connect() helper to connect native AudioNodes with the nodes provided by Tone.js
    Tone.connect(gainNode, pitchShift);
    Tone.connect(pitchShift, audioCtx.destination);
    

    【讨论】:

    • 谢谢老兄@chrisguttandin!你帮了我很多!
    • 我很高兴听到这个消息。如果您认为它是正确的,请继续并接受答案。 stackoverflow.com/help/someone-answers
    • 如果您的新 Tone 节点打算放置在图表的中间,您可以节省多个 Tone.connect 调用并使用每个 Tone 音频节点中的 .chain 方法。在此示例中,我们将用pitchShift.chain(audioCtx.destination) 替换最后一行,但想法是此方法将可变数量的音频节点作为参数,并且它们都按参数顺序依次连接。
    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多