AndreMiras / pycaw

Python Core Audio Windows Library
MIT License
377 stars 67 forks source link

Get list or Switch audio devices #49

Open askruga opened 3 years ago

askruga commented 3 years ago

Hi. Is it possible to:

1.Get a list of audio devices 2.Get current default audio device 3.Change the default device by your lib?

Or maybe anyone have idea how do this? Thanks for lib!

GastonMontiel commented 2 years ago

@askruga i need this too

trajano commented 2 years ago

These needs IPolicyConfig. In AutoHotKey it could be implemented as follows.

SetDefaultEndpoint(DeviceID, Role := 3)
{
    ; Undocumented COM-interface IPolicyConfig.
    IPolicyConfig := ComObject("{870AF99C-171D-4F9E-AF0D-E63Df40c2BC9}", "{F8679F50-850A-41CF-9C72-430F290290C8}")
    if (Role & 0x1)
        ComCall(13, IPolicyConfig, "Str", DeviceID, "Int", 0)  ; Default Device
    if (Role & 0x2)
        ComCall(13, IPolicyConfig, "Str", DeviceID, "Int", 2)  ; Default Communication Device
}

Stack overflow https://stackoverflow.com/questions/13054371/programmatically-switch-audio-devices-on-windows-7

Calling COM https://www.oreilly.com/library/view/python-programming-on/1565926218/ch05s02.html

Tried to do it but I can't find the part that allows me to do the two-parameter version ComObject that allows passing in a CLSID and IID in w32com.client

KillerBOSS2019 commented 1 year ago
def switchOutput(deviceId, role):
    policy_config = comtypes.CoCreateInstance(
        pc.CLSID_PolicyConfigClient,
        pc.IPolicyConfig,
        comtypes.CLSCTX_ALL
    )
    policy_config.SetDefaultEndpoint(deviceId, role)
    policy_config.Release()

You can change audio devices using this function. You can get list of audio devices using EnumAudioEndpoints. and role arg is from EDataFlow has 4 options, Output/Input/Default/Communications

tihoangyeudau commented 1 year ago

how can I listen event of Switching current default audio device?

Dearex commented 11 months ago
def switchOutput(deviceId, role):
    policy_config = comtypes.CoCreateInstance(
        pc.CLSID_PolicyConfigClient,
        pc.IPolicyConfig,
        comtypes.CLSCTX_ALL
    )
    policy_config.SetDefaultEndpoint(deviceId, role)
    policy_config.Release()

You can change audio devices using this function. You can get list of audio devices using EnumAudioEndpoints. and role arg is from EDataFlow has 4 options, Output/Input/Default/Communications

Hey @KillerBOSS2019 What modules do I need to import for this to work? What is pc? Thanks!

thomasrea0113 commented 10 months ago
def switchOutput(deviceId, role):
    policy_config = comtypes.CoCreateInstance(
        pc.CLSID_PolicyConfigClient,
        pc.IPolicyConfig,
        comtypes.CLSCTX_ALL
    )
    policy_config.SetDefaultEndpoint(deviceId, role)
    policy_config.Release()

You can change audio devices using this function. You can get list of audio devices using EnumAudioEndpoints. and role arg is from EDataFlow has 4 options, Output/Input/Default/Communications

Hey @KillerBOSS2019 What modules do I need to import for this to work? What is pc? Thanks!

Did you figure out what the pc import is supposed to be? This is so close to the exactly solution I need. I'm guessing it's supposed to model the same thing as https://github.com/frgnca/AudioDeviceCmdlets/blob/master/SOURCE/IPolicyConfig.cs, but I can't find those interface types predefined anywhere

thomasrea0113 commented 10 months ago
def switchOutput(deviceId, role):
    policy_config = comtypes.CoCreateInstance(
        pc.CLSID_PolicyConfigClient,
        pc.IPolicyConfig,
        comtypes.CLSCTX_ALL
    )
    policy_config.SetDefaultEndpoint(deviceId, role)
    policy_config.Release()

You can change audio devices using this function. You can get list of audio devices using EnumAudioEndpoints. and role arg is from EDataFlow has 4 options, Output/Input/Default/Communications

It would be super helpful if you could throw togother a quick example of how to actually call this method. I can't find interface definitions for IPolicyConfig anywhere

KillerBOSS2019 commented 10 months ago

Luckily I have my project open source (: https://github.com/KillerBOSS2019/TouchPortal-Windows-MediaMixer/blob/main/src/audioUtil/policyconfig.py

KillerBOSS2019 commented 10 months ago

My project allows me to interface the audio mixer directly, changing individual application output/input and switching global audio devices

Dearex commented 10 months ago

Thanks for the link! I figured out the clsid thing on my own but was stuck at the IPolicyConfig. I'll have a look at your project

Dearex commented 10 months ago

changing individual application output/input

This is exactly what I need 😄

thomasrea0113 commented 10 months ago

This is excellent! I had started the process of implementing the IPolicyConfig interface myself and quickly realized it wasn't worth the trouble and resulted to just using this powershell module instead: https://github.com/KillerBOSS2019/TouchPortal-Windows-MediaMixer/tree/main, which meant it wouldn't be a purely python solution. This worked for my purposes perfectly!

Dearex commented 10 months ago

Got it working! Thanks Two questions:

tihoangyeudau commented 10 months ago

Is there a way to get default audio input device change event from windows for python?

KillerBOSS2019 commented 10 months ago

@Dearex 1. I have tried to translate the program into python but no success. The dll is a simplified version of AudioSwitcher program. Basically a minimal required code to get it working. As for the second question. You will need to do your own research. I have that somewhere just Donno where

KillerBOSS2019 commented 10 months ago

Would be cool if you can reference my project if you use part of my project. Not required (: