Python Sound Device Recording

18 Views Asked by At

I am trying to create a .wav file by recording from a USB microphone connected to my computer. I am trying to accomplish this using Python. I keep seeing the following example (or something similar) to record, but I am having trouble understanding where the audio device is specified here? If there are multiple microphones, which one would it be recording from? Thanks!

duration = 10  # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2)
1

There are 1 best solutions below

0
AKX On

Paraphrasing the documentation for sounddevice:

Use sounddevice.query_devices() or python -m sounddevice to get a list of supported devices.

You can use the corresponding device ID to select a desired device by assigning to sounddevice.default.device or by passing it as device argument to sounddevice.play(), sounddevice.Stream() etc.

Instead of the numerical device ID, you can also use a space-separated list of case-insensitive substrings of the device name.

IOW,

myrecording = sounddevice.rec(
    duration * fs,
    samplerate=fs,
    channels=2,
    device="USB",
)

could do if your microphone shows up as e.g. "USB Audio Device".

You can see device here in the docs for InputStream; sounddevice.rec()'s docs refer to that for the other **kwargs.