Trouble resetting the Timer UGen

Hi everyone!

So I am building a Synth which reads back and forth through a buffer according to a mic input (see below).
I’ve managed to code it like this now, where input scrubs through the buffer, forward or backward, and if there is a silence of minimum1 second, then after that the input goes the other way(thanks to the Toggle Ugen).

The problem is that if the output of the Time Ugen is above 1 (and thus the toggle is made), it stays at its last value, meaning that if after this moment, if there is another silence of more then 1 second, the toggle will not be made because it doesnt cross the threshold from below 1.

I thus have to reset the timer to 0 after it crossed 1. But I just can’t manage to figure it out.

I’ve tried a lot with booleans, for example making another resetTime variable wit ha boolean in it measuring if the silenceTimer is smaller than 1, and then multiplying that outcome (a 0 or 1) by the SilenceTimer so it should resets:

resetTime = silenceTimer < 1; 

silenceTimer = silenceTimer * resetTime;

But it doesn’t work.

Anyone an idea how to achieve this?

(
SynthDef(\grainBufmic, {
    var sig, bufdur, pos, mic, amp, phasor, phaseRate, trigrate, hasInput, silenceTimer, resetTimer, direction;

    mic = SoundIn.ar(0);
	amp = Amplitude.kr(mic, \atk.kr(1.5), \rel.kr(1.5)).clip(0, 1).poll(10, "amp"); // Get amplitude & clamp values
	trigrate = Amplitude.kr(mic, 10, 10).clip(0,1).linlin(0, 0.01, 0, 100);

	phaseRate = \phaseRate.kr(0.000137);

    // Detect when input is present
	hasInput = (amp > 0.015).poll(10, "hasinput");  

    // Timer that counts silence duration
	silenceTimer = Timer.kr(hasInput).poll(10, "TrigTime"); 

    // Toggle direction after 1 second of silence
	direction = ToggleFF.kr(silenceTimer > 1).poll(10, "Toggle") * 2 - 1;


    // Phasor moves forward or backward based on direction
	phasor = Phasor.kr(0, (phaseRate + amp.range(0, 0.025) * \scrubDepth.kr(0.001)) * direction, 0, 1);

    pos = phasor.clip(0, 1);  // Keep position in range

    sig = GrainBuf.ar(
        numChannels: 2,
        trigger: Impulse.kr(trigrate),
        dur: \grainDur.kr(1/20),
        sndbuf: ~buforiginal,
        rate: \rate.kr(1),
        pos: pos,
        pan: \pan.kr(0)
    );

    Out.ar(0, sig);
}).add;
)

Synth(\grainBufmic);

I figured it out already! Adding a Select.kr to the silenceTimer definition line did the trick. For those who are curious, this is the working code:

(
SynthDef(\grainBufmic, {
    var sig, bufdur, pos, mic, amp, phasor, phaseRate, trigrate, hasInput, silenceTimer, resetTimer, direction;

    mic = SoundIn.ar(0);
	amp = Amplitude.kr(mic, \atk.kr(1.5), \rel.kr(1.5)).clip(0, 1).poll(10, "amp"); // Get amplitude & clamp values
	trigrate = Amplitude.kr(mic, 10, 10).clip(0,1).linlin(0, 0.01, 0, 100);

	phaseRate = \phaseRate.kr(0.000137);

    // Detect when input is present
	hasInput = (amp > 0.015).poll(10, "hasinput");  

    // Timer that counts silence duration
	silenceTimer = Select.kr(hasInput, [0, Timer.kr(hasInput).poll(10, "TrigTime")]); 

    // Toggle direction after 1 second of silence
	direction = ToggleFF.kr(silenceTimer > 0.1).poll(10, "Toggle") * 2 - 1;


    // Phasor moves forward or backward based on direction
	phasor = Phasor.kr(0, (phaseRate + amp.range(0, 0.025) * \scrubDepth.kr(0.001)) * direction, 0, 1);

    pos = phasor.clip(0, 1);  // Keep position in range
	
	
    sig = GrainBuf.ar(
        numChannels: 2,
        trigger: Impulse.kr(trigrate),
        dur: \grainDur.kr(1/20),
        sndbuf: ~buforiginal,
        rate: \rate.kr(1),
        pos: pos,
        pan: \pan.kr(0)
    );

    Out.ar(0, sig);
}).add;
)