Putting it All Together
A MATLAB driver script combines the pitch, key, and tempo detection algorithms to create a playlist from a group of songs. The user selects a folder containing audio files and the script automatically ignores files that aren’t .mp3, .m4a, or .wav. Stereo audio is converted to mono before processing. After keys and tempos are analyzed, the script assigns valid key transitions for each song according to the harmonic matching rules described in the background section. A tempo matching range is also assigned to be 80% - 120% of the song’s tempo.
The playlist is randomly seeded and the remaining songs are searched for harmonic matches. If multiple valid candidates are found, their tempos are checked to see if they meet the current song’s tempo matching range. If no tempo match is found, one of the harmonic matches is randomly selected as the next song. If there are no harmonic matches, a random song is selected without consideration for tempo as the tempo identification algorithm sometimes makes errors and is not robust enough to be the sole decision criterion.
The script outputs each song name, its detected key, and its detected tempo before outputting the playlist in order. These outputs are saved to a text file and shown in the command window so the user can view the results and order their songs in a music player. A sample playlist output is shown below. Although not all of the detected keys and tempos are correct, the song ordering algorithm obeys the harmonic matching rules when possible and used tempo to break the tie between Direct Address, Kyoto, and On + Off for the second position in the playlist.

The next sample playlist uses songs that were human-verified to be in the keys of C major, D major, and G major. The key analysis output is 100% accurate. Several of the songs have very high algorithmically-determined tempos, which reinforces our decision to use harmonic analysis as the primary determinant for song order. The first five songs alternate between the keys of C and G, which are related keys, and the playlist obeys the harmonic matching rules until the final position, where it chooses the only remaining song by default even though it isn't in a related key.

The final sample playlist is less accurate but provides a more realistic expectation for playlist output since the songs are all in a similar style and are in a mix of major and minor keys. Cough Syrup in B minor and was classified as its relative major key of D. Four other songs were classified as a perfect fifth off from their actual key, which is not unreasonable since related keys share four chords and differ only by one pitch. This playlist obeys the harmonic matching rules and correctly uses tempo as a tiebreaker, which can be seen in the transition from ICU to Night Shift when there were several harmonic matches available.
