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 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)
sd.wait()
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.
The following is the code that I have been using to play a test for the host message when the handset is picked up.
import RPi.GPIO as gpio
import pygame
gpio.setmode(gpio.BCM)
gpio.setup(23, gpio.IN, pull_up_down=gpio.PUD_UP)
gpio.setwarnings(False)
def rising(channel):
gpio.remove_event_detect(23)
print ('Button up')
pygame.mixer.init()
sound = pygame.mixer.Sound('/home/pi/Documents/E and R/test1.wav')
playing = sound.play()
while playing.get_busy():
pygame.time.delay(100)
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()
There are a few problems with this, I have tried changing the order of various aspects and either when you lift the handset and the audio is played it doesn't read if the handset is replaced while the audio is playing. Or in another version if you tap the handset switch a bunch of times it layers copies of the audio on top of each other.
Also I know that raw_input() is not used in python 3 but if I just use input() the code doesn't work and if I omit the whole section it doesn't work.
Basically, I'm illiterate and any help smashing this all together and showing me how to get audio recordings for an in-determinant length of time would be greatly appreciated.
Also if this helps I'm using a Raspberry pi 4 and Python 3.9.2.