spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
980 stars 145 forks source link

Failed to load ASIO driver when using GUI framework #502

Open my1e5 opened 7 months ago

my1e5 commented 7 months ago

When I run this minimal example, everything works fine.

import sounddevice as sd
import numpy as np

FS = 44100
sd.default.device = 45  # This is an ASIO device

def run():
    print("Running playrec...")
    sd.playrec(np.zeros((FS, 1)), FS, channels=1)
    print("Done!")

if __name__ == "__main__":
    run()

But when I introduce the DearPyGui framework and try to execute the same function from a callback I get an error.

import numpy as np
import sounddevice as sd
import dearpygui.dearpygui as dpg

FS = 44100
sd.default.device = 45  # This is an ASIO device

def run():
    print("Running playrec...")
    sd.playrec(np.zeros((FS, 1)), FS, channels=1)
    print("Done!")

if __name__ == "__main__":
    dpg.create_context()
    with dpg.window():
        dpg.add_button(label="Run playrec", callback=run)
    dpg.create_viewport()
    dpg.setup_dearpygui()
    dpg.show_viewport()
    dpg.start_dearpygui()
    dpg.destroy_context()
Running playrec...
Traceback (most recent call last):
  File "C:\Users\User\Documents\test.py", line 11, in run
    sd.playrec(np.zeros((FS, 1)), FS, channels=1)
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\sounddevice.py", line 369, in playrec
    ctx.start_stream(Stream, samplerate,
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\sounddevice.py", line 2582, in start_stream
    self.stream = StreamClass(samplerate=samplerate,
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\sounddevice.py", line 1800, in __init__
    _StreamBase.__init__(self, kind='duplex', wrap_callback='array',
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\sounddevice.py", line 898, in __init__
    _check(_lib.Pa_OpenStream(self._ptr, iparameters, oparameters,
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\sounddevice.py", line 2745, in _check
    raise PortAudioError(errormsg, err, hosterror_info)
sounddevice.PortAudioError: Error opening Stream: Unanticipated host error [PaErrorCode -9999]: 'Failed to load ASIO driver' [ASIO error 0]

This only happens when I try to use an ASIO device. For example, MME works fine. Does anyone know why this is happening?

Interestingly, if I put the import statement inside the function. It works. But you have to remove the initial import on line 2, if it's there it still breaks.

def run():
    print("Running playrec...")
    import sounddevice as sd
    sd.default.device = 45  # This is an ASIO device
    sd.playrec(np.zeros((FS, 1)), FS, channels=1)
    print("Done!")

Windows 10 sounddevice 0.4.6 Python 3.11.4

mgeier commented 7 months ago

See #442.