Control the stop of a live_loop with a condition

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