Voice-Human2Robot / in_out_micro /voice_record.py
Danh Tran
Upload 34 files
3190c86 verified
import keyboard
import pyaudio
import sounddevice as sd
import numpy as np
import soundfile as sf
import time
class Recorder():
def __init__(self, filename, sample_rate=22050, channels=1, input_device_index=2):
self.audio_format = pyaudio.paFloat32 # pyaudio.paInt16
self.channels = channels
self.sample_rate = sample_rate
self.chunk = int(0.03*self.sample_rate)
self.filename = filename
self.START_KEY = 's'
self.STOP_KEY = 'q'
self.input_device_index = input_device_index
def record_procssing(self, write_to_file):
recorded_data = []
p = pyaudio.PyAudio()
stream = p.open(format=self.audio_format, channels=self.channels,
rate=self.sample_rate, input=True,
frames_per_buffer=self.chunk, input_device_index=self.input_device_index)
while True:
data = stream.read(self.chunk)
recorded_data.append(data)
if keyboard.is_pressed(self.STOP_KEY) or keyboard.is_pressed(self.STOP_KEY.upper()):
print("Stop recording")
# stop and close the stream
stream.stop_stream()
stream.close()
p.terminate()
#convert recorded data to numpy array
recorded_data = [np.frombuffer(frame, dtype=np.float32) for frame in recorded_data]
audio_arr = np.concatenate(recorded_data, axis=0)
if write_to_file:
sf.write(self.filename, audio_arr, self.sample_rate)
print("Saved to Record directory")
return audio_arr
break
def record(self, write_to_file=True):
print(f"Press '{self.START_KEY}' to Start and '{self.STOP_KEY}' to Stop!")
while True:
if keyboard.is_pressed(self.START_KEY) or keyboard.is_pressed(self.START_KEY.upper()):
print("Recodrding...")
audio_arr = self.record_procssing(write_to_file)
return audio_arr, self.sample_rate
break
if __name__ == "__main__":
recorder = Recorder("audios/records/input_records/mic.mp3") #name of output file
audo_arr, sr = recorder.record()
print(audo_arr.shape)