Sonic Pi and TouchOSC

So, I have started using TouchOSC on the iPad and Sonic Pi on my Mac, great combination. I’m quite new to actually using Sonic Pi so this may seem like a very newbie question.

However, how can I keep this drum loop continuously going. As you can see from the screen shot, after you have selected the button on TouchOSC the resulting value becomes empty… when you select it again [1.0], then [], then switch it off [0.0]. Consequently the loop doesn’t get played again until you turn off the button and re turn it back on again.

What am I doing wrong?

Thanks for any help!
Marcus

Hi Marcus

You can split polling the toggle switch and playing the samples into two separate live_loops, and use
a time state variable to pass information from one to the other like this:

set :onOff,0 #initialise onOff time state to 0
live_loop :fButtonA do  #live loop to record toggle switch state onOff
  use_real_time
  v = sync "/osc/toggleB_1"
  puts v[0]
  set :onOff,v[0]
end

live_loop :drums do #live loop to play drums when onOff = 1.0
  if get(:onOff)==1.0
    sample :drum_heavy_kick
    sleep 1
    sample :drum_snare_hard
    sleep 1
  end
  else
    sleep 0.01
  end

@robin.newman Correct me if I’m wrong, but your approach seems to introduce drift as it’s sleeping for 0.01 every time round the live loop if the :onOff state isn’t 1.0.

@mtmoore, I think that Robin is on the right tracks though. sync is actually a special kind of timing construct in that it waits for the next event, so you use it to run some code at the time of the next event. If you’re building a simple drum trigger system where taps in touchOSC directly cause sounds to happen, then sync is definitely your friend.

However, if you’re looking to have Sonic Pi drive all the timing and for touchOSC to affect its behaviour, then get is your friend. Although, it’s important to point out that you can get anything you can sync, so I’m pretty sure that the live loop fButtonA isn’t required in the example above (although it could just be that I’ve misunderstood things).

What is it that you’re trying to do? Should there be a rhythmic drum beat looping round that’s enabled or disabled by toggling a button in touchOSC? If so, the following should be fine:

live_loop :drums do
  v, _ = get "/osc/toggleB_1"
  sample :drum_heavy_kick, on: v
  sleep 1
  sample :drum_snare_hard, on: v
  sleep 1
end

I don’t think the 0.01 sleep matters too much, as the loop will not fire again until the toggle is switched on, and then the 0.01 sleep is not selected. At worst it delays the start of the drum loop by 0.01 when the toggle is switched on. I don’t see that it accumulates.
Having said that your solution is neater.

I’m not sure mine is a solution as I’m not entirely convinced I understand the original question :slight_smile:

@samaaron what I am trying to achieve, is when a toggled button is set to on, the drum loop will be played in a continuous loop. When the toggle button is set to off, it stops.

Am testing out both of your suggestions above. Thanks for the prompt help!

They both worked and gave me what I was looking for, big thanks :slight_smile:

1 Like