Formatting
[pi-music.git] / keyboard.py
1 import pygame
2 import RPi.GPIO as gpio
3
4 gpio.setmode(gpio.BCM)
5 if gpio.RPI_REVISION == 1:
6 pins = [22, 21, 17, 4, 25, 24, 23, 18]
7 else:
8 pins = [22, 27, 17, 4, 25, 24, 23, 18]
9
10 notes = ['sounds/keyboard-g.wav',
11 'sounds/keyboard-a.wav',
12 'sounds/keyboard-b.wav',
13 'sounds/keyboard-c.wav',
14 'sounds/keyboard-d.wav',
15 'sounds/keyboard-e.wav',
16 'sounds/keyboard-f.wav',
17 'sounds/keyboard-g-high.wav']
18
19 pygame.mixer.init()
20
21 sounds = {}
22 for pin, wav in zip(pins, notes):
23 sounds[pin] = pygame.mixer.Sound(wav)
24
25 def handle_sound(pin):
26 if gpio.input(pin):
27 sounds[pin].play()
28 else:
29 sounds[pin].stop()
30
31 for pin in pins:
32 gpio.setup(pin, gpio.IN)
33 gpio.add_event_detect(pin, gpio.BOTH, callback=handle_sound,
34 bouncetime=50)
35
36 while True:
37 pass