Play sound in loop as long as condition is met

452 views
Skip to first unread message

ale....@gmail.com

unread,
Jun 24, 2021, 5:47:02 PM6/24/21
to Bonsai Users
Hi

I am a beginner with Bonsai and struggling with implementing an "if statement".

The task is the following (see scheme below).
I set an Integer. When this Int becomes <0, a sound should be played and repeated in loop as long as Int<0.
How can I start the sound playback in loop only when int becomes <0 and stop the playback when Int becomes >0?

Screenshot 2021-06-24 173829.png


The task exemplified here is intended to simulate a mouse moving in an arena, with a background sound being played when the mouse is in a given ROI, and no sound outside this ROI. When the mouse enters the ROI, the average brightness of the ROI drops below the baseline (baseline=0). So average<0 means that the mouse is in the ROI and the sound should be played. I got the mouse detection part to work, but I then do not manage to play the sound accordingly.

Thanks for any help!
Alessandro

brunocruz

unread,
Jun 25, 2021, 6:05:05 AM6/25/21
to Bonsai Users
Hey!

Let me just say I don't have much experience with audio resources, so there might be much better solutions. But maybe something like this would work ??

Capture.PNG
top branch: Load the resources and make sure you set the Sources' "Looping" to True. This will loop your audio file until you make it pause/stop, which saves you the trouble of detecting when the file has finished playing and restart it manually. I guess you could also just load a very long file :P

mid branch: Just a way to simulate the kind of input you seem to want to use. The timer is fed into a sin which generates periodic values between 1 and 0. Threshold these values (GreaterThan) and detect toggles (0->1 and 1->0) using distinctUntil Changed.

bottom branch: Keep listening to these toggles (myToggle): If rising edge (0->1) -> play the source.  If falling edge (1->0) PauseSource (or stopSource depending on whether you want to resume on the next toggle or play it from the beginning).

Hope it helps,
Bruno
sound.bonsai

ale....@gmail.com

unread,
Jun 25, 2021, 11:27:27 AM6/25/21
to Bonsai Users
Thank you very much Bruno! 
It does exactly what I need, and I now have a better idea on how to implement conditions resembling "if statements".

I would have another question. Is it possible to take the first value (only the very first) that comes out of the Sin node and use it to automatically set the threshold of GreaterThan?

Alessandro

Gonçalo Lopes

unread,
Jun 27, 2021, 12:32:08 PM6/27/21
to ale....@gmail.com, Bonsai Users
This should be possible by externalizing the Value property from GreaterThan and connecting to it the Sin followed by a Take(1) node in a separate branch.

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/e895a5df-669e-4cbe-a40b-dad3d963060bn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages