Music Analysis with Python
Chris Wilson
Link To Github https://github.com/jackparsons93/music_project
As someone trained in classical music, I sought to apply data science to that discipline using Python. I used it to explore two questions about composition and to develop a motif detector. For this project, I referred to works by Bach, Mozart, and Beethoven.
The questions I answered were:
- Is a skip in stepwise voice leading used to signal a change in compositional direction?
- Where do the highest notes in a Bach composition occur?
- Is it possible to use code to detect musical motifs?
The skip signal
I began with an analysis of the proposition that a composer usually changes direction after a skip, that is a major second or greater. Generally classical and baroque composers use stepwise voice leading, which is a progression through C-D-E, one note at a time. A skip in the music occurs when skipping a note in the sequence like knocking the D note out of C-D-E to play C-E. C-E skips are typically followed by a change in direction, as we will see in the analysis below.

I began by exploring the use of major jumps. A skip of a second is going from C to D; a skip of a third is going from C to E. The visualization below highlights jumps greater than a major second, which is to say of a third or greater followed by a change in direction for three classical composers: Bach, Mozart, and Beethoven.
Finding the high note in Bach
According to Kent Kennan, Bach usually goes to the highest note at the direct center of a piece. However, as you can see from the chart below, the mode of the piece is precisely at the midpoint, 50% into the piece.
Musical High Notes

The visualization that follows shows how we generate motifs using Python code. The goal is to identify recurring patterns in music as to program in the future GANs and generative AI, so that computers can automatically compose music. The motif generator is also useful for students of music to learn how composers make motifs.

#image_title
Melodic curve refers to music that starts off low, grows high close to the center, and then returns to low again. To visualize that, I used a piece of code that plots a line graph of the pitch of the music from the beginning to the end of the composition.

Marking the motifs
As music typically involves the short recurring musical theme or idea known as motifs, I sought a way to apply code to detecting and visualizing them. I used Python to set up a motif finder. It detects steps and jumps in the music to ascertain if the pattern is recurring and then displays the most common pattern in red and the second most common pattern in green. Below is the visualization for the piece invention 13 by Bach.

#image_title
As a result of applying Python code to musical patterns and visualizing the results of the data, we can confirm two answers about classical music. One is that composers tend to put in a change direction for music after skips. The other is that in Bachโs music the highest note occurs smack in the middle. The final diagram with a line plot of invention number 2 by Bach also shows the ascending melodic curve of the music.
Building on the insights gained with Python, , I created a computer program that automatically detects motifs. On that basis, it can contribute to motif data fed into a neural network that could train generative AI to produce music in the style of Beethoven, or J.S. Bach.