import librosa import numpy as np from .init import pipe TASK = "transcribe" BATCH_SIZE = 8 LIMIT = 60 class A2T: def __init__(self, mic): self.mic = mic def __transcribe(self, inputs, task: str = None): if inputs is None: print("Inputs None") transcribed_text = pipe(inputs, batch_size=BATCH_SIZE,)["text"] return transcribed_text def __preprocces(self, raw: np.ndarray, sampling_rate: int): chunk = raw.astype(np.float32) / 32678.0 if sampling_rate != 16000: chunk = librosa.resample(chunk, orig_sr=sampling_rate, target_sr=16000) # chunk = chunk[:16000*LIMIT] return chunk def predict(self): try: if self.mic is not None: chunk = self.mic.get_array_of_samples() chunk = np.array(chunk, dtype=np.int16) sampling_rate = self.mic.frame_rate audio = self.__preprocces(raw=chunk, sampling_rate=sampling_rate) print(f"audio : {audio} \n shape : {audio.shape} \n max : {np.max(audio)}") else: raise Exception("please provide audio") if isinstance(audio , np.ndarray): return self.__transcribe(inputs=audio, task=TASK) else: raise Exception("Audio is not np array") except Exception as e: return f"Oops some kinda error : {e}"