Writing to Virtual Audio Cable via Python (PyAudio, Sounddevice)

260 Views Asked by At

I am trying to write data to a virtual audio cable using python. So far I don't really care which module I use, so I tried PyAudio and Sounddevive. I installed the Virtual Cable from VB-Audio. It does show up in my device list. The problems arise once I want to initialize an output stream with the cable. An Error is raised: OSError: [Errno -9998] Invalid number of channels

Checking the device list, the indices and number of channels the cable options I have are:

3 CABLE Output (VB-Audio Virtual , MME (2 in, 0 out)
9 CABLE Input (VB-Audio Virtual C, MME (0 in, 2 out)
9 CABLE Input (VB-Audio Virtual C, MME (0 in, 2 out)
20 CABLE Input (VB-Audio Virtual Cable), Windows DirectSound (0 in, 2 out)
24 CABLE Input (VB-Audio Virtual Cable), Windows WASAPI (0 in, 2 out)
28 CABLE Output (VB-Audio Virtual Cable), Windows WASAPI (2 in, 0 out)
35 CABLE Output (VB-Audio Point), Windows WDM-KS (8 in, 0 out)

Instinctively I'd want to write to the CABLE Input. But somehow the inputs have no input channels available? And straight up using the output channel with the correct number of channels, also raises the same error. Any idea on what I could do?

import numpy as np
import soundcard as sc
import soundfile as sf
import sounddevice as sd
import pyaudio

# Initialize PyAudio
p = pyaudio.PyAudio()

buffer_size = 512

# set devices to in- and output from
mic_input_index = 1
input_sample_rate = int(sd.query_devices(mic_input_index)['default_samplerate'])
v_cable_output_index = 14        

# Open an audio stream for input
input_stream = p.open(format=pyaudio.paInt16,
                    channels=2,
                    rate=input_sample_rate,
                    input=True,
                    frames_per_buffer=buffer_size,
                    input_device_index=mic_input_index)

# Open an audio stream for output (virtual audio device)
output_stream = p.open(format=pyaudio.paInt16,
                    channels=8,
                    rate=input_sample_rate,
                    output=True,
                    output_device_index=v_cable_output_index)

while True:
    try:
        # Read audio data
        data = input_stream.read(buffer_size)
        audio_array = np.frombuffer(data, dtype=np.int16)
        processed_audio = audio_array

        # Play the processed audio to the virtual audio device
        output_stream.write(processed_audio.tobytes())

    except KeyboardInterrupt:
        break
0

There are 0 best solutions below