ZeroTimeLoopError despite sleep()

My aim isn’t to play chords, but to execute simultaneous calls to the same routine.

define :myMIDIplayer do |note|
  midi_note_on note
  sleep 1.0

define :myMIDIChordplayer do |noteArray|
  in_thread do
    noteArray.each {|note|
      myMIDIplayer note

live_loop :playMIDI do
  myMIDIplayer [60]

live_loop :playMIDIChord do
  myMIDIChordplayer [60, 61]

Both loops sleep, but playMIDIChord throws a ZeroTimeLoopError error. Why does Sonic Pi think it’s not sleeping and throwing the error? Is there a way to do this without throwing an error?

didn’t run the code but my guess is that because myMIDIChordplayer is in it’s own thread your live_loop playMIDIChord does not have a sleep.

When you call something in_thread it does not tell the parent thread to sleep. I use a lot for when I want triplets when I do drum loops and noticed that. Which makes sense to me.

1 Like

@din is correct :slight_smile:
A sleep in a child thread is isolated from a parent thread - so you would need to rewrite your code in some way to make the parent thread sleep too.

1 Like