From e6a81089de233fad279a8834a25dc27188d52a17 Mon Sep 17 00:00:00 2001 From: John Edgar Park Date: Wed, 25 Jul 2018 15:12:39 -0700 Subject: [PATCH] first commit Screaming Painting code --- Screaming_Painting/screaming_painting.py | 69 ++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Screaming_Painting/screaming_painting.py diff --git a/Screaming_Painting/screaming_painting.py b/Screaming_Painting/screaming_painting.py new file mode 100644 index 00000000..5574072c --- /dev/null +++ b/Screaming_Painting/screaming_painting.py @@ -0,0 +1,69 @@ +import time +import math +import array +import audiobusio +import audioio +import board +from adafruit_crickit import crickit + +# Number of samples to read at once. +NUM_SAMPLES = 160 + +# Remove DC bias before computing RMS. +def normalized_rms(values): + minbuf = int(mean(values)) + samples_sum = sum( + float(sample - minbuf) * (sample - minbuf) + for sample in values + ) + + return math.sqrt(samples_sum / len(values)) + +def mean(values): + return sum(values) / len(values) + +mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, + sample_rate=16000, bit_depth=16) + +# Record an initial sample to calibrate. Assume it's quiet when we start. +samples = array.array('H', [0] * NUM_SAMPLES) +mic.record(samples, len(samples)) + +head_servo = crickit.servo_1 +head_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500) +head_servo.angle = 90 # center the head. + +# Set audio out on speaker. +a = audioio.AudioOut(board.A0) + +# Start playing the file (in the background). +def play_file(wavfile): + print("Playing scream!") + with open(wavfile, "rb") as f: + wav = audioio.WaveFile(f) + a.play(wav) + while a.playing: + head_servo.angle = 60 + time.sleep(.01) + head_servo.angle = 120 + time.sleep(.01) + + +while True: + mic.record(samples, len(samples)) + magnitude = normalized_rms(samples) + print(((magnitude),)) # formatting is for the Mu plotter. + + if magnitude < 1000: # it's quiet, do nothing. + pass + else: + print("LOUD") + head_servo.angle = 60 + time.sleep(.05) + head_servo.angle = 120 + time.sleep(.05) + head_servo.angle = 90 + time.sleep(.02) + play_file("scream_low.wav") + head_servo.angle = 90 + time.sleep(2)