我正在嘗試使基本音頻過濾器正常工作。我想評估至少3-4個例子。如何使用JavaScript應用基本音頻過濾器
這裏是我評價,並試圖獲得一個工作JS代碼:
var QUAL_MUL = 30;
function FilterSample() {
this.isPlaying = false;
loadSounds(this, {buffer: 'techno.wav'});
};
FilterSample.prototype.play = function() {
// 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(filter);
filter.connect(context.destination);
// Play!
source.start(0);
source.loop = true;
// Save source and filterNode for later access.
this.source = source;
this.filter = filter;
};
FilterSample.prototype.stop = function() {
this.source.stop(0);
};
FilterSample.prototype.toggle = function() {
this.isPlaying ? this.stop() : this.play();
this.isPlaying = !this.isPlaying;
};
FilterSample.prototype.changeFrequency = function(element) {
// Clamp the frequency between the minimum value (40 Hz) and half of the
// sampling rate.
var minValue = 40;
var maxValue = context.sampleRate/2;
// Logarithm (base 2) to compute how many octaves fall in the range.
var numberOfOctaves = Math.log(maxValue/minValue)/Math.LN2;
// Compute a multiplier from 0 to 1 based on an exponential scale.
var multiplier = Math.pow(2, numberOfOctaves * (element.value - 1.0));
// Get back to the frequency value between min and max.
this.filter.frequency.value = maxValue * multiplier;
};
FilterSample.prototype.changeQuality = function(element) {
this.filter.Q.value = element.value * QUAL_MUL;
};
FilterSample.prototype.toggleFilter = function(element) {
this.source.disconnect(0);
this.filter.disconnect(0);
// Check if we want to enable the filter.
if (element.checked) {
// Connect through the filter.
this.source.connect(this.filter);
this.filter.connect(context.destination);
} else {
// Otherwise, connect directly.
this.source.connect(context.destination);
}
};
這將是非常好的,如果有人能解釋或提供更好的使用JS應用過濾器的簡單工作的例子。
預先感謝您。
很棒的回答。我認爲,而不是'filter.type = 3;'或'filter.type = filter.LOWSHELF;'你現在只是寫'filter.type =「lowshelf」;'。另外,對於跨瀏覽器支持,使用'var context = new(window.AudioContext || window.webkitAudioContext)();' – JoeRocc
@JoeRocc是的,他們每次都在不斷地改變標準,我會插入它。請注意,用於chrome的前綴webkitAudioContext [自35以來已被棄用](https://www.chromestatus.com/feature/6261718720184320)。雖然不知道Safari。 – MarijnS95