# Help understanding Midi time

**URL:** <https://forum.juce.com/t/help-understanding-midi-time/27997>\
**Category:** Audio Plugins\
**Created:** [June 5, 2018, 4:49am UTC](https://forum.juce.com/t/help-understanding-midi-time/27997 "2018-06-05T04:49:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 4:49am UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/1 "2018-06-05T04:49:39Z")

</div>

I’m trying to make sense of the timeStamp value in MidiMessages and the sampleNumber in MidiBuffer::addEvent().

What do these represent? The time since the track started? The time after the event is queued that it should be triggered? The time since the buffer was last filled? Or something else?

In the plugin I’m writing, the user triggers events in the UI, and I really want them to be sent out to other synths as soon as possible after that.

Does this mean I should timeStamp the MidiMessage as 0? Can I also put sampleNumber as 0?

If not, what should I be putting in these slots?

cheers

Phil

---

<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:** [June 5, 2018, 7:09am UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/2 "2018-06-05T07:09:24Z")

</div>

```
//==============================================================================
/** Returns the timestamp associated with this message.

    The exact meaning of this time and its units will vary, as messages are used in
    a variety of different contexts.

    If you're getting the message from a midi file, this could be a time in seconds, or
    a number of ticks - see MidiFile::convertTimestampTicksToSeconds().

    If the message is being used in a MidiBuffer, it might indicate the number of
    audio samples from the start of the buffer.

    If the message was created by a MidiInput, see MidiInputCallback::handleIncomingMidiMessage()
    for details of the way that it initialises this value.

    @see setTimeStamp, addToTimeStamp
*/
double getTimeStamp() const noexcept { return timeStamp; }

```

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 12:20pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/3 "2018-06-05T12:20:47Z")

</div>

That leaves me roughly as confused as I was previously.

What is this timeStamp FOR? I mean how is the value of the timeStamp treated when I put an event in the MidiBuffer in processBlock?

---

<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:** [June 5, 2018, 12:26pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/4 "2018-06-05T12:26:04Z")

</div>

It’s for holding any double value you want to put there. The meaning of that double is up to you, or the code that created the MidiMessage.

But MidiBuffer is completely different - it doesn’t hold MidiMessage objects, it’s a packed format with an integer sample offset for each of its messages. It’s analogous to an AudioBuffer.

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 12:28pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/5 "2018-06-05T12:28:03Z")

</div>

OK. So, as to the other part of my question. What about the sampleNumber?

What I really want to know is, when trying to send out Midi messages from my plugin, what should I put in both timeStamp and sampleNumber to ensure that the midi message gets sent out as quickly as possible.

---

<div class="post-metadata">

**Author:** ![JussiNeuralDSP](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@JussiNeuralDSP](https://forum.juce.com/u/JussiNeuralDSP)\
**Post date:** [June 5, 2018, 12:28pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/6 "2018-06-05T12:28:44Z")

</div>

With MIDI files it’s usually dependent on PPQ value defined in the MIDI file header, ie. beatPosition = tick / PPQ.

> **[Pulses per quarter note](https://en.wikipedia.org/wiki/Pulses_per_quarter_note)**
>
> PPQN (Pulses Per Quarter Note, also known as PPQ, Pulses Per Quarter and TPQN, Ticks Per Quarter Note) is the smallest unit of time used for sequencing note and automation events, both by step sequencers and in the MIDI standard. Most MIDI sequencers allow the number of PPQ to be varied for more or less temporal resolution depending on the needs of the performer. If there are too few PPQ any performance recorded into the MIDI sequencer may sound artificial (being quantised by the Pulse rate) or l...

In processBlock position means sample offset from the start of the current audio buffer.

---

<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:** [June 5, 2018, 12:31pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/7 "2018-06-05T12:31:52Z")

</div>

> [@interstar](#):
>
> what should I put in both timeStamp and sampleNumber to ensure that the midi message gets sent out as quickly as possible.

sample number: 0!

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 12:33pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/8 "2018-06-05T12:33:19Z")

</div>

But what does it mean to say “offset from start of sample buffer”? Does it mean “wait this many sample points before sending the message out”?

Because I’d like all the messages to be sent immediately. Which would suggest putting a 0 there. But elsewhere the documentation seems to tell me that sampleNumber defines the ordering and if I try to put two events at the same samplePoint the second will overwrite the first.

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 12:34pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/9 "2018-06-05T12:34:12Z")

</div>

And if I put two events at sampleNumber 0, one won’t overwrite the other?

---

<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:** [June 5, 2018, 12:40pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/10 "2018-06-05T12:40:14Z")

</div>

no

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 12:42pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/11 "2018-06-05T12:42:05Z")

</div>

OK. Thanks.

So basically there shouldn’t be any problem having both the timeStamp in the MidiMessage I create and the samplePoint be zero when I use midiBuffer::addEvent(myMsg, 0);

That just sends the messages out ASAP?

---

<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:** [June 5, 2018, 12:42pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/12 "2018-06-05T12:42:32Z")

</div>

yep

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 12:42pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/13 "2018-06-05T12:42:50Z")

</div>

cheers.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 5, 2018, 1:09pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/14 "2018-06-05T13:09:11Z")

</div>

Be aware though, that you discretise your midi events by doing so in bufferSize fragments. So it will not sound very musical.  
If the midi is captured by the host, it already has a number here, that you should preserve, if not, you should calculate the proper positions of the beat.  
The latency of one buffer, that cannot be avoided, is less noticeable than the misplacements of the individual notes to the grid.

One processBlock call is basically: “I’ll tell you what is happening in the next bufferSize samples, and when it happens…”

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 2:14pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/15 "2018-06-05T14:14:14Z")

</div>

When you say “not very musical” you mean not quantized to a grid?

That’s OK, I’m not trying to sync. anything to any other pattern (at least not yet). I may add that later. But for now it’s OK if when the event happens, the midi signal gets sent.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 5, 2018, 2:18pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/16 "2018-06-05T14:18:39Z")

</div>

> [@interstar](#):
>
> When you say “not very musical” you mean not quantized to a grid?

No, I mean quantized to a **random** grid, that is different to anything sensible. Events can only occur every bufferSize samples, if you ignore the sample position inside the block.

It will **not** occur when the event gets sent.

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 2:23pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/17 "2018-06-05T14:23:27Z")

</div>

Well, as I understand it, the earliest that I can send the Midi message out is at the beginning of the next processBlock.

If an event happens mid-processBlock I can’t magically get it into the currently playing one. I _have to_ wait for the next processBlock to start.

But once I’m in the next processBlock. The sooner I send the message out, the less lag there is.

Or am I misunderstanding something?

---

<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:** [June 5, 2018, 2:30pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/18 "2018-06-05T14:30:46Z")

</div>

> [@interstar](#):
>
> The sooner I send the message out, the less lag there is.

Sure, but the buffer is 200ms long, you’d be sending out sudden bursts of messages every 200ms, which is going to sound pretty crappy depending on what these messages are.

If your messages are generated then you should be able to work out their timing correctly. If they’re coming in from a live source you can juce::MidiMessageCollector to help to reschedule them evenly.

---

<div class="post-metadata">

**Author:** ![interstar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/interstar/32/4337_2.png) [@interstar](https://forum.juce.com/u/interstar)\
**Post date:** [June 5, 2018, 2:49pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/19 "2018-06-05T14:49:05Z")

</div>

Is the buffer not the same as the buffer you set the size of in the DAW then? I though it was something you could choose the length (and therefore frequency) of.

You’re saying you basically get 5 processBlocks per second?

---

<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:** [June 5, 2018, 2:53pm UTC](https://forum.juce.com/t/help-understanding-midi-time/27997/20 "2018-06-05T14:53:31Z")

</div>

> [@interstar](#):
>
> You’re saying you basically get 5 processBlocks per second?

I’m saying you get N blocks per second, where N is unknown.

[Next page](https://forum.juce.com/t/help-understanding-midi-time/27997.md?page=2)
