Sequencer plugin is jumping - what is wrong with my code?


#1

Hi,

i have a problem with the timing or I'm simply to stupid.
I want simply send for each step/16th a note on. This works, but not in same interval.

 

I have this code:

void GstepAudioProcessor::processBlock ( AudioSampleBuffer& buffer, 
                                         MidiBuffer& midiMessages ) noexcept
{
  static MidiMessage message = MidiMessage::noteOn ( 1, 60, uint8{127} );
  AudioPlayHead::CurrentPositionInfo pos;
  if ( getPlayHead() != 0 && getPlayHead()->getCurrentPosition ( pos ) )
    {
      const double samples_per_tick = 2.5 * getSampleRate() / pos.bpm;
      const double samples_per_step = samples_per_tick * 6;             

      // check if the current buffer include the next step
      for ( uint_fast32_t i = pos.timeInSamples; 
            i <= pos.timeInSamples + buffer.getNumSamples(); 
            ++i )
        {
          // if modula == 0 -> we have a step in this buffer
          if ( i % uint_fast32_t {samples_per_step} == 0 )
          {
            uint_fast32_t absolut_step = i / samples_per_step;
            uint_fast8_t relative_step = absolut_step % 16;

            // delay in samples for the massage from the current position
            int samples_delay = i - pos.timeInSamples;

            // output for debug
            if ( samples_delay <= 144 ) // jumps at 120 bpm if samples_delay 16, 
                                        // 32, 48... until 144
              {
                std::cout << pos.timeInSamples
                << " rs: " << int {relative_step}
                << " delay: " << samples_delay
                << " stick: " << samples_per_tick
                << " numS: " << buffer.getNumSamples()
                << std::endl;
              }

            midiMessages.addEvent ( message, samples_delay );
          }
     }
}

I tryed already some solutions from the net, but all do jump too.

What do i wrong?

Tommy

 

 

EDIT: i realize now that should be in the audioPlugin forum? Can anybody move this thread? Thx


#2

Now i have added this code to the audio plugin demo and use the midi message from the block ( post before ) to trigger a synth event. And the interval from the synth is correct - exactly every step.

Where goes the midi out message out of sync? And why? What can i do?


#3

what is the getPlayHead() function?

ah ok. got it