[SOLVED] Issue with result of MidiMessageSequence::extractMidiChannelMessages


#1

My compliments to the community. Maybe someone will be able to tell what am I wrong?

Why is the result of the function MidiMessageSequence::extractMidiChannelMessages extract the wrong& Not events for the specified MIDI channel, but any midi-events for any midi channel...

int midiTracksCount = MIDIFile.getNumTracks();

if (midiTracksCount == 1) //for MIDI format 0;
{

     MidiMessageSequence* TrackSequence = MIDIFile.getTrack(0);

     MidiMessageSequence mmSeq = MidiMessageSequence();

     int midiChan = 1;

     TrackSequence->extractMidiChannelMessages( midiChan, mmSeq, true);

     ExportToXmlFile(midiChan, &mmSeq); // TEST FUNCTION FOR RESULT PREVIEW

}


Result:

<?xml version="1.0" encoding="UTF-8"?>

<MIDI_EVENTS>
  <PROGRAMM_CHANGE ProgChangeNum="0" InstrName="Acoustic Grand Piano"/>
  <PROGRAMM_CHANGE ProgChangeNum="35" InstrName="Fretless Bass"/>
  <PROGRAMM_CHANGE ProgChangeNum="18" InstrName="Rock Organ"/>
  <PROGRAMM_CHANGE ProgChangeNum="80" InstrName="Lead 1 (square)"/>
  <PROGRAMM_CHANGE ProgChangeNum="60" InstrName="French Horn"/>
  <PROGRAMM_CHANGE ProgChangeNum="56" InstrName="Trumpet"/>
  <PROGRAMM_CHANGE ProgChangeNum="59" InstrName="Muted Trumpet"/>
  <PROGRAMM_CHANGE ProgChangeNum="60" InstrName="French Horn"/>
  <PROGRAMM_CHANGE ProgChangeNum="25" InstrName="Acoustic Guitar (steel)"/>
  <PROGRAMM_CHANGE ProgChangeNum="29" InstrName="Overdriven Guitar"/>
  <PROGRAMM_CHANGE ProgChangeNum="25" InstrName="Acoustic Guitar (steel)"/>
  <PROGRAMM_CHANGE ProgChangeNum="29" InstrName="Overdriven Guitar"/>
  <PROGRAMM_CHANGE ProgChangeNum="64" InstrName="Soprano Sax"/>
  <NOTE_ON noteOnKey="63" noteOnPos="384" noteOnVel="68" MidiChan="1"/>
  <NOTE_ON noteOnKey="60" noteOnPos="384" noteOnVel="65" MidiChan="1"/>
  <NOTE_ON noteOnKey="67" noteOnPos="384" noteOnVel="76" MidiChan="1"/>
  <NOTE_ON noteOnKey="36" noteOnPos="384" noteOnVel="116" MidiChan="2"/>
  <NOTE_ON noteOnKey="72" noteOnPos="384" noteOnVel="70" MidiChan="3"/>
  <NOTE_ON noteOnKey="60" noteOnPos="384" noteOnVel="65" MidiChan="9"/>
  <NOTE_ON noteOnKey="36" noteOnPos="384" noteOnVel="96" MidiChan="10"/>
  <NOTE_ON noteOnKey="69" noteOnPos="384" noteOnVel="47" MidChan="10"/>
  <NOTE_ON noteOnKey="42" noteOnPos="384" noteOnVel="86" MidiChan="10"/>
  <NOTE_ON noteOnKey="60" noteOnPos="384" noteOnVel="109" MidiChan="11"/>
  <NOTE_OFF noteOffKey="42" noteOffPos="389" noteOffVel="0" MidiChan="10"/>
  <NOTE_OFF noteOffKey="69" noteOffPos="389" noteOffVel="0" MidiChan="10"/>
  <NOTE_OFF noteOffKey="36" noteOffPos="389" noteOffVel="0" MidiChan="10"/>
  <NOTE_ON noteOnKey="69" noteOnPos="408" noteOnVel="27" MidiChan="10"/>
  <NOTE_OFF noteOffKey="67" noteOffPos="412" noteOffVel="0" MidiChan="1"/>
  <NOTE_OFF noteOffKey="63" noteOffPos="412" noteOffVel="0" MidiChan="1"/>
  <NOTE_OFF noteOffKey="69" noteOffPos="413" noteOffVel="0" MidiChan="10"/>
  <NOTE_OFF noteOffKey="60" noteOffPos="414" noteOffVel="0" MidiChan="1"/>
  <NOTE_ON noteOnKey="60" noteOnPos="430" noteOnVel="55" MidiChan="1"/>
  <NOTE_ON noteOnKey="63" noteOnPos="432" noteOnVel="56" MidiChan="9"/>
  <NOTE_ON noteOnKey="70" noteOnPos="432" noteOnVel="85" MidiChan="10"/>
  <NOTE_ON noteOnKey="62" noteOnPos="432" noteOnVel="75" MidiChan="11"/>
  <NOTE_OFF noteOffKey="60" noteOffPos="434" noteOffVel="0" MidiChan="11"/> etc...
 

 

 

 


#2

It seems that your ExportToXmlFile may have some kind of issue (maybe it's reading some uninitialized memory) as I cannot reproduce your problem. I've tried to reproduce your problem with the following program (most of it is the code you posted above):


int main (int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cerr << "Please specify at least one argument" << std::endl;
        return 1;
    }
    MidiFile midi;
    if (ScopedPointer<FileInputStream> input = File (argv[1]).createInputStream())
    {
        if (! midi.readFrom (*input))
        {
            std::cerr << "Error reading MIDI File" << std::endl;
            return 1;
        }
        int midiTracksCount = midi.getNumTracks();
        if (midiTracksCount == 1) //for MIDI format 0;
        {
            const MidiMessageSequence* TrackSequence = midi.getTrack(0);
            MidiMessageSequence mmSeq = MidiMessageSequence();
            int midiChan = 2;
            TrackSequence->extractMidiChannelMessages( midiChan, mmSeq, true);
            for (int i = 0; i < mmSeq.getNumEvents(); ++i)
            {
                MidiMessageSequence::MidiEventHolder* event = mmSeq.getEventPointer (i);
                if (event->message.isNoteOnOrOff())
                {
                    std::string eventType = event->message.isNoteOn() ? "On" : "Off";
                    std::cout << "Note " << eventType << " ["
                              << event->message.getChannel() << "]: " 
                              << MidiMessage::getMidiNoteName (event->message.getNoteNumber(), true, false, 3) << std::endl;
                }
            }
        }
    }
    else
    {
        std::cerr << "Unable to open specified MIDI file" << std::endl;
        return 1;
    }
    
    return 0;
}

You can find the complete source file here. I tested the code with this MIDI file.


#3

Thank you Fabian. The problem was really in my test function - extracted sequence overgrown with additional data from the outside, before at the time of the call.


#4

Very useful. Thank you.