# Saving MidiFile

**URL:** <https://forum.juce.com/t/saving-midifile/7239>\
**Category:** Audio Plugins\
**Created:** [August 2, 2011, 11:49am UTC](https://forum.juce.com/t/saving-midifile/7239 "2011-08-02T11:49:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Owen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/owen/32/305_2.png) [@Owen](https://forum.juce.com/u/Owen)\
**Post date:** [August 2, 2011, 11:49am UTC](https://forum.juce.com/t/saving-midifile/7239/1 "2011-08-02T11:49:46Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 2, 2011, 12:55pm UTC](https://forum.juce.com/t/saving-midifile/7239/2 "2011-08-02T12:55:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Owen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/owen/32/305_2.png) [@Owen](https://forum.juce.com/u/Owen)\
**Post date:** [August 3, 2011, 1:19am UTC](https://forum.juce.com/t/saving-midifile/7239/3 "2011-08-03T01:19:14Z")

</div>

Ah, of course 🙂 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).

---

<div class="post-metadata">

**Author:** ![Owen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/owen/32/305_2.png) [@Owen](https://forum.juce.com/u/Owen)\
**Post date:** [August 4, 2011, 2:45pm UTC](https://forum.juce.com/t/saving-midifile/7239/4 "2011-08-04T14:45:04Z")

</div>

\*\*\*\*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 ☹ 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]
