Cabbage Logo
Back to Cabbage Site

MIDI jitter compensation

HI, i decided to participate to community and share my research.
well known midi jittering on ksmps more then 16 is not acceptable for tight midi operations,
it happening when midi signal comes during cycle, and synth starting to react only on next cycle.
but same time big synthesizer consume allot CPU on small ksmps numbers.
My synth works well on ksmps 64, on 16 it gets bit too heavy specially for polyphonic sounds.
i found that midi jittering is periodical and correlated to BPM, and come up with compensation algorithm.

if ksmps 64 then in form caption(synth) … latency(128) ; double ksmps number

instr 1 ;always run
giSaw     ftgen     13,  0, 4096, -7, -1, 0, -1, 4096, 1, 0, 1   ; saw waveform as i found that fluctuations goes same way.

kcotrig trigger chnget:k("IS_PLAYING"),0.5,0 ;
if kcotrig == 1  then
reinit COMPS1
endif

kbpm = chnget:k("HOST_BPM")
kamp = 1/(2*kbpm)

COMPS1:
kcompens oscil kamp, kbpm,13,0   ; producing anti fluctuation  
rireturn
kcompas = 1/(2*bpm)-kcompens+0.000001 ;calculating delay 
chnset kcompas,"compas"
  endin

instr 2 ; midi playing instrument
xtratim ,chnget:i("compas") ; compensating sustain time , not sure xtratim work this way but seems ok. 
aoutfin delay aout ,chnget:i("compas") ; compensating audio by delay it with anti jitter signal

that is it. i have synth with ksmps 64 and it plays midi super tight.

Thanks for sharing this. I know a few people have struggled with this issue in the past too. I think @Retornz might be interested in this too.

1 Like

Hi, i have optimized anti jitter code . it works better this way.

kbpm = chnget:k("HOST_BPM")

kpla chnget “IS_PLAYING”
kcotrig trigger kpla,0.5,0

kamp = 1/(kbpm2)
karez loopseg kbpm
2,kcotrig,0, kamp,1,0

gkcompas = karez+0.0001

2 Likes

Definely something I am interested in !
Thanks for tagging me !
I will try it when I’ll get a chance to be on my computer :slight_smile:

hi) this algorithm sync well till 1/32 notes, also it depend from result latency of channel. if there is plugins after synth with big latency time, it can goes out of sync. this algo is not perfect but it works. will try to make it better)

1 Like

I just tried your code (the 1st post + this modification), and I can’t seem to make it works, I’m probably doing something wrong… could you share a small working example please ?

try this

kbpm = chnget:k("HOST_BPM")

kpla chnget “IS_PLAYING”
kcotrig trigger kpla,0.5,0

kamp = 1/(kbpm2)
karez loopseg kbpm
2,kcotrig,0, kamp,1,0

gkcompas = karez+0.0001

and then in your instrument you delaying output with gkompas

aoutfin delay aout ,gkompas

sorry here is right one

donno why but this forum dont like this char “*”

kbpm2 should be kbpm*2