Jump to content

Set Volume for another Program


Go to solution Solved by Floooooo24,

Recommended Posts

Hello Together,

I want to build a volume mixer, means i want to control the audio volum of other programms. Is there any Way to do this?

Note:
I don't want to change the volume for the skript in selfe or the Mastervolume of Windows.

Thanks for every Answer
Flo

Link to comment
Share on other sites

Not fully tested - for anybody who wants to try.

Edit: Ahh - this one is for the windows master vol and so ...😵

#include <GuiSlider.au3>

_SetVol("chrome", 50)

Func _SetVol($ProgName, $level)
    Local $hWnd, $hCW, $bclose = False, $i = 0
    $hWnd = WinGetHandle("Volume Mixer", "")
    If Not HWnd($hWnd) Then
        $bclose = True
        Run("sndvol.exe", "", @SW_HIDE)
        $hWnd = WinWaitActive("Volume Mixer")
    EndIf
    Do
        $i += 1
        $hCW = ControlGetHandle($hWnd, "", "[CLASS:ToolbarWindow32; INSTANCE:" & $i & "]")
        If StringInStr(ControlGetText($hWnd, "", $hCW), $ProgName) Then
            _GUICtrlSlider_SetPos(DllCall("user32.dll", "hwnd", "GetWindow", "hwnd", $hCW, "uint", $GW_HWNDPREV)[0], 100 - $level)
            ExitLoop
        EndIf
    Until Not HWnd(ControlGetHandle($hWnd, "", $hCW))
    If $bclose Then WinClose($hWnd, "")
EndFunc

 

Edited by Deye
Link to comment
Share on other sites

  • Solution


Thank you both,

I found an code witch use the same base code as the second link. I addet some comments. Some are German but the important are translated.

;~ ---------------------------------------------------------------------
;~ Copy to your Programm (all needed includes und variables):
;~ ---------------------------------------------------------------------

#include-once
;https://www.autoitscript.com/forum/topic/174460-is-there-a-way-to-detect-any-sound-file-played/
;Special Thanks to Danyfirex 02/08/2015
#include <WinAPIProc.au3>

Global Const $sCLSID_MMDeviceEnumerator = "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
Global Const $sIID_IMMDeviceEnumerator = "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
Global Const $sTagIMMDeviceEnumerator = _
        "EnumAudioEndpoints hresult(int;dword;ptr*);" & _
        "GetDefaultAudioEndpoint hresult(int;int;ptr*);" & _
        "GetDevice hresult(wstr;ptr*);" & _
        "RegisterEndpointNotificationCallback hresult(ptr);" & _
        "UnregisterEndpointNotificationCallback hresult(ptr)"

Global Const $sIID_IMMDevice = "{D666063F-1587-4E43-81F1-B948E807363F}"
Global Const $sTagIMMDevice = _
        "Activate hresult(struct*;dword;ptr;ptr*);" & _
        "OpenPropertyStore hresult(dword;ptr*);" & _
        "GetId hresult(wstr*);" & _
        "GetState hresult(dword*)"

Global Const $sIID_IAudioSessionManager2 = "{77aa99a0-1bd6-484f-8bc7-2c654c9a9b6f}"
Global Const $sTagIAudioSessionManager = "GetAudioSessionControl hresult(ptr;dword;ptr*);" & _
        "GetSimpleAudioVolume hresult(ptr;dword;ptr*);"
Global Const $sTagIAudioSessionManager2 = $sTagIAudioSessionManager & "GetSessionEnumerator hresult(ptr*);" & _
        "RegisterSessionNotification hresult(ptr);" & _
        "UnregisterSessionNotification hresult(ptr);" & _
        "RegisterDuckNotification hresult(wstr;ptr);" & _
        "UnregisterDuckNotification hresult(ptr)"

Global Const $sIID_IAudioSessionEnumerator = "{e2f5bb11-0570-40ca-acdd-3aa01277dee8}"
Global Const $sTagIAudioSessionEnumerator = "GetCount hresult(int*);GetSession hresult(int;ptr*)"

Global Const $sIID_IAudioSessionControl = "{f4b1a599-7266-4319-a8ca-e70acb11e8cd}"
Global Const $sTagIAudioSessionControl = "GetState hresult(int*);GetDisplayName hresult(ptr);" & _
        "SetDisplayName hresult(wstr);GetIconPath hresult(ptr);" & _
        "SetIconPath hresult(wstr;ptr);GetGroupingParam hresult(ptr*);" & _
        "SetGroupingParam hresult(ptr;ptr);RegisterAudioSessionNotification hresult(ptr);" & _
        "UnregisterAudioSessionNotification hresult(ptr);"

Global Const $sIID_IAudioSessionControl2 = "{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}"
Global Const $sTagIAudioSessionControl2 = $sTagIAudioSessionControl & "GetSessionIdentifier hresult(ptr)" & _
        "GetSessionInstanceIdentifier hresult(ptr);" & _
        "GetProcessId hresult(dword*);IsSystemSoundsSession hresult();" & _
        "SetDuckingPreferences hresult(bool);"

; http://answers.awesomium.com/questions/3398/controlling-the-sound-using-pinvoke-the-volume-mix.html
Global Const $sIID_ISimpleAudioVolume = "{87CE5498-68D6-44E5-9215-6DA47EF883D8}"
Global Const $sTagISimpleAudioVolume = _
        "SetMasterVolume hresult(float;ptr);" & _
        "GetMasterVolume hresult(float*);" & _
        "SetMute hresult(int;ptr);" & _
        "GetMute hresult(int*)"

Global $__oIAudioSessionManager2

;~ ---------------------------------------------------------------------
;~ Example for a Programm
;~ ---------------------------------------------------------------------

;The Programmname witch shoud be contolled(you have to use the real Name)
$SoundAPP = "Firefox.exe"

;Creats a random Number, witch should used for the new Volume. Output by Messagebox.
$newValue = Random ( 1,100,1 )
MsgBox ( 64,"","new random value: " & $newValue, 1 )

;Requests the Volume at the Moment
Local $CurrentVolume = Audio_GetMasterVolume(_GetProcessID_by_name($SoundAPP)) * 100

;Schrittweiße erhöhen oder senken der Lautstärke
;increase/decrease the Volume Step by Step (Faide)
If $CurrentVolume <> $newValue And $CurrentVolume > $newValue Then
        For $i = $CurrentVolume To $newValue Step -1
            ;Next Line changes the Volume
            Audio_SetMasterVolume(_GetProcessID_by_name($SoundAPP), $i / 100)
            Sleep(30)
        Next
Else
        For $i = $CurrentVolume To $newValue Step +1
            ;Next Line changes the Volume
            Audio_SetMasterVolume(_GetProcessID_by_name($SoundAPP), $i / 100)
            Sleep(30)
        Next
EndIf

;~ ---------------------------------------------------------------------
;~ Copy to your Programm (all needed Funktions)
;~ ---------------------------------------------------------------------

;auflösen des Programmnamens in die Prozess-ID
Func _GetProcessID_by_name($process_name)
    Local $Process_list = ProcessList() ;Listet alle Programme die Laufen auf in einem zwei dimensonalen Array auf
    Local $ProcessID_Func = 0 ;erstellt eine leere Variable für die Prozess-ID
    For $i = 1 To UBound($Process_list) - 1 ;Vorschleife, die durch alle Array elemente der ersten Dimention geht und diese mit dem Programmnamen vergleicht
        If $Process_list[$i][0] = $process_name Then
            $ProcessID_Func = $Process_list[$i][1] ;Wenn der Programmname gefunden wurde, wird die Prozess-ID aus dem Array gelesen und der Prozess-ID Variable zugewiesen
            ExitLoop ;Beendet die schleife
        EndIf
    Next
Return  $ProcessID_Func ;Gibt die Prozess-ID an zurück an die aufgerufene stelle
EndFunc   ;==>_GetProcessID_by_name

;Krasser scheiß der irgend was mit windows Objekten zutun hat
Func Audio_SetMasterVolume($pid, $value)
    If Not IsObj($__oIAudioSessionManager2) Then
        $__oIAudioSessionManager2 = Audio_GetIAudioSessionManager2()
    EndIf
    If Not IsObj($__oIAudioSessionManager2) Then Return

    Local $pIAudioSessionEnumerator, $oIAudioSessionEnumerator
    If $__oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator) < 0 Then Return
    $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator)
    If Not IsObj($oIAudioSessionEnumerator) Then Return SetError(1)

    Local $i, $nSessions, $pIAudioSessionControl2, $oIAudioSessionControl2
    Local $ProcessID, $oISimpleAudioVolume
    Local $bMute = 0, $error = 1
    If $oIAudioSessionEnumerator.GetCount($nSessions) >= 0 Then
        For $i = 0 To $nSessions - 1
            If $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2) >= 0 Then
                $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2)
                If @error Then ContinueLoop
                $oIAudioSessionControl2.GetProcessId($ProcessID)
                If $ProcessID = $pid Then
                    $oISimpleAudioVolume = ObjCreateInterface($pIAudioSessionControl2, $sIID_ISimpleAudioVolume, $sTagISimpleAudioVolume)
                    If @error Then ContinueLoop
                    $oIAudioSessionControl2.AddRef() ;stabilize
                    If $oISimpleAudioVolume.SetMasterVolume($value, 0) >= 0 Then
                        $error = 0
                        ExitLoop
                    EndIf
                EndIf
            EndIf
        Next
    EndIf
    $oISimpleAudioVolume = 0
    $oIAudioSessionControl2 = 0
    $oIAudioSessionEnumerator = 0
    ;MsgBox(0, $error, "App muted: " & $bMute)
    Return SetError($error, 0, $bMute)
EndFunc   ;==>Audio_SetMasterVolume

;Ist genaus Krass und hat auch was mit irgendwelchen Windows Klassen und Objekten zu tun
Func Audio_GetMasterVolume($pid)
    If Not IsObj($__oIAudioSessionManager2) Then
        $__oIAudioSessionManager2 = Audio_GetIAudioSessionManager2()
    EndIf
    If Not IsObj($__oIAudioSessionManager2) Then Return

    Local $pIAudioSessionEnumerator, $oIAudioSessionEnumerator
    If $__oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator) < 0 Then Return
    $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator)
    If Not IsObj($oIAudioSessionEnumerator) Then Return SetError(1)

    Local $i, $nSessions, $pIAudioSessionControl2, $oIAudioSessionControl2
    Local $ProcessID, $oISimpleAudioVolume
    Local $bMute = 0, $error = 1
    If $oIAudioSessionEnumerator.GetCount($nSessions) >= 0 Then
        For $i = 0 To $nSessions - 1
            If $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2) >= 0 Then
                $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2)
                If @error Then ContinueLoop
                $oIAudioSessionControl2.GetProcessId($ProcessID)
                If $ProcessID = $pid Then
                    $oISimpleAudioVolume = ObjCreateInterface($pIAudioSessionControl2, $sIID_ISimpleAudioVolume, $sTagISimpleAudioVolume)
                    If @error Then ContinueLoop
                    $oIAudioSessionControl2.AddRef() ;stabilize
                    If $oISimpleAudioVolume.GetMasterVolume($bMute) >= 0 Then
                        $error = 0
                        ExitLoop
                    EndIf
                EndIf
            EndIf
        Next
    EndIf
    $oISimpleAudioVolume = 0
    $oIAudioSessionControl2 = 0
    $oIAudioSessionEnumerator = 0
    ;MsgBox(0, $error, "App muted: " & $bMute)
    Return SetError($error, 0, $bMute)
EndFunc   ;==>Audio_GetMasterVolume

;Kann man auch nur als Krass und irgendwas mit windows objekten bezeichnen
Func Audio_GetIAudioSessionManager2()
    Local $oIAudioSessionManager2 = 0
    Local Const $eMultimedia = 1, $CLSCTX_INPROC_SERVER = 0x01
    Local $pIMMDevice, $oMMDevice, $pIAudioSessionManager2
    Local $oMMDeviceEnumerator = ObjCreateInterface($sCLSID_MMDeviceEnumerator, $sIID_IMMDeviceEnumerator, $sTagIMMDeviceEnumerator)
    If IsObj($oMMDeviceEnumerator) Then
        If $oMMDeviceEnumerator.GetDefaultAudioEndpoint(0, $eMultimedia, $pIMMDevice) >= 0 Then
            $oMMDevice = ObjCreateInterface($pIMMDevice, $sIID_IMMDevice, $sTagIMMDevice)
            If IsObj($oMMDevice) Then
                If $oMMDevice.Activate(__uuidof($sIID_IAudioSessionManager2), $CLSCTX_INPROC_SERVER, 0, $pIAudioSessionManager2) >= 0 Then
                    $oIAudioSessionManager2 = ObjCreateInterface($pIAudioSessionManager2, $sIID_IAudioSessionManager2, $sTagIAudioSessionManager2)
                EndIf
                $oMMDevice = 0
            EndIf
        EndIf
        $oMMDeviceEnumerator = 0
    EndIf

    If IsObj($oIAudioSessionManager2) Then
        Return $oIAudioSessionManager2
    EndIf
EndFunc   ;==>Audio_GetIAudioSessionManager2

Func __uuidof($sGUID)
    Local $tGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
    DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "struct*", $tGUID)
    If @error Then Return SetError(@error, @extended, 0)
    Return $tGUID
EndFunc   ;==>__uuidof

Func CLSIDFromString($sGUID)
    Local $tGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
    DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "struct*", $tGUID)
    Return $tGUID
EndFunc   ;==>CLSIDFromString

The Code ist cut in three parts:
1: "Copy to your Programm (all needed includes und variables):" This includes all nessesary stuff like variabels.
2: "Example for a Programm" there is an Exampel, where you have to hard code the Name of the aplication (firefox.com in this case), then will picked a random number. and the Program will fade the Volum to this.
3:"Copy to your Programm (all needed Funktions)" this funktions do the Magic in the background

I hope someboy else can use this one time.

P.S.: Here is the original Thread the code form the user Aelc:

 

Link to comment
Share on other sites

Hello again,

i copied the code in a Libary format. So you only have to copy this file to your include library directory. Default ist "C:\Program Files\AutoIt3\Include".

Then you can copy the example in the top of the libary an past it to a new skript. Then run it, analys ist, change it until you have understood the code.

Have Fun!

Flo

Programm_Volume.au3

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...