New to Using Libraries, Not Understanding How Vista Audio Control Functions works

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Brain-Shock Omega
Posts: 11
Joined: 13 Mar 2019, 03:41

New to Using Libraries, Not Understanding How Vista Audio Control Functions works

07 Jun 2019, 22:30

Hey guys, I am pretty new to making scripts and programs, this is the first time I have imported a library of functions before and i'm pretty sure i'm understanding what the various functions are supposed to be doing, but i'm not getting the values I expect when testing some out, even basic ones.

I'm using the code from this Github archive of the library, https://github.com/Masonjar13/AHK-Library/blob/master/Required-Libraries/VA.ahk

So something simple I would try would be something like this:

Code: Select all

device := VA_GetDeviceName("Speakers")
MsgBox % device
With that I figured it would return the name of my Head-phones that are plugged in or something, maybe a number, but it is only returning a blank message box. I looked at the function that had a lot of commands I did not understand and the use of a lot of registry and addresses and such, but I can't really see why it would return nothing at all.

I also tried this function:

Code: Select all

Device := VA_GetDevice(device_desc="playback")
MsgBox % Device
Which is returning 0, which if I am looking at the function correctly, means something is wrong and it got caught before running the rest.

Code: Select all

; device_desc = device_id
;               | ( friendly_name | 'playback' | 'capture' ) [ ':' index ]
VA_GetDevice(device_desc="playback")
{
    static CLSID_MMDeviceEnumerator := "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
        , IID_IMMDeviceEnumerator := "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
    if !(deviceEnumerator := ComObjCreate(CLSID_MMDeviceEnumerator, IID_IMMDeviceEnumerator))
        return 0
    
    device := 0
    
    if VA_IMMDeviceEnumerator_GetDevice(deviceEnumerator, device_desc, device) = 0
        goto VA_GetDevice_Return
    
    if device_desc is integer
    {
        m2 := device_desc
        if m2 >= 4096 ; Probably a device pointer, passed here indirectly via VA_GetAudioMeter or such.
        {
            ObjAddRef(device := m2)
            goto VA_GetDevice_Return
        }
    }
    else
        RegExMatch(device_desc, "(.*?)\s*(?::(\d+))?$", m)
    
    if m1 in playback,p
        m1 := "", flow := 0 ; eRender
    else if m1 in capture,c
        m1 := "", flow := 1 ; eCapture
    else if (m1 . m2) = ""  ; no name or number specified
        m1 := "", flow := 0 ; eRender (default)
    else
        flow := 2 ; eAll
    
    if (m1 . m2) = ""   ; no name or number (maybe "playback" or "capture")
    {
        VA_IMMDeviceEnumerator_GetDefaultAudioEndpoint(deviceEnumerator, flow, 0, device)
        goto VA_GetDevice_Return
    }

    VA_IMMDeviceEnumerator_EnumAudioEndpoints(deviceEnumerator, flow, 1, devices)
    
    if m1 =
    {
        VA_IMMDeviceCollection_Item(devices, m2-1, device)
        goto VA_GetDevice_Return
    }
    
    VA_IMMDeviceCollection_GetCount(devices, count)
    index := 0
    Loop % count
        if VA_IMMDeviceCollection_Item(devices, A_Index-1, device) = 0
            if InStr(VA_GetDeviceName(device), m1) && (m2 = "" || ++index = m2)
                goto VA_GetDevice_Return
            else
                ObjRelease(device), device:=0

VA_GetDevice_Return:
    ObjRelease(deviceEnumerator)
    if devices
        ObjRelease(devices)
    
    return device ; may be 0
}
No clue what:

Code: Select all

CLSID_MMDeviceEnumerator := "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
        , IID_IMMDeviceEnumerator := "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
means, i'm guessing the numbers are addresses in Windows memory? It seems like i'm not giving the right values though, since it's not running the rest, but I don't really know what to give it.

The only functions that made sense to me and gave me a number I expected were under "Master Controls" and just for finding audio volume, muting, setting audio volume, and giving amount of channels.

My current project i'm trying to get the audio levels that are coming to my audio output device, so I can make decisions in my program based on if there is sound coming through the head-phones, but i'm not really understanding what these functions are asking for exactly and how to interpret the addresses and such. I have also not done much object oriented stuff thus far just fyi. Thanks a lot for any help.
User avatar
Datapoint
Posts: 68
Joined: 18 Mar 2018, 17:06

Re: New to Using Libraries, Not Understanding How Vista Audio Control Functions works

07 Jun 2019, 23:39

Hi
Brain-Shock Omega wrote:
07 Jun 2019, 22:30
No clue what:

Code: Select all

CLSID_MMDeviceEnumerator := "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
        , IID_IMMDeviceEnumerator := "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
means, i'm guessing the numbers are addresses in Windows memory?
Not memory addresses... You can think of them as unique ID numbers (stored in the registry) that identify the type of object that he wants to create. When he uses them later - ComObjCreate(CLSID_MMDeviceEnumerator, IID_IMMDeviceEnumerator) - this creates an object specified by the two IDs.
Brain-Shock Omega wrote: I also tried this function:

Code: Select all

Device := VA_GetDevice(device_desc="playback")
MsgBox % Device
Which is returning 0, which if I am looking at the function correctly, means something is wrong and it got caught before running the rest.

Code: Select all

VA_GetDevice(device_desc="playback")
This is the function definition.
device_desc="playback" - specifies the default option - this means that if you don't supply an argument for device_desc then it will just use "playback".

This is how you could call it if you want to use the function:

Code: Select all

#Include %A_ScriptDir%\VA v2.3.ahk

Device := VA_GetDevice("playback")
MsgBox % Device
DeviceName := VA_GetDeviceName(Device)
MsgBox % DeviceName
ObjRelease(Device) ; edit - added this line as per the VA_GetDevice docs
Last edited by Datapoint on 08 Jun 2019, 00:37, edited 1 time in total.
User avatar
Brain-Shock Omega
Posts: 11
Joined: 13 Mar 2019, 03:41

Re: New to Using Libraries, Not Understanding How Vista Audio Control Functions works

08 Jun 2019, 00:03

Thanks for the reply. I tried what you put

Code: Select all

Device := VA_GetDevice("playback")
MsgBox % Device
DeviceName := VA_GetDeviceName(Device)
MsgBox % DeviceName
and it works giving me the correct device name on the second msgbox.
On the first one though, what does that number mean, if it stayed the same every-time it would make more sense to me, but it seems to give a different number ever time I run it, but the device name it gives stays the same. Is that number supposed to be what the device is currently addressed as in the registry at different times? Or is it something completely different.

So, does that mean anytime a function needs a device as an argument, that I need to first call VA_GetDevice and give whatever it returns to that other function?

Also, is that why I was getting a blank msgbox before, because I was not giving a proper registry number?

Appreciate the explanation and help.
User avatar
Datapoint
Posts: 68
Joined: 18 Mar 2018, 17:06

Re: New to Using Libraries, Not Understanding How Vista Audio Control Functions works

08 Jun 2019, 00:30

Brain-Shock Omega wrote:
08 Jun 2019, 00:03
Thanks for the reply. I tried what you put

Code: Select all

Device := VA_GetDevice("playback")
MsgBox % Device
DeviceName := VA_GetDeviceName(Device)
MsgBox % DeviceName
and it works giving me the correct device name on the second msgbox.
On the first one though, what does that number mean, if it stayed the same every-time it would make more sense to me, but it seems to give a different number ever time I run it, but the device name it gives stays the same. Is that number supposed to be what the device is currently addressed as in the registry at different times? Or is it something completely different.
The download link on the original page seems to still work. And it has the documentation (not sure if the download you are using has documentation) - https://autohotkey.com/board/topic/21984-vista-audio-control-functions/
Download it and open VA.html for help.
VA_GetDevice says:
Gets a pointer to the IMMDevice interface of an audio endpoint device.
So you are looking at a memory address in the first message box.
So, does that mean anytime a function needs a device as an argument, that I need to first call VA_GetDevice and give whatever it returns to that other function?
Probably? but I'm not an expert on this library.
Also, is that why I was getting a blank msgbox before, because I was not giving a proper registry number?
Before, you were basically passing only the value zero to the function.
try this MsgBox % device_desc="playback"
This is saying: "Does the variable named 'device_desc' (which will be empty/blank) equal 'playback'?" - The answer will always be false (0).
User avatar
Brain-Shock Omega
Posts: 11
Joined: 13 Mar 2019, 03:41

Re: New to Using Libraries, Not Understanding How Vista Audio Control Functions works

08 Jun 2019, 00:59

Oh OK, thanks for the information, I will have to read more of the documentation. Hopefully with what you gave me I will have a better idea of what I am reading, especially the arguments and IMM parts.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], doctorafterman, Flipeador, Google [Bot], kyuuuri, MalKov, Percy and 223 guests