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: object

Result of key detection.

key

Detected musical key (e.g., “C major”, “Eb minor”).

Type:

str

camelot

Camelot wheel notation (e.g., “8B”, “5A”).

Type:

str

confidence

Confidence score from 0.0 to 1.0.

Type:

float

key: str
camelot: str
confidence: float
__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:
  • audio (Any) – Audio signal as numpy array. Shape: (samples,) for mono or (channels, samples) for multi-channel.

  • sample_rate (int) – Sample rate in Hz.

Returns:

KeyResult with key name, Camelot code, and confidence.

Return type:

KeyResult

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:

list[str]

Example

>>> from mixref.detective.key import get_compatible_keys
>>>
>>> compatible = get_compatible_keys("8B")
>>> print(compatible)
['7B', '9B', '8A']