Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sounddevice with virtual audio device and minimize latency #520

Open
daocunyang opened this issue Feb 5, 2024 · 1 comment
Open

Comments

@daocunyang
Copy link

daocunyang commented Feb 5, 2024

Hi, not sure if this is the right place to ask (and sorry if it's not), but has anyone used sounddevice with a virtual audio device on a Windows machine to play audio files, and has experience with minimizing latency? In particular using sounddevice with ASIO.

I'm using sounddevice on a Windows machine to build a system, in which an audio file in WAV format is played (silently) using sd.play(...) through the microphone using VB-Audio Cable and sent over the Internet, and a voice signal is later received from the Internet by the virtual output device.

My goal is to minimize latency. One thing I'm now trying is to use ASIO with the virtual audio devices, since I know ASIO is preferred for low-latency audio on Windows, but I'm having some trouble getting that to work. I have installed ASIO4ALL and VB-Audio ASIO Bridge. However, when I run sd.query_devices(), unlike with MME, WASAPI etc. where it shows two virtual audio devices (one for input and one for output), for ASIO there is only one device (#16 below):

   1 Hi-Fi Cable Output (VB-Audio Hi, MME (2 in, 0 out)
   2 立体声混音 (Realtek(R) Audio), MME (2 in, 0 out)
   3 CABLE Output (VB-Audio Virtual , MME (2 in, 0 out)
   4 麦克风阵列 (Realtek(R) Audio), MME (2 in, 0 out)
   5 Microsoft 声音映射器 - Output, MME (0 in, 2 out)
   6 Hi-Fi Cable Input (VB-Audio Hi-, MME (0 in, 2 out)
   7 CABLE Input (VB-Audio Virtual C, MME (0 in, 2 out)
   8 主声音捕获驱动程序, Windows DirectSound (2 in, 0 out)
   9 Hi-Fi Cable Output (VB-Audio Hi-Fi Cable), Windows DirectSound (2 in, 0 out)
  10 立体声混音 (Realtek(R) Audio), Windows DirectSound (2 in, 0 out)
  11 CABLE Output (VB-Audio Virtual Cable), Windows DirectSound (2 in, 0 out)
  12 麦克风阵列 (Realtek(R) Audio), Windows DirectSound (2 in, 0 out)
  13 主声音驱动程序, Windows DirectSound (0 in, 2 out)
  14 Hi-Fi Cable Input (VB-Audio Hi-Fi Cable), Windows DirectSound (0 in, 2 out)
  15 CABLE Input (VB-Audio Virtual Cable), Windows DirectSound (0 in, 2 out)
  16 ASIO4ALL v2, ASIO (2 in, 2 out)
  17 CABLE Input (VB-Audio Virtual Cable), Windows WASAPI (0 in, 2 out)
  18 Hi-Fi Cable Input (VB-Audio Hi-Fi Cable), Windows WASAPI (0 in, 2 out)
  19 立体声混音 (Realtek(R) Audio), Windows WASAPI (2 in, 0 out)
  20 CABLE Output (VB-Audio Virtual Cable), Windows WASAPI (2 in, 0 out)
  21 Hi-Fi Cable Output (VB-Audio Hi-Fi Cable), Windows WASAPI (2 in, 0 out)
  22 麦克风阵列 (Realtek(R) Audio), Windows WASAPI (2 in, 0 out)
  23 Speakers (Realtek HD Audio output), Windows WDM-KS (0 in, 2 out)
  24 麦克风阵列 (Realtek HD Audio Mic input), Windows WDM-KS (2 in, 0 out)
  25 立体声混音 (Realtek HD Audio Stereo input), Windows WDM-KS (2 in, 0 out)
  26 Hi-Fi Cable Output (VB-Audio Hi-Fi Cable), Windows WDM-KS (8 in, 0 out)
  27 Speakers (VB-Audio Hi-Fi Cable), Windows WDM-KS (0 in, 8 out)
  28 CABLE Output (VB-Audio Point), Windows WDM-KS (8 in, 0 out)
  29 Speakers (VB-Audio Point), Windows WDM-KS (0 in, 8 out)

I wonder if it's possible for sounddevice to have two separate ASIO devices such as the following?

Hi-Fi Cable Input (VB-Audio Hi-Fi Cable, ASIO (0 in, 2 out)
Hi-Fi Cable Output (VB-Audio Hi-Fi Cable), ASIO (2 in, 0 out)

Following is my current code to play audio using ASIO:

to_play_data_array, sample_rate = soundfile.read(wave_filename)
sd.default.device[1] = 16

asio_in = sd.AsioSettings(channel_selectors=[0])
asio_out = sd.AsioSettings(channel_selectors=[1])
sd.default.extra_settings = None, asio_out

sd.play(to_play_data_array, blocking=False, samplerate=sample_rate, mapping=mapping_ch)

and with the above I'm getting an error:
('Error opening OutputStream: Unanticipated host error', -9999, (2, 0, 'Failed to load ASIO driver'))

Any help is appreciated. I'm looking for help to use ASIO with sounddevice, but other ways to optimize latency are welcome as well. Thanks in advance.

@mgeier
Copy link
Member

mgeier commented Feb 24, 2024

I don't know what could cause the ASIO error, could you please provide fully reproducible (but ideally minimal) code and the full error output?

If ASIO doesn't work for you, you could also try WASAPI. You should get good latency with the exclusive flag (see https://python-sounddevice.readthedocs.io/en/0.4.6/api/platform-specific-settings.html#sounddevice.WasapiSettings).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants