如何将Web Audio API连接到Tone.js? [英] How to connect Web Audio API to Tone.js?

查看:216
本文介绍了如何将Web Audio API连接到Tone.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing an Online Audio Player, so I want to integrate Pitch Shifter in my App, which is available on Tone js but not in Web Audio API...

所以我的想法是将 Tonejs 音调转换器连接到 Web Audio API的audioContext .

So my idea is to connect Tonejs Pitch Shifter to Web Audio API's 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);

推荐答案

我想您想实现这样的信号流:

I guess you want to achieve a signal flow like this:

mediaElement > gainNode > pitchShift > destination

要确保Tone.js使用相同的AudioContext,可以使用Tone对象上的setter对其进行分配.在使用Tone.js进行任何其他操作之前,需要完成此操作.

To make sure Tone.js is using the same AudioContext you can assign it by using the setter on the Tone object. This needs to be done before doing anything else with Tone.js.

Tone.context = context;

Tone.js还会导出一个帮助程序,该帮助程序可用于将本机AudioNode连接到Tone.js提供的节点.

Tone.js also exports a helper which can be used to connect native AudioNodes to the nodes provided by Tone.js.

Tone.connect(gainNode, pitchShift);

我对您的示例代码做了一些修改,以包含所做的更改.

I modified your example code a bit to incorporate the changes.

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);

这篇关于如何将Web Audio API连接到Tone.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆