(digmus-intro)=
# Animating a melody as a mathematical object
[CopenhagenR - useR Group meetup 2024](https://www.meetup.com/copenhagenr-user-group/events/301262743/)
This is an exploratory project beginning with the thought that given music can be digitised, there must be a way to translate it to a dataframe in R, and then wrangle to something that can be transformed to a graph, plotted using `ggraph`, and finally animated with `gganimate`.
## The melody
As is only right and proper, I used the _subject_ of Contrapunctus I from J.S. Bach's _The Art of Fugue_, a towering study of melody by the composer who defined the rules of melody.
![](https://upload.wikimedia.org/score/k/u/kuqreev9vn17n29y7rcsbjjw752x7we/kuqreev9.png)
(Image source: wikipedia)
Happily, wikipedia provides the midi along with the image of melody.
## The math
It's impossible to talk about graphs without beginning with Euler's solution to [The Seven Bridges of Konigsberg problem](https://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg), where the puzzle was to find a walking path through the city that crosses each of its seven bridges exactly once. Euler invented graph theory to show it could not be done.
A _graph_ comprises a set of nodes, and a set of edges, where each edge is a set of two nodes. The brilliance of Euler's solution was to realise the size and shape of the landmasses were irrelevant, only the the bridges (edges) between them.
(Image source: wikipedia)
## The code
To animate a midi as a graph, I took these [steps](https://github.com/softloud/digmus/tree/main/step-by-step):
1. Convert midi to R object using `pyramidi`.
2. Create melody dataframe.
3. Use `gt` to make a table of notes.
4. `tidygraph` graph of notes.
5. `ggraph` plot.
6. `gganimate` graph plot.
7. Add audio.
In the next sections, I describe each step in more detail.