Keeps crashing: "Timing Exception: thread got too far behind time"

Hi,
First day with Sonic pi. Loving it.

I am running into an issue with large sample sets though:
I have a big sound library (100k+ wavs) and am using the following code to randomly play some files.
It keeps crashing with “thread death” : looper" Timing exceptions: thread got too far behind time.
It first works fine and then this happens after around 30 samples played or so.

What I am trying to do is play all files from my sound library but filter to specific key words, in this case having “a” in the name.

Any ideas what is happening? I am on a very fast new mac pro.

Here is the code:

allinstruments ="/Volumes/HD_Libraries+sfx/instruments/**"

define :my_samp2 do
sample allinstruments, “a”, rrand_i(1, 10000000), attack: 0.3, release:2.75
sleep 0.3
end

in_thread(name: :looper) do
loop do
my_samp2
use_real_time
end
end

Thank you very much for any pointers on what to try!
D.

Looking at your code, you are starting samples 0.3 seconds apart. As some (many?) of them will last longer than this you are rapidly using up resources in Sonic PI and this is what will be causing your problems. for samples the attack sustain release times work differently to those of notes, see section 3.4 of the Tutorial on Enveloped Samples.
What you need to do is to decide on a suitable attack and release time that you would like but set sustain: to 0. Each sample will then last a maximum of the attack + release times. This should be the sleep time that you use.
Otherwise Sonic PI will keep on adding resources to play the next sample (in your case every 0.3 seconds) and will become swamped as they are not finishing soon enough.

Here is an example which uses all the built in samples and plays then with attack 0.2 and release 0.3, and sleep time of 0.5 You can alter these within the constraints of what I have explained.

I’ve also added a commented section which shows how to play the total length of each sample, using the sample duration times.
OOPS! edited the code below as I had deleted a comment #

#play samples

g = sample_groups
puts "groups are #{g}"
g.length.times do |x|
  puts "current group #{g[x]}"
  n= sample_names g[x]
  puts"samples in this group are #{n}"
  n.length.times do |s|
    puts n[s]
    #note sustain set to 0 or timings will vary
    sample n[s],attack: 0.2,sustain: 0,release: 0.3
    sleep 0.5 #sleep time equals total of attack and release
  end
end

comment do
  #this plays the total duration of each sample
  g = sample_groups
  g.length.times do |x|
    n= sample_names g[x]
    n.length.times do |s|
      puts n[s]
      #note sustain set to 0 or timings will vary
      sample n[s]
      sleep sample_duration(n[s]) #sleep for duration of sample
    end
  end
end #comment
1 Like

Hello everybody, I’ve been having a similar problem for a while. The code that I wrote in January and works perfectly normally is now getting this error. What’s even more ridiculous is that I’ve been getting this error very often lately. So I shared this code on github in January and took a screen record and shared it on my Instagram account.

The links are here:
https://github.com/quatronostro/curiosa-enterence-sonic-pi-codes/blob/main/song.rb

https://www.instagram.com/reel/CZAPS4do1G9/?utm_source=ig_web_button_share_sheet

Now when I run this code again I get this error

Im using version 3.1.1 and I tried to uninstall sonic pi and install again but it doesnt work. I will be grateful if you could help me. As I said before, sometimes when I run the codes that I wrote, even if it worked fine that day, I started getting this error the next day. Any ideas?

1 Like

@Berke - I think you mean you’ve been using v3.3.1.

I take it you didn’t do anything like install a different version of Sonic Pi in between your script appearing to work and later seeing this issue?

If so, then it may be down to one or a combination of several things - perhaps an Operating System setting was changed by an update for example, (Sonic Pi doesn’t always like it when the computer has aggressive power saving settings enabled).

Otherwise though, I’m not sure exactly how it might have been working fine before. In general, there are several things to be aware of when using FX - which basically starts with: some FX (reverbs, echoes, etc) are more resource intensive than others. To prevent or minimise the issue you’re observing here, there are several things you can do. If you haven’t already read about it, details are in Chapter 6.2 of the Tutorial.

Let us know if any of the strategies mentioned in there are able to help you - or if you have any further questions :slightly_smiling_face:

1 Like

First of all, thank you very much for these informations and help and sorry for my mistake, it has to be v3.3.1.

Good news is today I tried again uninstall and reinstall Sonic Pi and now codes are working. I hope I never encounter such a problem again, it’s really frustrating. :upside_down_face:

Hi Berke,

I quite like the sound (I think) you are aiming for so I took a little time to look at your code.

A lot of the with_fx commands eat up resources in SP, so you have to be quite careful
where you put them, as they create huge memory hogs… I’ve rewritten your code to make
the errors not happen, but obviously it could be re-written many ways.

Shifting the with _fx’s outside of loops means they dont get called so often, saving resources.

Also, if all your loops are using a bpm of 128, setting and getting the bpm in every loop is just
complicating things for no reason. :slight_smile:

This works for me… hopefully it works for you too.

use_debug false
use_bpm 128
set_sched_ahead_time! 2
set_mixer_control! lpf_slide: 30, lpf: 120
set_mixer_control! hpf_slide: 1, hpf: 12

notus = ring(:cb7, :bb6, :gb6, :f6,
             :gb6, :bb6, :cb7, :bb6,
             :gb6, :f6, :gb6, :bb6,
             :cb7, :gb6, :f6, :eb6,
             :cb6, :bb5, :f5, :gb5,
             :bb5, :cb6, :bb5, :gb5,
             :f5, :gb5, :bb5, :cb6,
             :bb5, :gb5, :f5, :eb6,)

notis = ring(:c2, :c2, :c3,
             :ds2, :ds2, :c3,
             :d2, :d2, :c3,
             :f2, :f2, :c3)

notas = ring(:c2, :ds2, :d2, :f2)

notes = ring(:c3, :c4, :c5, :c4)

live_loop :metro do
  sleep 1
end

with_fx :lpf, cutoff: 80 do
  with_fx :ping_pong, mix: 0.5 do
    define :c_arp do
      use_synth :sine
      #    notes=notes.shuffle
      tick
      play notes.look, amp: 0.5, cutoff: 70
      sleep 0.25
    end
  end
end

with_fx :eq, high: -1, low: -1 do
  with_fx :level, amp: 0.45 do
    with_fx :lpf, cutoff: 60 do
      
      define :bass do
        use_synth :saw
        tick
        play notas.look, decay: 3.5, decay_level: 6, sustain: 0.5, amp: 0.3
        sleep 4
      end
    end
  end
end

define :bass_eslik do
  use_synth :sine
  tick
  slptime = ring(2, 0.5, 1.5)
  play notis.look, amp: 0.7
  sleep slptime.look
end

with_fx :eq, high: 10, high_note: 10, low: -3, mid: -1 do
  with_fx :hpf, cutoff: 80 do
    with_fx :eq, low: -2, high: 3, high_q: 2, mid: -1 do
      
      define :melod do
        use_synth :kalimba
        tick
        play notus.look, amp: 1, release: 0.8
        sleep 1
      end
    end
  end
end


live_loop :melodi, sync: :metro, delay: 36 do
  melod
end

live_loop :arp, sync: :metro do
  c_arp
end

with_fx :eq, high: 0, low_note: 5, high_q: 0.1, low: 2 do
  live_loop :bass_line, sync: :metro, delay: 4 do
    bass
  end
end

with_fx :hpf, cutoff: 120 do
  live_loop :noihat, sync: :metro, delay: 4 do
    use_synth :cnoise
    sleep 0.5
    play :c5, amp: 0.8, release: 0.1, cutoff: 105
    sleep 0.5
  end
end

with_fx :distortion, mix: 0.1 do
  
  live_loop :kick, sync: :metro, delay: 4 do
    sample :bd_klub, amp: 1.5, release: 0.5
    sleep 1
  end
end

live_loop :bases, sync: :metro, delay: 4 do
  bass_eslik
end

I tend not to post much these days… I’ve been criticised for being too aggresive… but hell,
it if helps, then thats what the forums all about…my problem, not yours, obviously. :slight_smile:

Hope this sound is close to what you were aiming for.

Regards,

Eli…

1 Like

Hi Eli :slight_smile:

Thanks a lot for all this information and liking the sound :star_struck:
My main purpose in writing this code was to cover the intro of the song Curiosa with live coding. I totally agree with what you’re talking about that with fx on SP. But it was necessary so that I could fully decode the sounds in the song.

And about the using bpm in all loops, well i think its kind a necessary too on Windows. I am not a coder, I graduated from conservatory and I know music. After graduated I interested in live coding and SP is the easiest way for this because of not complex language. Back to our topic, using bpm in all loop is necessary on Win for me because even I synced all loops there may be minor glitches and shifts. Its not happen in Mac OS. When I figured this out I listened with my musician friends and they also noticed that.

I know it sounds ridiculous but as a musician sometimes I want sounds perfect as possible. Because of this I care more about how the code sounds than how hard it pushes the CPU.

Thanks a lot for codes, but if you listen the song that I tell you about it before its not that close to what Im aiming for neverthless thanks for your effort :smiley:

@Eli - maybe I can’t speak for everyone on the forum here but I know that in my opinion your presence and voice are very much valued here! :smiley:

@Berke:

I tried again uninstall and reinstall Sonic Pi and now codes are working.

I am curious, was this uninstalling and reinstalling exactly the same Sonic Pi version? because if you have timing problems due to FX overloading the resources, then doing this is unlikely to solve the problem :sweat_smile:

But it was necessary so that I could fully decode the sounds in the song

Unless I’m not understanding something here, the point we’re making here is that you can still have the effect that you were after in your original code, without the problematic overloading of the resources when the FX are inside the loops. If you are not interested in changing your FX on the fly, then simply lifting the FX outside so that they wrap the live_loops prevents the possibility of multiple FX being started every time the loops repeat - but the music should still sound the same :slight_smile:

(If you do want to be able to change the FX on the fly, then moving the FX outside the live_loops is not the way to go - in that case, it would be necessary as the Tutorial says to increase the time duration of each loop cycle, so that each FX instance has time to complete before the next one starts - using the reps: opt, or using something like 16.times do ... end (or similar) to play more than one sample or note per loop cycle).

Here’s an example of how you could reduce the FX resource usage using the ‘FX wrapping the loops’ strategy - does this version improve things for you?

use_debug false
use_bpm 128
set(:bpm, current_bpm)


live_loop :metro do
  use_bpm get(:bpm)
  sleep 1
end

set_mixer_control! lpf_slide: 30, lpf: 120
set_mixer_control! hpf_slide: 1, hpf: 12

define :c_arp do
  use_synth :sine
  tick
  notes = ring(:c3, :c4, :c5, :c4)
  
  play notes.look, amp: 0.5, cutoff: 70
  sleep 0.25
end

define :bass do
  use_synth :saw
  tick
  notas = ring(:c2, :ds2, :d2, :f2)
  
  play notas.look, decay: 3.5, decay_level: 6, sustain: 0.5, amp: 0.3
  sleep 4
end


define :bass_eslik do
  use_synth :sine
  tick
  notis = ring(:c2, :c2, :c3,
               :ds2, :ds2, :c3,
               :d2, :d2, :c3,
               :f2, :f2, :c3)
  slptime = ring(2, 0.5, 1.5)
  play notis.look, amp: 0.7
  sleep slptime.look
end

define :melod do
  use_synth :kalimba
  tick
  notus = ring(:cb7, :bb6, :gb6, :f6,
               :gb6, :bb6, :cb7, :bb6,
               :gb6, :f6, :gb6, :bb6,
               :cb7, :gb6, :f6, :eb6,
               :cb6, :bb5, :f5, :gb5,
               :bb5, :cb6, :bb5, :gb5,
               :f5, :gb5, :bb5, :cb6,
               :bb5, :gb5, :f5, :eb6,)
  
  play notus.look, amp: 1, release: 0.8
  sleep 1
end

with_fx :eq, high: 10, high_note: 10, low: -3, mid: -1 do
  with_fx :hpf, cutoff: 80 do
    live_loop :melodi, sync: :metro, delay: 36 do
      ##| stop
      
      use_bpm get(:bpm)
      melod
    end
  end
end

with_fx :eq, low: -2, high: 3, high_q: 2, mid: -1 do
  with_fx :lpf, cutoff: 80 do
    with_fx :ping_pong, mix: 0.5 do
      live_loop :arp, sync: :metro do
        ##| stop
        use_bpm get(:bpm)
        c_arp
      end
    end
  end
end

with_fx :eq, high: -1, low: -1 do
  with_fx :level, amp: 0.45 do
    with_fx :lpf, cutoff: 60 do
      live_loop :bass_line, sync: :metro, delay: 4 do
        ##| stop
        use_bpm get(:bpm)
        with_fx :eq, high: 0, low_note: 5, high_q: 0.1, low: 2 do
          bass
        end
      end
    end
  end
end

with_fx :hpf, cutoff: 120 do
  live_loop :noihat, sync: :metro, delay: 4 do
    ##| stop
    use_bpm get(:bpm)
    use_synth :cnoise
    
    sleep 0.5
    play :c5, amp: 0.8, release: 0.1, cutoff: 105
    sleep 0.5
  end
end

with_fx :distortion, mix: 0.1 do
  live_loop :kick, sync: :metro, delay: 4 do
    ##| stop
    use_bpm get(:bpm)
    
    sample :bd_klub, amp: 1.5, release: 0.5
    sleep 1
  end
end

live_loop :bases, sync: :metro, delay: 4 do
  ##| stop
  use_bpm get(:bpm)
  bass_eslik
end


And about the using bpm in all loops, well i think its kind a necessary too on Windows.

Not necessarily - (although I would be surprised if it was). Bpm is automatically inherited by live_loops if it is set with use_bpm at the top level. There should never be any issues with ‘drift’, so if you were noticing things not being in time, I suspect there was a different issue at fault.

I’d be interested in seeing what kind of ‘minor glitches and shifts’ you were noticing - is there some way you can share that - maybe a link to the problematic code as well as a video of the glitch or something?

Hello again :slight_smile:

I am curious too :sweat_smile: as I said before Im not a coder or devoloper or else, for this reason when I see some crush/error I have to much harder time solving. At these times it is more easy with soliving this errors with deleting and reinstalling for me.

Thank you very much for this example. This is the sound that i’m looking for :ok_hand:

Its actually not glitch but like you said things not being in time. I will also take a screen and sound recording about it at another time. Thank you for your attention :innocent:

1 Like