Skip to content

Commit f2ab6f9

Browse files
authored
Updates README file
1 parent 16713fe commit f2ab6f9

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,79 @@
11
# Advanced Analyser Node
2+
3+
Advanced Analyser Node aims to be an enhanced version of the native Analyser Node. Both Audio nodes provide real-time frequency and time-domain analysis information.
4+
5+
## Why this exists?
6+
The native Analyser Node offers two primary features: Request frequency data (`.getFloatFrequencyData|.getByteFrequencyData`) and time-domain data (`.getFloatTimeDomainData|.getByteTimeDomainData`). The main problem is that the Analyser Node only gives you the data at the time of the request, meaning that when you request for the frequency data, the node will apply the Fast Fourier Transform to the last n audio samples (n is the fftSize property), or in the case of time-domain data, the last n audio samples.
7+
8+
That's good enough if you are creating an audio visualization of the data currently being played, as the usual approach is to create a loop that requests and draws the data every frame (and if you're doing that, you should probably stick to the native Analyser Node as it will give you better performance).
9+
10+
The problem happens when you need to display the data **over time** (ex: Spectrograms, the waveform of the whole track). As the native Analyser Node gives you the data at the request time, it's impossible to guarantee that all requests will be evenly spaced in time. That means you will have inconsistent data overlaps or even skip some samples altogether. Depending on your goal, overlapping or skipping samples are not a problem per se; the issue is the inconsistency as the interval between samples will be completely based on your loop FPS.
11+
12+
The Advanced Analyser Node provides all the native version features (with a slightly different API), but also gives you the option of registering events that will be consistently triggered based on its configuration. For advanced users, you also have the option of choosing between different window functions.
13+
14+
## Installation
15+
16+
Using npm:
17+
```bash
18+
npm install --save-dev advanced-analyser-node
19+
```
20+
Using yarn:
21+
```bash
22+
yarn add --dev advanced-analyser-node
23+
```
24+
25+
26+
## Usage
27+
28+
```javascript
29+
import { createAdvancedAnalyserNode } from 'advanced-analyser-node';
30+
31+
const init = async () => {
32+
33+
const stream = await navigator.mediaDevices.getUserMedia({
34+
audio: true,
35+
video: false
36+
});
37+
38+
const context = new AudioContext();
39+
const audioSource = context.createMediaStreamSource(stream);
40+
41+
const analyserNode = await createAdvancedAnalyserNode(context, {
42+
fftSize: 2048, // needs to be a power of 2
43+
samplesBetweenTransforms: 1024 // fixed samples interval between transforms
44+
// it can be greater or smaller than the fftSize, which will cause overlaps or skip samples
45+
});
46+
47+
const transforms = [];
48+
analyserNode.addEventListener('bytefrequencydata', ({ detail }) => {
49+
transforms.push(detail)
50+
})
51+
52+
audioSource.connect(analyserNode)
53+
54+
}
55+
56+
init();
57+
```
58+
59+
```javascript
60+
const data = await analyserNode.getByteFrequencyData() // Differently from the native implementation, the request methods are asynchronous, and do not take an array as parameter
61+
```
62+
63+
64+
## Documentation
65+
66+
67+
## Caveats
68+
69+
- The performance is enough for most cases (around 1ms~2ms per request with the default settings). However, the native Analyser Node will always give you better performance, so if the native implementation suits your needs, use it.
70+
71+
- Differently from the native implementation, this node will down-mix its channels to mono, so do not use it as an intermediary node (don't connect this node to another).
72+
73+
- The native api allows you to reuse the same arrays between requests to save on memory. It's not possible to do that with this node due to technical limitations of the Audio Worklet api.
74+
75+
76+
77+
78+
## License
79+
[MIT](https://choosealicense.com/licenses/mit/)

0 commit comments

Comments
 (0)