I’m attempting to start a group of threads (that will run for an unknown amount of time), then when they’ve all finished start a second group of threads.
However in this simplified attempt I’ve made the second group of threads is raising an Runtime Error - "Timing Exception: thread got too far behind".
I would play one of the sequences directly, not in a thread. That would then set the duration for the sequence for you. Like this:
def riff(pan)
in_thread { play_pattern [:r, :r, :r, :r, :b, :b, :r], pan: pan * 1, release: 0.5}
play_pattern [:d, :d, :a, :a, :r, :r, :a], pan: pan * -1, release: 0.5
end
riff 1
puts "here0"
sleep 0.5 #allows for gap between riff calls
puts "here1"
riff -1
EDIT if the threads were not of the same duration, then “un thread” the longest one. You could have say 5 threads in there with the longest duration one playing directly, not in a thread.
And hope you don’t mind I’ve changed your code to pull the changes out into wrapper functions that could be played around with - even combined…
def with_guitar(amp: 1, fx: :none, &block)
use_synth :pluck
use_synth_defaults divisor: 0.5, depth: 4, attack: 0.01, sustain: 3, release: 1, amp: amp
with_fx(*fx) { yield }
end
def with_flute(amp: 1, fx: [:reverb, room: 0.75, damp: 0.25], &block)
use_synth :fm
use_synth_defaults divisor: 0.5, depth: 4, attack: 0.5, sustain: 0.95, release: 0.4, amp: amp
with_fx(*fx) { yield }
end
def with_instruments(instruments, &block)
instruments.each do |instrument, opts|
send("with_#{instrument}", opts, &block)
end
end
with_flute { play :c }
sleep 3
with_guitar amp: 5, fx: :whammy do
play :c
end
sleep 3
with_instruments guitar: { amp: 1, fx: :wobble }, flute: { amp: 3 } do
play_tabs stairway_to_heaven, bpm: 400
end
EDIT: I’ve modifed the play_tabs above and put it in its own thread so it behaves the same way as play, then all the instruments can be played together.
(BTW is there some “Instrument Library” that I’ve missed somewhere in Sonic-Pi, or am I just “doing it wrong” ?)
use_bpm 120
live_loop :beat4 do
sleep 4
end
define :hat do
at [0, 2, 3],
[{:amp=>0.3}, {:amp=> 1}, {:amp=>0.5}] do |p|
sample :drum_cymbal_pedal, p
end
end
define :drum do
at [1, rrand_i(2, 3)],
[{:amp=>1}] do |p|
sample [:drum_bass_hard, :drum_bass_soft].choose, p
end
end
live_loop :do_hat do
sync :beat4
hat
sleep 1
end
live_loop :do_drum do
sync :beat4
drum
sleep 1
end