2021-03-11 14:30:40 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Sandy Macdonald
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# A MIDI arpeggiator, with three different styles: up, down, or up-down. BPM and
|
|
|
|
# note length are both configurable, and LEDs cycle with the currently-played
|
|
|
|
# key/note to give some visual feedback.
|
2021-03-11 14:30:40 +01:00
|
|
|
|
|
|
|
# You'll need to connect Keybow 2040 to a computer running a DAW like Ableton,
|
|
|
|
# or other software synth, or to a hardware synth that accepts USB MIDI.
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Drop the keybow2040.py file into your `lib` folder on your `CIRCUITPY` drive.
|
|
|
|
|
2021-03-11 14:30:40 +01:00
|
|
|
# NOTE! Requires the adafruit_midi CircuitPython library!
|
|
|
|
|
|
|
|
import time
|
|
|
|
import board
|
|
|
|
from keybow2040 import Keybow2040
|
|
|
|
|
|
|
|
import usb_midi
|
|
|
|
import adafruit_midi
|
|
|
|
from adafruit_midi.note_off import NoteOff
|
|
|
|
from adafruit_midi.note_on import NoteOn
|
|
|
|
|
|
|
|
# Set up Keybow
|
|
|
|
i2c = board.I2C()
|
|
|
|
keybow = Keybow2040(i2c)
|
|
|
|
keys = keybow.keys
|
|
|
|
|
|
|
|
# Set USB MIDI up on channel 0.
|
|
|
|
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=5)
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# The colour to set the keys when pressed, orange-y.
|
2021-03-11 14:30:40 +01:00
|
|
|
rgb = (255, 50, 0)
|
|
|
|
|
|
|
|
# MIDI velocity.
|
|
|
|
start_note = 60
|
2021-03-11 21:43:13 +01:00
|
|
|
velocity = 100
|
|
|
|
|
|
|
|
# Beats per minute
|
|
|
|
bpm = 120
|
|
|
|
|
|
|
|
# Play 16th notes
|
|
|
|
note_length = 1/16
|
|
|
|
|
|
|
|
# Assumes BPM is calculated on quarter notes
|
|
|
|
note_time = 60 / bpm * (note_length * 4)
|
2021-03-11 14:30:40 +01:00
|
|
|
|
|
|
|
# Arpeggio style:
|
|
|
|
# 0 = up
|
|
|
|
# 1 = down
|
2021-03-11 21:43:13 +01:00
|
|
|
# 2 = up-down
|
2021-03-11 14:30:40 +01:00
|
|
|
arp_style = 2
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Start the arp in a forwards direction (1) if the style is up or up-down, or
|
|
|
|
# or backwards (-1) if the style is down.
|
2021-03-11 14:30:40 +01:00
|
|
|
if arp_style == 0 or arp_style == 2:
|
|
|
|
direction = 1
|
|
|
|
elif arp_style == 1:
|
|
|
|
direction = -1
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Keep track of time of last note played and last keys pressed
|
2021-03-11 14:30:40 +01:00
|
|
|
last_played = None
|
|
|
|
last_pressed = []
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# Always remember to call keybow.update()!
|
|
|
|
keybow.update()
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# If any keys are pressed, go through shenanigans
|
2021-03-11 14:30:40 +01:00
|
|
|
if keybow.any_pressed():
|
2021-03-11 21:43:13 +01:00
|
|
|
# Fetch a list of pressed keys
|
2021-03-11 14:30:40 +01:00
|
|
|
pressed = keybow.get_pressed()
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# If the keys pressed have changed...
|
2021-03-11 14:30:40 +01:00
|
|
|
if pressed != last_pressed:
|
2021-03-11 21:43:13 +01:00
|
|
|
# Keys that were pressed, but are no longer
|
|
|
|
missing = [k for k in last_pressed if k not in pressed]
|
|
|
|
|
|
|
|
# Any keys that were pressed, but are no longer, turn LED off
|
|
|
|
# and send MIDI note off for the respective note.
|
|
|
|
for k in missing:
|
|
|
|
note = start_note +k
|
|
|
|
midi.send(NoteOff(note, 0))
|
|
|
|
keys[k].set_led(0, 0, 0)
|
|
|
|
|
|
|
|
# Calculate MIDI note numbers
|
2021-03-11 14:30:40 +01:00
|
|
|
notes = [start_note + k for k in pressed]
|
|
|
|
last_pressed = pressed
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# If going forward (up or starting up-down), start at 0,
|
|
|
|
# otherwise start at the end of the list of notes.
|
2021-03-11 14:30:40 +01:00
|
|
|
if arp_style == 0 or arp_style == 2:
|
|
|
|
this_note = 0
|
|
|
|
elif arp_style == 1:
|
|
|
|
this_note = len(notes) - 1
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Send MIDI note on message for current note and turn LED on
|
2021-03-11 14:30:40 +01:00
|
|
|
midi.send(NoteOn(notes[this_note], velocity))
|
2021-03-11 21:43:13 +01:00
|
|
|
keys[pressed[this_note]].set_led(*rgb)
|
2021-03-11 14:30:40 +01:00
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Update last_played time, set elapsed to 0, and update current and
|
|
|
|
# last note indices.
|
2021-03-11 14:30:40 +01:00
|
|
|
last_played = time.monotonic()
|
|
|
|
elapsed = 0
|
|
|
|
last_note = this_note
|
|
|
|
this_note += direction
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# If the currently pressed notes are the same as the last loop, then...
|
2021-03-11 14:30:40 +01:00
|
|
|
else:
|
|
|
|
if notes != []:
|
2021-03-11 21:43:13 +01:00
|
|
|
# Check time elapsed since last note played
|
2021-03-11 14:30:40 +01:00
|
|
|
elapsed = time.monotonic() - last_played
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# If the note time has elapsed, then...
|
|
|
|
if elapsed > note_time:
|
|
|
|
# Reset at the end or start of the notes list
|
2021-03-11 14:30:40 +01:00
|
|
|
if this_note == len(notes) and direction == 1:
|
|
|
|
this_note = 0
|
|
|
|
elif this_note < 0:
|
|
|
|
this_note = len(notes) - 1
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Send a MIDI note off for the last note, turn off LED
|
2021-03-11 14:30:40 +01:00
|
|
|
midi.send(NoteOff(notes[last_note], 0))
|
2021-03-11 21:43:13 +01:00
|
|
|
keys[pressed[last_note]].set_led(0, 0, 0)
|
|
|
|
|
|
|
|
# Send a MIDI note on for the next note, turn on LED
|
2021-03-11 14:30:40 +01:00
|
|
|
midi.send(NoteOn(notes[this_note], velocity))
|
2021-03-11 21:43:13 +01:00
|
|
|
keys[pressed[this_note]].set_led(*rgb)
|
2021-03-11 14:30:40 +01:00
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Update time last_played, make this note last note
|
2021-03-11 14:30:40 +01:00
|
|
|
last_played = time.monotonic()
|
|
|
|
last_note = this_note
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# For the up-down style, switch direction at either end
|
2021-03-11 14:30:40 +01:00
|
|
|
if arp_style == 2 and this_note == len(notes) -1:
|
|
|
|
direction = -1
|
|
|
|
elif arp_style == 2 and this_note == 0:
|
|
|
|
direction = 1
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Increment note
|
2021-03-11 14:30:40 +01:00
|
|
|
this_note += direction
|
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# If nothing is now pressed, but was last time, then send MIDI note off
|
|
|
|
# for every note, and turn all the LEDs off.
|
2021-03-11 14:30:40 +01:00
|
|
|
elif len(last_pressed) and keybow.none_pressed():
|
|
|
|
for note in range(128):
|
|
|
|
midi.send(NoteOff(note, 0))
|
2021-03-11 21:43:13 +01:00
|
|
|
for key in keys:
|
|
|
|
key.set_led(0, 0, 0)
|
2021-03-11 14:30:40 +01:00
|
|
|
|
2021-03-11 21:43:13 +01:00
|
|
|
# Nothing is pressed, so reset last_pressed list
|
2021-03-11 14:30:40 +01:00
|
|
|
last_pressed = []
|