Web音频同时改变音量和频率

时间:2015-03-19 18:50:01

标签: html5-audio web-audio

我试图有两个"输入范围"改变音量和频率。

我可以单独控制它们,但不能同时适用于它们。我知道问题是我尝试同时设置目标filter.connect(context.destination)和this.gainNode.connect(context.destination)。如何应对这种情况?

这是HTML代码

<p><input type="range" min="0" max="100" value="100" onchange="sample.changeVolume(this);"> Volume</p>
<p><input type="checkbox" id="c1" checked="false" onchange="sample.toggleFilter(this);">
<p><input type="range" min="0" max="1" step="0.01" value="1" onchange="sample.changeFrequency(this);"> Frequency</p>

JavaScript代码

FilterSample.prototype.play = function() {
   this.gainNode = context.createGain();
   // Create the source.
   var source = context.createBufferSource();
   source.buffer = this.buffer;
   // Create the filter.
   var filter = context.createBiquadFilter();
   filter.type = filter.LOWPASS;
   filter.frequency.value = 5000;
   // Connect source to filter, filter to destination.
   source.connect(this.gainNode);
   this.gainNode.connect(context.destination);
   source.connect(filter);
   filter.connect(context.destination);

   // Play!
   source[source.start ? 'start' : 'noteOn'](0);
   source.loop = true;
   // Save source and filterNode for later access.
   this.source = source;
   this.filter = filter;
 };

1 个答案:

答案 0 :(得分:1)

尝试在一个链中连接它们:

source.connect(this.gainNode);        // source -> gain
this.gainNode.connect(filter);        // gain -> filter
filter.connect(context.destination);  // filter -> output

sample的代码未显示,因此我们无法查看其中是否有任何错误。