Create live_loops dynamically with a for loop

I have done this before using a definition to create the loops
Try this:

define :doLoop do |ln,n|
  loop_name=eval'"channel#{ln.to_s}_loop"'
  live_loop loop_name do
    puts "inside #{loop_name}"
    sample [:bd_haus,:elec_blup,:elec_flip][n]
    sleep 1.5
  end
end

for x in 0..(2)
  doLoop x.to_s,x
  sleep 0.5
end

generates three live loops, each of which plays a different samplesonic
The log output looks like this

=> Starting run 1

=> Defining fn :doLoop

=> Defining fn :live_loop_channel0_loop

=> Loaded sample "/Applications/Sonic Pi.app/etc/samples/bd_haus.flac"

{run: 1, time: 0.0, thread: :live_loop_channel0_loop}
 └─ "inside channel0_loop"
 
{run: 1, time: 0.0}
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "bd_haus.flac"
 
=> Defining fn :live_loop_channel1_loop

=> Loaded sample "/Applications/Sonic Pi.app/etc/samples/elec_blup.flac"

{run: 1, time: 0.5, thread: :live_loop_channel1_loop}
 └─ "inside channel1_loop"
 
=> Defining fn :live_loop_channel2_loop

{run: 1, time: 0.5}
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "elec_blup.flac"
 
=> Loaded sample "/Applications/Sonic Pi.app/etc/samples/elec_flip.flac"

{run: 1, time: 1.0, thread: :live_loop_channel2_loop}
 └─ "inside channel2_loop"
 
{run: 1, time: 1.0}
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "elec_flip.flac"
 
{run: 1, time: 1.5}
 └─ Stopped internal thread
 
{run: 1, time: 1.5, thread: :live_loop_channel0_loop}
 ├─ "inside channel0_loop"
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "bd_haus.flac"
 
{run: 1, time: 2.0, thread: :live_loop_channel1_loop}
 ├─ "inside channel1_loop"
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "elec_blup.flac"
 
{run: 1, time: 2.5, thread: :live_loop_channel2_loop}
 ├─ "inside channel2_loop"
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "elec_flip.flac"
 
{run: 1, time: 3.0, thread: :live_loop_channel0_loop}
 ├─ "inside channel0_loop"
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "bd_haus.flac"
 
{run: 1, time: 3.5, thread: :live_loop_channel1_loop}
 ├─ "inside channel1_loop"
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "elec_blup.flac"
 
{run: 1, time: 4.0, thread: :live_loop_channel2_loop}
 ├─ "inside channel2_loop"
 └─ sample "/Applications/Sonic Pi.app/etc/samples",
             "elec_flip.flac"
 
=> Stopping all runs...

=> Stopping run 1

=> Completed run 1

=> All runs completed

=> Pausing SuperCollider Audio Server

The order in which the log entries are printed may seem a bit confusing, but if you look at the times of the entries you can see that the loops are operating as you would expect, each generated and playing for the first iteration 0.5 beats after the previous one, and repeating every 1.5 beats.

I used an expanded version of this definition method in a project I did which also let me remove the live loops as well.
see https://gist.github.com/rbnpi/a86ced02b74817f3070887df30fb0701

1 Like