Javascript Web音频API:用于实现任意数量源的平移的布局
我试图实现用户控制的任意数量的同时网络音频源的平移。源本身是单声道的。我正在使用Javascript和web音频API() 目前,我遇到的问题是,我试图使用多通道输出(每个源一个),但通道解释覆盖了我的平移尝试(请参阅),这使我认为我在架构上做了一些错误的事情 我想把事情放在一个概念层面上,因为我相信这就是我的问题所在 当前设置 我的方法是让一个节点处理每个源的所有处理,这里称为“scriptNode”。创建的通道数量等于音频源的数量,同样创建的平移节点数量也相同。图表如下所示:Javascript Web音频API:用于实现任意数量源的平移的布局,javascript,audio,web,web-audio-api,Javascript,Audio,Web,Web Audio Api,我试图实现用户控制的任意数量的同时网络音频源的平移。源本身是单声道的。我正在使用Javascript和web音频API() 目前,我遇到的问题是,我试图使用多通道输出(每个源一个),但通道解释覆盖了我的平移尝试(请参阅),这使我认为我在架构上做了一些错误的事情 我想把事情放在一个概念层面上,因为我相信这就是我的问题所在 当前设置 我的方法是让一个节点处理每个源的所有处理,这里称为“scriptNode”。创建的通道数量等于音频源的数量,同样创建的平移节点数量也相同。图表如下所示: The bun
The bundle size (the '=' segments) is the number of channels, set to be equal to the number of sources.
scriptNode == splitter =+-- panner1 --+= merger == destination
\-- panner. --/
\-- panner. --/
\-- pannerN --/
一些杂项,我调用此函数来设置scriptNode:
scriptNode = firstPart.audioCtx.createScriptProcessor(2048, 0, numParts);
其中numParts是源的数量。我还将scriptNode的channelCountMode设置为“显式”,将ChannelTranslation设置为“演讲者”。这些设置中的一个最终可能很重要,但我在尝试摆弄这些设置时找不到任何东西
问题
当我实际使用这个架构测试代码时,我将根据我选择的部分数量得到以下行为。平移滑块与每个源的平移器节点的“平移”值相关联
- numParts=1:Mono输出,使用滑块平移只会影响输出的音量(向中间偏大)。我想这是从调音台降混音到单声道的副产品
- numParts=2:立体声输出,一个硬左,一个硬右。用滑块平移两个通道没有任何作用
- numParts=3:与=2相同,但第三个通道是静默的
- numParts=4:类似于=2,现在所有通道都重新工作,它们按L/R/L/R顺序进行了严格平移。再次使用滑块平移不会产生任何效果
function customAudioProcessCallback( audioProcessingEvent )
{
// First synth - process audio for all synths!
const outputBuffer = audioProcessingEvent.outputBuffer;
for ( var i = 0; i < numParts; i++ ) {
// Each part writes to one channel.
synthParts[ i ].synthesize(outputBuffer.getChannelData( i ), outputBuffer.length);
}
}
函数customAudioProcessCallback(audioProcessingEvent)
{
//第一个合成器-处理所有合成器的音频!
const outputBuffer=audioProcessingEvent.outputBuffer;
对于(变量i=0;i
播放函数的相关片段:
function play()
{
const contextClass = (window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext);
synthParts[ 0 ].audioCtx = new contextClass();
synthParts[ 0 ].scriptNode = synthParts[ 0 ].audioCtx.createScriptProcessor ? synthParts[ 0 ].audioCtx.createScriptProcessor(2048, 0, numParts+1) : synthParts[ 0 ].audioCtx.createJavaScriptNode(2048, 0, numParts+1); // 2048, 0 input channels, ? outputs
synthParts[ 0 ].scriptNode.onaudioprocess = customAudioProcessCallback;
synthParts[ 0 ].scriptNode.channelCountMode = 'explicit';
synthParts[ 0 ].scriptNode.channelInterpretation = 'speakers';
// Set up splitter and panners for all channels
var splitter = synthParts[ 0 ].audioCtx.createChannelSplitter( numParts+1 );
for ( var i = 0; i < numParts; i++ ) {
panList[ i ] = synthParts[ 0 ].audioCtx.createStereoPanner();
panList[ i ].pan = panValues[ i ];
}
// Connection:
// scriptNode -- splitter -+-- panner1 --+- destination
// \-- panner. --/
// \-- pannerN --/
synthParts[ 0 ].scriptNode.connect(splitter);
for ( var i = 0; i < numParts; i++ ) {
splitter.connect( panList[ i ], i);
// This line used to read:
// panList[ i ].connect( synthParts[ 0 ].audioCtx.destination, 0, i );
// However, this was connecting multiple parts to the input of the audio context destination, which is limited to 1 input. The correct call is below.
panList[ i ].connect( synthParts[ 0 ].audioCtx.destination );
}
}
函数播放()
{
const contextClass=(window.AudioContext | | window.webkitadiocontext | | window.mozadiocontext | | window.oAudioContext | window.msAudioContext);
synthParts[0]。audioCtx=新上下文类();
synthParts[0]。scriptNode=synthParts[0]。audioCtx.createScriptProcessor?synthParts[0]。audioCtx.createScriptProcessor(2048,0,numParts+1):synthParts[0]。audioCtx.createJavaScriptNode(2048,0,numParts+1);//2048,0个输入通道,输出
synthParts[0].scriptNode.onaudioprocess=customAudioProcessCallback;
synthParts[0].scriptNode.channelCountMode='explicit';
synthParts[0].scriptNode.ChannelExpression='speakers';
//为所有通道设置拆分器和平移器
var splitter=synthParts[0].audioCtx.createChannelSplitter(numParts+1);
对于(变量i=0;i
APannerNode
总是产生立体声输出。当您将平移器输出连接到合并的一个输入时,平移器的立体声输出将向下混合为单声道,从而有效地消除了大部分平移效果
有些信息缺失,但我不明白你为什么需要合并。您可以将每个窗格的立体声输出直接发送到目标。目标将适当地混合来自每个平移器的立体声输出,从而保留平移效果。我尝试了此方法,但在尝试连接平移器时出错。似乎这个问题以前就被发现过:(甚至是你,我刚刚注意到!)。在那个例子中,我的建议是像我现在这样使用合并,但我愿意接受一些建议,在不合并的情况下避免这个错误,以避免单声道下混音问题。与链接问题相同,即“Uncaught IndexSizeError:未能在“AudioNode”上执行“connect”:输入索引(1)超过输入数(1)。”尝试时会看到此消息