# Understanding live\_loops

**URL:** <https://in-thread.sonic-pi.net/t/understanding-live-loops/1616>\
**Category:** General\
**Created:** [October 27, 2018, 6:29pm UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616 "2018-10-27T18:29:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![wueschn](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@wueschn](https://in-thread.sonic-pi.net/u/wueschn)\
**Post date:** [October 27, 2018, 6:29pm UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/1 "2018-10-27T18:29:31Z")

</div>

Hello dear experts  
I have to admit that I have difficulties in understanding the live\_loops (but I like them very much).  
My question is exactly:  
a.) is it better to start every live\_loop with a sleep before making sounds/melodies or is it better to make sleeps after the sounds (or is it of no importance how to start).  
b.) what does sync exactly do with sleep commands?  
c.) is there an easy explanation somewhere?

Thanks a lot for your help!  
Barb.

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [October 27, 2018, 8:37pm UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/2 "2018-10-27T20:37:06Z")

</div>

Hi there,

have you taken a look at the tutorial sections on live loops?

[http://sonic-pi.net/tutorial.html#section-9](http://sonic-pi.net/tutorial.html#section-9)

---

<div class="post-metadata">

**Author:** ![wueschn](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@wueschn](https://in-thread.sonic-pi.net/u/wueschn)\
**Post date:** [October 27, 2018, 8:49pm UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/3 "2018-10-27T20:49:50Z")

</div>

Hello samaaron  
Thanks for your reply.

I want to give an example:  
This code

```
   live_loop :bass do
   use_bpm = 60
   sample :drum_heavy_kick
   sleep 1
   sample :drum_cymbal_pedal
   sleep 0.25
   sample :drum_snare_soft
   sleep 0.25
   sample :drum_cymbal_pedal
   sleep 0.25
   sample :drum_snare_soft
   sleep 0.25

   end

```

does the same than this code.

```
live_loop :bass do
use_bpm = 60
sample :drum_heavy_kick
sleep 1
end

live_loop :basic do
use_bpm = 60
sync :bass
sample :drum_cymbal_pedal
sleep 0.25
sample :drum_snare_soft
sleep 0.25
sample :drum_cymbal_pedal
sleep 0.25
sample :drum_snare_soft
sleep 0.25
end  

```

I can not understand why? How does :basic know about sleep 1 from :bass???  
Thanks

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [October 27, 2018, 9:42pm UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/4 "2018-10-27T21:42:24Z")

</div>

Hi there

The reason the basic loop delays by 1 beat before you hear anything even though both loops start together, is that the initial cue from the bass loop starting is missed by the basic loop which starts at the same time: the sync :bass command is not set up when the first :bass cue is sent, so that it is the second pass of the bass loop which allows the basic loop to proceed, when the second cue is sent and picked up by the sync :bass line.

There has been much discussion about this in [this thread](http://in-thread.sonic-pi.net/t/live-loops-sync-questions/1172) if you want to look into it further

---

<div class="post-metadata">

**Author:** ![wueschn](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@wueschn](https://in-thread.sonic-pi.net/u/wueschn)\
**Post date:** [October 28, 2018, 9:53am UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/5 "2018-10-28T09:53:45Z")

</div>

Thanks robin.newman  
I think all becomes a little bit clearer.  
I have one more question to understand the basics

```
use_bpm = 60

live_loop :chief do
sample :drum_heavy_kick
sleep 1
end

live_loop :servant do
sync :chief
sleep 0.5
sample :drum_splash_soft
end

```

a.) the “Master” that dominates the speed with sleep 1 is live\_loop :chief?  
b.) when does live\_loop :chief sent his cue, at the end after the sleep 1 ?  
c.) the live\_loop :servant has always to wait for one second/hit to start his sample?  
d.) when I change the live\_loop :chief to 2 samples with sleeps, when is the cue sent?  
e.) is it possible to send a cue manually in live\_loop ?

```
use_bpm = 60
live_loop :chief do
sample :drum_heavy_kick
sleep 1
sample :drum_snare_hard
sleep 1
end

live_loop :servant do
sync :chief
sleep 0.5
sample :drum_splash_soft
end

```

Thanks again very much!  
Bar.

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [October 28, 2018, 10:11am UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/6 "2018-10-28T10:11:09Z")

</div>

Not sure what your question is here as you don’t ask one.  
However the servant misses the first cue as chief is issued as a cue before sync :chief is ready to receive it first time around.

This is maybe what you are expecting

```ruby
live_loop :servant do
  sync :chief
  sleep 0.5
  sample :drum_splash_soft
  
end
sleep 0.005 #makes sure servant is ready BEFORE chief starts
live_loop :chief do
  
  sample :drum_heavy_kick
  sleep 1
  
end

```

The effect is that the whole thing starts 0.005 beats after the run button is pushed, but the timing therafter is accurate.  
You can think of it like a conductor holding up his baton to get everone ready (when run is pushed), then giving his down beat (0.005 later) when cheif starts and servant is in time with that.

---

<div class="post-metadata">

**Author:** ![wueschn](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@wueschn](https://in-thread.sonic-pi.net/u/wueschn)\
**Post date:** [October 28, 2018, 10:55am UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/7 "2018-10-28T10:55:15Z")

</div>

> [@robin.newman](#):
>
> Not sure what your question is here as you don’t ask one.  
> However the servant misses the first cue as chief is issued as a cue before sync :chief is ready to receive it first time around.

Thanks a lot robin.newman  
My question has been the questions a.) to e.) 😁  
sorry for confusing you 😁  
Would you be so kind to answer because these are the basics that I am confused about …

Thank you  
Bar.

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [October 28, 2018, 2:19pm UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/8 "2018-10-28T14:19:32Z")

</div>

OK, as far as q e and f) are concerned there are many solutions.  
One way of adding another sample is to put it in a separate live\_loop. This example also illustrated sending a cue from inside a live\_loop

```ruby
live_loop :servant do
  sync :chief
  sleep 0.5
  sample :drum_splash_soft
end

live_loop :servant2 do
  sync :p2
  sample :drum_cymbal_closed
end

sleep 0.005 #makes sure servant is ready BEFORE chief starts
live_loop :chief do
  sample :drum_heavy_kick
  sleep 0.75
  cue :p2
  sleep 0.25  
end

```

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [October 28, 2018, 7:56pm UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/9 "2018-10-28T19:56:41Z")

</div>

> #makes sure servant is ready BEFORE chief starts

Are you sure this is necessary? I programmed it so that you don’t need fiddly sleeps like this, so if you remove it and sometimes it works and sometimes it doesn’t then that’s _definitely_ a bug.

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [October 29, 2018, 9:41am UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/10 "2018-10-29T09:41:29Z")

</div>

hmm I think it IS necessary in this example. The sync :chief is inside the live\_loop not in the calling line line

```ruby
live_loop :servant do
sync :chief

```

not

```ruby
live_loop :servant,sync: :chief do

```

I find this doesn’t work without the short delay.  
It is not the way I would personally set out this code, but I was working with the example given in the original question.

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [October 29, 2018, 9:53am UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/11 "2018-10-29T09:53:16Z")

</div>

Ah yes, I see what’s happening. This is due to the `sync` in the first live loop being automatically placed at a lower priority to the `cue` generated by the `chief` live loop. This behaviour was introduced when I made this stuff deterministic - i.e. it always has the same behaviour rather than randomly doing different things.

However, I still consider it to either be a bug I’d like to fix, but if that doesn’t prove to be feasible, I’d like to be able to provide a work around by enabling people to set the priority on a thread, so that you can control the order of the behaviour manually and deterministically.

It’s also important to point out that the `sleep 0.005` won’t result in the threads being very slightly out of phase as the `sync` will override this difference by simply adopting the time of the thread that calls `cue` - so this is actually safe to do with respect to timing (although it does feel like a bit of a kludge!).

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [October 29, 2018, 5:08pm UTC](https://in-thread.sonic-pi.net/t/understanding-live-loops/1616/12 "2018-10-29T17:08:18Z")

</div>

Agreed. In fact I stated in a previous post in the thread that the “bodge” time doesn’t affect the overall accuracy but merely delays the start after run is pushed by a tiny amount (in fact irrelevant compared to the default scheduled ahead time).
