Get note data from MIDI file

19,727

Solution 1

Nik Reisman - sorry, but I don't agree with you...parsing midi in C#, C++ is something about 400 rows of code..It's nothing hard and it is nothing difficult.

I will advise you start with this link: https://web.archive.org/web/20141227205754/http://www.sonicspot.com:80/guide/midifiles.html
There is everything you need to know about midi and how to read it..

In the short description how the parser will work:
1)Open midi in byte mode
2)Read the header chunk where there is info about size, number of tracks and IMPORTANT file format!!
- There are 3 types of formats: 0,1,2 (type 2 is really "valuable", there are only few midi files with this type, so you don't need to read the midi if there is type 2)
- if there is not written: "MThd" (0x4D546864), end with error (it's a bad midi file)
3)Read track chunk
- if there is not written: "MTrk" (0x4D54726B) end with error (it's a bad midi file)
4)Read the midi events.. - There are very many events, you can read them all with if-else commands, or you can read only the events what you want to know, for example NOTE ON, NOTE OFF - Sometimes in some midi files are not NOTE OFF..this event is changed with NOTE ON and velocity 0

On the websites everything is explained really nicely. If you open the midi file in byte mode you will have only a few methods and everything is then only about if-else commands and there you will catch what is stored right now.
It is important to understand VARIABLE LENGTH, but on the websites it is also explained. It's not hard. You can google many sites where VARIABLE LENGTH is explained too, with some images and examples. So I don't think that it is hard to explain it in here.

If you want a bit more advice, write me, I will try it. But parsing midi is not as hard as how it looks. If you have some problems, write me..

Solution 2

Parsing MIDI files by hand is no fun, take my word on this. ;) The format, although well documented, is difficult to deal with since you are always on the raw byte level. Since you are interested in extracting some meaningful information from MIDI files themselves, I'd recommend using a framework such as Juce, which is written in C++ and has support for reading MIDI files.

Juce is pretty large, but the API is good and well-documented. The class for parsing MIDI files, for instance, is pretty straightforward and easy to use.

Share:
19,727
Bill
Author by

Bill

Updated on July 19, 2022

Comments

  • Bill
    Bill almost 2 years

    Is there a way to get the note data from a MIDI file? That is, I want to break down the MIDI file into its constituent parts so they are in the form of a unique word (or any other data type). What I want to do in the end is take in a MIDI file and find patterns in the notes. Get in each note, find it's frequency (of being played) and note how likely other notes are to be played after it.

    It would be nice to do this in C/C++, but any language would be fine.

  • thesaint
    thesaint almost 10 years
    NIH syndrom maybe? I mean a std::vector class is not hard to write on the surface its just a variable length array... std::log2() is not hard to write either... and so on. Why not write everything yourself? Stuff like file parsing by hand is the most redicolous thing of them all, even more so when dealing with "standard" formats that definitely should have a working solid implementation somewhere.
  • tomdelahaba
    tomdelahaba almost 10 years
    Sorry, I could send him 2198312093 frameworks and 3rd party solutions and he could get bigger problems then only with writing it by his own. Atleast if I use 3th party libraries i am getting bigger problems then if I wroted it by myself. And anyway SOMEONE needs to write the code. If I am the one or some Joseph Nowak from Taiwan write it is not important. All the time you can get some errors. And if you want really UNDERSTAND what some library is doing, you need to look at the code behined anyway. So in many cases it is easier to write it by yourself.