Thread got far behind the time - ERROR

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.


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


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


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

Thread death!

Timing Exception: thread got too far behind time.

/Applications/Sonic `ensure_good_timing!'

/Applications/Sonic `trigger_synth'

/Applications/Sonic `trigger_inst'

/Applications/Sonic `synth'

/Applications/Sonic `play'

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

/Applications/Sonic `block (2 levels) in loop'

/Applications/Sonic `block_duration'

/Applications/Sonic `block_slept?'

/Applications/Sonic `block in loop'

/Applications/Sonic `loop'

/Applications/Sonic `loop'

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


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.

1 Like