audio
@tabkit/audio
Web Audio API synthesis for tablature playback — play notes with realistic pluck envelopes.
Scale Playback with Audio
A full C major scale played with TabPlayer + TabAudio. Press play to hear each note synthesized as the cursor moves.
Measure 1:1
import { TabAudio } from "@tabkit/audio";
import { TabPlayer } from "@tabkit/player";
const audio = new TabAudio({ waveform: "triangle", volume: 0.7 });
const player = new TabPlayer(measures, {
tempo: 100,
onNote: (notes) => audio.playNotes(notes, 100),
onEnd: () => console.log("Done!"),
});
player.attachSvg(svgElement);
player.play();Note Playground
Click individual notes, chords, or raw frequencies to hear them instantly.
Open Strings
C Major Scale
Chords
import { TabAudio, Synth } from "@tabkit/audio";
// High-level: play a note on string 1, fret 5
const audio = new TabAudio({ waveform: "triangle" });
audio.playNote({ string: 1, fret: 5, duration: "4n" }, 120);
// Play a chord (multiple notes at once)
audio.playNotes([
{ string: 6, fret: 0, duration: "4n" },
{ string: 5, fret: 2, duration: "4n" },
{ string: 4, fret: 2, duration: "4n" },
], 120);
// Low-level: play a raw frequency
const synth = new Synth();
synth.playTone(440, 0.5, { waveform: "triangle" });Synth — Raw Frequencies
fretToFrequency
Live results from fretToMidi and midiToFrequency.
| String | Fret | MIDI | Frequency | fretToFrequency() |
|---|---|---|---|---|
| S6 (E2) | 0 | 40 | 82.41 Hz | 82.41 Hz |
| S5 (A2) | 0 | 45 | 110.00 Hz | 110.00 Hz |
| S4 (D3) | 0 | 50 | 146.83 Hz | 146.83 Hz |
| S3 (G3) | 0 | 55 | 196.00 Hz | 196.00 Hz |
| S2 (B3) | 0 | 59 | 246.94 Hz | 246.94 Hz |
| S1 (E4) | 0 | 64 | 329.63 Hz | 329.63 Hz |
| S1 (A4) | 5 | 69 | 440.00 Hz | 440.00 Hz |
| S1 (E5) | 12 | 76 | 659.26 Hz | 659.26 Hz |
import { fretToMidi, midiToFrequency, fretToFrequency } from "@tabkit/audio";
import { resolveInstrument } from "@tabkit/core";
const guitar = resolveInstrument("guitar");
// Step-by-step
const midi = fretToMidi(guitar, 1, 5); // → 69
const freq = midiToFrequency(midi); // → 440.00 Hz
// One-shot convenience
const hz = fretToFrequency(guitar, 1, 5); // → 440.00 HzPitch Conversion Table
Live results from fretToMidi and midiToFrequency.
| Note | String | Fret | MIDI | Frequency |
|---|---|---|---|---|
| E2 (6th open) | 6 | 0 | 40 | 82.41 Hz |
| A2 (5th open) | 5 | 0 | 45 | 110.00 Hz |
| C4 (3rd, 5th fret) | 3 | 5 | 60 | 261.63 Hz |
| E5 (1st, 12th fret) | 1 | 12 | 76 | 659.26 Hz |
Constants
TUNING_MIDI
{
"guitar": [
40,
45,
50,
55,
59,
64
],
"bass": [
28,
33,
38,
43
],
"ukulele": [
67,
60,
64,
69
],
"banjo": [
67,
50,
55,
59,
62
]
}PLUCK_ENVELOPE
{
"attack": 0.003,
"decay": 0.15,
"sustain": 0.15,
"release": 0.3
}API Quick Reference
TabAudio
| Name | Type | Description |
|---|---|---|
| playNote(note, tempo?) | void | Play a single note |
| playNotes(notes[], tempo?) | void | Play chord |
| setVolume(v) | void | Set volume (0-1) |
| setWaveform(w) | void | Change oscillator |
| mute() / unmute() | void | Toggle mute |
| destroy() | void | Clean up |
Pitch Utilities
| Name | Type | Description |
|---|---|---|
| fretToMidi(inst, s, f) | number | String+fret → MIDI |
| midiToFrequency(midi) | number | MIDI → Hz |
| fretToFrequency(inst, s, f) | number | String+fret → Hz |