Skip to content

Instantly share code, notes, and snippets.

@teunbrand
Last active July 5, 2020 00:37
Show Gist options
  • Save teunbrand/26507586cf3ab784082c093ad2243048 to your computer and use it in GitHub Desktop.
Save teunbrand/26507586cf3ab784082c093ad2243048 to your computer and use it in GitHub Desktop.
Making a plot of a midi file
library(ggplot2)
library(tuneR)
midi <- tuneR::readMidi("https://www.8notes.com/school/midi/piano/beethoven_ode_to_joy.mid")
df <- tuneR::getMidiNotes(midi)
# Difference between first two notes
mult <- 960
ggplot(df, aes(x = time / mult, y = note)) +
geom_point() +
geom_segment(
aes(xend = time / mult + length / mult,
yend = note)
) +
scale_x_continuous(
breaks = seq(0, max(df$time + df$length)/mult, by = 4),
labels = function(x){x/4},
name = "bars"
) +
scale_y_continuous(
breaks = function(x){
x <- seq(x[1], x[2], by = 12)
as.vector(outer(x, c(1,3,5,6,8,10,12), "+"))
},
labels = function(x){tuneR::notenames(-69:62)[x + 1]}
) +
ggtitle(
midi$parameterMetaSystem[midi$event == "Sequence/Track Name"][[1]],
midi$parameterMetaSystem[midi$event == "Text Event"][[1]]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment