import pyaudio import numpy as np from pynput import keyboard import wave def record_on_keypress(keycode, chunk=1024, rate=44100): """ Records audio from the default microphone when a key is pressed and stops when the key is released. :param keycode: The keycode of the key that triggers the recording. :param chunk: The number of audio frames per buffer. (default: 1024) :param rate: The sampling rate of the audio. (default: 44100) :return: A PyAudio stream object and a NumPy array of the recorded audio. """ def on_press(key): if key == keycode: # start recording print("start recording") frames = [] while True: data = stream.read(chunk) frames.append(data) if listener.current_key != keycode: break # convert frames to NumPy array audio = np.frombuffer(b''.join(frames), dtype=np.int16) # stop listener and 'return audio listener.stop() print(audio) return stream, audio p = pyaudio.PyAudio() stream = p.open(format=pyaudio.paInt16, channels=1, rate=rate, input=True, frames_per_buffer=chunk) print("listener starting recording") breakpoint() stream.start_stream() with keyboard.Listener(on_press=on_press) as listener: listener.join() print("end recording") # stop recording and close stream stream.stop_stream() stream.close() p.terminate() wf = wave.open("output.wav", 'wb') wf.setnchannels(2) wf.setsampwidth(p.get_sample_size(pyaudio.paInt16)) wf.setframerate(44100) wf.writeframes(b''.join(frames)) wf.close() print(record_on_keypress('+'))