0

I would like to continuously record and audio. This is easily done using python's sounddevice module. However, I also wanted to start sending the chunks to a thread that works in the background when the audio gets more than 20 frames. The sounddevice's input overflows when I do this, can you help me fix this, or find another solution?

def callbackAmbient(indata, frames, time, status):
    if (status):
        print(status)
    VadFrames.append(indata)        

    if (len(VadFrames) > 19):
        Process(target = start_vad, args = (numpy.array(numpy.multiply(VadFrames, 0.5)), numpy.array(VadFrames), RATE, RATE)).start()
        VadFrames.pop(0)

print("System Recording...")

try:
    with sd.InputStream(samplerate=192000, blocksize=6144, channels=1, device=sd.query_devices(kind='input')['name'], callback=callbackAmbient):
        while True: 
            pass
except KeyboardInterrupt:
    print("System stopped...")
    sys.exit()

1 Answers1

1

You can create an instance of your Stream, and pass it in argument to a python thread that will be responsible of data management, throwing errors, or whatever you want.

audio_stream = AudioStream() #create an audio stream w. my soundcard
websocket = Websocket(audio_stream) #create a websocket connection passing actual stream
websocket.start()

audio_stream.run()

Websocket class is responsible here of managing errors between audio stream and front-end (note that i used websockets in this case but you could simply choose another communication protocol) :

class Websocket(threading.Thread):
    def __init__(self, stream):
        super().__init__()
        self.audio_stream = stream

    def run(self):
        asyncio.set_event_loop(asyncio.new_event_loop())
        start_server = websockets.serve(self.handler, "localhost", 8083)
        asyncio.get_event_loop().run_until_complete(start_server)
        asyncio.get_event_loop().run_forever()

    #implementation.....

The logic you put in this post should be implemented in AudioStream class init method in order to work