我有一个 Raspberry Pi,带有 pyaudio 脚本。我正在录音,当我键盘中断时(通常这将是另一种关闭方式,但我键盘中断是出于演示目的)无论如何,当我键盘中断时,它会继续导出。关于如何阻止它这样做有什么想法吗?
我的代码:
import pyaudio
import time
import wave
import RPi.GPIO as GPIO
from datetime import datetime
rg = False #rg is short for recording if rg is false then I'm not recording
# Configuration
BUTTON_PIN = 27 # GPIO pin number for the button
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 0 # Not used directly in this case
# Initialize PyAudio
audio = pyaudio.PyAudio()
# GPIO setup
GPIO.setmode(GPIO.BCM)
#GPIO.setwarnings(False)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_PIN_TWO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(10, GPIO.OUT) # green
GPIO.setup(9, GPIO.OUT) # yellow
GPIO.setup(11, GPIO.OUT) # red
def record_audio(filename):
global stream, frames, rg
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True, frames_per_buffer=CHUNK)
GPIO.output(11, GPIO.HIGH)
print("Recording started")
rg = True #i'm recording
frames = []
while GPIO.input(BUTTON_PIN) == GPIO.LOW: #if button is being held down, record
data = stream.read(CHUNK)
frames.append(data)
close_recording(stream)
write_file(filename, frames)
def close_recording(recording):
global rg
recording.stop_stream()
recording.close()
GPIO.output(11, GPIO.LOW)
print("Recording stopped")
rg = False #i'm not
def write_file(filename, frames):
global rg
# Save to WAV file
wf = wave.open(filename, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
GPIO.output(9, GPIO.HIGH)
time.sleep(1)
GPIO.output(9, GPIO.LOW)
print(f"Audio saved to {wav_filename}")
def main():
global wav_filename, rg
try:
#light sequence to show that device turned on
GPIO.output(10, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(10, GPIO.LOW)
GPIO.output(9, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(9, GPIO.LOW)
GPIO.output(11, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(11, GPIO.LOW)
GPIO.output(9, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(9, GPIO.LOW)
GPIO.output(10, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(10, GPIO.LOW)
GPIO.output(10, GPIO.HIGH)
while True:
if GPIO.input(BUTTON_PIN) == GPIO.LOW: #when button is held down record
rg = True
# Button pressed: Record audio
timestamp = datetime.now().strftime("%m-%d-%Y_%H-%M-%S")
wav_filename = f"/home/pi/Desktop/Audio_Files/{timestamp}.wav"
record_audio(wav_filename)
# Wait a bit to avoid multiple recordings from a single press
time.sleep(1)
else:
rg = False
time.sleep(0.1) # Polling delay
except KeyboardInterrupt:
print("Exiting...")
except:
#flashing light to show error
time.sleep(1)
GPIO.output(9, GPIO.LOW)
GPIO.output(11, GPIO.LOW)
t_end = time.time() + 15 * 1
while time.time() < t_end:
time.sleep(0.3)
GPIO.output(9, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(9, GPIO.LOW)
time.sleep(0.3)
finally:
if(rg == True):
#rg = False
close_recording(stream)
write_file(wav_filename, frames)
audio.terminate()
GPIO.output(11, GPIO.LOW)
GPIO.output(10, GPIO.LOW)
GPIO.output(9, GPIO.LOW)
GPIO.cleanup()
else:
time.sleep(0.1)
audio.terminate()
GPIO.output(11, GPIO.LOW)
GPIO.output(10, GPIO.LOW)
GPIO.output(9, GPIO.LOW)
GPIO.cleanup()
if __name__ == "__main__":
main()
非常感谢任何帮助,我已经被困了两个月了。
except KeyboardInterrupt:
print("Exiting...")
防止其退出。将
return
语句添加到 except:
块。
except KeyboardInterrupt:
print("Exiting...")
return