# Toggle fx on/off (via mix:) while live coding?

**URL:** <https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676>\
**Category:** Support, Help & Resources\
**Created:** [March 2, 2024, 7:06pm UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676 "2024-03-02T19:06:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [March 2, 2024, 7:06pm UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676/1 "2024-03-02T19:06:24Z")

</div>

Hi all  
still having endless hours of fun here - we are commonly advised to use with\_fx blocks _outside_ any loops to avoid duplicating fx resources. I have found that I can’t use a ‘global’ variable to toggle fx on/off, unless I stop then restart my loops. I’d like to do this in real time, while loops are running.

```ruby
onoff = 0

with_fx :slicer, phase: 0.125, mix: onoff do
  with_fx :reverb, room: 1, damp: 0.8, mix: onoff do
    live_loop :cor do
      sample :ambi_choir, rpitch: -3.3, pan: -0.7
      sample :ambi_choir, rpitch: -3.7, pan: 0.7
      sleep 8
    end
  end
end

```

Don’t forget to like and subscribe 😃

---

<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 2, 2024, 10:53pm UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676/2 "2024-03-02T22:53:40Z")

</div>

This is a known problem, for which there is a bodge solution as follows:  
First, the reason it occurse is that each time you press RUN a new fx wrapper is allocated, but the live loop still “sees” the initial one, so when you change the mix value that is not reflected and it doesn’t appear to change.  
The solution is to use the code below, where the reference to the fx wrapper is stored in the |r| or |v"| for the two wrappers shown. Once you have pressed RUN and the code has started, then comment out the line `set :r,r;set :v,v`  
Then use the two control lines to change the mix: or the amp: settings and this will work picking up the value set each time you press RUN. If you STOP all the code running, then uncomment the same line until you have pressed RUN again, and then comment it out again. A bit kludgy, but it works.  
The alternative is to embed the fx within the live loop and put up with the overhead that will generate. Of course if you never want to change the fx settings then it doesn’t matter and teh external ones will work.

```ruby
with_fx :level,amp: 1 do |v|#allow volume to be controlled. Set intial value here
  with_fx :reverb,room: 0.8,mix: 0.8 do |r|#allow the mix to be controlled, set intial value here
    set :r,r;set :v,v #comment immediately after first run.
    #uncomment line above when stop pressed, then recomment after next run started
    live_loop :test do
      control get(:r),mix: 0.8
      control get(:v),amp: 1
      play scale(:c4,:minor_pentatonic,num_octaves: 2).choose,release: 0.2
      sleep 0.2
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [March 3, 2024, 9:47am UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676/3 "2024-03-03T09:47:27Z")

</div>

Hi

thank you for this Robin. If I read (and test) this correctly, it appears to work _while running_ only once - in order to reinitialise I have to press stop. I was hoping to have live control of fx levels without using stop. I do understand precisely WHY this occurs (“the live loop only sees the initial fx wrapper”).

There is Sonic Pi expertise here beyond mine that has grappled with this but I’ll keep experimenting and breaking stuff 😃

Having watched a video recently of Sam (in Australia, at Software Art Thou) using define and control to vary parameters of a running loop, in my n00bness I thought there might be a soution to this using something like:

`define :bla do  
s = fx in here  
end

live\_loop :smeg do  
control s  
sleep 0.125  
end`

Many thanks  
Brendan

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [March 3, 2024, 9:56am UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676/4 "2024-03-03T09:56:58Z")

</div>

Yeh, you can’t assign ‘with\_ fx do/end’ to a variable. 🤦‍♂️

---

<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 3, 2024, 1:15pm UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676/5 "2024-03-03T13:15:54Z")

</div>

Hmm. If you run then comment out the line specified, you can alter the mix and vol settings inside the live loop (not the values in the fx lines) and they will be used when you press run again. You only need to reinitialide by in commenting the line if you press STOP.  
In other words you can change things as much as you like, pressing run to take up the new values without stopping the Live Loops running.

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [March 3, 2024, 2:46pm UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676/6 "2024-03-03T14:46:32Z")

</div>

Ah ok, thanks - I was prob testing it incorrectly; thanks Robin

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [March 9, 2024, 9:51am UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676/7 "2024-03-09T09:51:18Z")

</div>

@robin.newman

I’m teaching myself how to use define in SPi, and this works for me, i.e. alter fx parameters without the need to press stop. Only question is, does it create a new fx wrapper each time I call the :notes function??:

```ruby
define :notes do
  |pitch, del, mix|
  with_fx :ping_pong, phase: del, mix: mix do
    use_synth :bass_foundation
    play pitch
  end
end

live_loop :sendToFunc do
  notes [72, 75, 77, 79, 82].pick, 0.625, 0.5 # << change mix level to > 0
  sleep 0.25
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 9, 2024, 1:57pm UTC](https://in-thread.sonic-pi.net/t/toggle-fx-on-off-via-mix-while-live-coding/8676/8 "2024-03-09T13:57:08Z")

</div>

Yes I think it will define a new wrapper each call.
