Saving MidiFile

I’m trying to write a VST that windows incoming MIDI CC data every beat, and then stores it as a MIDI file that I can look up later. I’m just trying to write the CC data to a MidiMessageSequence, and then print out the values… but the values seemed all jumbled when I print it back out? When I monitor the CC data coming in it seems all good, so I’m assuming the problem is when I pack or unpack the MidiMessageSequence. Any thoughts or suggestions?

[code]void MidivsttestAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{

MidiBuffer::Iterator inBufferIterator(midiMessages);
MidiMessage midi_message(0xf4);
int sample_number;
while(inBufferIterator.getNextEvent(midi_message,sample_number))
{
    if(midi_message.isControllerOfType(14)){
        //std::cout << midi_message.getControllerValue()  << std::endl;
        midiMessageSequence->addEvent(midi_message); //Adds CC_event to the midiMessageSequence
    }
}

AudioPlayHead::CurrentPositionInfo currentPosition; //make a new playhead position object
getPlayHead()->getCurrentPosition(currentPosition); //I think this will store the current playhead info in the position object
if(!currentPosition.isPlaying)
{
    if(test == false) {
        test = true;
        for(int i; i < midiMessageSequence->getNumEvents(); i++) 
            
        {
            std::cout << midiMessageSequence->getEventPointer(i)->message.getControllerValue() << std::endl;                
        }
        
        midiMessageSequence->clear();
    } 
} else {
    test = false;
}

}[/code]

Well, how are you creating appropriate timestamps for the incoming messages?

1 Like

Ah, of course :slight_smile: I keep forgetting the timestamp is context dependent. So I’m assuming I should convert from sample position in the buffer, to Ticks. Are there any convenience methods or tips for converting that? I’m assuming I would just use my own Tick resolution (960, or something like that).

****Edit: I realized I was creating my MidiMessageSequence in the audio callback. Doh!! That’s what I get for still coding at 4am.

I found the awesome “AudioPlayHead::CurrentPositionInfo” and got my VST to write the .mid file to disk, but when I load it back into the sequencer the data looks messed up. I’m guessing this is because I’m trying to write the file in the audioCallback :frowning: I just want to test this right now, is there any recommendations on where I could call the write function from?

[code]void MidivsttestAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
AudioPlayHead::CurrentPositionInfo pos;
getPlayHead()->getCurrentPosition(pos);

MidiMessageSequence midiMessageSequence;

MidiBuffer::Iterator inBufferIterator(midiMessages);
MidiMessage midi_message(0xf4);

int sample_number;
while(inBufferIterator.getNextEvent(midi_message,sample_number)) {
    if(midi_message.isControllerOfType(14)) {
        midi_message.setTimeStamp(pos.ppqPosition);
        midiMessageSequence.addEvent(midi_message); //Adds CC_event to the midiMessageSequence
    }
}


if(!pos.isPlaying) {
    if(test == false) {
        for(int i = 0; i < midiMessageSequence.getNumEvents(); i++) {

            std::cout << midiMessageSequence.getEventPointer(i)->message.getControllerValue() << std::endl;

        }
        
        MidiFile file;
        
        file.setTicksPerQuarterNote( 960 );
        
        File myFile("~/Desktop/test.mid");                
        
        FileOutputStream stream( myFile );
        
        file.addTrack( midiMessageSequence );
        
        file.writeTo( stream );
        
        test = true;
    } 
} else {
    test = false;
}

}[/code]