Fft and control signals

hi everyone, I’m thinking of a project to be created in a supercollider that involves this: the use of an external signal (in my case I have a box with a pulled string that acts as an “instrument”) which, being taken up in input, through a fft summary can return amplitude and frequency values ​​to me. in turn these values ​​will be used to parameterize other characteristics and properties of some systems, but the initial step is to have a system that generates values ​​with input amplitudes and frequencies, which works.

Below I’ll post what I did. this code doesn’t work as it should, for example I tried to generate (as an example) a kick every time my input signal is higher than 0.1 or 0.3; once the kick has been generated, however, it continues to be generated and I don’t want this to happen. it must only happen when the threshold is reached, then it must vanish to reappear only when the condition is met. finally I was wondering if there was a way to modify the freqscope display in order to display the amplitude and frequency values ​​in more detail.



(
FreqScope.new;

SynthDef(\spectralTrigger, {
    var buffer, input, spectrum, filteredSpectrum, reconstructedSignal, triggerLevel;

    buffer = Buffer.alloc(s, 512);
    input = SoundIn.ar(0); // Audio input from the microphone
    
    // FFT on the input signal
    spectrum = FFT(buffer, input);
    
    // Filters the spectrum to obtain only frequencies with amplitude above the threshold
    filteredSpectrum = PV_MagAbove(spectrum, 0.2); // Threshold set to 0.2
    
    // Reconstructs the signal in the time domain
    reconstructedSignal = IFFT(filteredSpectrum);
    
    // Uses the maximum amplitude of the signal to control another Synth
    triggerLevel = Amplitude.kr(reconstructedSignal);

    // Condition to generate an impulse if the trigger level exceeds the new threshold
    Out.ar(0, reconstructedSignal);
    
    // Generates an impulse (kick) when the amplitude exceeds the updated threshold of 0.3
    SendTrig.kr(triggerLevel > 0.3, 0, triggerLevel);
    
}).add;

// Kick drum definition
SynthDef(\kick, {
    Out.ar(0, SinOsc.ar(40, 0, Decay.kr(Impulse.kr(1), 0.3, 0.8)));
}).add;
)

// Start the synth
x = Synth(\spectralTrigger);

(
// Set up an action to receive the trigger and activate the kick
OSCdef(\triggerKick, { |msg|
    // msg[3] contains the amplitude value (it can be used to control dynamics)
    var amplitude = msg[3];
    // Print in the PostWindow when the kick is triggered
    ("Kick triggered! Amplitude: " ++ amplitude).postln;
    
    // Trigger the kick
    Synth(\kick);
}, '/tr');
)