Skip to content

Instantly share code, notes, and snippets.

@xavriley
Created June 10, 2014 21:22
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save xavriley/cdacb93dc127eafd1f16 to your computer and use it in GitHub Desktop.
Playing ASCII Drum Tabs with Sonic Pi
# Playing ASCII drum tabs with Sonic Pi
# Ruby is an awesome language for String manipulation.
# Lets use that fact to play some drums!
# Tab for the Amen break taken from Wikipedia
# http://en.wikipedia.org/wiki/Amen_break
# Note that %Q{ ... } is just another way
# to define a string in Ruby
amen_tab = %Q{
C |----------------|----------------|----------------|----------x-----|
R |x-x-x-x-x-x-x-x-|x-x-x-x-x-x-x-x-|x-x-x-x-x-x-x-x-|x-x-x-x-x---x-x-|
S |----o--o-o--o--o|----o--o-o--o--o|----o--o-o----o-|-o--o--o-o----o-|
B |o-o-------oo----|o-o-------oo----|o-o-------o-----|--oo------o-----|
}
# This is a random tab for the drum intro to "Cold Sweat" by James Brown
cold_sweat_tab = %Q{
C |----------o-----|----------o-----|----------o-----|----------o-----|
hh|x---x---x---x---|x---x---x---x---|x---x---x---x---|x---x---x---x---|
S |----o--g------o-|-o--o--g----o---|----o--g------o-|-o--o--g----o---|
B |o---------o-----|--oo----o-o-----|o---------o-----|--oo----o-o-----|
|1e+a2e+a3e+a4e+a|1e+a2e+a3e+a4e+a|1e+a2e+a3e+a4e+a|1e+a2e+a3e+a4e+a|
}
use_bpm 140.0 # tempo of the sampled break
# reduce to just essential characters
# in this case 'x', 'o', 'g', - (hyphen) and line break
drum_lines = amen_tab.strip.gsub!(/[^\-xog\n]/, '')
tab = drum_lines.split(/\n+/).map {|line|
line.chars.map do |c|
1 if (c == 'x' || c == 'o' || c == 'g')
end
}
# We've turned our text into an array of arrays
tab.each {|row| puts row }
define :beat do |crash, ride, snare, bass|
sample :drum_splash_hard if crash
sample :drum_cymbal_closed if ride
sample :drum_snare_hard if snare
sample :drum_heavy_kick if bass
end
# We get the number of beats
tab_length = tab.first.length
in_thread do
tab_length.times.with_index do |i|
# the '*' here is called a splat!
# it means we can call beat(0, 1, 0, 1)
# instead of beat([0, 1, 0, 1])
beat *tab.map {|row| row[i] }
sleep 0.25
# Make it sound like a bad drummer
# sleep rrand(0.15, 0.35)
end
end
# Uncomment this to see how we did
in_thread do
#sample :loop_amen_full
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment