Simple Analysis of MIDI filesIn this example we'll look at how to read in a MIDI file (created by jMusic or some other program) and perform a basic statistical analysis on it. We'll check the pitch and rhythm value range, the score length, and elements of the melodic contour. From this starting point you should be able to extend this example to check other aspects of the music you're interested in. Here is an example of the output from this program: ======================================================== Click here to view the source file. Let's have a closer look.
Lots of stuff is imported in this class. the jm
classes as usual, as well the vector and Enumeration classes for
unpacking the jMusic score structure so we can then analyse the notes
information, and the awt class is required because we do some simple GUI
moves to make the selecting of MIDI files easier.
The class is declared. Notice that is extends the
Frame class, this is used for the dialogue box from which MIDI files are
selected. We don't actually build a window that we see. We declare
variables for each of the attributes we want to collect data on.
The for loop goes forever - well until the Cancel
button on the file dialog causes a null file to be returned at which
point we break out of the loop. The last two lines of this segment place
a jMusic score into 's' by reading an converting the selected MIDI
file. The score's title is set to the the name of the file. This is
used later in printing the analysis to identify the file.
OK, there's lot's of work done in this code segment. basically we read every note in every phrase in every part of the score and update the analysis data after each. The Enumeration interface is used to help us with this task. It often looks complicated at first but is only so because of the number of layers we need to peel back from the score structure to get to the notes. The Enumeration gets the vector (a list) of the parts, phrases, notes (as the case may be) and loops through the list for each element while there are more elements. The use of Vectors and Enumeration helps us cope with the fact that there any be any number of notes, in any number of phrases, in any number of parts. A series of methods (that we'll get to later), are
called which update variables such as the highest and longest notes.
You should be able to add more of your own methods to check other
aspects of the notes which interest you.
After collecting the analysis information this
section of the code prints it to the screen. This is done after each
MIDI file is read. the data is accumulative, that is for example, the
highest note after three MIDI files is the highest note in any of the
three MIDI files.
This method does a simple check to see if the
current note's pitch is higher or lower than any previous pitch has
been, and if so updates the relevant variable.
This method similarly checks the longest and
shortest rhythmicValues.
This method checks the number of notes in a score
and updates the records of the longest and shortest values.
This method checks weather a pitch is higher or lower than the previous one, then updates the variables keeping stats on that info. (This is a little dodgy because it doesn't take into account part boundaries, but in large scores that is statistically negligible). More jMusic Analysis Information Check out Adam Kirby's analysis classes in the documentation for another readily availible source of jMusic analysis support. See also the PhraseAnalysis application. We encourage you to write you own analysis methods, then email them to us so we can post them for the whole jMusic community to use. To do histograms of a score: Use the following code segment after importing jm.util.View : View.histogram(myScore); |