# Question About 'control' Keyword

**URL:** <https://in-thread.sonic-pi.net/t/question-about-control-keyword/357>\
**Category:** \[P\] Patreon Support\
**Created:** [October 30, 2017, 3:48pm UTC](https://in-thread.sonic-pi.net/t/question-about-control-keyword/357 "2017-10-30T15:48:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcgreen24](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/bcgreen24/32/250_2.png) [@bcgreen24](https://in-thread.sonic-pi.net/u/bcgreen24)\
**Post date:** [October 30, 2017, 3:48pm UTC](https://in-thread.sonic-pi.net/t/question-about-control-keyword/357/1 "2017-10-30T15:48:57Z")

</div>

Hello, all! Quick question about ‘control’; when I run this code, the loop goes from ‘bright’ to ‘dull’ as expected:

```
with_fx :rlpf, cutoff: 120, cutoff_slide: 5 do |c|
  live_loop :loop do
    sample :loop_amen
    sleep sample_duration(:loop_amen)
    control c, cutoff: 40, cutoff_slide: 5
  end
end

```

However-- as the code is running, I would expect the sound to go from ‘dull’ to ‘bright’ if I change “cutoff: 40” to “cutoff: 120” in my ‘control’ statement and run the code again, but nothing happens when I do that. In other words, the cutoff value should transition from ‘40’ back to ‘120’…

Can someone help me understand how control works?

Thanks!  
Bryan

---

<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:** [October 30, 2017, 5:01pm UTC](https://in-thread.sonic-pi.net/t/question-about-control-keyword/357/2 "2017-10-30T17:01:21Z")

</div>

Hi Bryan  
Nice question. I’ve had a play and come up with the following code which I think works.

```ruby
#experimenting with changing cutoff using a live loop
#by Robin Newman

set :st,sample_duration(:loop_amen)

with_fx :rlpf, cutoff: 120 do |c|
  set :cv,c #comment after first run from stop, before re-run
  live_loop :cont do
    l=60;h=100
    st=get(:st)
    puts "change cuttoff down to",l
    control get(:cv), cutoff: l, cutoff_slide: 2*st
    sleep 4*st
    puts "change cuttoff up to ",h
    control get(:cv),cutoff: h, cutoff_slide: 2*st
    
    sleep 4*st
  end
  
  live_loop :loop do
    st=get(:st)
    sample :loop_amen
    sleep st
  end
end

```

The first time your code runs, the value of c in the live loop points correctly to the fx control and it reduces the cut off value. However you cannot change the value in the live loop and rerun, as this reruns everything, and the reference between the value of c in the live loop and with the fx call (and this has been rerun too) is not kept.  
In my version I store the value c using set :cv immediately after it is created, and get it back again using the reverse get(:cv) inside the controlling live\_loop :cont . However, it is important also to comment out the set :cv,c line AFTER the first run (where everything was previously stopped) but BEFORE you rerun the program to pick up the updated to a live\_loop you have changed. So If you run as shown, the cutoff will go up and down between the set values l (60) and h (100). If you then comment out the `set :cv,c` line, and alter either the l or h value and then rerun, it will pick up the new values on the next pass.

Of course if you don’t want it to oscillate, a simpler version is shown below. This just does a single change each time it is run, but as above remember to comment out the `set:cv,c` line AFTER the first run but BEFORE subsequent ones.

```ruby
#experimenting with changing cutoff using a single command line
#by Robin Newman

set :st,sample_duration(:loop_amen) #store duration of the sample

with_fx :rlpf, cutoff: 120 do |c|
  set :cv,c #comment after first run from stop, before re-run

  control get(:cv),cutoff: 40,cutoff_slide: 4*get(:st)
  
  live_loop :loop do
    st=get(:st)
    sample :loop_amen
    sleep st
  end
end

```

Also because I liked the changes to be sychronised to the live loop I changed the delays and time slide times to be multiples of the sample duration. Again I used the set and get functions to store and recover these as required.

---

<div class="post-metadata">

**Author:** ![bcgreen24](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/bcgreen24/32/250_2.png) [@bcgreen24](https://in-thread.sonic-pi.net/u/bcgreen24)\
**Post date:** [October 30, 2017, 5:07pm UTC](https://in-thread.sonic-pi.net/t/question-about-control-keyword/357/3 "2017-10-30T17:07:47Z")

</div>

Works like a charm-- thanks for this, I really appreciate it!

---

<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:** [October 30, 2017, 5:10pm UTC](https://in-thread.sonic-pi.net/t/question-about-control-keyword/357/4 "2017-10-30T17:10:48Z")

</div>

That’s great! One obvious comment I missed out in the asnwer is that of course you have to renable the set :cv,c line if you stop the program completely and re comment it after the first subsequent run the next time you use the program.

---

<div class="post-metadata">

**Author:** ![bcgreen24](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/bcgreen24/32/250_2.png) [@bcgreen24](https://in-thread.sonic-pi.net/u/bcgreen24)\
**Post date:** [October 30, 2017, 5:20pm UTC](https://in-thread.sonic-pi.net/t/question-about-control-keyword/357/5 "2017-10-30T17:20:43Z")

</div>

Yep, got it-- and I forgot to say thanks for explaining why the code works as it does (versus just giving a working example); it’s all starting to make more sense now. 🙂

Bryan
