首页 > 解决方案 > Tone.PitchShift 和 Howler.js 问题

问题描述

我喜欢在我的 (Meteor) 应用程序中使用 Howler.js。但是,播放速率功能导致了我不想要的音高偏移(我只想要时间拉伸,并保持音高)。因此,我的解决方案是对其实施音高转换以“纠正”音高。看起来很简单,这就是我选择使用https://tonejs.github.io/的原因

唯一的问题是,我一辈子都无法让它正常工作。在阅读了 Web Audio API 和 Tone.js 文档以及在线讨论/疑难解答论坛数小时后,我得到的最接近潜在解决方案的方法是这样的(在我的应用程序渲染期间调用,以防万一问题不得不解决)过早加载):

Tone.setContext(Howler.ctx); //set tone's context to the Howler.js audiocontext
var pShift = new Tone.PitchShift(3); //create the PitchShift effect, +3 semi-tones transposition
pShift.context = Howler.ctx; //set the PitchShift's context to the Howler.js audiocontext
pShift.connect(Howler.ctx.destination); //connect the PitchShift's output to the Howler's destination
Howler.masterGain.connect(pShift); //connect the Howler's master GainNode output to the PitchShift effect

//For debugging purposes:
console.log(Howler.masterGain)
console.log(pShift);

当我运行它时,我收到以下错误消息:

Tracker afterFlush 函数的异常:meteor.js?hash=857dafb4b9dff17e29ed8498a22ea5b1a3d6b41d:1059 TypeError:无法在“AudioNode”上执行“connect”:重载解析失败。

我还注意到,这些命令下方的 console.log() 命令甚至没有出现在控制台中,这很奇怪。但是,当我删除最后一行(mastergain.connect 到 pShift)时,它们会这样做。

我尝试了一些其他技术,例如https://github.com/mmckegg/soundbank-pitch-shift/(它有效,但无论我放置什么设置,它都会播放音高变化的声音和非音高变化的声音它在),或者只是使用 AudioBufferSourceNode.detune (我不知道如何让它与 Howler.js 一起工作,因为 Howler 只有可以公开 GainNode 和 AudioContext 的函数,无法弄清楚如何在仍然使用 Howler 的同时从那里读取输出)。

任何帮助/线索将不胜感激!

标签: javascriptweb-audio-apihowler.jstone.jspitch-shifting

解决方案


我认为您不需要片段中的第三行。由于您的第一行告诉 Tone.jsAudioContext已经使用由 howler.js 创建的。因此pShift.context应该等于Howler.ctx。但仔细检查可能是有意义的。

console.assert(pShift.context === Howler.ctx);

howler.jsmasterGain暴露的是一个原生的音频节点。这意味着它不能直接连接到使用 Tone.js 创建的节点,因为这些不是本机音频节点。但是 Tone.js 提供了一个帮手来做到这一点。

Tone.connect(Howler.masterGain, pShift);

我认为您还需要调用disconnect()删除masterGain任何现有连接。

以下代码段应该可以工作。

Tone.setContext(Howler.ctx);

const pShift = new Tone.PitchShift(3);

Howler.masterGain.disconnect();

Tone.connect(Howler.masterGain, pShift);
pShift.toDestination();

推荐阅读