首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将网络音频API与Tone.js连接?

如何将网络音频API与Tone.js连接?
EN

Stack Overflow用户
提问于 2019-11-03 02:19:08
回答 1查看 2K关注 0票数 2

我正在做一个在线音频播放器,所以我想在我的应用程序中集成音调移位器,它可以在音调js上获得,但不在网络音频API中.

所以我的想法是将Tonejs 音调移位器连接到Web的audioContext上。

有什么可能的办法吗?

这是我的代码供参考

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-03 12:54:42

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

代码语言:javascript
复制
mediaElement > gainNode > pitchShift > destination

为了确保Tone.js使用的是相同的AudioContext,您可以通过在Tone对象上使用setter来分配它。在使用Tone.js进行任何其他操作之前,都需要这样做。

代码语言:javascript
复制
Tone.context = context;

Tone.js还导出一个助手,该助手可用于将本机AudioNodes连接到Tone.js提供的节点。

代码语言:javascript
复制
Tone.connect(gainNode, pitchShift);

我对您的示例代码做了一些修改,以合并这些更改。

代码语言:javascript
复制
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);
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58676929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档