# Sonic Pi and TouchOSC

**URL:** <https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910>\
**Category:** Support, Help & Resources\
**Created:** [March 26, 2018, 1:47pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910 "2018-03-26T13:47:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtmoore](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mtmoore/32/490_2.png) [@mtmoore](https://in-thread.sonic-pi.net/u/mtmoore)\
**Post date:** [March 26, 2018, 1:47pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910/1 "2018-03-26T13:47:15Z")

</div>

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?

 ![56](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/1X/5c638e1a8aacc019b915b1ae1317b745abcb98d8.png)

Thanks for any help!  
Marcus

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [March 26, 2018, 2:33pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910/2 "2018-03-26T14:33:19Z")

</div>

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:

```ruby
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

```

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [March 26, 2018, 3:27pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910/3 "2018-03-26T15:27:50Z")

</div>

@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:

```ruby
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

```

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [March 26, 2018, 4:00pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910/4 "2018-03-26T16:00:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [March 26, 2018, 4:05pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910/5 "2018-03-26T16:05:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mtmoore](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mtmoore/32/490_2.png) [@mtmoore](https://in-thread.sonic-pi.net/u/mtmoore)\
**Post date:** [March 26, 2018, 4:10pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910/6 "2018-03-26T16:10:07Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![mtmoore](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/mtmoore/32/490_2.png) [@mtmoore](https://in-thread.sonic-pi.net/u/mtmoore)\
**Post date:** [March 26, 2018, 4:13pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-and-touchosc/910/7 "2018-03-26T16:13:49Z")

</div>

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