;@ map d RNDbpm
;@ map d MID000
;@ map d MID000
;@ map d MID000
;@ map d CLKswi
;TODO Something wrong with SWING
;@ args kkkkk,k,kkkk
opcode ClkGen,0, kkkkkkkkkk
kBPM,kOn,kSource,kSync,kSwing, kRsti,k96o,k16o,kClkAo,kSynco xin

krst zkr kRsti
if kSource == 0 goto InterS ;internal
	kBPM = gkBPM
InterS:
kClkFr = kBPM * 4 * 6 / 60

kRst trigger krst,0.00001,0 
kOnT trigger kOn,0.5,0
kST trigger kSource,0.5,0

if (kRst == 1) || (kOnT == 1) || (kST == 1) then 
	reinit Run
endif
Run:
kClk metro kClkFr,0

kCount1 init 0
if kClk != 1 goto nextOut
	kCount1 += 1
if kCount1 != 12 goto next2   ; 1/8 = 12 * 1/96
	kCount1 = 0
next2:
k8 trigger kCount1, 0.5, 0

kCount2 init 0
kCount2 += 1
if kCount2 != (12 * 4 * kSync) goto next3
	kCount2 = 0
next3:
kSy trigger kCount2, 0.5, 0
nextOut:
k8s vdel_k k8, 60/kBPM/2*kSwing*0.01, 0.95, 0
k16 = k8 + k8s 
;outs a(kSy)*kOn*0.9,a(k16)*kOn*0.9
	zkw kOn, kClkAo
	zkw kClk * kOn, k96o
	zkw k16 * kOn, k16o
	zkw kSy * kOn, kSynco
k16 = 0 ; kill if need 1 on the whole period 'twin ticks
k8 = 0
kSy = 0
endop
