@jgorset/beatnet 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +149 -2
- package/THIRD_PARTY_NOTICES.md +21 -0
- package/dist/dbn.d.ts +18 -0
- package/dist/dbn.js +600 -0
- package/dist/dbn.json +1 -0
- package/dist/dsp.d.ts +4 -0
- package/dist/dsp.js +672 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +72 -0
- package/dist/inference.d.ts +5 -0
- package/dist/inference.js +77 -0
- package/dist/preprocessing.json +1 -0
- package/dist/wasm.d.ts +7 -0
- package/dist/wasm.js +495 -0
- package/licenses/BeatNet-CC-BY-4.0.txt +395 -0
- package/licenses/madmom-BSD-2-Clause.txt +24 -0
- package/model.json +18 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Johannes Gorset
|
|
4
|
+
Copyright (c) 2024 Institute of Computational Perception, JKU Linz, Austria
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,150 @@
|
|
|
1
|
-
#
|
|
1
|
+
# BeatNet
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
BeatNet finds beats and downbeats in your browser. Give it some audio, get the
|
|
4
|
+
timestamps, and keep the audio on your machine. Pretty neat.
|
|
5
|
+
|
|
6
|
+
This is a TypeScript port of [BeatNet](https://github.com/mjhydri/BeatNet) by
|
|
7
|
+
Mojtaba Heydari and collaborators. It runs their model 1 with ONNX Runtime Web,
|
|
8
|
+
including madmom's preprocessing and DBN beat tracking.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @jgorset/beatnet
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Download [the model](https://github.com/jgorset/beatnet/releases/download/v1.0.0/beatnet.onnx)
|
|
17
|
+
and serve it from your application. It's about 1.6 MB. The preprocessing constants
|
|
18
|
+
and decoder state spaces are included in the package.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { createBeatNet } from '@jgorset/beatnet';
|
|
24
|
+
|
|
25
|
+
const tracker = await createBeatNet({ modelUrl: '/models/beatnet.onnx' });
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
// Mono audio sampled at 22,050 Hz.
|
|
29
|
+
const audio = new Float32Array(/* your audio samples */);
|
|
30
|
+
const { beats, downbeats } = await tracker.analyze(audio);
|
|
31
|
+
|
|
32
|
+
console.log(beats); // Seconds from the start of the audio.
|
|
33
|
+
console.log(downbeats); // The first beat of each bar.
|
|
34
|
+
} finally {
|
|
35
|
+
await tracker.dispose();
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Keep the tracker around if you're analyzing more than one recording. Loading the
|
|
40
|
+
model for every song would be a little rude to your computer. Each analysis starts
|
|
41
|
+
with fresh recurrent state.
|
|
42
|
+
|
|
43
|
+
Decoding files, mixing channels and resampling audio are up to your application.
|
|
44
|
+
The tracker accepts mono PCM at exactly 22,050 Hz. Run it in a Web Worker to keep
|
|
45
|
+
your interface responsive. ONNX Runtime uses WebAssembly on the CPU.
|
|
46
|
+
|
|
47
|
+
### Long recordings
|
|
48
|
+
|
|
49
|
+
You don't have to keep the whole recording in memory. Supply a sample count and
|
|
50
|
+
a reader that returns exactly the requested samples:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
const result = await tracker.analyze({
|
|
54
|
+
sampleCount,
|
|
55
|
+
read: async (first, count) => readAudioSamples(first, count),
|
|
56
|
+
}, {
|
|
57
|
+
signal: abortController.signal,
|
|
58
|
+
onProgress: (stage, completed, total) => console.log(stage, completed, total),
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Reads are sequential and cover roughly 10 seconds plus context. Recurrent state
|
|
63
|
+
continues across windows. `beats` and `downbeats` contain seconds;
|
|
64
|
+
`beatConfidence` and `downbeatConfidence` contain probabilities at 20 ms
|
|
65
|
+
intervals. Beat confidence includes downbeats. The confidence arrays and decoder
|
|
66
|
+
working memory still grow with the recording; the decoder checkpoints its
|
|
67
|
+
traceback instead of storing every state transition.
|
|
68
|
+
|
|
69
|
+
Cancellation takes effect between windows and after an in-flight inference
|
|
70
|
+
finishes. Decoding is synchronous. To stop immediately, terminate the worker.
|
|
71
|
+
Wait for analysis to settle before disposing the tracker, and run one analysis
|
|
72
|
+
at a time per tracker.
|
|
73
|
+
|
|
74
|
+
### Serving WebAssembly
|
|
75
|
+
|
|
76
|
+
Your bundler needs to serve ONNX Runtime's WASM assets. With Vite, exclude
|
|
77
|
+
`onnxruntime-web` and `onnxruntime-web/webgpu` from dependency optimization so
|
|
78
|
+
those assets keep their paths. You can also serve the files yourself:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const tracker = await createBeatNet({
|
|
82
|
+
modelUrl: '/models/beatnet.onnx',
|
|
83
|
+
wasmPaths: '/onnxruntime/',
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Copy the `ort-wasm-simd-threaded` WASM and module files from the pinned
|
|
88
|
+
`onnxruntime-web` package into that directory. The tracker uses one thread.
|
|
89
|
+
ONNX Runtime's WASM settings are shared within the page or worker, so set the
|
|
90
|
+
same paths before creating multiple trackers.
|
|
91
|
+
|
|
92
|
+
### Lower-level functions
|
|
93
|
+
|
|
94
|
+
`extractFeatures`, `inferBeatNet` and `decodeDbn` are exported if you want to
|
|
95
|
+
inspect features, work with an existing ONNX session or compare the decoder.
|
|
96
|
+
The preprocessing and decoder constants are pinned to madmom 0.16.1.
|
|
97
|
+
|
|
98
|
+
## Development
|
|
99
|
+
|
|
100
|
+
Use Node.js 24:
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
npm ci
|
|
104
|
+
npm test
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The tests cover frozen preprocessing outputs, streamed context, recurrent state,
|
|
108
|
+
decoder traceback, cancellation, invalid data and tracker lifecycle. The port
|
|
109
|
+
was also compared with the implementation it was extracted from using the real
|
|
110
|
+
model: features, activations and decoded event times matched exactly across
|
|
111
|
+
multiple recurrent windows.
|
|
112
|
+
|
|
113
|
+
### Exporting the model
|
|
114
|
+
|
|
115
|
+
Use Python 3.10 or newer in a virtual environment:
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
python -m pip install -r scripts/requirements.txt
|
|
119
|
+
python -m pip install --no-deps BeatNet==1.1.1
|
|
120
|
+
python scripts/export.py --out .tmp/export
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The exporter loads the upstream network and model 1 weights directly. It doesn't
|
|
124
|
+
need BeatNet's audio input or particle-filter dependencies. It checks the ONNX
|
|
125
|
+
model and prints its SHA-256 checksum; compare it with `model.json` before
|
|
126
|
+
replacing the released model. The export makes recurrent state explicit without
|
|
127
|
+
changing weights. Preprocessing and DBN constants stay pinned in `src`.
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
The TypeScript implementation is MIT licensed. BeatNet's model and the adapted
|
|
132
|
+
exporter forward pass retain [CC BY 4.0](licenses/BeatNet-CC-BY-4.0.txt). madmom's
|
|
133
|
+
source algorithms retain [BSD-2-Clause](licenses/madmom-BSD-2-Clause.txt).
|
|
134
|
+
See [the attribution](THIRD_PARTY_NOTICES.md) before redistributing the model.
|
|
135
|
+
|
|
136
|
+
## Contribute
|
|
137
|
+
|
|
138
|
+
* Fork the repository.
|
|
139
|
+
* Do your thing.
|
|
140
|
+
* Write a test.
|
|
141
|
+
* Open a pull request.
|
|
142
|
+
|
|
143
|
+
```sh
|
|
144
|
+
npm test
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## I love you
|
|
148
|
+
|
|
149
|
+
Johannes Gorset made this port. You should [open an issue](https://github.com/jgorset/beatnet/issues)
|
|
150
|
+
if you can't get it to work. In fact, you should tell me what you're building anyway.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Upstream attribution
|
|
2
|
+
|
|
3
|
+
BeatNet by Mojtaba Heydari and collaborators (2021):
|
|
4
|
+
https://github.com/mjhydri/BeatNet . This port uses BeatNet 1.1.1's model 1.
|
|
5
|
+
The model and adapted forward pass in `scripts/export.py` retain Creative Commons
|
|
6
|
+
Attribution 4.0 International, reproduced in `licenses/BeatNet-CC-BY-4.0.txt`.
|
|
7
|
+
The ONNX export makes the recurrent state explicit; it does not change the
|
|
8
|
+
checkpoint weights. No training data is included. Distributing the model must
|
|
9
|
+
retain this attribution and its license.
|
|
10
|
+
|
|
11
|
+
The spectrogram, positive-difference preprocessing, DBN/HMM/Viterbi decoding and
|
|
12
|
+
beat correction reproduce madmom 0.16.1:
|
|
13
|
+
https://github.com/CPJKU/madmom/tree/v0.16.1 . Its source code is BSD-2-Clause,
|
|
14
|
+
reproduced in `licenses/madmom-BSD-2-Clause.txt`. The TypeScript decoder uses
|
|
15
|
+
checkpointed traceback to bound memory. `src/preprocessing.json` and `src/dbn.json`
|
|
16
|
+
contain deterministically constructed filterbanks, windows and state spaces;
|
|
17
|
+
none of madmom's pretrained models are distributed.
|
|
18
|
+
|
|
19
|
+
The TypeScript implementation and other original contributions use the MIT
|
|
20
|
+
license in `LICENSE`. Upstream materials retain the licenses above. ONNX Runtime
|
|
21
|
+
Web and fft.js retain their notices in their installed packages.
|
package/dist/dbn.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type Hmm = {
|
|
2
|
+
states: number[];
|
|
3
|
+
pointers: number[];
|
|
4
|
+
logTransitions: number[];
|
|
5
|
+
observations: number[];
|
|
6
|
+
positions: number[];
|
|
7
|
+
initial: number[];
|
|
8
|
+
};
|
|
9
|
+
export type Dbn = {
|
|
10
|
+
hmms: Hmm[];
|
|
11
|
+
threshold: number;
|
|
12
|
+
observationLambda: number;
|
|
13
|
+
};
|
|
14
|
+
export declare function decodeDbn(scores: Float32Array, config?: Dbn): {
|
|
15
|
+
beats: number[];
|
|
16
|
+
downbeats: number[];
|
|
17
|
+
workingBytes: number;
|
|
18
|
+
};
|