# How to program mixing of live loops?

**URL:** <https://in-thread.sonic-pi.net/t/how-to-program-mixing-of-live-loops/2280>\
**Category:** Creations & Ideas\
**Created:** [April 20, 2019, 11:17pm UTC](https://in-thread.sonic-pi.net/t/how-to-program-mixing-of-live-loops/2280 "2019-04-20T23:17:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Artem](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/artem/32/929_2.png) [@Artem](https://in-thread.sonic-pi.net/u/Artem)\
**Post date:** [April 20, 2019, 11:17pm UTC](https://in-thread.sonic-pi.net/t/how-to-program-mixing-of-live-loops/2280/1 "2019-04-20T23:17:23Z")

</div>

Hi!

Do you have example of programs that launch, stop and mix different live\_loops? It can be useful for using Sonic Pi as DAW.

In my recent track ([github](https://github.com/Onuchin-Artem/algo-rave/blob/master/We-Love-Repetition.Rb), [soundcoud](https://soundcloud.com/artem-onuchin/we-love-repetition)) I created a wrapper for live\_loop:

```ruby
def my_live_loop(name)
  in_thread do
    with_fx :level, amp:0 do |level_control|
      set name.to_s.concat("_level"), level_control
      loop do
        yield
      end
    end
  end
end

```

And I use this wrapper to control loops volume in mix method:

```ruby
def mix()
  set_volume! 1
  control get["bd_level"], amp: 1.0
  control get["snare2_level"], amp: 1.0
  control get["algorave_level"], amp: 0.5
  increase = linear_slide(0.0, 0.5, 33)
  32.times do
    control get["bass_level"], amp: increase.tick(:bass)
    control get["algorave_level"], amp: 0.5 -increase.look(:bass)
    sleep 1
  end
  ...

```

That kind of works and it is better than having a lot of sleeps in each thread but still I don’t think this is optimal solution.

Do you have any example of using Sonic Pi to fully program the track?

---

<div class="post-metadata">

**Author:** ![chris.krakou](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@chris.krakou](https://in-thread.sonic-pi.net/u/chris.krakou)\
**Post date:** [April 22, 2019, 8:36am UTC](https://in-thread.sonic-pi.net/t/how-to-program-mixing-of-live-loops/2280/2 "2019-04-22T08:36:32Z")

</div>

you could use `cue :whatever` in a separate loop and have your loops sync to that cue using sync `sync :whatever`. Then you can comment out the cues to control whether or not your loops play. You could also expand on that by using something like `cue :whatever if (spread x,y).tick` or a counter function to create rhythms. I can post an example later of you want.

edit: I went ahead and made an example.

```
define :pattern do |pattern|
  return pattern.ring.tick == "x"
end

live_loop :cues do
  # 4-on-the-floor using patterns and spread
  cue :kick if pattern "x---x---x---x---"
  cue :kick if (spread 1,4).tick
  
  #cinquillo snares using patterns and spread
  cue :snare if pattern "x-xx-xx-"
  cue :snare if (spread 5,8).tick
  
  sleep 0.25
end

live_loop :kicks do
  sample :bd_haus, lpf: 90
  sync :kick
end

live_loop :snares do
  sample :sn_generic, lpf: 90
  sync :snare
end
```

---

<div class="post-metadata">

**Author:** ![perpetual\_monday](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/perpetual_monday/32/955_2.png) [@perpetual\_monday](https://in-thread.sonic-pi.net/u/perpetual_monday)\
**Post date:** [May 22, 2019, 3:38pm UTC](https://in-thread.sonic-pi.net/t/how-to-program-mixing-of-live-loops/2280/3 "2019-05-22T15:38:40Z")

</div>

Thank you for your example. I didn’t quite follow the intention of the `spread` method of triggering but that’s not important for me, as I was just looking for a simple examples of syncing loops and using patterns for drums/rhythms.

There’s a bug/oversight in your example - when `pattern` is called twice per loop `tick` is going to advance each time so the result is that each drum sequence is being stepped through by 2’s (every other element). I made a simplified version that executes that patterns as expected:

```ruby
define :pattern do |pattern, name|
  return pattern.ring.tick(name) == "x"
end

snare_hit = "x-xx-xx-".ring
kick_hit = "x---x---x---x---".ring

live_loop :cues do
  cue :kick if pattern kick_hit, :kicktick
  cue :snare if pattern snare_hit, :snaretick
  sleep 0.25
end

live_loop :kicks do
  sync :kick
  sample :bd_haus
end

live_loop :snares do
  sync :snare
  sample :sn_generic
end

```

---

<div class="post-metadata">

**Author:** ![chris.krakou](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@chris.krakou](https://in-thread.sonic-pi.net/u/chris.krakou)\
**Post date:** [May 22, 2019, 3:53pm UTC](https://in-thread.sonic-pi.net/t/how-to-program-mixing-of-live-loops/2280/4 "2019-05-22T15:53:51Z")

</div>

wow, thread resurrection! nice to hear my stuff is useful to people. Yeah, I later found out that ticking twice in a loop makes things go funky.

As for the euclidean algorithms, [I found a video which explains in fairly simple terms](https://www.youtube.com/watch?v=vwigqSwYNaQ). Fun fact, you can also feed the spread function floating point numbers. `spread(2.75,16)` gives you a drum’ n’ bass rhythm

---

<div class="post-metadata">

**Author:** ![perpetual\_monday](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/perpetual_monday/32/955_2.png) [@perpetual\_monday](https://in-thread.sonic-pi.net/u/perpetual_monday)\
**Post date:** [May 22, 2019, 8:15pm UTC](https://in-thread.sonic-pi.net/t/how-to-program-mixing-of-live-loops/2280/5 "2019-05-22T20:15:04Z")

</div>

I took time to see what `spread` actually is… at first glance I assumed it to be like a range function - makes a lot more sense now!

Thank you for the link, TIL. Now I know what `spread(5, 8)` does. When I run it gives me the variation `"x-x-xx-x"` 🙂

```ruby
puts spread(5,8)

{run: 3, time: 0.0}
 └─ (ring true, false, true, false, true, true, false, true)

```
