Hum a melody, get a cleaned-up ("autocorrected") MIDI back--plus optional similarity search against a local melody DB.
- Audio → f0 with
librosa.pyin - Note segmentation and quantization to key/tempo grid
- Simple autocorrect: scale/key snapping + smoothing
- MIDI export via
pretty_midi - Lightweight melody embeddings + cosine similarity search
This project is intentionally small so you can extend it (e.g., train a PyTorch model for smarter correction).
# 1) Create & activate a venv (recommended)
python -m venv .venv && source .venv/bin/activate # (Windows: .venv\Scripts\activate)
# 2) Install deps
pip install -r requirements.txt
# 3) Run CLI help
python -m melody_autocorrect.cli --help
# Example: Convert a WAV to autocorrected MIDI in C major
python -m melody_autocorrect.cli hum2midi --audio path/to/hum.wav --out out.mid --key C --mode major
# Example: Build a tiny DB, then search
python -m melody_autocorrect.cli index --midi_dir examples/midi --db melodies.json
python -m melody_autocorrect.cli search --query out.mid --db melodies.json --topk 5src/melody_autocorrect/
├── audio_io.py # load/record audio
├── f0.py # pitch tracking (pyin)
├── theory.py # scales, quantization helpers
├── midi.py # note building + MIDI I/O
├── autocorrect.py # end-to-end pipeline
├── embed.py # melody embeddings + similarity
└── cli.py # command line interface
- Default key is C major. Pass
--key/--modeto change; or use--auto_keyfor a naive key guess. - For real projects, consider better note segmentation, HMM/Viterbi decoding, and a trained corrector (Transformer/LSTM). The hooks are here.
- Recording from mic uses
sounddeviceif available; otherwise load WAVs.
None. :)