live_loop name (symbol)
Loop the do/end block forever. However, unlike a basic loop, a live_loop has two special properties. Firstly it runs in a thread - so you can have any number of live loops running at the same time (concurrently). Secondly, you can change the behaviour of a live loop whilst it is still running without needing to stop it. Live loops are therefore the secret to live coding with Sonic Pi.
As live loops are excecuted within a named in_thread, they behave similarly. See the in_thread documentation for all the details. However, it’s worth mentioning a few important points here. Firstly, only one live loop with a given name can run at any one time. Therefore, if you define two or more live_loop
s called :foo
only one will be running. Another important aspect of live_loop
s is that they manage their own thread locals set with the use_*
and with_*
fns. This means that each live_loop
can have its own separate default synth, BPM and sample defaults. When a live_loop
is first created, it inherits the thread locals from the parent thread, but once it has started, the only way to change them is by re-defining the do/end body of the live_loop
. See the examples below for details. Finally, as mentioned above, provided their names are different, you may have many live_loop
s executing at once.
A typical way of live coding with live loops is to define a number of them in a buffer, hit Run to start them and then to modify their do/end blocks and then hit Run again. This will not create any more thread, but instead just modify the behaviour of the existing threads. The changes will not happen immediately. Instead, they will only happen the next time round the loop. This is because the behaviour of each live loop is implemented with a standard function. When a live loop is updated, the function definition is also updated. Each time round the live loop, the function is called, so the new behviour is only observed next time round the loop.
Also sends a cue
with the same name each time the live_loop
repeats. This may be used to sync
with other threads and live_loop
s.
If the live_loop
block is given a parameter, this is given the result of the last run of the loop (with initial value either being 0
or an init arg). This allows you to ‘thread’ values across loops.
Finally, it is possible to delay the initial trigger of the live_loop on creation with both the delay:
and sync:
opts. See their respective docstrings. If both delay:
and sync:
are specified, on initial live_loop creation first the delay will be honoured and then the sync.
Introduced in v2.1
# Example 1 | |
|
|
# Example 2 | |
|
|
# Example 3 | |
|
|
# Example 4 | |
|
|
# Example 5 | |
|
|
# Example 6 | |
|
|
# Example 7 | |
|
|
# Example 8 | |
|
|