0

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. Is there a way to clear out the recording buffer?

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()
2
  • ultimately you can use standard open( ) with write mode (and later close it) and it should delete all data in file. Commented Oct 29 at 20:49
  • did you delete content in frames? It may still have value from previous recording and it may write again it in file. Commented Oct 29 at 20:51

3 Answers 3

1

I can't test it but as for me problem is that you don't delete content in frames and it still have values from previous recording(s).

So wave.open() may delete old content in file on disk but you write again old values from frames and this way you get 1234 and 567 in file.

You many need frames.clear() before every recording.

    # Record loop
   
    frames.clear()

    while recording:
        # ... rest ...
Sign up to request clarification or add additional context in comments.

Comments

0

It’s with your frames list. frames is GLOBAL and never cleared. So every time you start recording again, it keeps appending new audio chunks to the old ones.

global frames
frames.append(data)

Then, when you write the file:

wf.writeframes(b''.join(frames))

it writes all old + new frames into the same file.

To solution to this problem is clear frames at the start of every new recording:

global frames, recording, enroll, WAVE_OUTPUT_FILENAME
frames = [] ### Clear buffer for a new recording

Comments

0

I had frames = [] at the begning of my file I moved it into def record_audio(0 and that worked.

new code

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
    frames = []
    recording = True
    file_dir = "./queue-broker/enroll-recordings/enroll_"
    WAVE_OUTPUT_FILENAME =  file_dir+datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")+".wav"
    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 = 15
    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()

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.