关于网络音频节点,.connect() 做什么?
Concerning Web Audio nodes, what does .connect() do?
努力效仿here, which is basically a c&p of this
想我把大部分的部分都记下来了,除了所有 node.connect()
的
据我了解,需要此代码序列来为音频分析器提供音频流:
var source = audioCtx.createMediaStreamSource(stream);
source.connect(analyser);
analyser.connect(audioCtx.destination);
我似乎无法理解它,因为它在我看来更像衔尾蛇。
不幸的是,我似乎找不到关于 .connect()
的任何文档,所以我很迷茫,希望得到任何澄清!
哦,我正在通过纯 javascript new Audio('db.mp3').play();
加载一个 .mp3,并试图将其用作源而不创建 <audio>
元素。
是否可以由此创建一个 mediaStream 对象以馈入 .createMediaStreamSource(stream)
?
connect 只是定义过滤器的输出。
在这种情况下,您的源将流加载到缓冲区中并写入由连接函数定义的下一个过滤器的输入。这对您的分析器过滤器重复。
把它想象成管道。
这是我几年前使用网络音频编写的示例代码片段 api。
this.scriptProcessor = this.audioContext.createScriptProcessor(this.scriptProcessorBufferSize,
this.scriptProcessorInputChannels,
this.scriptProcessorOutputChannels);
this.scriptProcessor.connect(this.audioContext.destination);
this.scriptProcessor.onaudioprocess = updateMediaControl.bind(this);
//Set up the Gain Node with a default value of 1(max volume).
this.gainNode = this.audioContext.createGain();
this.gainNode.connect(this.audioContext.destination);
this.gainNode.gain.value = 1;
sewi.AudioResourceViewer.prototype.playAudio = function(){
if(this.audioBuffer){
this.source = this.audioContext.createBufferSource();
this.source.buffer = this.audioBuffer;
this.source.connect(this.gainNode);
this.source.connect(this.scriptProcessor);
this.beginTime = Date.now();
this.source.start(0, this.offset);
this.isPlaying = true;
this.controls.update({playing: this.isPlaying});
updateGraphPlaybackPosition.call(this, this.offset);
}
};
正如您所看到的,我的源连接到 gainNode,而 gainNode 又连接到 scriptProcessor。当音频开始播放时,数据从 source->gainNode->destination 和 source->scriptProcessor->destination 传递。流经连接它们的 "pipes",这是由 connect() 定义的。当音频数据通过gainNode时,可以通过改变音频波的幅度来调节音量。之后它被传递到脚本处理器,以便在处理音频时可以附加和触发事件。
努力效仿here, which is basically a c&p of this
想我把大部分的部分都记下来了,除了所有 node.connect()
的
据我了解,需要此代码序列来为音频分析器提供音频流:
var source = audioCtx.createMediaStreamSource(stream);
source.connect(analyser);
analyser.connect(audioCtx.destination);
我似乎无法理解它,因为它在我看来更像衔尾蛇。
不幸的是,我似乎找不到关于 .connect()
的任何文档,所以我很迷茫,希望得到任何澄清!
哦,我正在通过纯 javascript new Audio('db.mp3').play();
加载一个 .mp3,并试图将其用作源而不创建 <audio>
元素。
是否可以由此创建一个 mediaStream 对象以馈入 .createMediaStreamSource(stream)
?
connect 只是定义过滤器的输出。 在这种情况下,您的源将流加载到缓冲区中并写入由连接函数定义的下一个过滤器的输入。这对您的分析器过滤器重复。
把它想象成管道。
这是我几年前使用网络音频编写的示例代码片段 api。
this.scriptProcessor = this.audioContext.createScriptProcessor(this.scriptProcessorBufferSize,
this.scriptProcessorInputChannels,
this.scriptProcessorOutputChannels);
this.scriptProcessor.connect(this.audioContext.destination);
this.scriptProcessor.onaudioprocess = updateMediaControl.bind(this);
//Set up the Gain Node with a default value of 1(max volume).
this.gainNode = this.audioContext.createGain();
this.gainNode.connect(this.audioContext.destination);
this.gainNode.gain.value = 1;
sewi.AudioResourceViewer.prototype.playAudio = function(){
if(this.audioBuffer){
this.source = this.audioContext.createBufferSource();
this.source.buffer = this.audioBuffer;
this.source.connect(this.gainNode);
this.source.connect(this.scriptProcessor);
this.beginTime = Date.now();
this.source.start(0, this.offset);
this.isPlaying = true;
this.controls.update({playing: this.isPlaying});
updateGraphPlaybackPosition.call(this, this.offset);
}
};
正如您所看到的,我的源连接到 gainNode,而 gainNode 又连接到 scriptProcessor。当音频开始播放时,数据从 source->gainNode->destination 和 source->scriptProcessor->destination 传递。流经连接它们的 "pipes",这是由 connect() 定义的。当音频数据通过gainNode时,可以通过改变音频波的幅度来调节音量。之后它被传递到脚本处理器,以便在处理音频时可以附加和触发事件。