# How to truncate a loop

**URL:** <https://in-thread.sonic-pi.net/t/how-to-truncate-a-loop/6423>\
**Category:** Support, Help & Resources\
**Created:** [January 22, 2022, 9:17pm UTC](https://in-thread.sonic-pi.net/t/how-to-truncate-a-loop/6423 "2022-01-22T21:17:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![quekou](https://avatars.discourse-cdn.com/v4/letter/q/87869e/32.png) [@quekou](https://in-thread.sonic-pi.net/u/quekou)\
**Post date:** [January 22, 2022, 9:17pm UTC](https://in-thread.sonic-pi.net/t/how-to-truncate-a-loop/6423/1 "2022-01-22T21:17:34Z")

</div>

Hi folks, fairly new to Sonic Pi. I have been trying to sort this out for the whole day. I have a loop with know BPM but it’s more than 4 bars and less than 5 bars. I want it to loop very 4 bars, so I set sleep as 16 so. that works. The issues is that when the loop starts again, the 5th bar of the previous loop still ring out. I try to use finish function but I don’t exactly where to finish since I don’t really know the length in time how long the loop is and what length would be for only the first 4 bars. so I can’t really calculate the ratio. How do you folks handle situations like this? Thank you,

---

<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:** [January 23, 2022, 12:55am UTC](https://in-thread.sonic-pi.net/t/how-to-truncate-a-loop/6423/2 "2022-01-23T00:55:45Z")

</div>

Hello @quekou!

Seeing your code might make it easier to work out a potential solution for you - any chance you can show us?

As a rough starting idea, is keeping track of which iteration through the loop you are in so that you can potentially do something different that time around, useful at all?

---

<div class="post-metadata">

**Author:** ![quekou](https://avatars.discourse-cdn.com/v4/letter/q/87869e/32.png) [@quekou](https://in-thread.sonic-pi.net/u/quekou)\
**Post date:** [January 23, 2022, 1:27am UTC](https://in-thread.sonic-pi.net/t/how-to-truncate-a-loop/6423/3 "2022-01-23T01:27:22Z")

</div>

```ruby
live_loop :test do
  use_bpm 70
  GuitarRiff1 = "/Users/macos/Library/Mobile Documents/com~apple~CloudDocs/Guitar Books/Riffs/The hunger/0005 2022_01_14 23_24_11 E 70bpm.wav"
  sample GuitarRiff1
  sleep 16
end

```

Thanks, The GuitarRiff1 is between 4 bars and 5 bars long. It’s bpm is 70. The above code will start he riff at the right start point but the issue is that the section after bar 4 still gets played after the loop has gone around staring at bar 1. so how am I supposed to make sure the loop only goes up to the end of bar 4 when it starts to loop itself from bar 1.  
Thanks very much

---

<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:** [January 23, 2022, 3:02am UTC](https://in-thread.sonic-pi.net/t/how-to-truncate-a-loop/6423/4 "2022-01-23T03:02:24Z")

</div>

I see. So is the issue here that you only ever want to play part of the sample, and ignore the last little bit of it? Then yes, using the `finish:` opt is possibly the easiest way to do this - if you are not sure at what point to stop playing, you can find this by using a calculation like the following:

```ruby
sample GuitarRiff1, finish: (16.0 / sample_duration(GuitarRiff1))

```

(Since we know that in 4/4 timing, 16 beats is 4 bars, and `sample_duration` gives the length of the whole sample in beats).

Does that suit your needs? 🙂

(Btw, to get nice code highlighting and indentation, you can put a blank line before and after your code examples here, and on these blank lines, put three backticks ``` - it makes code examples nice and easy to read 🙂 (I took the liberty of formatting your code example above to show this).

---

<div class="post-metadata">

**Author:** ![quekou](https://avatars.discourse-cdn.com/v4/letter/q/87869e/32.png) [@quekou](https://in-thread.sonic-pi.net/u/quekou)\
**Post date:** [January 23, 2022, 4:05am UTC](https://in-thread.sonic-pi.net/t/how-to-truncate-a-loop/6423/5 "2022-01-23T04:05:50Z")

</div>

wow, it worked. Thanks very much. I need to get myself familiar with more functions…and thanks for the tip on the formatting as well Cheers.

---

<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:** [January 23, 2022, 4:12am UTC](https://in-thread.sonic-pi.net/t/how-to-truncate-a-loop/6423/6 "2022-01-23T04:12:21Z")

</div>

You’re welcome, always happy to help where I can 👍
