DEV Community

Tania Alwan
Tania Alwan

Posted on Edited on

How to Find Your Singing Range With Python: Pitch Tracking From Scratch in Under 50 Lines

By Tania Alwan

Singers usually learn how to find their singing range with a piano: sing down until your voice gives out, sing up until it cracks, write down the two notes. It works, but it depends on your ear and on someone pressing keys.

A pitch tracker does the same job from a recording. In this post I build a small one in plain NumPy (no audio libraries), explain each step, and turn the output into a lowest note, a highest note and a more useful "comfortable range".

What "singing range" means in numbers

Every sung note has a fundamental frequency (f0), measured in hertz. A4 is 440 Hz by convention, and each semitone multiplies the frequency by 2^(1/12). That gives a direct formula from frequency to MIDI note number:

midi = 69 + 12 * log2(f / 440)
Enter fullscreen mode Exit fullscreen mode

MIDI 69 is A4, 60 is middle C (C4). So finding your singing range is really two steps: estimate f0 over time, then convert the lowest and highest values to note names.

Step 1: record a siren

Record yourself gliding slowly from your lowest comfortable note to your highest and back, on a vowel like "oo" or a lip trill. Use a quiet room, a phone or USB mic at a steady distance, and export a mono WAV. Don't push to the point of strain: a tracker will happily measure a painful squeak, and that is not a note you want to plan songs around.

Step 2: estimate pitch with autocorrelation

A sung note is periodic: the waveform roughly repeats every 1/f0 seconds. Autocorrelation compares a short frame with shifted copies of itself; the shift (lag) with the strongest match is the period.

import numpy as np

A4 = 440.0
NAMES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]

def hz_to_note(f):
    midi = int(round(69 + 12 * np.log2(f / A4)))
    return f"{NAMES[midi % 12]}{midi // 12 - 1}"

def frame_pitch(frame, sr, fmin=70.0, fmax=1100.0, threshold=0.5):
    """Autocorrelation pitch estimate for one frame, or None if unvoiced."""
    frame = frame - frame.mean()
    if np.sqrt(np.mean(frame ** 2)) < 0.01:  # too quiet: treat as silence
        return None
    ac = np.correlate(frame, frame, mode="full")[len(frame) - 1:]
    ac = ac / ac[0]
    lo, hi = int(sr / fmax), int(sr / fmin)
    lag = lo + int(np.argmax(ac[lo:hi]))
    if ac[lag] < threshold:  # weak periodicity: probably noise or breath
        return None
    return sr / lag

def pitch_track(signal, sr, frame_ms=40, hop_ms=10):
    n, h = int(sr * frame_ms / 1000), int(sr * hop_ms / 1000)
    out = []
    for start in range(0, len(signal) - n, h):
        f = frame_pitch(signal[start:start + n], sr)
        if f is not None:
            out.append(f)
    return np.array(out)

def singing_range(f0, low_pct=5, high_pct=95):
    """Comfortable range: ignore the extreme 5% at each end."""
    lo, hi = np.percentile(f0, [low_pct, high_pct])
    return hz_to_note(lo), hz_to_note(hi)

if __name__ == "__main__":
    sr = 16000
    # Fake "siren": a voice-like tone gliding from about A2 (110 Hz) to A4 (440 Hz)
    t = np.linspace(0, 3, 3 * sr, endpoint=False)
    freq = 110 * 2 ** (2 * t / 3)
    phase = 2 * np.pi * np.cumsum(freq) / sr
    voice = 0.6 * np.sin(phase) + 0.3 * np.sin(2 * phase) + 0.1 * np.sin(3 * phase)
    f0 = pitch_track(voice, sr)
    print("frames with pitch:", len(f0))
    print("lowest / highest detected:", hz_to_note(f0.min()), hz_to_note(f0.max()))
    print("comfortable range (5th-95th percentile):", singing_range(f0))
Enter fullscreen mode Exit fullscreen mode

Run it and the synthetic glide from A2 to A4 comes back as:

frames with pitch: 296
lowest / highest detected: A2 A4
comfortable range (5th-95th percentile): ('B2', 'G#4')
Enter fullscreen mode Exit fullscreen mode

To use your own recording, load the WAV with the standard wave module or soundfile, convert it to a float array between -1 and 1, and pass it to pitch_track.

Step 3: why the code ignores some frames

Three guards keep the result honest:

  • Silence gate. Frames with very low RMS energy are skipped, so breaths and room noise don't count as notes.
  • Search window. fmin=70 and fmax=1100 Hz limit the lags we check. That roughly covers adult voices from a low bass to a high soprano. Widen it if your voice goes beyond.
  • Periodicity threshold. If the best normalized autocorrelation peak is below 0.5, the frame is probably noisy or breathy, so it's dropped.

Step 4: lowest note vs comfortable range

The absolute minimum and maximum are fragile: one octave error or one strained note moves them a lot. Percentiles are more stable. Taking the 5th and 95th percentile of all voiced frames gives a range you actually spent time in, which is closer to what a vocal coach means by a comfortable or usable range.

Known limitations

  • Octave errors. Autocorrelation sometimes locks onto twice the period and reports a note an octave low. Production trackers such as YIN and pYIN add extra steps to reduce this.
  • One voice only. This works on a solo, dry vocal. On a full mix you'd need to separate the vocal first.
  • Range isn't voice type. Two singers with the same range can be very different voice types; timbre and where the voice feels easy matter too.

Using the result

Once you know your comfortable range, you can choose keys that sit inside it. If you sing covers, compare it with the song's melody range: when a melody goes beyond your comfortable top note, transpose the song down rather than straining. A key detector such as the Voicestars BPM and key finder tells you the original key, so you know how many semitones to move.

Range also matters for AI voice work: if you sing a guide vocal for a voice model, you get cleaner results when you stay in your own easy register.

Disclosure: this article was written in partnership with Voicestars.

Top comments (0)