Control the stop of a live_loop with a condition

Hi everybody,

I’m working on a apckey 25 project and try to start or stop a live_loop if a toggle is on.
So i try with this piece of code to try to understand what i’m doing :slight_smile:

live_loop :metronome do
  sleep 1
end

live_loop :foo do
  sync :metronome
  stop if get[:toggleOn]
  sample :bass_dnb_f
  #  sleep 1
end


live_loop :toggle_inspector do
  sync :metronome
  tick :boo
  if look(:boo)==3
    set :toggleOn, true
    tick_set :boo, 0
  else
    set :toggleOn, false    
  end
end

So why the live_loop doesn’t restart after stopping. You see it’s a big mess in my head today…
Sure there’s an answer.

Hi @nlb,

well, the problem is, that once you’ve stopped the live_loop you’d have to (re)evaluate the code to start it again. One way how to circumvent this is, to let the live_loop run but stop only the sample (or synth) code from being run:

live_loop :foo, sync: :metronome do # sync just once: http://in-thread.sonic-pi.net/t/live-loops-sync-questions/1172/16
  sample :bass_dnb_f if get(:toggleOn) == false
  sleep 1
end

(not tested)

thanks @Martin.
yes i forgot that point : we can’t stop a live_loop and relaunch it. so let’s the live_loop living and play the
sample if the toggle is on. It’s a way.
But finally i find an old code source that solves my problem, i will need to adapt to my purpose. Of course it comes from @robin.newman :slight_smile:

live_loop :p1 do
  
  toggle_1_state = sync "/midi:apc_key_25_midi_1:4:1/note_on"
  set :t1, toggle_1_state[0] # 1 if pushed 0 when released
  doLoopAmen if toggle_1_state[0]==1
end


define :doLoopAmen do # function to deal with loop_amen sample
  set :kill1,false
  live_loop :controlLoopAmen do
    s1 = sample :loop_amen #s1 stores reference to the running sample
    
    in_thread do #in a thread poll for button up cue (:C1=>0)
      loop do
        if get(:t1)==0 #if button up cue then
          kill s1 #kill the sample pointed to by the s1 reference
          set :kill1,true #set kill1 flag true
          stop # quit loop
        end
        sleep 0.15 #time between checks for button up cue
      end
    end
    40.times do # split sleep time to insert poll for kill1
      sleep (sample_duration :loop_amen) / 40.0
      stop if get(:kill1)
    end
  end #of live loop
end #of function
1 Like