MidiKeyboardState timing question


#1

Hi there,

I have a question about the MidiKeyboardState class. I currently have code that adds MidiMessages to this object (e.g. when OSC messages are received). When I process these to add them to the main MidiBuffer in the processAudio block (as in the code below), they all have time 0, i.e. they only appear at the start of the buffer.

processBlock(AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
...
        myKeyboardState.processNextMidiBuffer(midiMessages, 0, numSamples, true);

        MidiBuffer::Iterator i( midiMessages );
        MidiMessage message (0xf4, 0.0);
        int time;

        // extract any messages from it
        while(i.getNextEvent(message, time))
            DBG(time << " " << ( (message.isNoteOn()) ? "on" : "off"));    // time always is 0

Is there any way to place & retrieve sample accurate MidiMessages using MidiKeyboardState (rather than rounding to the nearest block) or do I need a more elaborate solution for this level of accuracy? And if not what do I need to look at / read? Similar posts on the forums here have left me more confused that when I started…

Any help very much appreciated!


#2

You can manually set the timestamp of your messages with juce::MidiMessage::setTimeStamp(double) to whatever you need it to be… (such as time: juce::Time::getMillisecondCounterHiRes())


#3

Great thanks, I think my problem was that I assumed that MidiKeyboardState / MidiMessageCollector objects timestamped the messages internally whereas actually they need some sort of timestamp passed with the message you pass them to be able to calculate the spacing. Anyway, working great now thanks!