pythonpython-3.xaudiomicrophonepython-sounddevice

How to specify which recording device to use with python sounddevice


I want to use the following function twice.

This function converts live audio input into a wav file.

For the first time, I use my pc built-in microphone and for the second run, I use my headphone's microphone that I plugged in before I run the code.

But when I tried my function it used only my headphone's microphone:

import sounddevice as sd
from scipy.io.wavfile import write
from playsound import playsound
def audio_to_wav(dst, device):
    """
    converts live audio to wav file
    :param dst: destination wav file
    """
    # Sample rate:
    fs = 4410
    # Duration of recording:
    seconds = 5

    myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2, device=device)
    # Wait until recording is finished:
    sd.wait()
    # Save as WAV file:
    write(dst, fs, myrecording)

In my main I called this function twice:

def main():
    # transfer mic1 audio to wav:
    audio_to_wav("file1.wav", 0)
    # transfer mic2 audio to wav:
    audio_to_wav("file2.wav", 1)
    # play the wav files audio:
    playsound("file1.wav")
    playsound("file2.wav")

But when i ran this code the function used only the headphone's microphone.

How can I use both my pc built-in microphone and my headphone's microphone at the same time?


Solution

  • The problem was that in the audio manager (in my case it's Realtek audio manager), the recording device advance settings were set to "Tie up same type of input jacks...".

    When I changed it to "Separate all input jacks as independent input devices" it fixed the problem.