我正在做在线音频播放器,所以我想在我的应用程序中集成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);
mediaElement > gainNode > pitchShift > destination
为了确保Tone.js使用相同的AudioContext,可以使用Tone对象上的setter对其进行分配。在使用Tone.js进行任何其他操作之前,需要完成此操作。
Tone.context = context;
Tone.js还会导出一个可用于将本机AudioNode连接到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);