spatialaudio / python-sounddevice

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

Add your id to the device output and input #377

Open Gas999X opened 2 years ago

Gas999X commented 2 years ago

Greetings, there is a code on the link,

import sounddevice as sd
duration = 5.5  # seconds

def callback(indata, outdata, frames, time, status):
    if status:
        print(status)
    outdata[:] = indata

with sd.Stream(channels=2, callback=callback):
    sd.sleep(int(duration * 1000))

and it suits me completely, I just didn't understand how to enter my equipment IDs, I want to output the signal from the microphone to a virtual cable. Id = 3

HaHeho commented 2 years ago

You can provide the desired interface with the device= parameter (see https://python-sounddevice.readthedocs.io/en/0.4.3/api/streams.html#sounddevice.Stream).

Gas999X commented 2 years ago

with sd.RawStream(device=10, channels=2, dtype='int24', callback=callback): <-----------

When I substitute Device = 10 it gives an error.

 File "C:\Users\karpo\AppData\Local\Programs\Python\Python39\Lib\site-packages\sounddevice.py", line 2741, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening RawStream: Invalid number of channels [PaErrorCode -9998]
HaHeho commented 2 years ago

I don't know formats or how many channels your respective input and output interfaces (you will have to provide both names or IDs) support. But https://github.com/spatialaudio/python-sounddevice/blob/0.4.3/examples/wire.py seems to do everything you need. Try to find a working configuration with that implementation (sd.query_devices() should help) and then simplify / adapt the code for your purposes.

Gas999X commented 2 years ago

sd.default.samplerate = None sd.default.device = None, 7

sd.default.device = input(None = default) , output

I found the answer here https://python-sounddevice.readthedocs.io/en/0.3.11/#sounddevice.default

mgeier commented 2 years ago

Just to clarify: you can do both things mentioned above, both should work:

I either case you can use None to get the default device.

You can specify your devices either by their numeric ID or by (space-separated) substring(s).