I am trying to make wedding guest answer phone for my sister. The basic idea of these things is you pick up the handset, hear a message from the host, leave you own, replace the handset, and the recording stops and saves. I have set all of the hardware up fine, it works, but as I have essentially no experience with coding getting all of the individual aspects to work is my issue.

For the recording aspect I tried a few different ways, they all ended up giving error codes that I couldn't find fixes for or crashing. Scipy seems to work the best, I have been using this for 5 second test recordings.

import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv
import uuid
freq = 44100

duration = 5
recording = sd.rec(int(duration * freq), 
                   samplerate=freq, channels=2)
write("recording0_"+str(uuid.uuid4())+".wav", freq, recording)

The main issue is these messages are only 5 seconds, I don't know how to make them start recording after the host message and stop when the handset is replaced. Does anyone know how to modify this to make it record whilst a button is pressed?

If this helps I'm using a Raspberry pi 4 and Python 3.9.2.

  • what is your specific, answerable question? ... this is a Q&A site, not a tutorial forum
    – jsotola
    Sep 10, 2022 at 19:28
  • Thanks, I've made it more specific
    – hieronopro
    Sep 11, 2022 at 9:10

1 Answer 1


This example may help you. There they are catching KeyboardInterrupt to stop the continuous recording. What you may do is: using while not button_pressed(): instead of while True: in the example, where button_pressed() should be a function checking button state and outputting True if button is pressed (to stop the recording). The difficulty can be however, if you miss the moment when the button is pressed, if it is released quickly after pressing. But if I understood your application well, this won't be the case. Otherwise you may need to consider running checking of the button state in a separate thread (check threading module examples) and use an event to connect the thread to your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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