# \[SOLVED\] Osc and fader in a live\_loop

**URL:** <https://in-thread.sonic-pi.net/t/solved-osc-and-fader-in-a-live-loop/848>\
**Category:** \[P\] Patreon Support\
**Created:** [March 9, 2018, 9:36pm UTC](https://in-thread.sonic-pi.net/t/solved-osc-and-fader-in-a-live-loop/848 "2018-03-09T21:36:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [March 9, 2018, 9:36pm UTC](https://in-thread.sonic-pi.net/t/solved-osc-and-fader-in-a-live-loop/848/1 "2018-03-09T21:36:55Z")

</div>

Hello,

I keep trying to control sonic pi via open stage control and it is a bit tough 🙂

Just try to use a fader to handle the volume of a live loop. The code below works as expect the live\_loop is launched each time as change the fader…

```ruby
live_loop :test_01 do
  use_real_time
  x = sync "/osc/fader_1"
  sample :loop_amen, sustain:8, amp: x[0]
  sleep sample_duration :loop_amen
end

live_loop :test_02 do
  use_real_time
  x = sync "/osc/fader_1"
  s= play :c3, release:8, amp: 1
  control s, amp: x[0]
  sleep 2
end

```

Is there a tip to achieve this goal ?  
thanks for your help !

Nicolas

---

<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 9, 2018, 10:44pm UTC](https://in-thread.sonic-pi.net/t/solved-osc-and-fader-in-a-live-loop/848/2 "2018-03-09T22:44:48Z")

</div>

Hi Nicolas

I think this may do what you want. It works nicely on my computer.

```ruby
set :vol,0 

live_loop :playsound do
  s = sample :loop_amen,amp: get(:vol)
  set :s,s
  sleep sample_duration :loop_amen
end

live_loop :getvol do
  b= sync "/osc/fader_1"
  puts b[0]
  set :vol,b[0]
  control get(:s),amp: b[0]
end

```

Basically there are two live loops. The first one plays loop\_amen and sets the volume to the value stored in the time state variable `:vol`, which is initially set to 0 before the two live loops start. A reference to the playing :loop\_amen is stored in the variable s. This is then stored in teh time state as :s  
The loop duration is set by the sleep sample\_duration :loop\_amen statement, so that it repeats seamlessly.

The second loop waits for changes in the fader\_1 setting, which triggers the sync statement when an osc message is received.

The value is in b[0] which is printed on the screen, and stored in he :vol time state, so that next time the sample is triggered it starts at the current volume setting.  
The reference stored in :s is retrived with the get(:s) statement and used to control the amp: setting of the loop\_amen which is set to the current state of b[0]. Thus as the fader is varied, the volume of the loop\_amen will vary as well.  
Note when you start the program initially you will hear nothing until the slider position is altered.

The trick is to keep the reading of the slider in a separate loop to the one playing the loop\_amen, and to use time state variables to allow data to be transferred between them.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [March 10, 2018, 6:56am UTC](https://in-thread.sonic-pi.net/t/solved-osc-and-fader-in-a-live-loop/848/3 "2018-03-10T06:56:49Z")

</div>

Hi Robin,

Thanks (again and again) for your answer and for this usefull and clever solution. It opens the fied of possibilites. I missed the concept of state time…  
I think this kind of trick will be useful for others users ; this is the strength of this forum and people as you sharing their acknowledge !

Nicolas

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [March 10, 2018, 8:40pm UTC](https://in-thread.sonic-pi.net/t/solved-osc-and-fader-in-a-live-loop/848/4 "2018-03-10T20:40:14Z")

</div>

It works like a charm :-).
