Skip to content

Instantly share code, notes, and snippets.

@amiika
Forked from xavriley/drum_tab_player.rb
Last active September 5, 2018 16:22
Show Gist options
  • Save amiika/543556a4cd061c4e74dabd6c75d63cef to your computer and use it in GitHub Desktop.
Save amiika/543556a4cd061c4e74dabd6c75d63cef to your computer and use it in GitHub Desktop.
Playing ASCII Drum Tabs with Sonic Pi
# Transposed tab array and changed type to ring to play nicely in live_loop
# 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-----|
}
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 = cold_sweat_tab.strip.gsub!(/[^\-xog\n]/, '')
tab = drum_lines.split(/\n+/).map {|line|
line.chars.map { |c|
(c == 'x' || c == 'o' || c == 'g') ? true : false
}
}.transpose.ring
# We've turned our text into an array of arrays
tab.each {|row| puts row }
define :beats 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
live_loop :test do
beats *tab.tick
sleep 0.25
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment