JAP..Host syncing midi plug

Here are selected portions of my process method. Think of it as a midi echo.
Shouldn’t this keep time with the host? It doesn’t, it drifts.

It keeps time with itself though.

    CurrentPositionInfo pos;
    if (getCurrentPositionInfo (pos)){
        SampelsPerStep = ((60 / pos.bpm) * getSampleRate()) / timeSig; //0.5 * 44100 / 2
    }


    SampleCount += input.getNumSamples(); //count number of samples from the input buffer


        if(SampleCount >= SampelsPerStep){ //Play
            outMidiBuffer.addEvent(MidiMessage::noteOn(1, 64, (uint8)velocity), SampleCount - SampelsPerStep);
            SampleCount = 0;
        }

    inMidiBuffer.clear();
    inMidiBuffer = outMidiBuffer;

EDIT:
To clarify my question: Is sample counting not a valid method of timeing, or am I doing it wrong?

This is the same issue I ran into with delphi. Everyone’s all “you got to count the samples, that the only way to get it sample accurate” but then when I do it, its not in sync… and no one can tell me what wrong.

:frowning:

So I tried using timeInSeconds, but that also drifts!

All I want is to be able to send events at 4th,8th,16th,32nd notes with some degree of acuracy. Is that so wrong?

:expressionless:

Hmm. Rather buggy code there… For a start, “SampleCount = 0” is wrong: try “SampleCount -= SampelsPerStep”. You’re also calculating the event time wrong, and should be using a while loop instead of the if statement.

(Not to mention the spelling mistake in SampelsPerStep!)

4/10 :wink:

Well, I may be a noob, but at least I know the proper bracing style.8)

I interprete your answer to say that counting samples with input.getNumSamples(); is a valid method of timing and it is just my shoddy coding that makes it drift.

I thank you most humbly, for I am a humble dude.

…and thats not a spelling mistake. Sampels are like samples, but much better.

I use them all the time.

[quote=“Pukeweed”]Well, I may be a noob, but at least I know the proper bracing style.8)

I interprete your answer to say that counting samples with input.getNumSamples(); is a valid method of timing and it is just my shoddy coding that makes it drift.

I thank you most humbly, for I am a humble dude.[/quote]

Don’t start me ranting about braces!..

Yes, counting samples is fine, though you have to watch out for the time jumping around when they scrub or loop, etc.

oooh brace fight!!

the only reason to keep the opening brace on the same line as the function is if you need to save space to put it on an overhead projector!

stylewars last forever. i think mine is similar to jules’ except that i still can’t, try as i might, shake the habit of putting private before public in my class declarations. i really prefer it with the privates at the bottom tho (crumbs if that doesn’t sound filthy! :oops: ) so really ought to think about changing…

Well, I dont think there is any need to fight. I have trouble spelling samples right. You guys dont know where the braces go. We just have to accept eachother for what we are.
:smiley:

HEAR HEAR!

Indeed. Putting private and protected declarations above public is just silly. :evil: