# Example 1 |
loop do
play 50
sleep 1
end
loop do
play 55
sleep 0.5
end
|
# If you write two loops one after another like this,
# then only the first loop will execute as the loop acts
# like a trap not letting the flow of control out
# This code is never executed.
|
# Example 2 |
in_thread do
loop do
play 50
sleep 1
end
end
loop do
play 55
sleep 0.5
end
|
# In order to play two loops at the same time, the first loops need to
# be in a thread (note that it's probably more idiomatic to use live_loop
# when performing):
# By wrapping our loop in an in_thread block, we split the
# control flow into two parts. One flows into the loop (a) and
# the other part flows immediately after the in_thread block (b).
# both parts of the control flow execute at exactly the same time.
# (a)
# (a)
# (b)
# This loop is executed thanks to the thread above
|
# Example 3 |
use_bpm 120
use_synth :dsaw
in_thread do
play 50
use_synth :fm
sleep 1
play 38
end
play 62
sleep 2
play 67
|
# Set the bpm to be double rate
# Set the current synth to be :dsaw
# Create a new thread
# Play note 50 at time 0
# Switch to fm synth (only affects this thread)
# sleep for 0.5 seconds (as we're double rate)
# Play note 38 at time 0.5
# Play note 62 at time 0 (with dsaw synth)
# sleep 1s
# Play note 67 at time 1s (also with dsaw synth)
|
# Example 4 |
in_thread(name: :foo) do
loop do
sample :drum_bass_hard
sleep 1
end
end
in_thread(name: :foo) do
loop do
sample :elec_chime
sleep 0.5
end
end
|
# Here we've created a named thread
# This thread isn't created as the name is
# the same as the previous thread which is
# still executing.
|
# Example 5 |
define :foo do
play 50
sleep 1
end
in_thread(name: :main) do
loop do
foo
end
end
|
# Named threads work well with functions for live coding:
# Create a function foo
# which does something simple
# and sleeps for some time
# Create a named thread
# which loops forever
# calling our function
# We are now free to modify the contents of :foo and re-run the entire buffer.
# We'll hear the effect immediately without having to stop and re-start the code.
# This is because our fn has been redefined, (which our thread will pick up) and
# due to the thread being named, the second re-run will not create a new similarly
# named thread. This is a nice pattern for live coding.
|