Pitch Detector
Abstract realtime pitch detector — feed audio buffers, get pitch per frame.
What is a PitchDetector?
A stateful object that accumulates audio samples and produces PitchPoint results at each detection frame. Handles variable buffer sizes by internally accumulating until enough samples are available (important because iOS delivers audio at hardware sample rate in variable-sized chunks, which after resampling may be smaller than the required frame size).
Also maintains pitchContour — a lossless, append-only record of the detected contour, readable whole (scoring) or windowed (visualization).
When to Use
| Scenario | Use This | Why |
|---|---|---|
| Live singing feedback (karaoke, practice) | Yes | Frame-by-frame with live contour |
| Offline extraction from a complete recording | No | Use PitchContourExtractor via PitchDetection.createContourExtractor |
Construct instances via PitchDetection.createDetector — do not subclass.
Quick Start (Swift)
let detector = PitchDetection.createDetector()
// Feed audio from recorder (ADR-017: pass hardware rate, resampling internal)
let point = detector.detect(samples: audioBuffer, sampleRate: 48000)
let amplitude = detector.getAmplitude(samples: audioBuffer, sampleRate: 48000)
// Cleanup
detector.close()Common Pitfalls
Call
close()when done: Detectors hold native resources.Call
clearPitchContour()between segments: pitchContour accumulates across calls. Reset it when starting a new recording.
See also
Factory method
Configuration (BALANCED, PRECISE, RELAXED)
For batch extraction instead of realtime
The per-frame result
The accumulated session contour
Properties
The configuration used to create this detector
Check if post-processing is active
Per-emission pitch stream — same source and rate as pitchContour, but as individual events instead of an accumulating record. Each new pitch point fed into the contour is also emitted here.
The session's pitch contour — every detected frame, in detection order, recorded losslessly. Filled by feedContour; each frame is back-spread from the supplied anchor by the detector's hop, so the latest frame lands at anchorTime and earlier ones at anchorTime - K*hopSec.
Whether post-processing is currently enabled. Set to enable/disable at runtime.
Functions
Clear the accumulated pitch contour.
Drop all contour points with timestamp >= timeSeconds, keep everything before.
Single-shot pitch detection. Returns the latest pitch the detector can compute from the supplied audio.
Create a duplicate detector with the same configuration.
Stream raw audio into pitchContour.
Get the amplitude (RMS) of the audio samples.
Look up the contour point closest to timeSeconds.