1

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.

1

A coworker helped me figure it out, you need to setup the recorder just before listening, and then make the recfile variable a global:

import RPi.GPIO as gpio
from recorder import Recorder
gpio.setmode(gpio.BCM)

gpio.setup(23, gpio.IN, pull_up_down=gpio.PUD_UP)



def rising(channel):
    global recfile
    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):
    global recfile
    gpio.remove_event_detect(23)
    print 'Button down'
    gpio.add_event_detect(23, gpio.RISING, callback=rising, bouncetime=10)
    rec = Recorder(channels=2)
    recfile = rec.open('nonblocking.wav', 'wb') 
    recfile.start_recording()

gpio.add_event_detect(23, gpio.FALLING, callback=falling, bouncetime=10)

try:
    raw_input()

except KeyboardInterrupt:
    gpio.cleanup()
gpio.cleanup()

Of course, there's a better way than using globals and that'd be creating a class:

import RPi.GPIO as gpio 
from recorder import Recorder 
gpio.setmode(gpio.BCM)  

class ButtonRecorder(object): 
    def __init__(self, filename): 
        self.filename = filename 
        gpio.setup(23, gpio.IN, pull_up_down=gpio.PUD_UP) 
        self.rec = Recorder(channels=2) 

    def start(self): 
        gpio.add_event_detect(23, gpio.FALLING, callback=self.falling, bouncetime=10) 

    def rising(self, channel): 
        gpio.remove_event_detect(23) 
        print 'Button up' 
        gpio.add_event_detect(23, gpio.FALLING, callback=self.falling, bouncetime=10) 
        self.recfile.stop_recording() 
        self.recfile.close() 

    def falling(self, channel): 
        gpio.remove_event_detect(23) 
        print 'Button down' 
        gpio.add_event_detect(23, gpio.RISING, callback=self.rising, bouncetime=10) 
        self.recfile = self.rec.open(self.filename, 'wb')    
        self.recfile.start_recording() 

rec = ButtonRecorder('nonblocking.wav')
rec.start() 

try: 
    raw_input() 

except KeyboardInterrupt: 
    pass 

gpio.cleanup()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.