Thread got far behind the time - ERROR


#1

Hi everyone!

I just have a little question why my code is not working?
I just added a potentiometer via the midi canal to the sketch to set to sleep time.
But when the sleep time gets below zero I always get the same error.

CODE:

loop do
  var, var1  = sync "/midi/iac-treiber_bus_1/0/1/control_change"
  
  play 60
  sleep (var1/10)
  
  
end

PROTOCOL

/midi/iac-treiber_bus_1/0/1/control_change [21, 1]

ERROR:

Runtime Error: [buffer 0, line 5] - RuntimeError

Thread death!

Timing Exception: thread got too far behind time.

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/sound.rb:4840:in `ensure_good_timing!'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/sound.rb:4446:in `trigger_synth'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/sound.rb:4386:in `trigger_inst'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/sound.rb:1533:in `synth'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/sound.rb:1627:in `play'

workspace_zero:5:in `block (3 levels) in __spider_eval'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/core.rb:2055:in `block (2 levels) in loop'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/core.rb:2276:in `block_duration'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/core.rb:2313:in `block_slept?'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/core.rb:2054:in `block in loop'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/core.rb:2052:in `loop'

/Applications/Sonic Pi.app/app/server/ruby/lib/sonicpi/lang/core.rb:2052:in `loop'

workspace_zero:2:in `block (2 levels) in __spider_eval'

/Applications/Sonic Pi.app/app/

#2

Hmm your loop is quite unusual. It will only trigger when a CHANGE in the control value is detected. If you continuously vary your potentiometer it will keep on triggering and you will get some audio output. However, var1, which holds the control value is an integer. You divide it by 10 (another integer to get your sleep time. Thus when the control value var1 is in the range 0->10 this will give an integer result of 0, so your sleep time will be zero. This is likely to make the loop object, especially as the decay time of the playing note is 1 beat. If you get enough triggers it will give the error above.

Some things you can try.
First you will get a less sluggish response from the loop by including the line use_real_time.
Secondly I suggest you use sleep(var1/10.0) or sleep(var1.to_f/10) both of which will force floating point division giving sleep times like 0.01, 0.8,11.5 etc

You could set a lower limit to make sure there is always some sleep time in the loop using sleep (0.05 + var1/10.0) which would help.
The other thing that will affect this is probably the computer you are running on. A Raspberry Pi is more likely to give the error than running on say a Mac.

Hope this helps.