Hi all. I’m looking at different ways of adding swing to rhythms. I note that there is a ‘with_swing’ method, but I don’t think it does what I usually think of as swing. I’ve found other examples of drum machines with swing, but I want to look for general solutions.
I’ve coded up the following which creates a ring of 16th note positions. at commands can then be used to schedule drum hits (or other notes) at the correct position in a bar by indexing into the ring, as per the example drum loop.
This appears to be, once I have the function, a reasonably easy way to find the right times to play notes. Finding the right lengths of notes is going to be more tricky, but I can create a ring of rings to solve that. I think.
Is the following code correct? I’m swinging the 8ths, and then creating the 16ths as equal halves of eighths. But, should I be splitting the 8ths into 16ths unequally as well?
The results of this sound plausibly correct to me, but I’m not sure.
swing = use_bpm 120
use_debug false
#
# calculate and return a ring representing the positions
# of 16th notes in a 4/4 bar with a defined amount of
# swing. 0 = no swing, 1 = shuffle
#
define :swing do |swing|
ret = (ring )
time = 0
4.times do
length1 = 0.5 + (0.1666 * swing)
length2 = 1 - length1
bit = (ring time, time+length1/2, time+length1,
time+length1+length2/2 )
ret = ret + bit
time = time + 1
end
return ret
end
# uncomment one and execute to hear what a
# different amount of swing sounds like
## set :pattern, swing( 0 )
## set :pattern, swing( 0.1 )
## set :pattern, swing( 0.2 )
set :pattern, swing( 0.5 )
## set :pattern, swing( 0.75 )
## set :pattern, swing( 1 )
print get :pattern
#
# drum pattern so that we can hear the swing.
#
live_loop :drums do
p = get :pattern
at [ p[0], p[4], p[8], p[12] ] do
sample :bd_haus
end
at [ p[4], p[12], p[9] ],
[ 1, 1, 0.5] do |a|
sample :sn_dolf, amp: a
end
at [ p[0], p[2], p[4],
p[5],
p[6], p[8], p[10],
p[12], p[14], p[13] ] do
sample :drum_cymbal_closed
end
sleep 4
end