Skip to content

Instantly share code, notes, and snippets.

@darinwilson
Last active March 2, 2024 20:11
Show Gist options
  • Save darinwilson/a3e5909db339838a67fe to your computer and use it in GitHub Desktop.
Save darinwilson/a3e5909db339838a67fe to your computer and use it in GitHub Desktop.
Sonic Pi Drum Machine
#########################################
## Sonic Pi Drum Machine
## coded by Darin Wilson
##
use_bpm 95
in_thread(name: :drum_machine) do
# choose your kit here (can be :acoustic, :acoustic_soft, :electro, :toy)
use_kit :acoustic
# program your pattern here - each item in the list represents 1/4 of a beat
# for each item, enter a number between 0 and 9 (0=silent,9=loudest)
hat [5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
kick [9, 0, 9, 0, 0, 0, 0, 0, 9, 0, 0, 3, 0, 0, 0, 0]
snare [0, 0, 0, 0, 9, 0, 0, 2, 0, 1, 0, 0, 9, 0, 0, 1]
end
##################################################################
##
## The gory details - you don't need to change anything down here,
## unless you're curious :)
##
drum_kits = {
acoustic: {
hat: :drum_cymbal_closed,
kick: :drum_bass_hard,
snare: :drum_snare_hard
},
acoustic_soft: {
hat: :drum_cymbal_closed,
kick: :drum_bass_soft,
snare: :drum_snare_soft
},
electro: {
hat: :elec_triangle,
kick: :elec_soft_kick,
snare: :elec_hi_snare
},
toy: {
hat: :elec_tick,
kick: :elec_hollow_kick,
snare: :elec_pop
}
}
current_drum_kit = drum_kits[:acoustic]
define :use_kit do |kit_name|
current_drum_kit = drum_kits[kit_name]
end
live_loop :pulse do
sleep 4
end
define :run_pattern do |name, pattern|
live_loop name do
sync :pulse
pattern.each do |p|
sample current_drum_kit[name], amp: p/9.0
sleep 0.25
end
end
end
define :hat do |pattern|
run_pattern :hat, pattern
end
define :kick do |pattern|
run_pattern :kick, pattern
end
define :snare do |pattern|
run_pattern :snare, pattern
end
@xavriley
Copy link

Great!

@darinwilson
Copy link
Author

Thanks @xavriley! :)

@mbutz
Copy link

mbutz commented Nov 20, 2015

Darin,
that's very enlightening. I will play a bit around with this. One of the first things I tried to create starting with Sonic Pi was a sequencer. But your approach is much more elegantly. Thanks for that.
Martin

@joesh1
Copy link

joesh1 commented Mar 24, 2017

Brilliant, just what I came here looking for. I'd seen (somewhere) a way of writing beats in an array and stumbled across this, which is even better!

Big thanks.

@PhilJungschlaeger
Copy link

Found your drum machine and changed it a bit, to suite my live coding:
PhilJungschlaeger/druMachim@b8ff4ab

@frankyhollywood
Copy link

'use_kit' was unknown to me but it still works, it is a completely undocumented feature https://sonic-pi.net/tutorial.html

thanks!

@nise
Copy link

nise commented Mar 2, 2024

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment