Combining the snip-its of code from above, but changing the MIDI slightly (now it’s nonsensical and non-musical):
i.e.
{//hacky test
MidiMessageSequence sequence;
sequence.addEvent( MidiMessage::noteOn( 1, 38, (juce::uint8)0x3f ), 0.0 );
sequence.addEvent( MidiMessage::noteOn( 1, 38, (juce::uint8)0x3f ), 0.0 );
sequence.addEvent( MidiMessage::noteOn( 2, 39, (juce::uint8)0x7f ), 96.0 );
sequence.addEvent( MidiMessage::noteOn( 2, 39, (juce::uint8)0x7f ), 96.0 );
sequence.addEvent( MidiMessage::noteOff( 3, 40 ), 124.0 );
sequence.addEvent( MidiMessage::noteOff( 3, 40 ), 124.0 );
FileChooser chooser( T("Select MIDI file to save"), File::getCurrentWorkingDirectory(), T("*.mid") );
if ( chooser.browseForFileToSave( true ) )
{
MidiFile file;
chooser.getResult().File::deleteFile();
FileOutputStream stream( chooser.getResult() );
//stream.setPosition(0);
file.setTicksPerQuarterNote( 96 );
file.addTrack( sequence );
file.writeTo( stream );
//stream.flush();
}
{
MidiFile file;
FileInputStream stream( chooser.getResult() );
file.readFrom( stream );
for ( int i = 0; i < file.getNumTracks(); i++ )
{
const MidiMessageSequence *sequence = file.getTrack( i );
for ( int j = 0; j < sequence->getNumEvents(); j++ )
{
MidiMessageSequence::MidiEventHolder *holder =
sequence->getEventPointer( j );
Logger::outputDebugPrintf( T("Message: chn:%d Note:%d Vel:%d T:%f"),
holder->message.getChannel(),
holder->message.getNoteNumber(),
holder->message.getVelocity(),
holder->message.getTimeStamp() );
}
}
}
}//Hacky test
Here’s the output I get: (same on both XP and OSX machines), which leaves me thinking there’s an OBOE somewhere!
[quote]Message: chn:1 Note:38 Vel:63 T:0.000000
Message: chn:1 Note:38 Vel:0 T:0.000000
Message: chn:1 Note:38 Vel:63 T:0.000000
Message: chn:2 Note:39 Vel:127 T:96.000000
Message: chn:2 Note:39 Vel:0 T:96.000000
Message: chn:2 Note:39 Vel:127 T:96.000000
Message: chn:3 Note:40 Vel:0 T:124.000000
Message: chn:0 Note:47 Vel:0 T:124.000000
Message: chn:3 Note:40 Vel:0 T:124.000000[/quote]
Those bolded items are interesting…
I haven’t had a chance to step through everything, but that’s where I’m off to next…
EDIT Thurs PM
A cruel, but valid test tonight… (only tested on Windows)
Creating an empty track, and adding that, saving and reloading it generates one “Note”
which is the endOfTrack Message. Which is OK.
but I don’t understand this line (in void MidiFile::readNextTrack)
[quote] // use a sort that puts all the note-offs before note-ons that have the same time
result.list.sort (*this);[/quote]
But that seems that the endOfTrack’s cropping up in odd places (IMHO) -
Should the comparator ignore comparisons (return 0) ie either message is an endOfTrack message, or something?
Actually, that might be wrong- I tried removing the sort, and I got this:
(after I added debugs to writeTrack/nextTrack)
[quote]MidiFile::writeTrack - Message: chn:1 Note:38 Vel:63 T:0.000000
MidiFile::writeTrack - Message: chn:1 Note:38 Vel:63 T:1.000000
MidiFile::writeTrack - Message: chn:2 Note:39 Vel:127 T:96.000000
MidiFile::writeTrack - Message: chn:2 Note:39 Vel:127 T:97.000000
MidiFile::writeTrack - Message: chn:3 Note:40 Vel:0 T:124.000000
MidiFile::writeTrack - Message: chn:3 Note:40 Vel:0 T:125.000000
MidiFile:readNextTrack - Message: chn:1 Note:38 Vel:63 T:0.000000
MidiFile:readNextTrack - Message: chn:1 Note:38 Vel:63 T:1.000000
MidiFile:readNextTrack - Message: chn:2 Note:39 Vel:127 T:96.000000
MidiFile:readNextTrack - Message: chn:2 Note:39 Vel:127 T:97.000000
MidiFile:readNextTrack - Message: chn:3 Note:40 Vel:0 T:124.000000
MidiFile:readNextTrack - Message: chn:3 Note:40 Vel:0 T:125.000000
MidiFile:readNextTrack - Message: chn:0 Note:47 Vel:0 T:125.000000
final Message: chn:1 Note:38 Vel:63 T:0.000000
final Message: chn:1 Note:38 Vel:0 T:1.000000
final Message: chn:1 Note:38 Vel:63 T:1.000000
final Message: chn:2 Note:39 Vel:127 T:96.000000
final Message: chn:2 Note:39 Vel:0 T:97.000000
final Message: chn:2 Note:39 Vel:127 T:97.000000
final Message: chn:3 Note:40 Vel:0 T:124.000000
final Message: chn:3 Note:40 Vel:0 T:125.000000
final Message: chn:0 Note:47 Vel:0 T:125.000000[/quote]
It looks like it’s happening after the MidiFile is loaded (even without the sort in place, I think)
Additional: Running the above on OSX somehow manages to pop-up 2 “File Open” windows?