forked from BareConductive/picap-touch-mp3-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
touch-mp3.py
51 lines (41 loc) · 1.21 KB
/
touch-mp3.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
import MPR121
from gpiozero import RGBLED
import subprocess
import pygame
from pygame.mixer import Sound
from glob import glob
from time import sleep
sensor = MPR121.begin()
sensor.set_touch_threshold(40)
sensor.set_release_threshold(20)
led = RGBLED(6, 5, 26, active_high=False)
electrodes = range(12)
# convert mp3s to wavs with picap-samples-to-wav
led.blue = 1
subprocess.call("picap-samples-to-wav tracks", shell=True)
led.off()
# initialize mixer and pygame
pygame.mixer.pre_init(frequency=44100, channels=64, buffer=1024)
pygame.init()
sounds = [Sound(path) for path in glob("tracks/.wavs/*.wav")]
def play_sounds_when_touched():
if sensor.touch_status_changed():
sensor.update_touch_data()
touched = [sensor.get_touch_data(e) for e in electrodes]
new_touched = [e for e in electrodes if sensor.is_new_touch(e)]
for e in new_touched:
print("playing sound: {}".format(e))
sound = sounds[e]
sound.play()
if any(touched):
led.red = 1
else:
led.off()
running = True
while running:
try:
play_sounds_when_touched()
except KeyboardInterrupt:
led.off()
running = False
sleep(0.01)