Reputation: 129
I wrote a program in c++ that counts the number of notes in a .mid file, outputs the number of each note (A to G#), then outputs that to a file. It doesn't find enough notes, but I can't figure out why. I've built it based on the MIDI file documentation from midi.org.
While reading the file, all it does is look for the status byte for note on, 1001nnnn, and then reads the next byte as a note. I used Anvil Studio to make a MIDI file with only 1 note, and used the program to analyze it and it found that it only had 1 note which is correct, however when I use it on much larger files (2000+ notes), it won't find nearly all of them, and sometimes it will find that 90%+ of the notes are one or two pitches.
This is the segment of the program that searches for notes. The file is open in byte mode with ios::binary
//Loop through every byte of the file
for (int i = 0; i <= size; i++) {
//Read next byte of file to tempblock
myfile.read(tempblock, 1);
//Put int value of byte in b
int b = tempblock[0];
//x = first 4 binary digits of b, appended with 0000
unsigned int x = b & 0xF0;
//If note is next, set the next empty space of notearray to the notes value, and then set notenext to 0
if (notenext) {
myfile.read(tempblock, 1);
int c = tempblock[0];
i++;
//Add the note to notearray if the velocity data byte is not set to 0
if (c != 0) {
notearray[notecount] = b;
notenext = 0;
notecount++;
}
}
//If note is not next, and x is 144 (int of 10010000, status byte for MIDI "Note on" message), set note next to true
else if (x == 144) {
notenext = 1;
}
}
Does anyone know whats going on? Am I just missing a component of the file type, or could it be a problem with the files I'm using? I am primarily looking at classical piano pieces, downloaded from midi repositories
Upvotes: 2
Views: 158
Reputation: 180210
Channel message status bytes can be omitted when they are identical with the last one; this is called running status.
Furthermore, 1001nnnn
bytes can occur inside delta time values.
You have to correctly parse all messages to be able to detect notes.
Upvotes: 2
Reputation: 1133
The problem is very likely to be how your MIDI editor is creating the files. Many MIDI editors do not actually switch notes off - they just set their velocities to 0. This can make them a royal pain to parse.
Have a look at the raw MIDI messages contained in the file and you should see a lot of velocity messages.
Upvotes: 0