Spaces:
Runtime error
Runtime error
File size: 1,846 Bytes
9690d29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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('+'))
|