The "Music Key Finder" GitHub project is designed to help musicians, analysts, and enthusiasts identify the musical key of any given audio file. Using advanced algorithm and signal processing techniques, this tool analyzes the harmonic content of the audio and determines its key, making it easier for users to understand and work with music.
To install the librosa
module from the command prompt, run the following command:
pip install librosa
To learn more about the features of the module, here is a useful link:
The Krumhansl-Schmuckler key-finding algorithm is a widely-used computational model designed to analyze musical pieces and determine their key. Developed by Carol Krumhansl and Edward Schmuckler, this algorithm is based on the concept of tonal hierarchies, which posits that certain tones are perceived as more stable or central within a given key. The algorithm leverages these tonal hierarchies to compute the most likely key of a piece of music by comparing the distribution of pitch classes in the music to pre-defined key profiles.
The Krumhansl-Schmuckler key-finding algorithm operates through the following steps:
-
Pitch Class Profile (PCP) Calculation:
- The musical piece is analyzed to extract the frequencies or durations of each pitch class (e.g., C, C#, D, etc.).
- These values are aggregated to form a PCP, a vector representing the distribution of pitch classes in the piece.
-
Correlation With Key Profiles:
- The PCP is compared against pre-defined key profiles for all major and minor keys. These key profiles represent the ideal distribution of pitch classes for each key.
- The comparison involves calculating a correlation coefficient between the PCP and each key profile, resulting in a set of correlation values.
-
Key Determination:
- The key with the highest correlation value is identified as the most likely key of the musical piece.
During the development of this project, particular attention was given to implementing the Krumhansl-Schmuckler key-finding algorithm efficiently and accurately. Here’s a detailed outline of the steps and considerations involved.
The core of the Krumhansl-Schmuckler algorithm relies on calculating the Pearson correlation coefficient between the pitch profile
where:
-
$P_i$ is the frequency of the$i$ -th note in the musical passage. -
$K_i$ is the value of the$i$ -th element in the key profile. -
$\bar{P}$ is the mean of the values in$P$ . -
$\bar{K}$ is the mean of the values in$K$ .
To compute
These means are essential for normalizing the data and ensuring the correlation calculation reflects the relative contributions of each note or pitch class.
The detailed implementation involves substituting the means into the correlation formula:
This approach ensures that the algorithm is adaptable to different musical systems, accommodating various numbers of notes or pitch classes beyond the traditional 12-tone chromatic scale.
For each potential key
By following these steps, the Krumhansl-Schmuckler key-finding algorithm achieves a systematic and empirical approach to key detection. Its reliance on tonal hierarchies and statistical correlation ensures a high level of accuracy and robustness across different musical genres and styles, making it a versatile and preferred method in the field of computational music analysis for musicians and analysts alike.
Two annexed documents are attached to the project:
- A pedagogical music theory lesson (simplified, discovery) on the major notions of music, written by me [FR]
- An article, a very interesting study on the Krumhansl-Schmuckler Key-Finding algorithm [EN]
Tip
If you have a curious profile, reading these two documents is a good thing.
music-key-finder/
├─ README.md
├─ LICENSE
├─ Formula A.png
├─ Formula B.png
├─ The_Fourth_Art.png
├─ L’Art de la Musique - Music Theory Lessons.pdf
├─ Krumhansl-Schmuckler Key-Finding Algorithm - Article.pdf
└─ src/
├─ script.py
└─ Miroirs No. 3, Une Barque Sur L'Océan (Maurice Ravel).mp3
Created by @Corentin-Lcs. Feel free to contact me !
Distributed under the GNU GPLv3 license. See LICENSE for more information.