# Binary Drum Sequencer - new ways to code drums

**URL:** <https://in-thread.sonic-pi.net/t/binary-drum-sequencer-new-ways-to-code-drums/1618>\
**Category:** Creations & Ideas\
**Created:** [October 27, 2018, 11:17pm UTC](https://in-thread.sonic-pi.net/t/binary-drum-sequencer-new-ways-to-code-drums/1618 "2018-10-27T23:17:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marc](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@marc](https://in-thread.sonic-pi.net/u/marc)\
**Post date:** [October 27, 2018, 11:17pm UTC](https://in-thread.sonic-pi.net/t/binary-drum-sequencer-new-ways-to-code-drums/1618/1 "2018-10-27T23:17:52Z")

</div>

Hello!

I’ve been playing around with some new ways to create drum tracks in Sonic Pi. This one is fast and fun and I think some of you will be tempted to try it out.

Briefly, this style of live coding keeps all your drums in a single live\_loop, uses a single ticker and a single sleep command. It should save your fingers from working too hard to create complex drum loops.

All it takes is a bit of binary know-how, and some ruby sugar to make it work.

```ruby
use_bpm 240

live_loop :drums do
  tick
  on 5[look%4] {sample :bd_tek}
  on 3[look%4] {sample :bd_klub}
  on 7[look%4] {sample :bd_boom}
  on 10[look%4] {sample :elec_cymbal, rate: 3}
  sleep 1
end

```

What’s great about ruby is that when you index into an integer it treats it like a binary number and spits out 1s and 0s. When combined with sonic pi’s ‘on’ which accepts 1s and 0s, you have a fast drum sequencer.

Play with the integers to change the pattern. Change the modulo to create polyrhythms. It’s very flexible that way.

Any feedback would be appreciated. Have a nice day!

Marc

---

<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, 9:38am UTC](https://in-thread.sonic-pi.net/t/binary-drum-sequencer-new-ways-to-code-drums/1618/2 "2018-10-28T09:38:04Z")

</div>

hmm  
I got an error using your syntax with { }

Runtime error: NoMethodError  
Thread death ±-\> :live\_loop\_drums  
undefined method `call’ for Nil:nilClass

Changing to the version below worked

```ruby
use_bpm 240

live_loop :drums do
  tick
  on 5[look%4] do;sample :bd_tek ;end
  on 3[look%4] do;sample :bd_klub;end
  on 7[look%4] do;sample :bd_boom;end
  on 10[look%4] do;sample :elec_cymbal, rate: 3;end
  sleep 1
end

```

EDIT or use this alternative using if instead of on

```ruby
use_bpm 240

live_loop :drums do
  tick
  sample :bd_tek if 5[look%4] >0
  sample :bd_klub if 3[look%5] >0
  sample :bd_boom if 7[look%4] >0
  sample :elec_cymbal, rate: 3 if 10[look%4] >0
  sleep 1
end

```

---

<div class="post-metadata">

**Author:** ![Hussein](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/hussein/32/1972_2.png) [@Hussein](https://in-thread.sonic-pi.net/u/Hussein)\
**Post date:** [May 26, 2021, 5:26pm UTC](https://in-thread.sonic-pi.net/t/binary-drum-sequencer-new-ways-to-code-drums/1618/4 "2021-05-26T17:26:37Z")

</div>

@marc @robin.newman  
I like this approach. Thanks you for the code and refinements.  
Will be giving it a go.

Cheers

---

<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:** [July 10, 2021, 8:22pm UTC](https://in-thread.sonic-pi.net/t/binary-drum-sequencer-new-ways-to-code-drums/1618/5 "2021-07-10T20:22:25Z")

</div>

What an absolute game changer for drum sequencing. Smart idea!

---

<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:** [July 10, 2021, 10:53pm UTC](https://in-thread.sonic-pi.net/t/binary-drum-sequencer-new-ways-to-code-drums/1618/6 "2021-07-10T22:53:45Z")

</div>

After playing around with this a little and pulling in custom samples I got some pretty interesting results. I added some rings to alter the pitches of some samples in a pattern.

https://w.soundcloud.com/player/?visual=false&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F1085363914&show_artwork=true&maxheight=166

---

<div class="post-metadata">

**Author:** ![din](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@din](https://in-thread.sonic-pi.net/u/din)\
**Post date:** [July 30, 2021, 6:34am UTC](https://in-thread.sonic-pi.net/t/binary-drum-sequencer-new-ways-to-code-drums/1618/7 "2021-07-30T06:34:52Z")

</div>

Your idea is giga brain!  
There is a lot of tricks that could be pulled off. Think bitwise operators!

```ruby
x = 0xA96B
y = 0x4889
z = 0x4647
w = 0xABAB

puts "The bits or sequencer of x #{x.to_s(2)}"
puts "The bits or sequencer of y #{y.to_s(2)}"
puts "The bits or sequencer of z #{z.to_s(2)}"
puts "The bits or sequencer of w #{w.to_s(2)}"

live_loop :drums do
  use_bpm 90
  tick
  sample :bd_tek if x[look % (x.size*4)] == 1
  sample :bd_klub if y[look % (y.size*4)] == 1
  sample :bd_boom if z[look % (z.size*4)] == 1
  sample :elec_cymbal, rate: 3 if w[look % (w.size*4)] == 1
  sleep 1
end

```

Although I kind of don’t get how ruby knows to do this or what size it will be based on the dec number.

It’s not like you know if it’s a int or long int or unsigned etc…

If you keep it to hex I think you could have better timing. Knowing it will be one measure. A word is a measure now! Programming mode on the calculator just get real.
