Created
December 16, 2015 15:04
-
-
Save mandyRae/82f31e7ea5ff421d3abf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
PROTOTYPE SOFTWARE -- Drum Kit with Raspberry Pi | |
Amanda @ electrothoughts.wordpress.com 2015 | |
Please reuse this code to your heart's content! | |
Notes: | |
- Uses pygame.mixer module to play audio files | |
- Uses indexing for all functions with drums and LEDs | |
- Some extraneous functions, such as the extra colors in pinColor | |
- This will run headless, so eventually the KeyboardInterrupt will be removed | |
- Volume up/down scaling will need to be changed later | |
''' | |
import pygame.mixer, RPi.GPIO as GPIO, time | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
pygame.mixer.init() | |
GPIO.cleanup() | |
DRUM1 = 22 | |
DRUM2 = 27 | |
DRUM3 = 17 | |
DRUM4 = 4 | |
DRUMS = [DRUM1, DRUM2, DRUM3,DRUM4] | |
RED_LEDS= [5,6,13,19] | |
BLUE_LEDS = [21,20,16,12] | |
GREEN_LEDS = [18, 23, 24, 25] | |
VOLUMEUP = 7 | |
VOLUMEDOWN = 8 | |
CHANGE_SOUNDSCHEME_BUTTON = 14 | |
COLOR_BUTTON = 15 | |
GPIO.setup(DRUMS, GPIO.IN) | |
GPIO.setup(RED_LEDS, GPIO.OUT) | |
GPIO.setup(GREEN_LEDS, GPIO.OUT) | |
GPIO.setup(BLUE_LEDS, GPIO.OUT) | |
GPIO.setup(VOLUMEUP,GPIO.IN) | |
GPIO.setup(VOLUMEDOWN, GPIO.IN) | |
GPIO.setup(CHANGE_SOUNDSCHEME_BUTTON, GPIO.IN) | |
base_color = 'dark' | |
#kept low for testing, initial will probably 0.5 in the future | |
volume = 0.02 | |
high = 0 | |
low = 1 | |
#this list indicates whether each drum is hit or not, initially they're all low | |
drum_states = [low, low, low, low] | |
#turns volume up at each press of vol up button | |
def volumeUp(): | |
global volume | |
if GPIO.input(VOLUMEUP) == high: | |
volume += 0.01 | |
round(volume, 2) | |
#turns volume up at each press of vol down button | |
def volumeDown(): | |
global volume | |
if GPIO.input(VOLUMEDOWN) == high: | |
if volume > 0.01: | |
volume -= 0.01 | |
round(volume, 2) | |
#shuts of all RGB LEDs completely, 'dark' means off | |
def allLedsOff(): | |
for light in BLUE_LEDS: | |
GPIO.output(light, False) | |
for light in GREEN_LEDS: | |
GPIO.output(light, False) | |
for light in RED_LEDS: | |
GPIO.output(light, False) | |
#turns on selected LED to indicated color | |
def pinColor(led_index, color): | |
if color == 'white': | |
GPIO.output(RED_LEDS[led_index], True) | |
GPIO.output(GREEN_LEDS[led_index], True) | |
GPIO.output(BLUE_LEDS[led_index], True) | |
if color == 'dark': | |
GPIO.output(RED_LEDS[led_index], False) | |
GPIO.output(GREEN_LEDS[led_index], False) | |
GPIO.output(BLUE_LEDS[led_index], False) | |
if color == 'purple': | |
GPIO.output(RED_LEDS[led_index], True) | |
GPIO.output(GREEN_LEDS[led_index], False) | |
GPIO.output(BLUE_LEDS[led_index], True) | |
if color == 'red': | |
GPIO.output(RED_LEDS[led_index], True) | |
GPIO.output(GREEN_LEDS[led_index], False) | |
GPIO.output(BLUE_LEDS[led_index], False) | |
if color == 'blue': | |
GPIO.output(RED_LEDS[led_index], False) | |
GPIO.output(GREEN_LEDS[led_index], False) | |
GPIO.output(BLUE_LEDS[led_index], True) | |
if color == 'teal': | |
GPIO.output(RED_LEDS[led_index], False) | |
GPIO.output(GREEN_LEDS[led_index], True) | |
GPIO.output(BLUE_LEDS[led_index], True) | |
if color == 'yellow': | |
GPIO.output(RED_LEDS[led_index], True) | |
GPIO.output(GREEN_LEDS[led_index], True) | |
GPIO.output(BLUE_LEDS[led_index], False) | |
if color == 'green': | |
GPIO.output(RED_LEDS[led_index], False) | |
GPIO.output(GREEN_LEDS[led_index], True) | |
GPIO.output(BLUE_LEDS[led_index], False) | |
#this are the colorschemes, each drum lights up with its respective color in the list | |
colorscheme1 = ['red', 'green', 'blue', 'purple'] | |
colorscheme = [colorscheme1] | |
current_colorscheme_index = 0 | |
#Next the soundfiles for the drums are imported; | |
#these are arbitrary Sonic Pi .wav files that will be organized in the future | |
#explore Sonic Pi audio files in the /opt/sonic-pi/etc/samples dir | |
#d1_s1 represents the first sound of drum1 | |
d1_s1 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/elec_flip.wav') | |
d1_s2 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/ambi_piano.wav') | |
d1_s3 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/ambi_swoosh.wav') | |
d2_s1 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/elec_beep.wav') | |
d2_s2 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/elec_bell.wav') | |
d2_s3 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/elec_blup.wav') | |
d3_s1 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/perc_bell.wav') | |
d3_s2 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/perc_snap.wav') | |
d3_s3 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/perc_snap2.wav') | |
d4_s1 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/elec_snare.wav') | |
d4_s2 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/elec_pop.wav') | |
d4_s3 = pygame.mixer.Sound('/opt/sonic-pi/etc/samples/elec_tick.wav') | |
#these are the soundschemes | |
soundscheme1 = [d1_s1, d2_s1, d3_s1, d4_s1] | |
soundscheme2 = [d1_s2, d2_s2, d3_s2, d4_s2] | |
soundscheme3 = [d1_s3, d2_s3, d3_s3, d4_s3] | |
#all sound files are accessed through this list | |
soundschemes = [soundscheme1, soundscheme2, soundscheme3] | |
current_soundscheme_index = 1 | |
channels = [pygame.mixer.Channel(0), pygame.mixer.Channel(1), pygame.mixer.Channel(2), pygame.mixer.Channel(4)] | |
#changes the current colorscheme | |
def changeColorscheme(button): | |
global current_colorscheme_index | |
inc = 0 | |
if GPIO.input(button) == high: | |
if current_colorscheme_index <= 0: | |
inc = 1 | |
if current_colorscheme_index >= len(colorschemes)-1: | |
inc = -1 | |
current_colorscheme_index += inc | |
#changes the current soundscheme | |
def changeSoundScheme(soundscheme_button): | |
global current_soundscheme_index | |
inc = 0 | |
if GPIO.input(soundscheme_button) == high: | |
if current_soundscheme_index <= 0: | |
inc = 1 | |
if current_soundscheme_index >= len(soundschemes)-1: | |
inc = -1 | |
current_soundscheme_index += inc | |
#determines if drum is being tapped, turns on LEDs and plays audio | |
def checkDrums(): | |
global volume | |
for drum in DRUMS: | |
if GPIO.input(drum) == high and drum_states[DRUMS.index(drum)] == low: | |
for channel in channels: | |
channel.set_volume(volume) | |
channels[DRUMS.index(drum)].play((soundschemes[current_soundscheme_index])[DRUMS.index(drum)]) | |
pinColor(DRUMS.index(drum), colorscheme[DRUMS.index(drum)]) | |
else: | |
if drum_states[DRUMS.index(drum)] == high: | |
pinColor(DRUMS.index(drum), colorscheme[DRUMS.index(drum)]) | |
else: | |
pinColor(DRUMS.index(drum), base_color) | |
drum_states[DRUMS.index(drum)] = GPIO.input(drum) | |
#main loop | |
try: | |
while True: | |
checkDrums() | |
volumeUp() | |
volumeDown() | |
changeSoundScheme(CHANGE_SOUNDSCHEME_BUTTON) | |
#debugging print(current_soundscheme_index) | |
#debugging print(volume) | |
time.sleep(0.1) | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
pygame.mixer.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment