toggle~ ?

Florent Ghys's icon

Hello,
In the signal domain, I need an object that would act like toggle: every time it receives a certain value in signal, it sends alternatively zero and one in signal.
I am pretty sure it is obvious, but I don't know how to do and can't find any leads...

I know I could use edge~ to bang a toggle and then use sig~ to go back in signal but I would rather stay in the signal domain all the way through.
Thanks

arokhsar's icon
Max Patch
Copy patch and select New From Clipboard in Max.

What are you trying to do? Don't forget that signals are essentially floating point numbers streaming through at the sampling rate, so when you say "sends alternatively zeros and ones:, you'll need to specify how frequently you want them to switch. The help file for >~ shows you how to make a signal that alternates between 0 and 1. You could also look at the train~ object which goes from 0 and 1 as well. Here's an example of what I think you are looking for:

LSka's icon

Maybe thresh~ is what you need

Florent Ghys's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Thanks guys for your help.
I have an incoming signal that is constantly at 0 except for very short peaks at 1 (coming from the metronome samm~ by Eric Lyon).
I want to send 1 in signal to a sfplay~ the first time there is a peak from the metronome, and send it a zero the second time, and so on.
I ended up doing this with edge~, toggle and sig~

but I wonder if leaving the signal domain for a short time with edge~ and coming back with sig~ keeps the sample accurate precision of the sfplay~

Maurizio Giri's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Try this:

Florent Ghys's icon

Oh thanks!!!

ak's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Or this:

Above toggle~ in GEN (the simplest way I can think about atm).
if [+=~] -> [%~] accumulates too much it would stop working (with 64bit floats it would take some time, but nevertheless...).

Florent Ghys's icon

Thanks a lot this is very helpful!

Riccardo Santalucia's icon
James Harkins's icon

The [gen] way looks nice and direct -- worth an abstraction.

As for overflow with the accumulator-mod approach: 32 bit float mantissa represents 24 bits (23 encoded, 1 implicit) = 16.7 million. One day = 86400 seconds so you could trigger it 194 times per second and not overflow for a whole day. Triggering it < 10 times per second gives you over 2 weeks... practically speaking, that should still be fine.

hjh

James Harkins's icon

OK, last one... taking AK's approach, if we define a trigger in the SuperCollider way (transition from nonpositive to positive) then a few objects can disappear... and it turns into a nice transparent abstraction.

Max Patch
Copy patch and select New From Clipboard in Max.

hjh

Florent Ghys's icon

thanks for reviving this old post!