Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
# 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-----|
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-----|
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| do |c|
1 if (c == 'x' || c == 'o' || c == 'g')
# 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
# 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 * {|row| row[i] }
sleep 0.25
# Make it sound like a bad drummer
# sleep rrand(0.15, 0.35)
# Uncomment this to see how we did
in_thread do
#sample :loop_amen_full
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment