Skip to content

Commit b6d781e

Browse files
authored
Add Sparky automaton audio playback functionality
1 parent 32cc398 commit b6d781e

File tree

1 file changed

+51
-0
lines changed
  • Crickits/Crickit_Sparky_Automaton

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os
6+
import time
7+
import random
8+
import board
9+
import audioio
10+
import audiocore
11+
from adafruit_crickit import crickit
12+
13+
# Sparky automaton
14+
15+
# Find all Wave files on the storage
16+
wavefiles = [file for file in os.listdir("/")
17+
if (file.endswith(".wav") and not file.startswith("._"))]
18+
print("Audio files found: ", wavefiles)
19+
20+
# mouth servo
21+
mouth_servo = crickit.servo_1
22+
# TowerPro servos like 500/2500 pulsewidths
23+
mouth_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
24+
25+
# Servo angles
26+
MOUTH_START = 100
27+
MOUTH_END = 90
28+
29+
# Starting servo location
30+
mouth_servo.angle = MOUTH_START
31+
32+
# Audio playback object and helper to play a full file
33+
a = audioio.AudioOut(board.A0)
34+
35+
# Play a wave file and move the mouth while its playing!
36+
def play_file(wavfile):
37+
print("Playing", wavfile)
38+
with open(wavfile, "rb") as f:
39+
wav = audiocore.WaveFile(f)
40+
a.play(wav)
41+
while a.playing: # turn servos, motors, etc. during playback
42+
mouth_servo.angle = MOUTH_END
43+
time.sleep(0.15)
44+
mouth_servo.angle = MOUTH_START
45+
time.sleep(0.15)
46+
47+
while True:
48+
# Play a random quip
49+
play_file(random.choice(wavefiles))
50+
# then hang out for a few seconds
51+
time.sleep(3)

0 commit comments

Comments
 (0)