Skip to content

Commit

Permalink
add ability to read from serial port to trigger script
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Bachmann committed Oct 8, 2014
1 parent d57bf9b commit fd2e02c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ I don't own the audio. I think I can make an argument for fair use but I can tak
To install:
Install homebrew
brew install portaudio
pip install --allow-external pyaudio pyaudio --allow-unverified pyaudio
pip install -r requirements.txt


running python sort.py builds and plays a script randomly generated from
some stalling lines a potential announcement of 'I KNOW', then finally the house.

Only the house is guaranteed, any other sound is random

Pass in a serial port and the script will read from it. If it gets ```YEP``` the script will play. Once it is done the script will block until something other than ```YEP``` is read.

And yes, the ravenclaw sound is kinda lame... sorry.. the hat does not seem to say it in the movie
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PyAudio==0.2.8
pyserial==2.7
51 changes: 35 additions & 16 deletions sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import wave
import time
import pyaudio
import serial
import sys

CHUNK = 1024

BASE_AUDIO_DIR = "audio"
BASE_AUDIO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "audio")

WAIT_TIME = .25

Expand Down Expand Up @@ -63,22 +65,22 @@ def play_sound(sound_file_path):
:param sound_file_path:
string path
"""
with wave.open(sound_file_path, 'rb') as wf:
p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
wf = wave.open(sound_file_path, 'rb')
p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(CHUNK)
while data:
stream.write(data)
data = wf.readframes(CHUNK)
while data:
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
p.terminate()

if __name__ == '__main__':
stream.stop_stream()
p.terminate()
wf.close()

def generate_script():
# Consider some stalling lines
script = list_all_sound_files('stalling')
random.shuffle(script)
Expand All @@ -92,7 +94,24 @@ def play_sound(sound_file_path):

# Pick a house
script.append(get_random_wav_file('houses'))
return script

def play_script(script):
for sound in script:
play_sound(sound)
time.sleep(WAIT_TIME)
time.sleep(WAIT_TIME)

if __name__ == '__main__':
# If a port was passed in we must be using a external device to trigger the
# script. Read from the device serial port
if len(sys.argv) > 1:
running = False
ser = serial.Serial(sys.argv[1], 115200)
while 1:
if ser.readline().strip() == 'YEP':
running = True
play_script(generate_script())
while ser.readline().strip() == 'YEP':
continue
else:
play_script(generate_script())

0 comments on commit fd2e02c

Please sign in to comment.