Guest User

GeekDude's volume library

a guest
Mar 16th, 2021
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;-------------------------------------------------------------------;
  2. ; GeekDude's volume library
  3.  
  4. AppVolume(app:="", device:="") {
  5.     return new AppVolume(app, device)
  6. }
  7.  
  8. class AppVolume {
  9.     ISAVs := []
  10.    
  11.     __New(app:="", device:="") {
  12.         static IID_IASM2 := "{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}"
  13.              , IID_IASC2 := "{BFB7FF88-7239-4FC9-8FA2-07C950BE9C6D}"
  14.              , IID_ISAV := "{87CE5498-68D6-44E5-9215-6DA47EF883D8}"
  15.        
  16.         ; Activate the session manager of the given device
  17.         pIMMD := VA_GetDevice(device)
  18.         VA_IMMDevice_Activate(pIMMD, IID_IASM2, 0, 0, pIASM2)
  19.         ObjRelease(pIMMD)
  20.        
  21.         ; Enumerate sessions for on this device
  22.         VA_IAudioSessionManager2_GetSessionEnumerator(pIASM2, pIASE)
  23.         ObjRelease(pIASM2)
  24.        
  25.         ; Search for audio sessions with a matching process ID or Name
  26.         VA_IAudioSessionEnumerator_GetCount(pIASE, Count)
  27.         Loop, % Count {
  28.             ; Get this session's IAudioSessionControl2 via its IAudioSessionControl
  29.             VA_IAudioSessionEnumerator_GetSession(pIASE, A_Index-1, pIASC)
  30.             pIASC2 := ComObjQuery(pIASC, IID_IASC2)
  31.             ObjRelease(pIASC)
  32.            
  33.             ; If its PID matches save its ISimpleAudioVolume pointer
  34.             VA_IAudioSessionControl2_GetProcessID(pIASC2, PID)
  35.             if (PID == app || this.GetProcessName(PID) == app)
  36.                 this.ISAVs.Push(ComObjQuery(pIASC2, IID_ISAV))
  37.            
  38.             ObjRelease(pIASC2)
  39.         }
  40.        
  41.         ; Release the IAudioSessionEnumerator
  42.         ObjRelease(pIASE)
  43.     }
  44.    
  45.     __Delete() {
  46.         for k, pISAV in this.ISAVs
  47.             ObjRelease(pISAV)
  48.     }
  49.    
  50.     AdjustVolume(Amount) {
  51.         return this.SetVolume(this.GetVolume() + Amount)
  52.     }
  53.    
  54.     GetVolume() {
  55.         for k, pISAV in this.ISAVs
  56.         {
  57.             VA_ISimpleAudioVolume_GetMasterVolume(pISAV, fLevel)
  58.             return fLevel * 100
  59.         }
  60.     }
  61.    
  62.     SetVolume(level) {
  63.         level := level>100 ? 100 : level<0 ? 0 : level ; Limit to range 0-100
  64.         for k, pISAV in this.ISAVs
  65.             VA_ISimpleAudioVolume_SetMasterVolume(pISAV, level / 100)
  66.         return level
  67.     }
  68.    
  69.     GetMute() {
  70.         for k, pISAV in this.ISAVs
  71.         {
  72.             VA_ISimpleAudioVolume_GetMute(pISAV, bMute)
  73.             return bMute
  74.         }
  75.     }
  76.    
  77.     SetMute(bMute) {
  78.         for k, pISAV in this.ISAVs
  79.             VA_ISimpleAudioVolume_SetMute(pISAV, bMute)
  80.         return bMute
  81.     }
  82.    
  83.     ToggleMute() {
  84.         return this.SetMute(!this.GetMute())
  85.     }
  86.    
  87.     GetProcessName(PID) {
  88.         hProcess := DllCall("OpenProcess"
  89.         , "UInt", 0x1000 ; DWORD dwDesiredAccess (PROCESS_QUERY_LIMITED_INFORMATION)
  90.         , "UInt", False  ; BOOL  bInheritHandle
  91.         , "UInt", PID    ; DWORD dwProcessId
  92.         , "UPtr")
  93.         dwSize := VarSetCapacity(strExeName, 512 * A_IsUnicode, 0) // A_IsUnicode
  94.         DllCall("QueryFullProcessImageName"
  95.         , "UPtr", hProcess  ; HANDLE hProcess
  96.         , "UInt", 0         ; DWORD  dwFlags
  97.         , "Str", strExeName ; LPSTR  lpExeName
  98.         , "UInt*", dwSize   ; PDWORD lpdwSize
  99.         , "UInt")
  100.         DllCall("CloseHandle", "UPtr", hProcess, "UInt")
  101.         SplitPath, strExeName, strExeName
  102.         return strExeName
  103.     }
  104. }
  105.  
  106.  
  107. ; --- Vista Audio Additions ---
  108.  
  109. ;
  110. ; ISimpleAudioVolume : {87CE5498-68D6-44E5-9215-6DA47EF883D8}
  111. ;
  112.  
  113. VA_ISimpleAudioVolume_SetMasterVolume(this, ByRef fLevel, GuidEventContext="") {
  114.     return DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "float", fLevel, "ptr", VA_GUID(GuidEventContext))
  115. }
  116.  
  117. VA_ISimpleAudioVolume_GetMasterVolume(this, ByRef fLevel) {
  118.     return DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this, "float*", fLevel)
  119. }
  120.  
  121. VA_ISimpleAudioVolume_SetMute(this, ByRef Muted, GuidEventContext="") {
  122.     return DllCall(NumGet(NumGet(this+0)+5*A_PtrSize), "ptr", this, "int", Muted, "ptr", VA_GUID(GuidEventContext))
  123. }
  124.  
  125. VA_ISimpleAudioVolume_GetMute(this, ByRef Muted) {
  126.     return DllCall(NumGet(NumGet(this+0)+6*A_PtrSize), "ptr", this, "int*", Muted)
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment