-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduckmid2_parallel_audio.py
65 lines (54 loc) · 2.33 KB
/
duckmid2_parallel_audio.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
from adafruit_circuitplayground import cp
import time
import random
import board
import audiocore
import audiopwmio
import digitalio
#https://docs.circuitpython.org/en/latest/shared-bindings/audiopwmio/index.html
#https://learn.adafruit.com/make-it-talk/circuitpython
thresh = 2 #change in acceleration threshold
x0, y0, z0 = 0, 0, 0 #previous acceleration value
duck = ["duck1.wav", "duck2.wav", "duck3.wav"] #duck sounds on CIRCUITPY
lasttime = time.monotonic() #gets current time
timer = 10 #quack ever __ seconds
last = 0 #records last quack time
RED = (255, 0, 0)
ORANGE = (255, 51, 0)
YELLOW = (255, 153, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (153, 0, 255)
rainbow = (RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PURPLE)
data = open("duck1.wav", "rb")
wav = audiocore.WaveFile(data)
a = audiopwmio.PWMAudioOut(board.SPEAKER)
quacking = 0
def sound():
i = random.randint(0, len(duck) - 1) #choose random duck sound
data = open(duck[i], "rb") #load duck sound
wav = audiocore.WaveFile(data) #load duck sound
while not a.playing:
a.play(wav) #play audio if audio isn't already playing
quacking = 0 #quacking used to turn off/on sparkle lights
while a.playing:
quacking = 1
while True:
## Sparkle 1 of 10 neopixels with random colors
if quacking == 0 : #sparkle only when there's no quacking sound active
pix = random.randint(0, 9) #choose 1 of 10 neopixels
color = random.randint(0, len(rainbow) - 1) #choose random color from rainbow
cp.pixels[pix] = rainbow[color] #display color
cp.pixels.fill((0, 0, 0)) #clear color
## Check for sudden acceleration
x1, y1, z1 = cp.acceleration
if x1 - x0 > thresh or y1 - y0 > thresh or z1 - z0 > thresh:
cp.pixels.fill((255, 0, 0)) #shine red
sound() #play sound
x0, y0, z0 = cp.acceleration #set last accel to current accel
## Quack every (timer) seconds
if time.monotonic() - lasttime > timer:
cp.pixels.fill((0, 0, 255)) #shine blue
sound() #play sound
lasttime = time.monotonic() #restart timer count down