MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How do I import midi files into Mathematica?

On Mar 23, 6:34 am, Roger Bagula <roger.bag... at> wrote:
> I asked this in a Mathematica help group
> and came up with an xml conversion of midi
> that Mathematica will read, but stripping down the xml tree
> is hard and I missed the pauses on the first successful track.
> Do anyone else know of a better method to
> get a midi track into a times series in Mathematica?

This is something that I've been working on recently, so the code is
pretty nascent and has only been used on simple one track midi files
consisting of four bars of notes. It might be a useful spring for you


(*This is the imported data from a MIDI file that I have. Execute this
rather than the line above so that you can see how it should work. You
can also export this to see how it should sound. It's a modern
electronic bassline!*)

ch=1;     (*assuming a range of 1-16*)



timeEvents=Flatten[Union[noteonPos,noteoffPos]-1];     (*These are
timing events since the last timing event.*)

realTimeEvents=Accumulate[midiIn[[timeEvents]]];     (*Time events are
accumulated to give actual timing events.*)

{i,Length[timeEvents]}]];   (*The original timing events are replaced
by the real timing events*)

+2}}];     (*This function is for extracting timing, note event, note
number, and velocity for each note on/off evnt*)

noteOn=Flatten[{#,extractEvents[midi,#]}]&/@noteonPos;     (*This
extracts each note on event and prepends with the events position in
the imported list*)

noteOff=Flatten[{#,extractEvents[midi,#]}]&/@noteoffPos;     (*This
extracts each note off event and prepends with the events position in
the imported list*)

1]]&&#[[4]]==noteOn[[i,4]]&]]},{i,Length[noteOn]}];     (*Pair the
correct note off messages with each note on message*)

{2,4}]]+{0,0.5}]}&/@notes,Frame->True,AspectRatio->0.5]     (*Show the
notes on a grid*)

I should point out that the MIDI file that I have used here only
contains note on and off events and consists of only one MIDI channel.
There are no controller events present. If you want to work with MIDI
files that contain more than one channel and events other than note on
or off then you will need to significantly change some of this code.

Hopefully, this is a start to go on to bigger, better things! I will
try to incorporate controller events and multi-channel data myself at
some point.

Good Luck!


  • Prev by Date: Re: new functional operator
  • Next by Date: Re: OpenCL OpenCLFractalRender3D invalid binary error
  • Previous by thread: How do I import midi files into Mathematica?
  • Next by thread: Re: How do I import midi files into Mathematica?