Please explain how this code works

  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 cycle.look[look%4] >0
    sample :elec_cymbal, rate: 3 if 10[look%4] >0
    sleep 0.5
    
  end
end

I’m interested mostly in the ‘3[look%5] >0’ stuff, and just what
it’s up to…

Eli…

Hi @Eli,

I don’t think, this code is complete and/or correct. There is an end to much but more importantly cycle is not defined and the expression 5[look%4] make - at least to me - no sense.

You know the modulo operator %, do you? It returns the rest of an integer division: It tries to divide an integer by another integer and if this works out it returns 0 because this is the rest of the division; there is nothing left to devide. If it does not work out evenly, it returns the first number which is the ’ rest’ .

Let’s see for different values of look:

look is 0: look % 4 = 0
look is 1: look % 4 = 1
look is 2: look % 4 = 2
look is 3: look % 4 = 3
look is 4: look % 4 = 0
look is 5: look % 4 = 1
look is 6: look % 4 = 2
look is 7: look % 4 = 3
look is 8: look % 4 = 0

You can use modulo division to get a counter, which counts only from 0 to 3 (see the example) and then starts again. Very handy in the context of music. See some of my experiments for further use cases.

I hope this helps although I couldn’t help with the example you provided.

1 Like

Hi Martin,

Normally I can follow what you are saying, but this time I’m stumped.

Eli…

Hi @Eli,

it took me a while to understand, what modulo does (although it is not too complicated, which is always easy to say after you’ve wrapped your head around something). Even more important IMHO is, what you can do with it. Check out the following code for one suggestion:

use_bpm 170

live_loop :bass do
# this counter only counts from 0 - 7 and then starts all over again  
cnt = tick % 8

  n = :cs1

# here we use cnt to change the notes during the course of the bass phrase
  if cnt < 4 then
    n = :cs1
  elsif cnt < 6 then
    n = :e1
  elsif cnt < 8
    n = :fs1
  end

  use_synth :fm
  use_synth_defaults attack: 0.001, attack_level: 8, sustain: 3, release: 0.025, divisor: 1, depth: 1, amp: 0.75

    s = play n
    i = 0
    32.times do
      i += 1
      control s, depth: (ring 2, 8)[i]
      sleep 0.25
    end

end

PS.: ‘stumped’: I didn’t know that. The translation to colloquial German is: ‘to be at the end with your Latin’ :wink:

Experimenting with this, I think that it looks at the intial number in binary and returns the various digits of that.
Try first

5.times do |i|
  puts i,5[i]
end

You will see the various values of i and the value returned from 5[i]
The separate issue is the use of look with the modulo operator %. This returns the remainder when look is divided by the second number ie look%4 returns 0,1,2,3,0,1,2,3 as look increases in value. So you get:

{run: 2, time: 0.0}
 └─ 0 0 1
 
{run: 2, time: 0.1}
 └─ 1 1 0
 
{run: 2, time: 0.2}
 └─ 2 2 1
 
{run: 2, time: 0.3}
 └─ 3 3 0
 
{run: 2, time: 0.4}
 └─ 4 0 1
 
{run: 2, time: 0.5}
 └─ 5 1 0
 
{run: 2, time: 0.6}
 └─ 6 2 1
 
{run: 2, time: 0.7}
 └─ 7 3 0
 
{run: 2, time: 0.8}
 └─ 8 0 1
 
{run: 2, time: 0.9}
 └─ 9 1 0

when you run

10.times do
  tick
  puts look, look%4,  5[look%4]
  sleep 0.1
end
1 Like

Hi @robin.newman,

so I am wrong, it does make sense :wink:

Interesting. I’ll have a closer look at it. Thanks!

use_bpm 120
cycle = (ring 2, 4, 6,8)

co = 10 #cutoff
sc = scale(:e4, :minor_pentatonic) #scale stored in variable

live_loop :drums do
  if rand(1) > 0.75 then
    8.times do
      tick
      sample :bd_tek if 5[look%4] >0
      sample :bd_klub if 3[look%5] >0
      sample :bd_boom if cycle.look[look%4] >0
      sample :elec_cymbal, rate: 4 if 10[look%4] >0
      sleep 0.5
    end
  else
    4.times do
      tick
      sample :bd_tek if 5[look%4] >0
      sample :bd_klub if 3[look%5] >0
      sample :bd_boom if cycle.look[look%4] >0
      sample :elec_cymbal, rate: 3 if 10[look%4] >0
      sleep 0.5
    end
  end
end


with_synth  :dpulse do
  live_loop :starp1 do
    sync :drums
    with_transpose -12 do
      if rand() < 0.65
        play_pattern_timed [:Gs3, :Fs3, :A3, :b3, :c5, :b4, :g4, :e4], [0.25],
          attack: 0.1,
          release: 0.5,
          cutoff: co
      else
        play_pattern_timed [:c4, :e4, :g4, :b4, :c5, :b4, :g4, :e4], [0.25],
          
        attack: 0.1,
          release: 0.5,
          cutoff: co
      end
    end
  end
end

Heres the complete code, for Martins sake. :slight_smile:
Eli…

1 Like

Thanks @Eli for the code. Did you code this? Sick beats :wink:

Yes. I think, that’s it @robin.newman. The expression n1[n2] returns the binary representation of n1 if you have enough digits (e. g. n2 = 1…4):

n = 0
11.times do
  s = ""
  p = 0
  4.times do
    sleep 0.125
    s += n[p].to_s
    p += 1
  end
  puts " -- #{n} --"
  puts s
  puts "---"
  n += 1
  sleep 0.125
end

returns:

{run: 39, time: 0.5}
 ├─ " -- 0 --"
 ├─ "0000"
 └─ "---"
 
{run: 39, time: 1.125}
 ├─ " -- 1 --"
 ├─ "1000"
 └─ "---"
 
{run: 39, time: 1.75}
 ├─ " -- 2 --"
 ├─ "0100"
 └─ "---"
 
{run: 39, time: 2.375}
 ├─ " -- 3 --"
 ├─ "1100"
 └─ "---"
 
{run: 39, time: 3.0}
 ├─ " -- 4 --"
 ├─ "0010"
 └─ "---"
 
{run: 39, time: 3.625}
 ├─ " -- 5 --"
 ├─ "1010"
 └─ "---"
 
{run: 39, time: 4.25}
 ├─ " -- 6 --"
 ├─ "0110"
 └─ "---"
 
{run: 39, time: 4.875}
 ├─ " -- 7 --"
 ├─ "1110"
 └─ "---"
 
{run: 39, time: 5.5}
 ├─ " -- 8 --"
 ├─ "0001"
 └─ "---"
 
{run: 39, time: 6.125}
 ├─ " -- 9 --"
 ├─ "1001"
 └─ "---"
 
{run: 39, time: 6.75}
 ├─ " -- 10 --"
 ├─ "0101"
 └─ "---"

Looks somehow tricky if applied like in the example…

1 Like