Real-time BPM updating miracle?

Putting this here in case I stumbled upon something helpful for future searchers:

I got TouchOSC communicating with Sonic Pi, which I am running in OSX.

I made a slider to adjust my BPM.

I Frankensteined this code together:

theBPM = 44
use_real_time
b = sync "/osc:myipaddress:9000/1/fader1"
theBPM = b[0]

live_loop :getBPM do
  use_real_time
  b = sync "/osc:myipaddress:9000/1/fader1"
  theBPM = b[0]
end

live_loop :gonow do
use_bpm theBPM
  play 50
  sleep 1
end

And for reasons I absolutely cannot understand, the BPM of that playing tone increases and decreases LIVE without me needing to press command R.

As far as I knew, any changes made to the code had to be run with command-R before being heard and executed.

Is what I just discovered common knowledge or a hack?

If it’s common knowledge, why is this able to update in real time without a new run?
Can any other things be updated live like this?

1 Like

This is where the magic lies: Your loop getBPM is constantly listening to any sync signal coming from the given address. The second loop is updating the value of theBPM every beat.

1 Like

This is expected behaviour. I have used it many times.
In essence your getBPM loop reads the value of your fader each time it changes and stores the results in theBPM. This variable is then read and used in the :gonow live_loop in the statement use_bpm theBPM

I would suggest using set and get commands to store the BPM value. It is the preferred way to share data between different loops. Basically it sets a value in the time state and the get reads the last stored time state.

set :theBPM,60

live_loop :getBPM do
  use_real_time
  b = sync  "/osc*/1/fader1"
  set :theBPM, b[0]
end

live_loop :gonow do
  use_bpm get(:theBPM)
  play 50
  sleep 1
end

one other point. Most sliders will give output in range 0->1 so I think you might need something like
set :theBPM, (b[0] * 200 + 60)
for example to give a bpm range of 60 -> 260

The same technique can be used to adjust volume, or pitch of a note for example.

1 Like

Thanks a lot for the replies! I only discovered Sonic Pi two days ago, so I am still very much in a euphoric throwing-code-pasta-at-the-wall frenzy to see what this does.

Can this same technique be used to update things like note played or chord played? I can figure out the code if I know it’s possible so I don’t waste anyone’s time.

With regard to limiting the BPM range, I did that in the TouchOSC editor, which works great. BUT my Synthstrom Deluge has a BPM range of 0.001 to 1000 BPM. Which is absurd, but lets me do some pretty absurd stuff. I can make songs where the beat drops once every 12 hours.

I want to open a 24-hour club where the kick only happens twice each day.

Thanks again for the replies. I have devoured a LOT of posts from both of you in here. So thank you for those as well.

Hi Robin,
I’ve been hunting for “real time” changes, and this is a great answer…
One thing I’ve been trying to do, but still not been successful, is to change a parameter on a (long) sample “real time”… Where I’m looking to change something like an LPF “over” the duration of a sample… is that possible?
Currently, any change to a level / parameter happens when the sample loops again, so I am looking to have the changes smoothly sweep over the duration of the sample playing (if that makes sense?)
yours
Phil.

Hi @philAwesomeTech,

By default, all opts are static for the duration of a sample/synth trigger. However, most of them can be changed during the execution of the synth using control.

Take a look at section 7 of the tutorial for more information about this: Sonic Pi - Tutorial (sonic-pi.net)](Sonic Pi - Tutorial)

1 Like

Cheers Sam!
That’s fantastic! Much appreciated. I’d started to chop up samples & tweak the params like that…
Phil

Hi Phil
I hope Sam’s answer is OK for you. I saw this yesterday, but my Wife is in hospital and I was visiting and unable to respond then.

Hi Robin, apologies for my own (far later) reply! I’ve been busy with work at the start of the year (Graphic Design) - so I’ve not dipped in to in-thread in a while!
Hope your wife made a full recovery & indeed, hope you are well.
I’m amazed my family have avoided Covid so far, but, my wife & I are self employed so rarely go out anyway!
Take care,
Thanks for the reply.
yours
Phil.

Hi Phil
I’m sad to say she passed away at Easter, so things have been a bit tough since then. But life goes on.

Oh my goodness, I’m so sorry to hear that. Sending you all our love…
do take care.
yours,
Phil.