Skip to content

Commit cb33b7a

Browse files
committed
first commit drum trigger code
1 parent 1276083 commit cb33b7a

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed

Drum_Trigger_2040/code.py

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# SPDX-FileCopyrightText: 2022 John Park for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
# Drum Trigger Sequencer 2040
5+
# Based on code by Tod Kurt @todbot https://github.com/todbot/picostepseq
6+
7+
# Uses General MIDI drum notes on channel 10
8+
# Range is note 35/B0 - 81/A4, but classic 808 set is defined here
9+
10+
import time
11+
from adafruit_ticks import ticks_ms, ticks_diff, ticks_add
12+
import board
13+
from digitalio import DigitalInOut, Pull
14+
import keypad
15+
import adafruit_aw9523
16+
import usb_midi
17+
from adafruit_seesaw import seesaw, rotaryio, digitalio
18+
from adafruit_debouncer import Debouncer
19+
from adafruit_ht16k33 import segments
20+
21+
22+
# define I2C
23+
i2c = board.STEMMA_I2C()
24+
25+
num_steps = 16 # number of steps/switches
26+
num_drums = 11 # primary 808 drums used here, but you can use however many you like
27+
# Beat timing assumes 4/4 time signature, e.g. 4 beats per measure, 1/4 note gets the beat
28+
bpm = 120 # default BPM
29+
beat_time = 60/bpm # time length of a single beat
30+
beat_millis = beat_time * 1000 # time length of single beat in milliseconds
31+
steps_per_beat = 4 # subdivide beats down to to 16th notes
32+
steps_millis = beat_millis / steps_per_beat # time length of a beat subdivision, e.g. 1/16th note
33+
34+
step_counter = 0 # goes from 0 to length of sequence - 1
35+
sequence_length = 16 # how many notes stored in a sequence
36+
curr_drum = 0
37+
playing = False
38+
39+
# Setup button
40+
start_button_in = DigitalInOut(board.A2)
41+
start_button_in.pull = Pull.UP
42+
start_button = Debouncer(start_button_in)
43+
44+
45+
# Setup switches
46+
switch_pins = (
47+
board.TX, board.RX, board.D2, board.D3,
48+
board.D4, board.D5, board.D6, board.D7,
49+
board.D8, board.D9, board.D10, board.MOSI,
50+
board.MISO, board.SCK, board.A0, board.A1
51+
)
52+
switches = keypad.Keys(switch_pins, value_when_pressed=False, pull=True)
53+
54+
# Setup LEDs
55+
leds = adafruit_aw9523.AW9523(i2c, address=0x5B) # both jumperes soldered on board
56+
for led in range(num_steps): # turn them off
57+
leds.set_constant_current(led, 0)
58+
leds.LED_modes = 0xFFFF # constant current mode
59+
leds.directions = 0xFFFF # output
60+
61+
# Values for LED brightness 0-255
62+
offled = 0
63+
dimled = 2
64+
midled = 20
65+
highled = 150
66+
67+
for led in range(num_steps): # dramatic boot up light sequence
68+
leds.set_constant_current(led, dimled)
69+
time.sleep(0.05)
70+
time.sleep(0.5)
71+
#
72+
# STEMMA QT Rotary encoder setup
73+
rotary_seesaw = seesaw.Seesaw(i2c, addr=0x36) # default address is 0x36
74+
encoder = rotaryio.IncrementalEncoder(rotary_seesaw)
75+
last_encoder_pos = 0
76+
rotary_seesaw.pin_mode(24, rotary_seesaw.INPUT_PULLUP) # setup the button pin
77+
knobbutton_in = digitalio.DigitalIO(rotary_seesaw, 24) # use seesaw digitalio
78+
knobbutton = Debouncer(knobbutton_in) # create debouncer object for button
79+
encoder_pos = -encoder.position
80+
81+
# MIDI setup
82+
midi = usb_midi.ports[1]
83+
84+
drum_names = [
85+
"Bass", "Snar", "LTom", "MTom", "HTom",
86+
"Clav", "Clap", "Cowb", "Cymb", "OHat", "CHat"
87+
]
88+
drum_notes = [36, 38, 41, 43, 45, 37, 39, 56, 49, 46, 42] # general midi drum notes matched to 808
89+
90+
# default starting sequence needs to match number of drums in num_drums
91+
sequence = [
92+
[ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 ], # bass drum
93+
[ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 ], # snare
94+
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 ], # low tom
95+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ], # mid tom
96+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ], # high tom
97+
[ 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], # rimshot/claves
98+
[ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0 ], # handclap/maracas
99+
[ 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0 ], # cowbell
100+
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], # cymbal
101+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 ], # hihat open
102+
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0 ] # hihat closed
103+
]
104+
105+
def play_drum(note):
106+
midi_msg_on = bytearray([0x99, note, 120]) # 0x90 is noteon ch 1, 0x99 is noteon ch 10
107+
midi_msg_off = bytearray([0x89, note, 0])
108+
midi.write(midi_msg_on)
109+
midi.write(midi_msg_off)
110+
111+
def light_steps(step, state):
112+
if state:
113+
leds.set_constant_current(step, midled)
114+
else:
115+
leds.set_constant_current(step, offled)
116+
117+
def light_beat(step):
118+
leds.set_constant_current(step, highled)
119+
120+
def edit_mode_toggle():
121+
# pylint: disable=global-statement
122+
global edit_mode
123+
# pylint: disable=used-before-assignment
124+
edit_mode = (edit_mode + 1) % num_modes
125+
display.fill(0)
126+
if edit_mode == 0:
127+
display.print(bpm)
128+
elif edit_mode == 1:
129+
display.print(drum_names[curr_drum])
130+
131+
def print_sequence():
132+
print("sequence = [ ")
133+
for k in range(num_drums):
134+
print(" [" + ",".join('1' if e else '0' for e in sequence[k]) + "], #", drum_names[k])
135+
print("]")
136+
137+
# set the leds
138+
for j in range(sequence_length):
139+
light_steps(j, sequence[curr_drum][j])
140+
141+
display = segments.Seg14x4(i2c, address=(0x71))
142+
display.brightness = 0.3
143+
display.fill(0)
144+
display.show()
145+
display.print(bpm)
146+
display.show()
147+
148+
edit_mode = 0 # 0=bpm, 1=voices
149+
num_modes = 2
150+
151+
print("Drum Trigger 2040")
152+
153+
154+
display.fill(0)
155+
display.show()
156+
display.marquee("Drum", 0.05, loop=False)
157+
time.sleep(0.5)
158+
display.marquee("Trigger", 0.075, loop=False)
159+
time.sleep(0.5)
160+
display.marquee("2040", 0.05, loop=False)
161+
time.sleep(1)
162+
display.marquee("BPM", 0.05, loop=False)
163+
time.sleep(0.75)
164+
display.marquee(str(bpm), 0.1, loop=False)
165+
166+
167+
while True:
168+
start_button.update()
169+
if start_button.fell: # pushed encoder button plays/stops transport
170+
if playing is True:
171+
print_sequence()
172+
playing = not playing
173+
step_counter = 0
174+
last_step = int(ticks_add(ticks_ms(), -steps_millis))
175+
print("*** Play:", playing)
176+
177+
if playing:
178+
now = ticks_ms()
179+
diff = ticks_diff(now, last_step)
180+
if diff >= steps_millis:
181+
late_time = ticks_diff(int(diff), int(steps_millis))
182+
last_step = ticks_add(now, - late_time//2)
183+
184+
light_beat(step_counter) # brighten current step
185+
for i in range(num_drums):
186+
if sequence[i][step_counter]: # if there's a 1 at the step for the seq, play it
187+
play_drum(drum_notes[i])
188+
light_steps(step_counter, sequence[curr_drum][step_counter]) # return led to step value
189+
step_counter = (step_counter + 1) % sequence_length
190+
encoder_pos = -encoder.position # only check encoder while playing between steps
191+
knobbutton.update()
192+
if knobbutton.fell:
193+
edit_mode_toggle()
194+
else: # check the encoder all the time when not playing
195+
encoder_pos = -encoder.position
196+
knobbutton.update()
197+
if knobbutton.fell: # change edit mode, refresh display
198+
edit_mode_toggle()
199+
200+
# switches add or remove steps
201+
switch = switches.events.get()
202+
if switch:
203+
if switch.pressed:
204+
i = switch.key_number
205+
sequence[curr_drum][i] = not sequence[curr_drum][i] # toggle step
206+
light_steps(i, sequence[curr_drum][i]) # toggle light
207+
208+
if encoder_pos != last_encoder_pos:
209+
encoder_delta = encoder_pos - last_encoder_pos
210+
if edit_mode == 0:
211+
bpm = bpm + encoder_delta # or (encoder_delta * 5)
212+
bpm = min(max(bpm, 10), 400)
213+
beat_time = 60/bpm # time length of a single beat
214+
beat_millis = beat_time * 1000
215+
steps_millis = beat_millis / steps_per_beat
216+
display.fill(0)
217+
display.print(bpm)
218+
if edit_mode == 1:
219+
curr_drum = (curr_drum + encoder_delta) % num_drums
220+
# quickly set the step leds
221+
for i in range(sequence_length):
222+
light_steps(i, sequence[curr_drum][i])
223+
display.print(drum_names[curr_drum])
224+
last_encoder_pos = encoder_pos

0 commit comments

Comments
 (0)