[SOLVED] Osc and fader in a live_loop


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

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…

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

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

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


Hi Nicolas

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

set :vol,0 

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

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

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.


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 !


1 Like

It works like a charm :-).