Can't be done in sclang
, because µs-accurate timing is important for this application and sclang runs at a much higher resolution.
In that Pd example, we are hard resetting a ramp oscillator when the pulse is sent to the TRIG pin and sampling its value when the ECHO pin goes high. We send the TRIG pulse at regular intervals. This allows sample-accurate measurements, so you have a resolution of about 22.7us (1/44100Hz). Using the equation time[us] / 58 = distance[cm]
, this gives a nominal resolution of 22.7/58 = 0.39cm, with minimal jitter. Not bad.
I don't know much about supercollider at all, but I think in scsynth there is (perhaps informally) the concept of "trigger", i.e.: some audio-rate signals that are either 0 or 1 and that some ugens accept such a signal to "trigger" some events. Using the DigitalOut
/ DigitalIn
/ DigitalIo
ugens at audio rate you could attempt a similar approach, if you have a ramp generator and a sample-and-hold that can handle a trigger input, then you could send into their trigger input the same value sent to the DigitalOut
that sends to the TRIG pin and the DigitalIn
that receives from the ECHO pin. If you only have a ramp generator that takes a trigger input for reset , but no sample and hold, then you could feed both signals summed together into the trigger input of the ramp generator and then send its output to an envelope detector.