import torch import pyaudio class AudioInput: def __init__(self): self.audio = pyaudio.PyAudio() def stream(self, frames_at_once): stream = self.audio.open( format=pyaudio.paFloat32, channels=1, rate=48000, input=True, frames_per_buffer=frames_at_once ) try: while True: data = stream.read(frames_at_once) tensor = torch.frombuffer(data, dtype=torch.float) yield tensor finally: stream.stop_stream() stream.close() def __del__(self): self.audio.terminate() if __name__ == '__main__': for chunk in AudioInput().stream(1024): print(chunk)