# Sync loops in sequencer

**URL:** <https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454>\
**Category:** Support, Help & Resources\
**Created:** [August 31, 2018, 8:09am UTC](https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454 "2018-08-31T08:09:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [August 31, 2018, 8:09am UTC](https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454/1 "2018-08-31T08:09:11Z")

</div>

Hi, I am testing out Sonic Pi and I have been trying to find a way for creating a simple sequencer. I have looked at the sync manual but I would like to get some ideas of the best way of doing this.

If you listen to the code below, why are the chords loop skipping a bar? What will be the best strategy to create a sequencer?

(I also thought about having a main triggering every sixteenth note but that did not work well either)

```
use_bpm 120

live_loop :main do
  sample :elec_blip2
  sleep 4
end

live_loop :loop_chord do
  sync :main
  play :c4
  play :e4
  play :g4
  sleep 2
  play :f4
  play :a4
  play :c4
  sleep 2
end
```

---

<div class="post-metadata">

**Author:** ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)\
**Post date:** [August 31, 2018, 9:29am UTC](https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454/2 "2018-08-31T09:29:07Z")

</div>

Hi @keys,

from time to time there are questions about the `sync` command similar to yours. Here is a link to such a discussion, which will probably help: [First little diddy](http://in-thread.sonic-pi.net/t/first-little-diddy/1449/7)

Basically the `sync` will and can not `sync` to an event in the past. One way to cope with this in your case is to write your `sync` in the `live_loop` line such as

```ruby
live_loop :loop_chord, sync: :main do
  # code goes here ...
end

```

But I recommend to read the linked thread and the material in the tutorial to understand more fully what actually is going on.

Martin

---

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [August 31, 2018, 11:38am UTC](https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454/3 "2018-08-31T11:38:03Z")

</div>

Thank you for the advise. I tried to put sync in the upper line as you suggested but it still does not behave as I expect.

```
use_bpm 120

live_loop :main do
  sample :elec_blip2
  sleep 4
end

live_loop :loop_chord, sync: :main do
  sync :main
  play :c4
  play :e4
  play :g4
  sleep 2
  play :f4
  play :a4
  play :c4
  sleep 2
end
```

---

<div class="post-metadata">

**Author:** ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)\
**Post date:** [August 31, 2018, 1:13pm UTC](https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454/4 "2018-08-31T13:13:00Z")

</div>

I see. It seems you forgot to remove the second line in your second loop:

```ruby
live_loop :loop_chord, sync: :main do
  sync :main # this one has to be removed
  play :c4
  # ...

```

---

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [August 31, 2018, 6:46pm UTC](https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454/5 "2018-08-31T18:46:06Z")

</div>

Ah that explains it 🙂 Thank you so much for the advise. It works fine now.

---

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [September 7, 2018, 11:55am UTC](https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454/6 "2018-09-07T11:55:30Z")

</div>

I hope it is ok to add one more follow up question in this thread to clarify. In the code below the “metronome” wakes up “loop1” and the loop skips the first run as espected. But why is “loop2” started simultaniously as “loop1”? Shouldn’t “loop2” also wait for “loop1” to finish its first run?

```
live_loop :metronome do
  sleep 1
end

live_loop :loop1, sync: :metronome do
  #bar 1
  sample :elec_filt_snare
  sample :drum_bass_hard
  sleep 0.5
  sample :drum_bass_soft
  sleep 0.5
  
  #bar 2
  sample :elec_beep
  sample :drum_snare_hard
  sleep 1
  
  #bar 3
  sample :elec_beep
  sample :drum_bass_hard
  sleep 0.5
  sample :drum_bass_soft
  sleep 0.5
  
  #bar 4
  sample :elec_beep
  sample :drum_snare_hard
  sleep 0.5
  sample :drum_snare_hard
  sleep 0.5
end

live_loop :loop2, sync: :loop1 do
  #bar 1 to 8
  sample :elec_blip2
  16.times do
    sample :drum_cymbal_pedal
    sleep 0.5
  end
end
```

---

<div class="post-metadata">

**Author:** ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)\
**Post date:** [September 8, 2018, 10:17am UTC](https://in-thread.sonic-pi.net/t/sync-loops-in-sequencer/1454/7 "2018-09-08T10:17:50Z")

</div>

Hi @keys,

I think it works like this: When you run `metronome`, both other `live_loop`s will ‘start’ also (= code being evaluated), and ‘note’ to which `live_loop`s they are told to listen to:

- `loop1` to `metronome`
- `loop2` to `loop1`

So as soon as `loop1` starts `loop2` also will start because it had already had time to sync. Maybe someone with more internal knowledge can explain that more accurately (or correct me) but I thing the `sync` command (positioned in the `live_loop` line) will be honored by another `live_loop` even if the first one is still waiting.
