Key Detection
Musical key detection for audio tracks.
This module provides key estimation using chroma features and pattern matching, with Camelot wheel notation support for DJs.
- class mixref.detective.key.KeyResult[source]
Bases:
objectResult of key detection.
- __init__(key, camelot, confidence)
- mixref.detective.key.detect_key(audio, sample_rate)[source]
Detect musical key of an audio signal.
Uses chroma features to estimate the most likely musical key. Prefers flat notation (Eb instead of D#) as per mixref conventions.
- Parameters:
- Returns:
KeyResult with key name, Camelot code, and confidence.
- Return type:
Example
>>> import numpy as np >>> from mixref.detective.key import detect_key >>> >>> # Generate C major-ish signal >>> sr = 22050 >>> duration = 10 >>> audio = np.sin(2 * np.pi * 261.63 * np.arange(sr * duration) / sr) >>> >>> result = detect_key(audio, sr) >>> print(result.key) C major
- mixref.detective.key.get_compatible_keys(key)[source]
Get harmonically compatible keys for mixing.
Returns keys that are adjacent on the Camelot wheel (same number ±1, or same letter).
- Parameters:
key (str) – Musical key (e.g., “C major”, “8B”).
- Returns:
List of compatible keys in Camelot notation.
- Return type:
Example
>>> from mixref.detective.key import get_compatible_keys >>> >>> compatible = get_compatible_keys("8B") >>> print(compatible) ['7B', '9B', '8A']