python-sounddevice icon indicating copy to clipboard operation
python-sounddevice copied to clipboard

Add your id to the device output and input

Open Gas999X opened this issue 4 years ago • 5 comments

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

Gas999X avatar Oct 25 '21 05:10 Gas999X

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).

HaHeho avatar Oct 25 '21 08:10 HaHeho

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]

Gas999X avatar Oct 25 '21 10:10 Gas999X

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.

HaHeho avatar Oct 25 '21 11:10 HaHeho

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

Gas999X avatar Oct 25 '21 22:10 Gas999X

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

  • use device=mydevice or device=(myinputdevice, myoutputdevice) in the stream constructor
  • use sd.default.device = mydevice or sd.default.device = myinputdevice, myoutputdevice before you create your stream (either directly or via some helper function like playrec())

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).

mgeier avatar Oct 26 '21 20:10 mgeier