# How can I make an even-spaced polyrhythm?

**URL:** <https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006>\
**Category:** Support, Help & Resources\
**Created:** [February 15, 2019, 2:09am UTC](https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006 "2019-02-15T02:09:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagofdragonite](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/bagofdragonite/32/2049_2.png) [@bagofdragonite](https://in-thread.sonic-pi.net/u/bagofdragonite)\
**Post date:** [February 15, 2019, 2:09am UTC](https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006/1 "2019-02-15T02:09:52Z")

</div>

I often use “spread” for making polyrhythmic songs but would like to know if there’s a way that I can instead evenly space notes over a bar such that multiple time-signatures are overlapping.

For example I want one live\_loop to be playing a note sequence in 4/4 while another overlaps it in 5/4. This would mean that the bar’s sleep must be divided by 5. I fear if I divide the sleep by 5 it will return a rounded number which will eventually lead to the loops straying out of time. Or can I perhaps sync the 5/4 loop up such that no straying happens?

Or maybe a better way would be if each loop had an independent bpm… I’m not sure how to do that though.

I’m sure someone must have done this so what’s the best way to go about it?

---

<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:** [February 15, 2019, 9:00am UTC](https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006/2 "2019-02-15T09:00:39Z")

</div>

Here’s one I did before.

> [@Polyrhythmic Drum Generator for Sonic Pi](http://in-thread.sonic-pi.net/t/polyrhythmic-drum-generator-for-sonic-pi/554):
>
> PolyRhythmicDrum Generator for Sonic Pi. Driven using a TouchOSC interface screen running on an iPad. The Sonic Pi program works fine on a Pi3, but requires Sonic Pi3. 30 min video on youtube in three parts. Part1 shows it working, part 2 gives more detailed insight and part3. shows how TouchOSC screen is linked to Sonic Pi. Gist with full code is at

---

<div class="post-metadata">

**Author:** ![bagofdragonite](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/bagofdragonite/32/2049_2.png) [@bagofdragonite](https://in-thread.sonic-pi.net/u/bagofdragonite)\
**Post date:** [February 15, 2019, 10:13am UTC](https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006/3 "2019-02-15T10:13:13Z")

</div>

Thanks. I may end up playing around with that but not sure that it would be ideal for what I’m doing. I’d still prefer a simple coded way so I can manipulate the coded for my purposes. I’d especially like if I could use “ring” to cycle through time signatures part way through the bars for some really intricate stuff. I assume if you built a whole interface which which is capable of dividing the sleep into equal segments without the aforementioned issues then you must know how to accomplish this within SonicPi itself.

---

<div class="post-metadata">

**Author:** ![themeplard](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/themeplard/32/838_2.png) [@themeplard](https://in-thread.sonic-pi.net/u/themeplard)\
**Post date:** [February 15, 2019, 2:14pm UTC](https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006/4 "2019-02-15T14:14:16Z")

</div>

Keep It Simple and Stupid ^^

```ruby
live_loop :foo do; tick
  sample :perc_bell if bools(1,0,0,0).look
  sample :perc_bell2 if bools(1,0,0,0,0).look
  sleep 1
end

```

---

<div class="post-metadata">

**Author:** ![bagofdragonite](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/bagofdragonite/32/2049_2.png) [@bagofdragonite](https://in-thread.sonic-pi.net/u/bagofdragonite)\
**Post date:** [February 15, 2019, 3:10pm UTC](https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006/5 "2019-02-15T15:10:00Z")

</div>

EDIT: Oh wait this doesn’t accomplish what I want I don’t think. I want to the notes evenly spaced over a bar. For example 4 notes played equally spaced over the course of 1 sleep while another loop plays 5 notes played equally spaced over the length of one sleep. How can I do that?

_Ahh wow I feel stupid focusing on the sleep when I could have just kept that constant. This is great and I think I can see a way to manipulate the time signature realtime or with rings so thats good._

---

<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:** [February 15, 2019, 3:43pm UTC](https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006/6 "2019-02-15T15:43:49Z")

</div>

Here is a cut down of my project. The idea is that you specify 1 beat as the length for each different sample stream. Thus you can have :bd\_haus say played once per cycle.  
Or you could have two …elec\_twips played with equal spacing in 1 beat, and so on for three, four… samples per beat. You do this simply by adjusting the sleep time and using a loop to play the sample the number of times required. eg

```ruby
4.times do
  sample :bd_haus
  sleep 1.0/4
end

```

You can put this in a definition with a few more bells and whistles, and run this definition inside a live loop.

```ruby
define :pl do |s,n,p=0,a=0,sp| #params samplename,beats/cycle,pan,amp,spread value
  t=1.0/n
  sp = [[n,sp].min,1].max #this line restricts the range of sp from 1..n
  use_bpm get(:bpm) #set current bpm
  n.times do #play number of samples per cycle for current element
    sample s,amp: a,pan: p if spread(sp,n).tick
    sleep t
  end
end

```

I add pan and amp settings, and a spread function with range spread(1,n) up to spread(n,n) where n is the number of pulses per beat.  
The whole program them becomes

```ruby
set :bpm,45

define :pl do |s,n,p=0,a=0,sp| #params samplename,beats/cycle,pan,amp,spread value
  t=1.0/n
  sp = [[n,sp].min,1].max
  use_bpm get(:bpm) #set current bpm
  n.times do #play number of samples per cycle for current element
    sample s,amp: a,pan: p if spread(sp,n).tick
    sleep t
  end
end

live_loop :metro do
  use_bpm get(:bpm)
  sleep 1
end

live_loop :s1,sync: :metro do
  pl(:elec_twip,6,-1,1,3)
end

live_loop :s2,sync: :metro do
  pl(:bd_haus,5,1,1,3)
end
live_loop :s3,sync: :metro do
  pl(:drum_snare_soft,3,-0.5,1,2)
end
live_loop :s4,sync: :metro do
  pl(:elec_wood,7,-0.7,0.5,3)
end
live_loop :s5,sync: :metro do
  pl(:elec_flip,11,-0.5,1,8)
end

```

The metro loop keeps everything in sync. You can alter any of the paramters for one of the playing loops to adjust sample, number of pulses per beat, pan, amp and spread setting. When you rerun the new settings are taken up next time the live loop starts.  
You could use boolean lists instead of spread, and pass in parameters if you want, and you can have further live loops to increase the number of samples used.  
You can also adjust the tempo using the first line, and the new value is picked up when the next cycle starts keeping everything in sync.

---

<div class="post-metadata">

**Author:** ![bagofdragonite](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/bagofdragonite/32/2049_2.png) [@bagofdragonite](https://in-thread.sonic-pi.net/u/bagofdragonite)\
**Post date:** [February 16, 2019, 6:11am UTC](https://in-thread.sonic-pi.net/t/how-can-i-make-an-even-spaced-polyrhythm/2006/7 "2019-02-16T06:11:57Z")

</div>

Ahh perfect. Yeah thanks for showing how you kept that all in sync. I’m excited to try this out!
