Control live_loops within variable switches?

Hi, my first idea was to build my file using switches to mute/unmute live_loops during performance.
it seems that i miss something here during expressing my conditions.

use_sample_bpm :loop_amen
use_debug false

########## LivePad Mixer :
#     PLAY-SWITCHES
switch =       1     
switchPhone =  1   
switchBeat =   0  
#     PARAM-VALUES
sTime =        1     
mainSync :dnb

### PAD 1 SETUP :
with_fx :rlpf, cutoff: 10, cutoff_slide: 4 do |c|
  live_loop :dnb do
    if switch == 1
      sample :bass_dnb_f, amp: 4
      sample :loop_amen, amp: 5
      sleep sTime
      sample :bass_dnb_f, amp: 5
      sample :loop_amen, amp: 4
      sleep sTime
      sample :bass_dnb_f, amp: 4
      sample :loop_amen, amp: 5
      sleep sTime
      sample :loop_amen, amp: 5, onset: pick
      sample :bass_dnb_f, amp: 4
      sleep sTime
      sample :bass_dnb_f, amp: 5
      sleep sTime
      play 44
      sample :loop_amen, amp: 5, onset: pick
      #sample :bass_dnb_f, amp: 4
      sleep sTime
      #sleep sTime
      control c, cutoff: rrand(100, 120), cutoff_slide: rrand(1, 3)
    else
      sleep 1
    end
  end
end


with_fx :ixi_techno do
  live_loop :effects do
    #sync mainSync
    if switchPhone == 1
      sample :mehackit_phone3, amp: 0.9
      sleep 1
    else
      sleep 1
    end
  end
end

###----------------------------------------------------------
define :simpleKickSample do
  sample :bd_tek, rate: 0.8, amp: 3
end

live_loop :DRUMS do
  #sync mainSync
  if switchBeat == 1
    simpleKickSample
    sleep 0.25
  else
    sleep 0.25
  end
end

As you can see, i was thinking a simple condition will allow me to control those loops, but it seems that something is wrong. Also i was looking for a way to ā€œMuteā€ straight a running loop, but i didnt find the coresponding docā€¦
Alsoā€¦ :wink: i was surprised to read the doc and only find an exemple of a simple condition (like a bool) and not something about ā€œelse ifā€, and double conditionsā€¦ is this so obvious that it is not present in the educational doc ?

Thank you
uriel

hi @uriel

hum, i guess you will have to kill to mute

Technically, those simple conditions will in fact work.
It may be helpful for you to describe in more detail what you are observing that is ā€˜wrongā€™.

There is no built-in volume control for an individual loop. This has been thought about a little bit, and may be introduced in a future update, but itā€™s not yet fully discussed.
In the meantime, there are several ways to ā€˜muteā€™ a loop:

  • one is to use a variable in much the same way that you have, that either causes the code to play nothing, or play sound at a volume of zero (either individual synths, or by wrapping the loop contents in a :level fx and causing that to respond to the volume variable).
  • Another is to use kill to stop individual synths or fx that are already playing, as nlb refers to above.
  • still another is to use the stop function to halt a loopā€™s execution altogether.

There may be other techniques that Iā€™m forgetting.

I canā€™t say for sure about the exact reason for this.
While the tutorial covers ā€˜elseā€™ in chapter 5.3 - ā€˜Conditionalsā€™:

Perhaps it was decided that complex multi-conditional statements (else-if/case/switch type statements etc) were not useful to cover in the documentation or include as ā€˜official Sonic Pi languageā€™ for the original target audience of Sonic Pi (young school children learning the very basics of computing through live-coding). There may be merit in adding such information to the tutorial, but itā€™s up to Sam really :slightly_smiling_face:

1 Like

Hi Ethan,
Thanks for all thoses detailsā€¦ this community is new to me, and i always prefer asking when i dont find what i need for the course of for myself. Anyway, conditional statements is generaly in the first part of any coding book, and i was surprised that nothing refers to it. In general, that one of the first block of code that students try to implement. but i would t be rude or anything, i know how documentation release is a pain.

by wrapping the loop contents in a :level fx and causing that to respond to the volume variable

yes, much better ! i dont really need to kiil any process at this stage, i am not running out of memory, so you are completely right, level change (mix) will be perfect. i gonna apply this right now. thank you.

Hi @uriel there is an interesting approach in this video with code available on their blog. They set up a mixer variables

# Our mixer!
master = (ramp *range(0, 1, 0.01))
kick_volume = 1
bass_volume = 1
revbass_volume = 1
snare_volume = 0.5
hats_volume = 0.5
open_hats_volume = 1
synth_volume = 1
pad_volume = 1
beep_volume = 0.5

and set different values during performance for mutes, dynamic changes etc.

There are quite a few implementations out there, all revealing slightly different approaches to solve the issue. Just have to spend the time going through them all and adapting them to your requirements.

Hope that helps.

1 Like

Hi Hussein, thanks a lot, looks what i needā€¦ in this manners, it is very easy to include a kind of ā€œcode blocksā€ that will serve the purpose of control UI for performanceā€¦ great !

1 Like

hi, i just discover a new undocumented featureā€¦ related to our topic here

# Switch: 1=ON, 0=OFF
testloop_switch = 1
# ...
live_loop :test do
  use_synth :beep
  attr = [on: testloop_switch,
          note: 50,
          amp: 0.5,
          pan: 0,
          attack: 0.1,
          decay: 0.2,
          sustain: 0.1,
          release: 0.5,
          env_curve: 1]
  play attr[0]
  sleep 2
end

the on/off switch ! butā€¦ wait a minuteā€¦ i didn t see anything in the params of the Beep synth with this optionā€¦ but it works for any of thoses synths ! so powerful! and very practical for live set !

amazing!

1 Like

Yes. Looks like a good reason to update the documentation :slightly_smiling_face:

2 Likes

hi Ethan, is the ā€œon:ā€ option is available for all synth ? even for sample ?
it looks that this a common option but you may know better than me :wink:

thx, have a nice day
uriel

1 Like

Yes, that is correct :slightly_smiling_face:

1 Like