-
Notifications
You must be signed in to change notification settings - Fork 0
/
marche_piano.py
executable file
·99 lines (85 loc) · 3.49 KB
/
marche_piano.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
import time
import pygame
import RPi.GPIO as GPIO
class Piano:
def __init__(self, ports, switch_time = 3600):
self.auto_change = True
self.switch_time = switch_time
self.sound_sets = os.listdir("sounds")
self.current_sound_set_index = 0
pygame.mixer.pre_init(channels=2, buffer=1024)
pygame.mixer.init()
self.prepare_notes()
#GPIO ports
GPIO.setmode(GPIO.BCM)
self.push_button = ports[0]
GPIO.setup(self.push_button, GPIO.IN)
self.LED_button = ports[1]
GPIO.setup(self.LED_button, GPIO.OUT, initial=1)
self.step_ports = ports[2:]
for port in self.step_ports:
GPIO.setup(port, GPIO.IN)
def prepare_notes(self):
# Empty the note variable
self.notes = []
# List the file of the directory corresponding to the current index
folder = self.sound_sets[self.current_sound_set_index]
notefile_list = os.listdir(folder)
# Load all the sounds in the class variable
for note in notefile_list:
if note.endswith(".wav"):
self.notes.append(pygame.mixer.Sound(os.path.join(folder, note)))
self.last_changed_time = time.time()
def handle_sound_set_change(self):
current_time = time.time()
if self.auto_change and current_time-self.last_changed_time >= self.switch_time:
self.current_sound_set_index = (self.current_sound_set_index + 1)%len(self.sound_sets)
self.prepare_notes()
def handle_buttons(self):
if GPIO.input(self.push_button):
if self.auto_change:
self.auto_change = False
self.current_sound_set_index = 0
self.prepare_notes()
else:
self.current_sound_set_index = (self.current_sound_set_index + 1)%len(self.sound_sets)
self.prepare_notes()
if self.current_sound_set_index == 0: # Case all the sets have been done, reactivate the auto switch
self.auto_change = True
def main(self):
while True:
# Part for switching between sounds sets
self.handle_sound_set_change()
self.handle_buttons()
# Part for the GPIO inputs
for port in self.step_ports:
if GPIO.input(port):
self.notes[self.step_ports.index(port)].play()
# Part for the LED
GPIO.output(self.LED_button, self.auto_change)
class PianoEdge(Piano):
def __init__(self, ports, switch_time=3600):
super().__init__(ports, switch_time=switch_time)
for port in self.step_ports:
GPIO.add_event_detect(port, GPIO.RISING, callback=self.play_channel)
def play_channel(self, channel):
self.notes[self.step_ports.index(channel)].play()
def main(self):
while True:
# Part for switching between sounds sets
self.handle_sound_set_change()
self.handle_buttons()
# Part for the LED
GPIO.output(self.LED_button, self.auto_change)
time.sleep(0.5) # to avoid too many loops
if __name__ == "__main__":
PORTS = []
try:
PIANO = PianoEdge(PORTS)
PIANO.main()
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit