I have pyaudio starting a recording when a GPIO button is pressed then stops the recording when the GPIO button is pressed again, any reason why it's appending the audio to the file. I want a new file with the new audio, it can overwrite output.wav.
So for example If i press the button say 1234, press the button it creates output.wav, if I press the button again and say 567, press the button again to stop it does not overwrite output.wav with 567 it appends it to the 1234 first recording, so then I get output.wav with the audio 1234 then 567 in it.
def record_audio():
draw.rectangle((0,0,disp.width,disp.height), outline=0, fill=0)
disp.LCD_ShowImage(image,0,0)
global frames, recording, enroll, WAVE_OUTPUT_FILENAME
recording = True
# Initialize PyAudio
audio = pyaudio.PyAudio()
# Open stream
stream = audio.open(format=FORMAT,
input_device_index=10,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("Recording... Press Enter to stop.")
# Record loop
while recording:
data = stream.read(CHUNK, exception_on_overflow = False)
frames.append(data)
if disp.digital_read(disp.GPIO_KEY_PRESS_PIN ) == 1:
print("recording stopping")
recording = False
time.sleep(1)
print("Recording stopped.")
# Stop and close stream
stream.stop_stream()
stream.close()
audio.terminate()
# Save to file
with wave.open(WAVE_OUTPUT_FILENAME, 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
print(f"Saved recording as {WAVE_OUTPUT_FILENAME}")
# Wait for Enter key
time.sleep(.5)
enroll = 4
main()
# Thread to handle recording
recording_thread = threading.Thread(target=record_audio)
recording_thread.start()
input() # Press Enter to stop
recording = False
recording_thread.join()