Skip to content

Commit 8be7908

Browse files
authored
refactor: biquad filter node (#572)
* Made BiquadFilterNode::processNode a-rate * Simplified coefficient calculations across different filter types * Reimplemented BiquadFilterNode::getFrequencyResponse * Added documentation for BiquadFilterNode * Added mono input support to StereoPannerNode
1 parent fba4835 commit 8be7908

File tree

7 files changed

+222
-114
lines changed

7 files changed

+222
-114
lines changed

packages/audiodocs/docs/core/base-audio-context.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ The above method lets you create `StereoPannerNode`.
130130

131131
#### Returns [`StereoPannerNode`](/effects/stereo-panner-node).
132132

133+
### `createBiquadFilter
134+
135+
The above method lets you create `BiquadFilterNode`.
136+
137+
#### Returns [`BiquadFilterNode`](/effects/biquad-filter-node).
138+
133139
### `decodeAudioData`
134140

135141
The above method lets you decode audio data. It decodes with in memory audio data block.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
import AudioNodePropsTable from "@site/src/components/AudioNodePropsTable"
6+
import { ReadOnly } from '@site/src/components/Badges';
7+
8+
# BiquadFilterNode
9+
10+
The `BiquadFilterNode` interface represents a low-order filter. It is an [`AudioNode`](/core/audio-node) used for tone controls, graphic equalizers, and other audio effects.
11+
Multiple `BiquadFilterNode` instances can be combined to create more complex filtering chains.
12+
13+
14+
#### [`AudioNode`](/core/audio-node#read-only-properties) properties
15+
<AudioNodePropsTable numberOfInputs={1} numberOfOutputs={1} channelCount={2} channelCountMode={"max"} channelInterpretation={"speakers"} />
16+
17+
## Constructor
18+
19+
[`BaseAudioContext.createBiquadFilter()`](/core/base-audio-context#createbiquadfilter)
20+
21+
## Properties
22+
23+
| Name | Type | Rate | Description |
24+
| :--: | :--: | :----------: | :-- |
25+
| `frequency` | [`AudioParam`](/core/audio-param) | [`a-rate`](/core/audio-param#a-rate-vs-k-rate) | The filter’s cutoff or center frequency in hertz (Hz) |
26+
| `detune` | [`AudioParam`](/core/audio-param) | [`a-rate`](/core/audio-param#a-rate-vs-k-rate) | Amount by which the frequency is detuned in cents |
27+
| `Q` | [`AudioParam`](/core/audio-param) | [`a-rate`](/core/audio-param#a-rate-vs-k-rate) | The filter’s Q factor (quality factor). |
28+
| `gain` | [`AudioParam`](/core/audio-param) | [`a-rate`](/core/audio-param#a-rate-vs-k-rate) | Gain applied by specific filter types, in decibels (dB) |
29+
| `type` | `string` || Defines the kind of filtering algorithm the node applies (e.g. `"lowpass"`, `"highpass"`). |
30+
31+
#### BiquadFilterType enumeration description
32+
| `type` | Description | `frequency` | `Q` | `gain` |
33+
|:------:|:-----------:|:-----------:|:---:|:------:|
34+
| `lowpass` | Second-order resonant lowpass filter with 12dB/octave rolloff. Frequencies below the cutoff pass through; higher frequencies are attenuated. | The cutoff frequency. | Determines how peaked the frequency is around the cutoff. Higher values result in a sharper peak. | Not used |
35+
| `highpass` | Second-order resonant highpass filter with 12dB/octave rolloff. Frequencies above the cutoff pass through; lower frequencies are attenuated. | The cutoff frequency. | Determines how peaked the frequency is around the cutoff. Higher values result in a sharper peak. | Not used |
36+
| `bandpass` | Second-order bandpass filter. Frequencies within a given range pass through; others are attenuated. | The center of the frequency band. | Controls the bandwidth. Higher values result in a narrower band. | Not used |
37+
| `lowshelf` | Second-order lowshelf filter. Frequencies below the cutoff are boosted or attenuated; others remain unchanged. | The upper limit of the frequencies where the boost (or attenuation) is applied. | Not used | The boost (in dB) to be applied. Negative values attenuate the frequencies.|
38+
| `highshelf` | Second-order highshelf filter. Frequencies above the cutoff are boosted or attenuated; others remain unchanged. | The lower limit of the frequencies where the boost (or attenuation) is applied. | Not used | The boost (in dB) to be applied. Negative values attenuate the frequencies. |
39+
| `peaking` | Frequencies around a center frequency are boosted or attenuated; others remain unchanged. | The center of the frequency range where the boost (or an attenuation) is applied. | Controls the bandwidth. Higher values result in a narrower band. | The boost (in dB) to be applied. Negative values attenuate the frequencies. |
40+
| `notch` | Notch (band-stop) filter. Opposite of a bandpass filter: frequencies around the center are attenuated; others remain unchanged. | The center of the frequency range where the notch is applied. | Controls the bandwidth. Higher values result in a narrower band. | Not used |
41+
| `allpass` | Second-order allpass filter. All frequencies pass through, but changes the phase relationship between the various frequencies. | The frequency where the center of the phase transition occurs (maximum group delay). | Controls how sharp the phase transition is at the center frequency. Higher values result in a sharper transition and a larger group delay. | Not used |
42+
43+
## Methods
44+
45+
### `getFrequencyResponse`
46+
47+
| Parameters | Type | Description |
48+
| :--------: | :--: | :---------- |
49+
| `frequencyArray` | `Float32Array` | Array of frequencies (in Hz), which you want to filter. |
50+
| `magResponseOutput` | `Float32Array` | Output array to store the computed linear magnitude values for each frequency. For frequencies outside the range [0, sampleRate / 2], the corresponding results are NaN. |
51+
| `phaseResponseOutput` | `Float32Array` | Output array to store the computed phase response values (in radians) for each frequency. For frequencies outside the range [0, sampleRate / 2], the corresponding results are NaN. |
52+
| `length` | `number` | The number of frequency values to process. It should match the length of all input and output arrays. |
53+
54+
#### Returns `undefined`.
55+
56+
## Remarks
57+
#### `frequency`
58+
- float, default value is 350.
59+
#### `detune`
60+
- float, default value is 0.
61+
#### `Q`
62+
- float, default value is 1.
63+
#### `gain`
64+
- [`BiquadFilterType`](#biquadfiltertype-enumeration-description), default is "lowpass".

packages/audiodocs/docs/effects/gain-node.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 1
2+
sidebar_position: 2
33
---
44

55
import AudioNodePropsTable from "@site/src/components/AudioNodePropsTable"
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 3
33
---
44

55
import AudioNodePropsTable from "@site/src/components/AudioNodePropsTable"
@@ -11,19 +11,20 @@ The `StereoPannerNode` interface represents the change in ratio between two outp
1111

1212
#### [`AudioNode`](/core/audio-node#read-only-properties) properties
1313

14-
<AudioNodePropsTable numberOfInputs={1} numberOfOutputs={1} channelCount={2} channelCountMode={"explicit"} channelInterpretation={"speakers"} />
14+
<AudioNodePropsTable numberOfInputs={1} numberOfOutputs={1} channelCount={2} channelCountMode={"clamped-max"} channelInterpretation={"speakers"} />
1515

1616
## Constructor
1717

1818
[`BaseAudioContext.createStereoPanner()`](/core/base-audio-context#createstereopanner)
1919

2020
## Properties
2121

22-
| Name | Type | Default value | Description |
23-
| :----: | :----: | :-------- | :------ |
24-
| `pan` | [`AudioParam`](/core/audio-param) <ReadOnly /> | 0 | [`a-rate`](/core/audio-param#a-rate-vs-k-rate) `AudioParam` representing value of pan to apply. |
22+
| Name | Type | Description |
23+
| :--: | :--: | :---------- |
24+
| `pan` | [`AudioParam`](/core/audio-param) | [`a-rate`](/core/audio-param#a-rate-vs-k-rate) `AudioParam` representing how the audio signal is distributed between the left and right channels. |
2525

2626
## Remarks
2727

2828
#### `pan`
29+
- Default value is 0
2930
- Nominal range is -1 (only left channel) to 1 (only right channel).

0 commit comments

Comments
 (0)