网络音频同时改变音量和频率
Web audio change volume and frequency at the same time
我正在尝试使用两个 "input range" 来改变音量和频率。
我可以分别控制它们,但不能同时应用到两者。我知道问题是我尝试同时设置目标 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;
};
尝试将它们连接成一条链:
source.connect(this.gainNode); // source -> gain
this.gainNode.connect(filter); // gain -> filter
filter.connect(context.destination); // filter -> output
(sample
的代码未显示,因此我们无法查看其中是否有任何错误)。
我正在尝试使用两个 "input range" 来改变音量和频率。
我可以分别控制它们,但不能同时应用到两者。我知道问题是我尝试同时设置目标 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;
};
尝试将它们连接成一条链:
source.connect(this.gainNode); // source -> gain
this.gainNode.connect(filter); // gain -> filter
filter.connect(context.destination); // filter -> output
(sample
的代码未显示,因此我们无法查看其中是否有任何错误)。