I’m just wondering when I should use in_thread
? I understand that it creates a new thread, but since I’m using live_loops
, I don’t really see the need for it. What am I missing?
you might use it for example when wanting two or more bits of code to run once at the same time.
In the example below four identical threads play each delayed by 8 beats after the previous one starts.
# #four part Frere Jaques
with_fx :reverb,room: 0.9 do
use_synth :tri
use_bpm 180
notes=[:c4,:d4,:e4,:c4]*2+[:e4,:f4,:g4]*2+[:g4,:a4,:g4,:f4,:e4,:c4]*2+[:c4,:g3,:c4]*2
durations=[1,1,1,1]*2+[1,1,2]*2+[0.5,0.5,0.5,0.5,1,1]*2+[1,1,2]*2
in_thread do
play_pattern_timed notes,durations,amp: 0.2,pan: -1,release: 0.1
end
sleep 8
use_synth :pulse
in_thread do
play_pattern_timed notes,durations,amp: 0.2,pan: -0.33,release: 0.1
end
sleep 8
use_synth :pulse
in_thread do
play_pattern_timed notes,durations,amp: 0.2,pan: 0.33,release: 0.1
end
sleep 8
use_synth :saw
in_thread do
play_pattern_timed notes,durations,amp: 0.2,pan: 1,release: 0.1
end
end
Thanks for the reply and the example. But I rewrote it using live_loop. Would in_thread provide any benifits etc compared to the version with live_loop?
with_fx :reverb, room: 0.9 do
use_bpm 180
notes = [:c4, :d4, :e4, :c4] * 2 + [:e4, :f4, :g4] * 2 +
[:g4, :a4, :g4, :f4, :e4, :c4] * 2 + [:c4, :g3, :c4] * 2
durations = [1, 1, 1, 1] * 2 + [1, 1, 2] * 2 +
[0.5, 0.5, 0.5, 0.5, 1, 1] * 2 + [1, 1, 2] * 2
live_loop :clock do
sleep 1
end
live_loop :melody1 do
sync :clock
use_synth :tri
play_pattern_timed notes, durations, amp: 0.2, pan: -1, release: 0.1
sleep 8
stop
end
live_loop :melody2 do
sync :clock
sleep 8
use_synth :pulse
play_pattern_timed notes, durations, amp: 0.2, pan: -0.33, release: 0.1
stop
end
live_loop :melody3 do
sync :clock
sleep 16
use_synth :pulse
play_pattern_timed notes, durations, amp: 0.2, pan: 0.33, release: 0.1
stop
end
live_loop :melody4 do
sync :clock
sleep 24
use_synth :saw
play_pattern_timed notes, durations, amp: 0.2, pan: 1, release: 0.1
stop
end
end
in_thread is useful if you want to write loops of finite length – a piece with a beginning, middle and end. Of course you can do this by computing when to put a stop command in a live_loop, but in_thread is cleaner & more intuitive.
Often wondered about this myself. Superb explanations by Harry and Robin.
PD-Pi
thank you Robin @robin.newman for the example and explanation, I rediscover also in_thread!