I'm trying to set it up so that I can record some audio using RPi.GPIO and pyaudio using a button press to determine the record time (hold to record). However, whenever I press the button, the program just quits with no errors.
I'm using this gist to have the recorder set up/taken down.
This code works fine, records for 5 seconds, and then quits. The audio is saved to the file fine.
from recorder import Recorder
import time
rec = Recorder(channels=2)
recfile = rec.open('nonblocking.wav', 'wb')
recfile.start_recording()
time.sleep(5.0)
recfile.stop_recording()
recfile.close()
This code also works fine, it says "button down" when I press the button, and "button up" when I let it go:
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(23, gpio.IN, pull_up_down=gpio.PUD_UP)
def rising(channel):
gpio.remove_event_detect(23)
print 'Button up'
gpio.add_event_detect(23, gpio.FALLING, callback=falling, bouncetime=10)
def falling(channel):
gpio.remove_event_detect(23)
print 'Button down'
gpio.add_event_detect(23, gpio.RISING, callback=rising, bouncetime=10)
gpio.add_event_detect(23, gpio.FALLING, callback=falling, bouncetime=10)
try:
raw_input()
except KeyboardInterrupt:
gpio.cleanup()
gpio.cleanup()
However, when I put the 2 together:
import RPi.GPIO as gpio
from recorder import Recorder
gpio.setmode(gpio.BCM)
gpio.setup(23, gpio.IN, pull_up_down=gpio.PUD_UP)
rec = Recorder(channels=2)
recfile = rec.open('nonblocking.wav', 'wb')
def rising(channel):
gpio.remove_event_detect(23)
print 'Button up'
gpio.add_event_detect(23, gpio.FALLING, callback=falling, bouncetime=10)
recfile.stop_recording()
recfile.close()
def falling(channel):
gpio.remove_event_detect(23)
print 'Button down'
gpio.add_event_detect(23, gpio.RISING, callback=rising, bouncetime=10)
recfile.start_recording()
gpio.add_event_detect(23, gpio.FALLING, callback=falling, bouncetime=10)
try:
raw_input()
except KeyboardInterrupt:
gpio.cleanup()
gpio.cleanup()
It just says "button down" when I press the button, then quits, with no errors or anything. Nothing gets recorded to the file.
I'm super new to python, so I'm sure that there's something simple I'm doing wrong, but I can't figure out what it is.