@playground-sessions/basic-pitch
v0.1.0
Published
Experimental browser pitch detection sandbox (naive autocorrelation/AMDF/FFT). My first try at Web Audio pitch detection, rough
Readme
@playground-sessions/basic-pitch
A sandbox implementation of three different pitch detection algorithms for comparison:
- Autocorrelation (time-domain)
- Average Magnitude Difference Function (AMDF)
- Fast Fourier Transform (FFT) with Harmonic Product Spectrum
This is kind of a failure. I didn't get it to work.
Features
- Three different pitch detection methods in one package
- Compare algorithms side-by-side
- Interactive demo with real-time visualization
- TypeScript types included
- Zero dependencies
Installation
npm install @playground-sessions/basic-pitchUsage
import {
AutocorrelationPitchDetector,
AmdfPitchDetector,
FftPitchDetector,
CombinedPitchDetector
} from '@playground-sessions/basic-pitch';
// Use individual detectors
const detector = new AutocorrelationPitchDetector({
frameSize: 2048,
sampleRate: 44100,
minFrequency: 50,
maxFrequency: 2000
});
// Or use the combined detector that picks the best result
const combined = new CombinedPitchDetector({
frameSize: 2048,
sampleRate: 44100
});
// Analyze audio frames
const frame = new Float32Array(2048); // Your audio data here
const result = detector.analyze(frame);
console.log(`Frequency: ${result.frequency} Hz`);
console.log(`Confidence: ${result.confidence}`);
console.log(`Method: ${result.method}`);Demo
An interactive demo is included that lets you compare all three methods in real-time:
- Clone this repository
- Run
npm install - Run
npm run dev - Open your browser and try the pitch detector
How it Works
Autocorrelation Method
- Compares the signal with delayed versions of itself
- Finds periodic patterns in the time domain
- Good for clean, monophonic signals
AMDF Method
- Similar to autocorrelation but uses differences instead of products
- Can be more robust to noise in some cases
- Computationally efficient
FFT Method
- Transforms the signal to the frequency domain
- Uses Harmonic Product Spectrum for better accuracy
- Good for complex tones with strong harmonics
Options
All detectors accept these options:
frameSize: Size of the analysis frame (default: 2048)sampleRate: Sample rate of the audio (default: 44100)minFrequency: Minimum frequency to detect (default: 50 Hz)maxFrequency: Maximum frequency to detect (default: 2000 Hz)
Contributing
This is an educational project built during the WebAudio Pitch Jam '25. Contributions are welcome!
