#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function, division | |
from pygame.locals import * | |
import os | |
import pygame | |
import random | |
import serial | |
import sys | |
import time | |
import RPi.GPIO as GPIO | |
pins = { | |
2: 'amin', | |
3: 'bmin', | |
4: 'cmin', | |
5: 'dmin', | |
6: 'emin', | |
7: 'fmin', | |
8: 'gmin', | |
9: 'amaj', | |
10: 'bmaj', | |
11: 'cmaj', | |
12: 'dmaj', | |
13: 'emaj', | |
14: 'fmaj', | |
15: 'gmaj', | |
16: 'clean', | |
17: 'dirty', | |
} | |
chords = { | |
'amaj': [0, 0, 2, 2, 2, 0], | |
'bmaj': [2, 2, 4, 4, 4, 2], | |
'cmaj': [3, 3, 2, 0, 1, 0], | |
'dmaj': [0, 0, 0, 2, 3, 2], | |
'emaj': [0, 2, 2, 1, 0, 0], | |
'fmaj': [1, 3, 3, 2, 1, 1], | |
'gmaj': [3, 2, 0, 0, 3, 3], | |
'amin': [0, 0, 2, 2, 1, 0], | |
'bmin': [2, 2, 4, 4, 3, 2], | |
'cmin': [3, 1, 3, 3, 2, 1], | |
'dmin': [0, 0, 0, 2, 3, 1], | |
'emin': [0, 0, 0, 0, 0, 0], | |
'fmin': [0, 2, 2, 0, 0, 0], | |
'gmin': [3, 2, 0, 0, 3, 0], | |
} | |
GPIO.setmode(GPIO.BCM) | |
NUM_STRINGS = 6 | |
NUM_FRETS = 5 | |
for pin in pins: | |
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
ser = serial.Serial('/dev/ttyACM0') | |
pygame.mixer.pre_init(44100, -16, 2, 2048) | |
pygame.init() | |
sounds = { | |
'clean': [[0 for x in range(NUM_FRETS)] for y in range(NUM_STRINGS)], | |
'dirty': [[0 for x in range(NUM_FRETS)] for y in range(NUM_STRINGS)], | |
} | |
for sound in sounds: | |
for string in range(NUM_STRINGS): | |
for fret in range(NUM_FRETS): | |
filename = sound + '-' + str(string) + '-' + str(fret) + '.wav' | |
sounds[sound][string][fret] = pygame.mixer.Sound( | |
os.path.join('audio', filename)) | |
sounds[sound][string][fret].set_volume(0.1) | |
chord = chords[random.choice(chords.keys())] | |
mode = 'dirty' | |
while True: | |
for pin in range(2, 18): | |
if GPIO.input(pin) == GPIO.LOW and pin < 16: | |
chord = chords[pins[pin]] | |
elif GPIO.input(pin) == GPIO.LOW and pin > 15: | |
mode = pins[pin] | |
if ser.inWaiting() > 2: | |
serial_data = ser.read(3) | |
data_event = int(serial_data.split('-')[0]) | |
data_string = int(serial_data.split('-')[-1]) | |
if data_event is 0: | |
sounds[mode][data_string][chord[data_string]].play() | |
elif data_event is 1: | |
for fret in range(NUM_FRETS): | |
for m in sounds: | |
sounds[m][data_string][fret].fadeout(250) | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment