Zero-Crossing Instrument

Hi Mark. Can you not write a UDO with setksmps 1 that will check each sample, and output a 0/1 each time a sample values changes from plus to minus? Or am i over simplifying this?

I’m sure it’s been attempted before, but not by me I’m afraid!

[some time later…]

opcode ZeroCross, k,a
    setksmps 1
    aSig xin
    kSwitch init 0
    kSampleValue = aSig

    if kSampleValue > 0 then
        kSwitch = 1
    else
        kSwitch = 0
    endif

    xout kSwitch
endop

instr 1
    a1, a2 diskin2 "DutchLadyTalking.aif", 1, 0, 1

    kSwitch ZeroCross a1

    if changed(kSwitch) == 1 then
        //it just crossed zero....
    endif
endin