pythonaudiopython-sounddevice

How do I open an audio output stream using sounddevice in python?


I am trying to make my audio interface continuous play the same audio in a loop. Someone recommended the use of the "OutputStream" function in the sounddevice library. This is the code I wrote to do this:

sounddevice.default.device = "Focusrite USB ASIO, ASIO"
sounddevice.default.samplerate = 48000
stream = sounddevice.OutputStream()
stream.start()
stream.write(data)

The last line code is giving me the error:

sounddevice.PortAudioError: Wait timed out [PaErrorCode -9987]

What am I doing wrong?


Solution

  • The way I got it working was by using:

    with sounddevice.OutputStream(device="Focusrite USB ASIO, ASIO", channels=8, callback=callback, samplerate=samplesPerSecond)
    

    This repeatedly calls the callback function. In the callback function I set the output:

    def callback(outdata, frames, time, status):
         for i in range(8):
              channels[i] = audioData
         multiChannel = np.column_stack((channels[0], channels[1], channels[2], channels[3], channels[4], channels[5], channels[6], channels[7]))
         outdata[:] = multiChannel