# How to visualize music from midi file format?

From What I understand midi format is a digital format computers can read and reproduce.

Is there a way to assign shapes and colors to certain frequencies and durations to visualize the music encoded in the midi files? If so how do I begin?

What I would like to see is shapes and colors change as the scale progresses, maybe scroll left to right which is natural progression from playing music standpoint.

Any help and insights would be appreciated.

• For starters, try something like mf = Import["C:\Users\W10\Downloads\c scale.mid"] to get this. And see ref/format/MIDI
– kglr
Mar 10, 2017 at 23:21

So,

mf = INSERT YOUR FILE HERE (DRAGGING WORKS)


Then run

Needs["Music"]

i = 1
freq = ConstantArray[0, Length[mf[[1]]]];
duration = ConstantArray[{0, 0}, Length[mf[[1]]]];

For[i, i <=  Length[mf[[1]]], i++,

timehold = mf[[1]][[i]][[2]][[2]] - mf[[1]][[i]][[2]][[1]];

duration =  ReplacePart[duration, timehold, i];
freq = ReplacePart[freq, {{i, mf[[1]][[i]][[1]]}}, i];]

freq = ToExpression[freq];

ListPlot[freq, PlotStyle -> (PointSize /@ (duration/3)),
PlotRange -> {{-3, 33}, {100, 600}}]


There might be a better way to do it than that, but I'm relatively new to mathematica and this works. Anyway, this then gives you a plot where the radius of the point corresponds to the duration the note is played and y axis corresponds to the pitch and the x axis in what order the notes are played.

EDIT:

To include animation

Animate[
ListPlot[freq[[1 ;; j]], PlotStyle -> (PointSize /@ (duration/3)),
PlotRange -> {{-3, 33}, {100, 600}}], {j, 1, Length[mf[[1]]], 1}]
`

• Wow that's beautiful! But It doesn't animate. Like as the the midi plays i wanted the shapes and colors to move across the screen like I've seen some mathematica demo's have shown. Or maybe it was some other platform, I don't remember exactly. Mar 11, 2017 at 3:14
• @bo reddude Added an animation.
– Tomi
Mar 11, 2017 at 11:55
• This is fantastic, and also quite fun! @Tomi Mar 11, 2017 at 17:17
• All these [[]] is so confusing. Would you mind terribly giving me a line by line explanation of what you're doing. I'm guessing it's because you just want the relevant part of the midi file, right? I guess I have to figure out what midi file actually looks like in Mathematica, too... Mar 11, 2017 at 20:41
• Its to get the relevant part of the file. If you just take them away in stages you can see whats in the file.
– Tomi
Mar 12, 2017 at 0:33