pitch-transcription
v4.4.0
Published
The pitched notes in a recording — Spotify's Basic Pitch model on ONNX Runtime, its readings turned into notes with pitch bends and written as MIDI.
Maintainers
Readme
pitch-transcription
The notes played in a recording: each note's pitch, start, length, loudness and pitch bend, heard by Spotify's Basic Pitch model on ONNX Runtime and written as MIDI if you want a file.
Design notes live in the specs.
Table of Contents
🚀 Getting Started
pnpm i pitch-transcription onnxruntime-common onnxruntime-nodeimport { InferenceSession } from "onnxruntime-node";
import { fileURLToPath } from "node:url";
import {
addPitchBends,
convertNotesToSeconds,
createNotes,
MODEL_URL,
readModel,
writeMidi,
} from "pitch-transcription";
// Under Node, the session reads the shipped model file; in a browser, `InferenceSession.create(servedModelUrl)` from `onnxruntime-web` instead
const session = await InferenceSession.create(fileURLToPath(MODEL_URL));
// One channel at 22050 Hz as a Float32Array, or an AudioBuffer at 22050 Hz with any number of channels
const readings = await readModel(session, samples, (_windowReadings, progress) => console.log(progress));
const notes = createNotes(readings, { minNoteLength: 11 });
const bentNotes = addPitchBends(readings.contours, notes);
const timedNotes = convertNotesToSeconds(bentNotes);
// Each note's startTimeSeconds, durationSeconds, pitchMidi, amplitude and pitchBends
const midiBytes = writeMidi(timedNotes);
await session.release();A worker or a server that already holds the readings imports note creation from pitch-transcription/notes, which never loads ONNX Runtime.
pitch-transcription takes onnxruntime-common as a peer; the runtime the session runs on is yours to choose, onnxruntime-node under Node or onnxruntime-web in a browser.
🧩 Key Exports
| Export | Kind | Purpose |
| :---------------------- | :------- | :----------------------------------------------------------------------------------------------------------------- |
| addPitchBends | function | Each note's bend at every frame, in contour bins of a third of a semitone |
| convertNotesToSeconds | function | Notes timed in seconds rather than the model's frames |
| createNotes | function | The readings turned into notes, with Basic Pitch's thresholds, inferred onsets, frequency range and melodia trick |
| MODEL_URL | const | The shipped model file, Basic Pitch's ONNX export |
| readModel | function | The model's frame, onset and contour readings over a recording, a window at a time, each output disposed once read |
| writeMidi | function | Notes as a MIDI file's bytes, each bend written as its share of the bend's range |
⚖️ License
This project is licensed under the Apache-2.0 license. It is derived from basic-pitch-ts, Copyright 2022 Spotify AB, and ships its trained model unmodified, both under the Apache-2.0 license; see NOTICE.
