Skip to content

Commit b58ba39

Browse files
maciejmakowski2003Maciej Makowski
andauthored
Docs: AudioNode + AnalyserNode (#273)
* feat: added AnalyserNode to docs and trying to provide AudioVisaulizer example * feat: added docs for AudioNode * feat: added AudioNode to docs * refactor: refactor AudioNode docs layout * fix: removed audio channels header * refactor: refactored bolds and methods signatures * fix: fixed naming in methods * Feat/docs/analyser node (#274) * feat: added AnalyserNode to docs * fix: fixed bolds * feat: finished AnalyserNode docs * fix: fixed example time-domian data size based on docs --------- Co-authored-by: Maciej Makowski <maciej.makowski2608@gmail.com> * refactor: applied requested changes added types bookmark * fix: small fixes --------- Co-authored-by: Maciej Makowski <maciej.makowski2608@gmail.com>
1 parent 636b934 commit b58ba39

File tree

24 files changed

+375
-27
lines changed

24 files changed

+375
-27
lines changed

apps/common-app/src/examples/AudioVisualizer/AudioVisualizer.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ const AudioVisualizer: React.FC = () => {
2121
const [isPlaying, setIsPlaying] = useState(false);
2222
const [isLoading, setIsLoading] = useState(false);
2323

24-
const [times, setTimes] = useState<number[]>(
25-
new Array(FFT_SIZE / 2).fill(127)
26-
);
24+
const [times, setTimes] = useState<number[]>(new Array(FFT_SIZE).fill(127));
2725
const [freqs, setFreqs] = useState<number[]>(new Array(FFT_SIZE / 2).fill(0));
2826

2927
const audioContextRef = useRef<AudioContext | null>(null);
@@ -56,13 +54,14 @@ const AudioVisualizer: React.FC = () => {
5654
return;
5755
}
5856

59-
const bufferLength = analyserRef.current.frequencyBinCount;
57+
const timesArrayLength = analyserRef.current.fftSize;
58+
const frequencyArrayLength = analyserRef.current.frequencyBinCount;
6059

61-
const timesArray = new Array(bufferLength);
60+
const timesArray = new Array(timesArrayLength);
6261
analyserRef.current.getByteTimeDomainData(timesArray);
6362
setTimes(timesArray);
6463

65-
const freqsArray = new Array(bufferLength);
64+
const freqsArray = new Array(frequencyArrayLength);
6665
analyserRef.current.getByteFrequencyData(freqsArray);
6766
setFreqs(freqsArray);
6867

@@ -107,6 +106,7 @@ const AudioVisualizer: React.FC = () => {
107106
<FreqTimeChart
108107
timeData={times}
109108
frequencyData={freqs}
109+
fftSize={analyserRef.current?.fftSize || FFT_SIZE}
110110
frequencyBinCount={
111111
analyserRef.current?.frequencyBinCount || FFT_SIZE / 2
112112
}

apps/common-app/src/examples/AudioVisualizer/Charts.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ function weightWithIndex(value: number, index: number, indexMax: number) {
3737

3838
interface ChartProps {
3939
data: number[];
40+
fftSize: number;
4041
frequencyBinCount: number;
4142
}
4243

4344
const TimeChart: React.FC<ChartProps> = (props) => {
4445
const { size } = useCanvas();
45-
const { data, frequencyBinCount } = props;
46+
const { data, fftSize } = props;
4647

4748
const circlePaint = useMemo(() => {
4849
const paint = Skia.Paint();
@@ -61,12 +62,12 @@ const TimeChart: React.FC<ChartProps> = (props) => {
6162
const startHight = maxHeight - (maxHeight - 2 * INNER_RADIUS) / 2;
6263

6364
return data.map((value, index) => {
64-
const x = startWidth + (index * 2 * INNER_RADIUS) / frequencyBinCount;
65-
const y = startHight - (value / 256) * 2 * INNER_RADIUS;
65+
const x = startWidth + (index * 2 * INNER_RADIUS) / fftSize;
66+
const y = startHight - (value / 255) * 2 * INNER_RADIUS;
6667

6768
return vec(x, y);
6869
});
69-
}, [size, data, frequencyBinCount]);
70+
}, [size, data, fftSize]);
7071

7172
return (
7273
<>

apps/common-app/src/examples/AudioVisualizer/FreqTimeChart.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ import Charts from './Charts';
66
interface FreqTimeChartProps {
77
timeData: number[];
88
frequencyData: number[];
9+
fftSize: number;
910
frequencyBinCount: number;
1011
}
1112

1213
const FreqTimeChart: React.FC<FreqTimeChartProps> = (props) => {
13-
const { timeData, frequencyData, frequencyBinCount } = props;
14+
const { timeData, frequencyData, fftSize, frequencyBinCount } = props;
1415

1516
return (
1617
<>
17-
<Charts.TimeChart data={timeData} frequencyBinCount={frequencyBinCount} />
18+
<Charts.TimeChart
19+
data={timeData}
20+
fftSize={fftSize}
21+
frequencyBinCount={frequencyBinCount}
22+
/>
1823
<Charts.FrequencyChart
1924
data={frequencyData}
25+
fftSize={fftSize}
2026
frequencyBinCount={frequencyBinCount}
2127
/>
2228
</>

packages/audiodocs/docs/nodes/_category_.json renamed to packages/audiodocs/docs/core/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"label": "Nodes",
2+
"label": "Core",
33
"position": 3,
44
"link": {
55
"type": "generated-index"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
import { Optional, ReadOnly } from '@site/src/components/Badges';
6+
7+
# AudioNode
8+
9+
`AudioNode` interface serves as a versatile interface for constructing an audio processing graph, representing individual units of audio processing functionality.
10+
Each `AudioNode` is associated with a certain number of audio channels that facilitate the transfer of audio data through processing graph.
11+
12+
We usually represent the channels with the standard abbreviations detailed in the table below:
13+
14+
| Name | Number of channels | Channels |
15+
| :----: | :------: | :-------- |
16+
| Mono | 1 | 0: M - mono |
17+
| Stereo | 2 | 0: L - left <br /> 1: R - right |
18+
| Quad | 4 | 0: L - left <br /> 1: R - right <br /> 2: SL - surround left <br /> 3: SR - surround right |
19+
| Stereo | 6 | 0: L - left <br /> 1: R - right <br /> 2: C - center <br /> 3: LFE - subwoofer <br /> 4: SL - surround left <br /> 5: SR - surround right |
20+
21+
## Read-only properties
22+
23+
| Name | Type | Description |
24+
| :----: | :----: | :-------- |
25+
| `context` | [`BaseAudioContext`](/core/base-audio-context) | Returns the associated context. |
26+
| `numberOfInputs` | `number` | Returns value representing the number of input connections for the node. Source nodes are characterized by having a `numberOfInputs` value of 0. |
27+
| `numberOfOutputs` | `number` | Returns value representing the number of output connections for the node. Destination nodes are characterized by having a `numberOfOutputs` value of 0. |
28+
| `channelCount` | `number` | Returns value representing the number of channels utilized to decide how many channels are engaged during up-mixing or down-mixing with the node's inputs. |
29+
| `channelCountMode` | [`ChannelCountMode`](/types/channel-count-mode) | Returns an enumerated value that specifies the method by which channels are mixed between the node's inputs and outputs. |
30+
| `channelInterpretation` | [`ChannelInterpretation`](/types/channel-interpretation) | Returns an enumerated value that specifies how input channels are mapped to output channels when number of them is different. |
31+
32+
## Methods
33+
34+
### `connect`
35+
36+
The `connect` method lets you connect one of the node's outputs to a destination.
37+
38+
| Parameters | Type | Description |
39+
| :---: | :---: | :---- |
40+
| `destination` | [`AudioNode`](/core/audio-node) | Audio node to which to connect. |
41+
42+
**Returns** `undefined`.
43+
44+
### `disconnect`
45+
46+
The `disconnect` method lets you disconnect one or more nodes from the node.
47+
48+
| Parameters | Type | Description |
49+
| :---: | :---: | :---- |
50+
| `destination` <Optional /> | [`AudioNode`](/core/audio-node) | Audio node to which to connect. |
51+
52+
If no arguments provided node disconnects from all outgoing connections.
53+
54+
**Returns** `undefined`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# BaseAudioContext

packages/audiodocs/docs/other/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Other",
3-
"position": 4,
3+
"position": 6,
44
"link": {
55
"type": "generated-index"
66
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "Types",
3+
"position": 5,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# ChannelCountMode
6+
7+
`ChannelCountMode` type determines how the number of input channels affects the number of output channels in an audio node.
8+
9+
**Acceptable values:**
10+
- `max`
11+
12+
The number of channels is equal to the maximum number of channels of all connections. In this case, `channelCount` is ignored and only up-mixing happens.
13+
14+
- `clamped-max`
15+
16+
The number of channels is equal to the maximum number of channels of all connections, clamped to the value of `channelCount`(serves as the maximum permissible value).
17+
18+
- `explicit`
19+
20+
The number of channels is defined by the value of `channelCount`.
21+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# ChannelInterpretation
6+
7+
`ChannelInterpretation` type specifies how input channels are mapped out to output channels when the number of them are different.
8+
9+
**Acceptable values:**
10+
- `speakers`
11+
12+
Use set of standard mapping rules for all combinations of common input and output setups.
13+
14+
- `discrete`
15+
16+
Covers all other cases. Mapping depends on relationship between number of input channels and number of output channels.
17+
18+
19+
## Channels mapping table
20+
21+
### `speakers`
22+
23+
| Number of input channels | Number of output channels | Mixing rules |
24+
| :------------------------: | :------------------------- | :------------ |
25+
| 1 (Mono) | 2 (Stereo) | output.L = input.M <br /> output.R = input.M |
26+
| 1 (Mono) | 4 (Quad) | output.L = input.M <br /> output.R = input.M <br /> output.SL = 0 <br /> output.SR = 0 |
27+
| 1 (Mono) | 6 (5.1) | output.L = 0 <br /> output.R = 0 <br /> output.C = input.M <br /> output.LFE = 0 <br /> output.SL = 0 <br /> output.SR = 0 |
28+
| 2 (Stereo) | 1 (Mono) | output.M = 0.5 \* (input.L + input.R) |
29+
| 2 (Stereo) | 4 (Quad) | output.L = input.L <br /> output.R = input.R <br /> output.SL = 0 <br /> output.SR = 0 |
30+
| 2 (Stereo) | 6 (5.1) | output.L = input.L <br /> output.R = input.R <br /> output.C = 0 <br /> output.LFE = 0 <br /> output.SL = 0 <br /> output.SR = 0 |
31+
| 4 (Quad) | 1 (Mono) | output.M = 0.25 \* (input.L + input.R + input.SL + input.SR) |
32+
| 4 (Quad) | 2 (Stereo) | output.L = 0.5 \* (input.L + input.SL) <br /> output.R = 0.5 \* (input.R + input.SR) |
33+
| 4 (Quad) | 6 (5.1) | output.L = input.L <br /> output.R = input.R <br /> output.C = 0 <br /> output.LFE = 0 <br /> output.SL = input.SL <br /> output.SR = input.SR |
34+
| 6 (5.1) | 1 (Mono) | output.M = 0.7071 \* (input.L + input.R) + input.C <br /> + 0.5 \* (input.SL + input.SR) |
35+
| 6 (5.1) | 2 (Stereo) | output.L = input.L + 0.7071 \* (input.C + input.SL) <br /> output.R = input.R + 0.7071 \* (input.C + input.SR) |
36+
| 6 (5.1) | 4 (Quad) | output.L = input.L + 0.7071 \* input.C <br /> output.R = input.R + 0.7071 \* input.C <br /> output.SL = input.SL <br /> output.SR = input.SR |
37+
38+
### `discrete`
39+
40+
| Number of input channels | Number of output channels | Mixing rules |
41+
| :------------------------: | :------------------------- | :------------ |
42+
| x | y where y > x | Fill each output channel with its counterpart(channel with same number), rest of output channels are silent channels |
43+
| x | y where y < x | Fill each output channel with its counterpart(channel with same number), rest of input channels are skipped |
44+
45+

0 commit comments

Comments
 (0)