BiquadFilterNode: phương thức getFrequencyResponse()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Phương thức getFrequencyResponse() của giao diện BiquadFilterNode lấy các thiết lập hiện tại của thuật toán lọc và tính đáp ứng tần số cho các tần số được chỉ định trong một mảng tần số xác định.
Hai mảng đầu ra, magResponseOutput và phaseResponseOutput, phải được tạo trước khi gọi phương thức này; chúng phải có cùng kích thước với mảng giá trị tần số đầu vào (frequencyArray).
Cú pháp
getFrequencyResponse(frequencyArray, magResponseOutput, phaseResponseOutput)
Tham số
frequencyArray-
Một
Float32Arraychứa mảng các tần số, được chỉ định theo Hertz, mà bạn muốn lọc. magResponseOutput-
Một
Float32Arraynhận các độ lớn đã tính của đáp ứng tần số cho từng giá trị tần số trongfrequencyArray. Với bất kỳ tần số nào trongfrequencyArraycó giá trị nằm ngoài phạm vi từ 0.0 đếnsampleRate/2 (trong đósampleRatelà tốc độ lấy mẫu củaAudioContext), giá trị tương ứng trong mảng này sẽ làNaN. Đây là các giá trị không có đơn vị. phaseResponseOutput-
Một
Float32Arraynhận các giá trị đáp ứng pha đã tính theo radian cho từng giá trị tần số trongfrequencyArrayđầu vào. Với bất kỳ tần số nào trongfrequencyArraycó giá trị nằm ngoài phạm vi từ 0.0 đếnsampleRate/2 (trong đósampleRatelà tốc độ lấy mẫu củaAudioContext), giá trị tương ứng trong mảng này sẽ làNaN.
Giá trị trả về
Không có (undefined).
Ngoại lệ
InvalidAccessError-
Ba mảng được cung cấp không có cùng độ dài.
Ví dụ
Trong ví dụ sau, chúng ta dùng một bộ lọc biquad trên một luồng media (để xem toàn bộ bản minh họa, hãy xem trực tiếp bản demo stream-source-buffer của chúng tôi hoặc đọc mã nguồn.) Là một phần của bản minh họa này, chúng ta lấy đáp ứng tần số của bộ lọc biquad này cho năm tần số mẫu. Trước tiên, chúng ta tạo các Float32Array cần dùng: một mảng chứa các tần số đầu vào, và hai mảng để nhận các giá trị độ lớn và pha đầu ra:
const myFrequencyArray = new Float32Array(5);
myFrequencyArray[0] = 1000;
myFrequencyArray[1] = 2000;
myFrequencyArray[2] = 3000;
myFrequencyArray[3] = 4000;
myFrequencyArray[4] = 5000;
const magResponseOutput = new Float32Array(5);
const phaseResponseOutput = new Float32Array(5);
Tiếp theo, chúng ta tạo một phần tử <ul> trong HTML để chứa kết quả, và lấy tham chiếu đến nó trong JavaScript:
<p>Đáp ứng tần số của bộ lọc biquad cho:</p>
<ul class="freq-response-output"></ul>
const freqResponseOutput = document.querySelector(".freq-response-output");
Cuối cùng, sau khi tạo bộ lọc biquad, chúng ta dùng getFrequencyResponse() để tạo dữ liệu đáp ứng và đưa chúng vào các mảng, sau đó lặp qua từng tập dữ liệu và hiển thị chúng thành một danh sách dễ đọc ở cuối trang:
const biquadFilter = audioCtx.createBiquadFilter();
biquadFilter.type = "lowshelf";
biquadFilter.frequency.value = 1000;
biquadFilter.gain.value = range.value;
// …
function calcFrequencyResponse() {
biquadFilter.getFrequencyResponse(
myFrequencyArray,
magResponseOutput,
phaseResponseOutput,
);
for (let i = 0; i <= myFrequencyArray.length - 1; i++) {
const listItem = document.createElement("li");
listItem.textContent = `: Magnitude ${magResponseOutput[i]}, Phase ${phaseResponseOutput[i]} radians.`;
listItem.insertBefore(
document.createElement("strong"),
listItem.firstChild,
).textContent = `${myFrequencyArray[i]}Hz`;
freqResponseOutput.appendChild(listItem);
}
}
calcFrequencyResponse();
Thông số kỹ thuật
| Specification |
|---|
| Web Audio API> # dom-biquadfilternode-getfrequencyresponse> |