> MIDI > Musical Data > Reading a MIDI file | ||||||||
Reading a MIDI fileWe are going to learn how to read that MIDI file do a little manipulation
to it and then write it to a new MIDI file. This program reads in the scale.mid file, transposes the scale up a fifth and then writes a new MIDI file called transposed.mid, lets have a closer look at how it works. Note: At the time of writing this tutorial the Read.midi() method will only handle format 1 MIDI files with monophonic parts! We're going to skip the initial stuff and get straight to the working part of the code this time . . . on with the show!
The JMC is requred to use music-like language such as CROTCHET and C4,
and STACCATO.
This is where we read in our scale.mid file. Looks pretty much the same as writing huh : ) Just remember that you need to create a new Score to read into whenever you read from a MIDI file.
Once we have read our MIDI file into our new score we need to access the many Parts, Phrases and Notes that may be contained within. We know that our scale.mid file is simple and only has one part and one phrase so looking for the right one is simple (as there is only one to get we know that it must be the first), just grab the first object in the list (jmusic data index's are Vectors and, like arrays, start from 0). First we get our Part from the score, then we get the phrase from the part, and finally we get Notes from the phrase.
This is were we do the transposition. The basic idea is this. Cycle through the phrase, getting each note from in turn. As we get each note, grab its pitch and add 7 semitones to it. Then set the note's pitch value to reflect the change.
Well now isn't that easy. You can read and write MIDI files before you
can even program in Java :) |
||||||||