Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get list or Switch audio devices #49

Open
askruga opened this issue Oct 9, 2021 · 16 comments
Open

Get list or Switch audio devices #49

askruga opened this issue Oct 9, 2021 · 16 comments

Comments

@askruga
Copy link

askruga commented Oct 9, 2021

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
Copy link

@askruga i need this too

@trajano
Copy link

trajano commented Oct 13, 2022

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
Copy link
Contributor

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
Copy link

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

@Dearex
Copy link

Dearex commented Nov 12, 2023

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
Copy link

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
Copy link

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
Copy link
Contributor

@KillerBOSS2019
Copy link
Contributor

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

@Dearex
Copy link

Dearex commented Nov 22, 2023

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
Copy link

Dearex commented Nov 22, 2023

changing individual application output/input

This is exactly what I need 😄

@thomasrea0113
Copy link

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
Copy link

Dearex commented Nov 23, 2023

Got it working! Thanks
Two questions:

  • Do I need the DLL to change the Device of a single Application or is it possible without?
  • How do I set a Application back to "default"?

@tihoangyeudau
Copy link

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

@KillerBOSS2019
Copy link
Contributor

@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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants