stringpitcher-wasm
v0.1.3
Published
WebAssembly bindings for the StringPitcher pitch detection library
Readme
StringPitcher
Ultra low-latency pitch detection library for guitar and bass instruments. Works in browsers and Node.js via WebAssembly.
Features
- Real-time pitch detection using McLeod Pitch Method (MPM)
- Support for 6-string guitar, 4-string bass, and 5-string bass
- 12 built-in tuning presets (Standard, Drop D, Open G, etc.)
- Chromatic mode for detecting any note
- Configurable A4 reference pitch (432-448 Hz)
- < 20ms latency
Installation
npm install stringpitcher-wasm
# or
pnpm add stringpitcher-wasm
# or
yarn add stringpitcher-wasmQuick Start
import { StringPitcher, Instrument, TuningPreset } from 'stringpitcher-wasm';
// Create tuner with your audio sample rate
const tuner = new StringPitcher(48000);
// Configure instrument and tuning
tuner.setInstrument(Instrument.Guitar6String);
tuner.setTuningPreset(TuningPreset.Standard);
// Process audio samples (from Web Audio API, etc.)
function processAudio(audioBuffer) {
const samples = audioBuffer.getChannelData(0); // Float32Array
const result = tuner.processAudio(samples);
if (result) {
console.log(`Note: ${result.noteName}${result.octave}`);
console.log(`Frequency: ${result.frequency.toFixed(1)} Hz`);
console.log(`Cents off: ${result.centDeviation.toFixed(1)}`);
console.log(`In tune: ${result.isInTune}`);
console.log(`Direction: ${result.tuningDirection}`); // -1: flat, 0: in tune, 1: sharp
}
}Web Audio API Example
import { StringPitcher } from 'stringpitcher-wasm';
async function startTuner() {
const audioContext = new AudioContext();
const tuner = new StringPitcher(audioContext.sampleRate);
// Get microphone access
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const source = audioContext.createMediaStreamSource(stream);
// Create analyzer for getting audio data
const analyzer = audioContext.createAnalyser();
analyzer.fftSize = 4096;
source.connect(analyzer);
const buffer = new Float32Array(analyzer.fftSize);
function detect() {
analyzer.getFloatTimeDomainData(buffer);
const result = tuner.processAudio(buffer);
if (result && result.confidence > 0.5) {
updateUI(result);
}
requestAnimationFrame(detect);
}
detect();
}API Reference
StringPitcher
Constructor
new StringPitcher(sampleRate: number)Methods
| Method | Description |
|--------|-------------|
| processAudio(samples: Float32Array) | Process audio samples, returns PitchResult or null |
| reset() | Reset tuner state (call after audio interruption) |
| setInstrument(instrument: number) | Set instrument type |
| setTuningPreset(preset: number) | Set tuning preset |
| setReferencePitch(pitch: number) | Set A4 reference (432-448 Hz) |
| setChromaticMode(enabled: boolean) | Enable/disable chromatic mode |
| setNoiseGate(thresholdDb: number) | Set noise gate (-60 to 0 dB) |
PitchResult
| Property | Type | Description |
|----------|------|-------------|
| frequency | number | Detected frequency in Hz |
| confidence | number | Detection confidence (0-1) |
| noteName | string | Note name (e.g., "E", "A#") |
| octave | number | Octave number |
| centDeviation | number | Cents off from target (-50 to +50) |
| isInTune | boolean | Whether note is in tune (within threshold) |
| tuningDirection | number | -1: flat, 0: in tune, 1: sharp |
| matchedString | number | Matched string index (0-5) or -1 |
| signalLevel | number | Input signal level (0-1) |
Instrument
| Constant | Value | Description |
|----------|-------|-------------|
| Guitar6String | 0 | 6-string guitar |
| Bass4String | 1 | 4-string bass |
| Bass5String | 2 | 5-string bass |
TuningPreset
| Constant | Value | Description |
|----------|-------|-------------|
| Standard | 0 | Standard tuning |
| HalfStepDown | 1 | Half step down |
| WholeStepDown | 2 | Whole step down |
| DropD | 5 | Drop D |
| DropC | 6 | Drop C |
| OpenG | 9 | Open G |
| OpenD | 10 | Open D |
| DADGAD | 11 | DADGAD |
License
MIT
