CSV to MIDI Converter

This file contains a set of java code that will convert a formatted CSV (Comma Separated Values) file into a MIDI file. To run the program, you need to have Java SE Development Kit installed. Just run "CSV2MIDI.bat". You will need the java/bin folder in your path, or you can just change the .bat file to suit your needs. This will convert "ExampleMIDI.csv" into OutFile.midi, which should sounds like this.

The input CSV file is formatted with a header and a data section. Look at "ExampleMIDI.csv" for an example. The header contains the timing resolution in units of pulses per quarter note, and a list of instruments to use. The number of instruments determines the number of MIDI channels to use, one channel per instrument. In the entire file, all fields that are not integers are ignored, so you can put comments in the file. In the data section, however, row and column number of integers are important.

Instruments are specified by an instrument number. This number can range from 0 to 127. The available numbers to choose from and their corresponding instrument name are listed in "midi instruments.csv". These instrument numbers were found with "listInstruments.java". For some reason Java has instruments numbered from 0 to 410, but only the ones up to 127 can be used with this program. If someone knows how I can change that, enlighten me.


Header

The header is formatted as follows: The first integer is the timing resolution. The next integer determines the first instrument number. All following integers on the same line as the first instrument determine the other instruments to use. There can be a maximum of 16 instruments. The end of this line determines the end of the header section.

For example, look at "ExampleMIDI.csv". The header looks like this:

Timing Resolution (pulses per quarter note)
4

Instrument,105,Banjo,Instrument,114,Steel Drum,Instrument,71,Clarinet

Here, the first integer is 4, this is the timing resolution. The next integer is 105, this is the first instrument number (this is the number for a banjo). The rest of the integers on this same line determine the other instrument numbers. So MIDI channel 0 will be instrument 105 (banjo), channel 1 will be instrument 114 (steel drum) and channel 2 will be instrument 71 (clarinet).


Data

Next, notes are listed in the data section. Each note is specified by a tick number, a note number and a velocity.

The tick number determines where in the song the note will be placed. For example, if the timing resolution is 4 pulses per quarter note, a tick number of 20 will put the note 5 quarter notes after the start of the song. Tick numbers start at 0.

The note number is the note to play. This number can range from 0 to 127. Where 0 is C-1 and 127 is G9.

The velocity number is like the volume of the note. The higher the velocity the higher the volume. This number can range from 0 to 127. Some instruments will keep the note on until the velocity of that same note is set to 0.

In the data section, all fields not containing an integer are ignored. However, the column and row numbers of each field are important. Each note of an instrument is specified by 3 consecutive integers in the same row. If the three numbers are in columns 1, 2 and 3 (the first three columns) then the note is applied to channel 0. If they are in columns 4, 5 and 6 then the note is applied to channel 1. Columns 7, 8 and 9 are for channel 2. Etc. The three numbers are in order of tick number, note number, velocity number. There can be an unlimited number of rows but the maximum number of channels is 16.

For example, look at "ExampleMIDI.csv". The beginning of the data section looks like this:

Tick, Note (0-127), Velocity (0-127), Tick, Note (0-127), Velocity (0-127), Tick,Note (0-127), Velocity (0-127)
0,60,90,5,60,90,10,80,100
1,65,90,5,64,90,11,82,100
2,69,90,5,67,90,,,
3,72,90,5,72,90,,,
4,73,90,15,53,90,,,
5,72,90,15,65,90,,,
6,69,90,15,68,90,,,
7,65,90,15,73,90,,,
8,60,90,25,55,90,,,
9,55,90,25,64,90,,,
10,51,90,25,67,90,,,
11,48,90,25,72,90,,,
12,48,90,35,65,90,51,82,0
...

Here, the entire first row is ignored because none of the fields contain a single integer. The rest of the rows contain integers and are read in as note data. The first three numbers are 0, 60, 90, so a note with note number 60 is applied at tick 0 with velocity 90 on channel 0. The next three numbers are 5, 60, 90, so a note with note number 60 is applied at tick 5 with velocity 90 on channel 1. Etc. The first three numbers in the next row are 1, 65, 90, so a note with note number 65 is applied at tick 1 with velocity 90 on channel 0. Etc. On the next row there is no data specified for channel 2, so those fields are ignored. It's not until the last row shown here that another note is specified for channel 2. This is allowed; any number of rows or columns can be blank. Also, tick numbers can go in any order and the highest tick number for all notes determines the length of the .midi file.

Enjoy. Please contact me if you have any questions.

Comments are closed.