# Carrying on from a convo a month ago (solved by d0lfyn)

**URL:** <https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428>\
**Category:** Support, Help & Resources\
**Created:** [April 16, 2021, 3:21am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428 "2021-04-16T03:21:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Future](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/future/32/1871_2.png) [@Future](https://in-thread.sonic-pi.net/u/Future)\
**Post date:** [April 16, 2021, 3:21am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/1 "2021-04-16T03:21:19Z")

</div>

related to [this topic](https://in-thread.sonic-pi.net/t/how-to-end-the-script/5271/9)

So I have this melody:

```
live_loop :Melody do
  sample :habanera
  use_synth :fm
  use_transpose -12
  play (ring :d, :r, :r, :a, :f5, :r, :a, :r).tick
  sleep 0.25
end

```

but when I use this code:

```
  if tick == 3
    set :run, 0
    stop
  end

```

to automatically stop it, it breaks the melody.

```
live_loop :Melody do
  sample :habanera
  use_synth :fm
  use_transpose -12
  play (ring :d, :r, :r, :a, :f5, :r, :a, :r).tick
  sleep 0.25
  if tick == 3
    set :run, 0
    stop
  end
end

```

can anyone help make it so that the melody doesn’t break?

thanks.

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [April 16, 2021, 3:37am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/2 "2021-04-16T03:37:56Z")

</div>

Hi again 🙂

You should use `look == 3` if you only intend to check the tick value. `tick` advances the tick value whenever it’s invoked.

It would look like this:

```ruby
  if look == 3
    set :run, 0
    stop
  end

```

---

<div class="post-metadata">

**Author:** ![Future](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/future/32/1871_2.png) [@Future](https://in-thread.sonic-pi.net/u/Future)\
**Post date:** [April 16, 2021, 3:40am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/3 "2021-04-16T03:40:32Z")

</div>

this worked. now I need to figure out how many individual habanera sounds I need so I know when to stop the melody 😂😂  
thanks heaps

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [April 16, 2021, 3:46am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/4 "2021-04-16T03:46:29Z")

</div>

@Future - just a suggestion - IMO it’s very useful if you give the link some descriptive text, (for example, many folks recommend writing links like ‘see [how to xyz]’ instead of using something like ‘for how to xyz, [click here]’). This gives much more immediate context about what the link points to 🙂 Even better in this case though, you can allow discourse to show a preview of the page that you are linking to, if you place the link on its own line 😄

---

<div class="post-metadata">

**Author:** ![Future](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/future/32/1871_2.png) [@Future](https://in-thread.sonic-pi.net/u/Future)\
**Post date:** [May 6, 2021, 2:29am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/5 "2021-05-06T02:29:57Z")

</div>

carrying on again: how do I do this with my own samples? I’ve been trying for ages with no success.

```
in_thread do
  live_loop :clap do
    sample clap, :d.tick, amp: 2
    sleep 2
  end
  if look == 2
    set :run, 0
    stop
  end
end

```

This doesn’t work, along with a bunch of other ways I tried. fyi - `clap` is the name of a sample.

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [May 6, 2021, 3:18am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/6 "2021-05-06T03:18:33Z")

</div>

Long time no see!

Hmm, here’s a template solution, which I’ll try to explain:

```ruby
use_bpm 120

##| initialise time state variable :run
set :run, true

live_loop :kick do
  ##| alternate between loud and soft
  sample :drum_bass_soft, amp: [0.8, 0.4].ring.tick
  sleep 1
  
  stop unless get :run
end

in_thread do
  ##| offset by 1 beat
  sleep 1
  live_loop :snare do
    sample :drum_snare_soft, amp: 0.8
    sleep 2
    
    stop unless get :run
  end
end

with_synth :tri do
  live_loop :arp do
    ##| cycle through arpeggio ring
    play [:a3, :c4, :e4].ring.tick, amp: 0.6, sustain: 0, release: 1/8.to_f
    sleep 1/4.to_f
    
    stop unless get :run
  end
end

live_loop :clock do
  ##| advance tick up to limit
  if tick == 6
    set :run, false
    stop
  end
  sleep 1
end

```

Basically, you want to keep a separate `clock` loop the moment you have more than one loop you want to synchronise to stop.

Notice how each `live_loop` keeps its own `tick`, whereas the time state variable `:run` is shared everywhere.

In this template, you can also see how a snare (or clap) can be offset by a beat using an `in_thread` on its inception.

I hope this helps! 🙂

---

<div class="post-metadata">

**Author:** ![Future](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/future/32/1871_2.png) [@Future](https://in-thread.sonic-pi.net/u/Future)\
**Post date:** [May 17, 2021, 11:46pm UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/7 "2021-05-17T23:46:42Z")

</div>

I’ve set the clap to tick twice for experimenting sake, but it is not turning itself off after 2, it was doing it after 11.

Also, my drums all come in different times, but I want them all to cut out at the same time. will this clock thing allow that?

code:

```
# Written by Sam Humphreys

#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

# Setting BPM to 70
use_bpm 70

#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

# Making variables:

# The clap is a single one-shot hit, that I repeat every two seconds on line 67
clap = "/Users/samhumphreys/Library/Mobile Documents/com~apple~CloudDocs/Logic Pro X/Music Plug-Ins/Cymatics/Cymatics - Drill Essential Drum Kit/Drum One Shots/Claps/Cymatics - Wake Clap.wav"

# The hi hats is a loop that I created on Logic Pro, and then bounced out as a .aif file.
# I made the loop on Logic because I thought it would be too hard to make a good
# sounding hi hat loop on Sonic Pi because of my experience with it (absolutely zero).
hi_hats = "/Users/samhumphreys/Library/Mobile Documents/com~apple~CloudDocs/2020/2021/TeKura/Music/SonicPi/bouncedLoops/Hi-hat-loop-for-Sonic-Pi.wav"

# The 808 is another loop I made on Logic, for the same reason as the hi hat loop. The 808s are
# way to complicated to make in Sonic Pi for someone with my knowledge of Ruby and Sonic Pi.
bass = "/Users/samhumphreys/Library/Mobile Documents/com~apple~CloudDocs/2020/2021/TeKura/Music/SonicPi/bouncedLoops/SonicPiBass.wav"

# This is the kick. I was going to make this loop in Logic again but I thought that if I did too much in Logic, my grade would be lowered.
kick = "/Users/samhumphreys/Library/Mobile Documents/com~apple~CloudDocs/Logic Pro X/Music Plug-Ins/Cymatics/Cymatics - Drill Essential Drum Kit/Drum One Shots/Kicks/Cymatics - Ultimate Kick - G.wav"

# This is the perc loop. Made on Logic again.
perc = "/Users/samhumphreys/Library/Mobile Documents/com~apple~CloudDocs/2020/2021/TeKura/Music/SonicPi/bouncedLoops/Percloop.aif"

# This is a white noise up effect.
wn_u_fx = "/Users/samhumphreys/Library/Mobile Documents/com~apple~CloudDocs/Logic Pro X/Music Plug-Ins/Cymatics/Cymatics - FX Essentials/White Noise/Up/Cymatics - FX Essentials White Noise Riser 18 - 140 BPM.wav"

# This is an impact effect.
impact = "/Users/samhumphreys/Library/Mobile Documents/com~apple~CloudDocs/Logic Pro X/Music Plug-Ins/Cymatics/Cymatics - FX Essentials/Impacts/Cymatics - FX Essentials Impact 2.wav"

# This is a white noise down effect.
wn_d_fx = "/Users/samhumphreys/Library/Mobile Documents/com~apple~CloudDocs/Logic Pro X/Music Plug-Ins/Cymatics/Cymatics - FX Essentials/White Noise/Down/Cymatics - FX Essentials White Noise Downlifter 6.wav"

#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

# Melody
live_loop :melody do
  sample :habanera
  use_synth :fm
  use_transpose -12
  play (ring :d, :r, :r, :a, :f5, :r, :a, :r).tick
  sleep 0.25
  if look == 257
    set :run, 0
    stop
  end
end

with_fx :reverb do
  live_loop :dNote do
    with_fx :slicer, phase: 0.25 do
      use_synth :blade
      play :d.tick, release: 8, cutoff: 100, amp: 2
    end
    sleep 8
    if look == 7
      set :run, 0
      stop
    end
  end
end

#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

set :run, true

sleep 16

in_thread do
  live_loop :clap do
    sleep 1
    sample clap, amp: [2].ring.tick
    sleep 1
    
    stop unless get :run
  end
end

in_thread do
  loop do
    with_fx :distortion do
      sample bass, amp: [1].ring.tick
      sleep 16
      
      stop unless get :run
    end
  end
end

sleep 8

in_thread do
  loop do
    sample wn_u_fx .ring.tick
    sleep 32
    
    stop unless get :run
  end
end

sleep 8

in_thread do
  loop do
    sample hi_hats .ring.tick
    sleep 16
    
    stop unless get :run
  end
end

in_thread do
  loop do
    sample kick, amp: [2].ring.tick
    sleep 0.25
    sample kick
    sleep 1.25
    sample kick
    sleep 1
    sample kick
    sleep 0.75
    sample kick
    sleep 0.75
    sample kick
    sleep 0.75
    sample kick
    sleep 0.75
    sample kick
    sleep 1
    sample kick
    sleep 1.5
    
    stop unless get :run
  end
end

in_thread do
  loop do
    sample perc .ring.tick
    sleep 16
    
    stop unless get :run
  end
end

in_thread do
  loop do
    sample impact .ring.tick
    sleep 32
    
    stop unless get :run
  end
end

in_thread do
  loop do
    sample wn_d_fx .ring.tick
    sleep 32
    
    stop unless get :run
  end
end

live_loop :clock do
  if tick == 2
    set :run, false
    stop
  end
  sleep 1
end
```

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [May 18, 2021, 1:54am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/8 "2021-05-18T01:54:58Z")

</div>

1. The 2 intermediate calls to `sleep 8` that you’ve placed in the main thread block the onset of the clock loop for a total of 16 beats. That’s why it claps 11 times: the countdown hasn’t begun. To fix this, I would put the calls to `sleep` inside each thread individually. That way, they don’t block the clock.  
E.g.

```ruby
in_thread do
  sleep 8
  loop do
    sample wn_u_fx .ring.tick
    sleep 32
    
    stop unless get :run
  end
end

```

1. While it may not affect what you hear, the irregular sleep times in your loops (e.g. `sleep 16` and `sleep 32`) mean that the loops detect the change of `:run` at different times. To fix this, I would sleep at the same interval of 1 beat in every loop. I would use the modulus operator to determine when to play.  
E.g.

```ruby
in_thread do
  sleep 8
  loop do
    sample wn_u_fx if (tick % 32).zero?
    sleep 1
    
    stop unless get :run
  end
end

```

Because the onset of your latest loop is delayed by up to 16 beats, your clock should run for at least 16 beats. Any shorter, and you’ll have stuff playing when it shouldn’t.

If you follow 1 and 2, then the drums should cut out at the same time.

---

<div class="post-metadata">

**Author:** ![Future](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/future/32/1871_2.png) [@Future](https://in-thread.sonic-pi.net/u/Future)\
**Post date:** [May 31, 2021, 10:50pm UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/9 "2021-05-31T22:50:27Z")

</div>

I got your first point done, but I don’t understand your second point about the modulus operator. can you pls try to explain a bit more simpler? thanks for your patience

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [June 1, 2021, 3:09am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/10 "2021-06-01T03:09:40Z")

</div>

Certainly! I’ll illustrate what I mean with my DAW.

Currently, you have code for several “tracks” that end at different times:

![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/6/694afc5d6de38f7d5fa441f41c3fb1c6b9e62a8d.png)

The reason these tracks end at different times is that their sleep times are different. As a result, the tracks don’t check for the end of the piece until their sleep is over.

What we want is for all our tracks to end at the same time:

![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/e/e551cb6598a3c6100ed93df6b0ab60acc68e4ffb.png)

In order to do this, every track needs to check for the end of the piece at the same time. That’s why I replaced your different sleep times (e.g. `8`, `16`, `32`) with the same sleep time: `1`. But because each loop is only sleeping for 1 beat now, everything is playing on every beat! So to get the instruments to play once every 8, 16, or 32 beats, I add a condition to their play statements depending on how frequently they’re supposed to play. E.g. `if (tick % 32).zero?` plays every 32 beats, meanwhile `if (tick % 16).zero?` plays every 16 beats.

I hope this clears things up a bit? Please feel free to ask for more clarification. I don’t know if I’ve done a good job simplifying.

---

<div class="post-metadata">

**Author:** ![Future](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/future/32/1871_2.png) [@Future](https://in-thread.sonic-pi.net/u/Future)\
**Post date:** [June 1, 2021, 3:26am UTC](https://in-thread.sonic-pi.net/t/carrying-on-from-a-convo-a-month-ago-solved-by-d0lfyn/5428/11 "2021-06-01T03:26:52Z")

</div>

awesome. I understand now. Ill probably get back to you tomorrow about how this is going. thanks so much!
