Go Down

Topic: Reading MIDI File, and parsing (Read 7462 times) previous topic - next topic


I am working on a project that reads a type 0 midi file from an SD card, and parses the commands.
First, to play it to the sound generator in the proper timing, I need to calculate the timing of the commands (tempo and other commands buried in the file provide the info to do this).
Then, I need to parse the commands (they are of various lengths, depending on the command) so that they are sent to the Fluxamasynth board at the proper time.
I can create manual commands, and make the Fluxamasynth operate, but i need to be able to play .mid files from the SD card. 
I use a serial port for the fluxamasynth (Modern Devices, very nice shield).
Most of the MIDI libraries I have found work on serial data arriving, and going out again to an external sequencer or other device.
I am using a DUE, and have no problem finding and opening and reading the files, I just need to parse them to determine the "dividers" and breaks between the commands.
Any assistance on this wold be appreciated.
I don't want to re-invent the wheel here!!!! I'm hoping to find a used wheel that will work.


I don't want to re-invent the wheel here

Invent away, I have been looking for that for years and have not found one that anyone is willing to document.


I think you can find enough documentation on the web.
Subscribe my youtube channel: http://www.youtube.com/user/MKDS3


I now have a working midi type 0 file parser that works with Modern Devices  Fluxamasynth boards.
And I found a program (Sweet MIDI Converter) that will convert type 1 and 2 files to type 0 to convert the file before loading on the SD card the parser reads the file from.
It is newbie code, but does operate, if anyone would like to view this code, please let me know.
It can read through the file, throwing out the MIDI items it does not need, and playing the various notes and instruments available with the General MIDI set of voices.  It does require a Due to have enough horsepower to operate correctly. :.


Yes I would like a look at your code please.


I am working on something very similar.
I built an 8-channel floppy drive device based on this design: http://www.youtube.com/watch?v=w6tuMn5sPyM
The huge problem is that it is tethered to a PC of some sort.
I want to make a stand-alone device.
I believe that I can still use all eight channels and tie in an SD card reader.

The idea is to grab a file from the SD card, read it, parse it out into the appropriate data, and sequence it out to the device.
The fact that there is little RAM on the standard UNO means that I would be using some empty space on the SD card for data storage while parsing the file.
The final output of the parser would be a readily usable set of MIDI commands that the player portion of the program would read and send commands to the device.

Feasible?  Probably not.  But, it would be cool as hell if I can get the code together.
I foresee a LOT of head-pounding with this.  I have not coded with C++ in well over a decade.  It makes my head hurt!


The attached file includes many display statements, which can be deleted,
Also note that I turn on and off audio output controls and amplifiers.  These can be used or not used as required.
IT only works with type 0 midi files, there is a nice converter available called sweet midi converter (roni music) available that does an excellent job of converting to type 0.
In addition, I felt it was necessary to use a Due to get the speed needed to process fast notes,
This program sends the midi commands, voices etc.. via simple serial port (at Midi serial speed) to a Modern devices Fluxamasynth board.  it does a nice job.  It includes many programmatically accessible features.
If you have any questions, please send them on. 
Sorry for the delay, I have been busy finalizing  the entire code set for my carillon machine, which is what I am creating.


This is great -- I've been wanting to tackle a similar project as well. 
Would you  mind posting the .ino file for this? 



The attached .txt above is really all you need to add a midi file play in your own .ino program.  In fact you'll need to take the display stuff out of it (myGLCD calls).  You can also remove the power on and destination (PlayDest code bits),  as those are to control the external amplifiers.

Simply set up a second serial port on the DUE to connect to pin 4 of the fluxamasynth board (you only need the DUE TX pin to the Flux board pin 4 because the Flux does not talk back.  Set the baud rate on second serial port like this:

Serial1.begin(31250); // Fluxamasynth MIDI board Serial port

In my system I did not plug the Fluxamasynth shield directly into the DUE because I have a Cold tears display shield on the DUE, and that would interfere mechanically, so I run power and ground to the board separately, along with the Serial1 TX out to pin 4. 

You could rewrite the attached  to start the at the line
myfile = SD.open(fnbuf,FILE_READ);

Simply replace fnbuf with a valid type 0 Midi file name, (in my case the file is on an SD card, hence the SD.open command), so modify that line as required for your application.
The MidErr() calls simply print out various messages based on file reading errors that could occur.

The fluxamasynth board has a stereo audio output that you can run to any amplifier to listen to your midi file.
Refer to  the support pages of Modern Devices website for details on the Fluxamasynth  board.  I don't use their librarys, as my code simply parses the midi file, and sends the proper midi commands at the proper time out the Serial1 port.
I used the data sheet (also posted on that site) from the DREAM 2195 IC used on the Fluxamasynth board to help figure out how MIDI actually works.  You find some other sites helpful as well, such as  Somascape and Sonic spot, both of which have excellent info on decoding midi.

Let me know if you have any additional questions!!!


you guys should check out this library.  http://arduinocode.codeplex.com/releases/view/115256

I'm not using the current library, but it sends MIDI from and SD card out a serial port ( "plays" the file)

Go Up