-1

I am trying to implement a piece of code to record voice from a microphone and control this from a bluetooth device by pressing two different buttons for star and stop. i am able to detect button (KEY_PAGEDOWN) press to start the recording, but when i press the other button to stop the recording and save to a .wav file, it does not work because the inner While loop keeps running forever and it does not get interrupted when i press the stop button (KEY_PAGEUP).

Any help is much appreciated

for event in bluetooth_device.read_loop(): if event.type == ecodes.EV_KEY and event.value == 1: keyevent = categorize(event) if keyevent.keycode == 'KEY_PAGEDOWN': recorder.start() print("Recording... press > to stop") while True: frame = recorder.read() audio.extend(frame) if keyevent.keycode == 'KEY_PAGEUP': recorder.stop() # save the audio to a wave file with wave.open("audio.wav", 'w') as f: f.setparams((1, 2, 16000, 0, "NONE", "NONE")) f.writeframes(struct.pack("h" * len(audio), *audio)) 

And to shed more light on what i am trying to do, the below code works fine on a try except block, but in my case i want to control the recording using a bluetooth device buttons

try: recorder.start() print("Recording... press Ctrl+C to stop") while True: frame = recorder.read() audio.extend(frame) except KeyboardInterrupt: print("Recording stopped") recorder.stop() # save the audio to a wave file with wave.open("audio.wav", 'w') as f: f.setparams((1, 2, 16000, 0, "NONE", "NONE")) f.writeframes(struct.pack("h" * len(audio), *audio)) print("Audio saved to audio.wav") finally: recorder.delete() 

1 Answer 1

0
  • The main issue is that your while True: loop inside the KEY_PAGEDOWN block is preventing the program from checking for the KEY_PAGEUP event.

  • To fix this, you need to break out of the inner loop when the stop button is pressed.

  • You can achieve this is by using a flag to control the recording loop.

recording = False audio = [] def start_recording(): global stream stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024) def stop_recording(): global stream stream.stop_stream() stream.close() with wave.open("audio.wav", 'w') as f: f.setnchannels(1) f.setsampwidth(p.get_sample_size(pyaudio.paInt16)) f.setframerate(16000) print(len(audio)) f.writeframes(b''.join(audio)) print("Recording stopped and saved to audio.wav") for event in bluetooth_device.read_loop(): if event.type == ecodes.EV_KEY and event.value == 1: keyevent = categorize(event) if keyevent.keycode == 'KEY_PAGEDOWN': if not recording: start_recording() print("Recording... press > to stop") recording = True if keyevent.keycode == 'KEY_PAGEUP': if recording: stop_recording() recording = False if recording: frame = stream.read(1024) audio.append(frame) 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @phantom for your quick response. i tried your solution and even though the interruption of the stop button now works to exit from recording, but the recorded audio file is always empty and no voice is being recorded, what else can be done!!
Can you try with refactored code.
Unfortunately, this does not work and throws an error (Invalid input device) when i run it, it could be to do with pyaudio and how it should be set up, i am using a Raspberry Pi 4. The try except block above works perfectly, but i want to implement it with my bluetooth device controller
Solved using threads, thank you for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.