;@ map DLY_5m DLY25m DLY100 DLY500 DLY_1s DLY_2s DLY2p7 DLYCLK

;@ ins a a a a a
;@ outs a a a a a
opcode DelayQuad, 0, kkkkkkkkkkkkkkkkkkkk
  kTime1,kMod1,kTime2,kMod2,kTime3,kMod3,kTime4,kMod4,kClk,kRange,kin,kmod1,kmod2,kmod3,kmod4,kout,kout1,kout2,kout3,kout4 xin
  ain zar kin
  amod1 zar kmod1
  amod2 zar kmod2
  amod3 zar kmod3
  amod4 zar kmod4

  kRangeN[] fillarray 0.0053, 0.0251, 0.101, 0.5, 1, 2, 2.7

if kClk != 1 goto Time
    kTime1 table kTime1, giDLYCLK
    kTime2 table kTime2, giDLYCLK
    kTime3 table kTime3, giDLYCLK
    kTime4 table kTime4, giDLYCLK
    kTime1 /= gkBPM
    kTime2 /= gkBPM
    kTime3 /= gkBPM
    kTime4 /= gkBPM
    kgoto Next
Time:
  if kRange == 0 then
    kTime1 table kTime1, giDLY_5m
    kTime2 table kTime2, giDLY_5m
    kTime3 table kTime3, giDLY_5m
    kTime4 table kTime4, giDLY_5m
  elseif kRange == 1 then
    kTime1 table kTime1, giDLY25m
    kTime2 table kTime2, giDLY25m
    kTime3 table kTime3, giDLY25m
    kTime4 table kTime4, giDLY25m
  elseif kRange == 2 then
    kTime1 table kTime1, giDLY100
    kTime2 table kTime2, giDLY100
    kTime3 table kTime3, giDLY100
    kTime4 table kTime4, giDLY100
  elseif kRange == 3 then
    kTime1 table kTime1, giDLY500
    kTime2 table kTime2, giDLY500
    kTime3 table kTime3, giDLY500
    kTime4 table kTime4, giDLY500
  elseif kRange == 4 then
    kTime1 table kTime1, giDLY_1s
    kTime2 table kTime2, giDLY_1s
    kTime3 table kTime3, giDLY_1s
    kTime4 table kTime4, giDLY_1s
  elseif kRange == 5 then
    kTime1 table kTime1, giDLY_2s
    kTime2 table kTime2, giDLY_2s
    kTime3 table kTime3, giDLY_2s
    kTime4 table kTime4, giDLY_2s
  elseif kRange == 6 then
    kTime1 table kTime1, giDLY2p7
    kTime2 table kTime2, giDLY2p7
    kTime3 table kTime3, giDLY2p7
    kTime4 table kTime4, giDLY2p7
Next:
  kR = kRangeN[kRange]
  aTime1 = a(kTime1) + kR * amod1 * a(kMod1)
  aTime2 = a(kTime2) + kR * amod2 * a(kMod2)
  aTime3 = a(kTime3) + kR * amod3 * a(kMod3)
  aTime4 = a(kTime4) + kR * amod4 * a(kMod4)
  aTime1 limit aTime1, 0.001, kR
  aTime2 limit aTime2, 0.001, kR
  aTime3 limit aTime3, 0.001, kR
  aTime4 limit aTime4, 0.001, kR

  abuf delayr 2.7 ; max delay time
  aout1 deltapi aTime1
  aout2 deltapi aTime2
  aout3 deltapi aTime3
  aout4 deltapi aTime4
  aout  deltapi a(kR)
  delayw ain

  zaw aout1, kout1
  zaw aout2, kout2
  zaw aout3, kout3
  zaw aout4, kout4
  zaw aout,  kout
endop
