What I want to accomplish in a plugin is as follows:
- All Midi Input Events should be stored in a data structure (MidiMessageSequence?)
- While saving each event, I may make some modification when the message is ‘Note On’ or I work on the entire sequence of midi events as a whole.
- The data structure should persist till I close the plugin.
What I am doing currently
- I keep a public MidiMessageSequence variable tempSequence.
- In the processBlock I add the event to the sequence using
tempSequence.addEvent(message.getMessage())
withfor (auto messages : midibuffer)'.
This ‘midibuffer’ is the buffer from processBlock. - Then I have a button in the GUI which when clicked gives an option to save a file to which the MidiMessageSequence is stored. I use the approach shown here.
- The exact code I am using is as follows :
std::cout << "save button clicked" << std::endl;
chooser = std::make_unique<juce::FileChooser> ("Select location to save file...",
juce::File{},
"*.mid");
auto chooserFlags = juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::saveMode ;
chooser->launchAsync (chooserFlags, [this] (const juce::FileChooser& fc)
{
auto file = fc.getResult();
juce::MidiFile midiFile;
midiFile.addTrack(audioProcessor.tempSequence);
juce::FileOutputStream myStream (fc.getResult());
midiFile.writeTo (myStream);
});
The main questions regarding above are :
- Will tempSequence keep the added MidiMessage Events?
- If yes, is the approach of saving the file correct?
Update
When I add the following line just before saving tempSequence, it does show more than 0 number of events. This means the format of midi getting saved is wrong.
std::cout << audioProcessor.tempSequence.getNumEvents() << std::endl;
Q : Is the way I am adding events to the MidiMessageSequence object wrong?