Help With SoundGet/SoundSet

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Help With SoundGet/SoundSet

Post by tatermakes » 08 Jan 2021, 23:31

Hi, I'm a relative neophyte trying to add a hotkey to mute/unmute an audio source. The use case is that I have an audio line into my PC from a gaming console so I can get game audio to my headphones which are plugged into my computer. When I'm not using the console, the line has a constant low-grade buzz, so I generally mute it. When I'm going to use the console, I currently open up the volume mixer and unmute the audio source. I'd like to speed this up with a hotkey.

I've been messing around with the following scratch script to see if I can identify the correct audio source to mute (switching MASTER out for each of the options in turn), and none of them correspond to the console's audio source.

Code: Select all

#SingleInstance, Force
q::
SoundGet, woo, MASTER
MsgBox, %woo%
return
MASTER and WAVE appear to be my headphones, and every other option mentioned on the SoundSet page (DIGITAL, LINE, MICROPHONE, SYNTH, CD, TELEPHONE, PCSPEAKER, AUX, ANALOG, HEADPHONES, and N/A) all come back blank in the above code, while the console's audio source is sitting at 18 in the volume mixer.

I'm aware there's also a way to mute/unmute a specific window (and possibly application?) but if there's a window or application associated with this audio source I'm not aware of it. The console audio is recognized as a recording device, and I followed steps in this link in order to get it playing out of my headphones.

Is there a better way to go about this? Am I missing something?

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 13:38

I've gone through this many times myself. The first thing to do is set the volume of your headphones (in the Volume properties on your PC) to somewhere around, say 75% (give or take). Then, run the script below. A window will pop up with any audio devices attached to your computer. Under the 'Setting' column, look for the entry which is at about 75%. This will be your headphones. Then, look under the 'Mixer' column on the far right and the corresponding number there will give you the headphone's id.

Code: Select all

;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         A.N.Other <myemail@nowhere.com>
;
; Script Function:
;	Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; SOUNDCARD ANALYSIS
; Use the following script to discover your soundcard's capabilities (component types and control types).
; It displays the results in a simple ListView.

SetBatchLines -1
SplashTextOn,,, Gathering Soundcard Info...

; Most of the pure numbers below probably don't exist in any mixer, but they're queried for completeness.
; The numbers correspond to the following items (in order): CUSTOM, BOOLEANMETER, SIGNEDMETER, PEAKMETER,
; UNSIGNEDMETER, BOOLEAN, BUTTON, DECIBELS, SIGNED, UNSIGNED, PERCENT, SLIDER, FADER, SINGLESELECT, MUX,
; MULTIPLESELECT, MIXER, MICROTIME, MILLITIME
ControlTypes = VOLUME,ONOFF,MUTE,MONO,LOUDNESS,STEREOENH,BASSBOOST,PAN,QSOUNDPAN,BASS,TREBLE,EQUALIZER,0x00000000, 0x10010000,0x10020000,0x10020001,0x10030000,0x20010000,0x21010000,0x30040000,0x30020000,0x30030000,0x30050000,0x40020000,0x50030000,0x70010000,0x70010001,0x71010000,0x71010001,0x60030000,0x61030000

;ComponentTypes = MASTER,HEADPHONES,DIGITAL,LINE,MICROPHONE,SYNTH,CD,TELEPHONE,PCSPEAKER,WAVE,AUX,ANALOG,N/A
ComponentTypes = N/A ;this is what the logitech webcam mic is determined as


; Create a ListView and prepare for the main loop:
Gui, Add, Listview, w400 h400 vMyListView, Component Type|Control Type|Setting|Mixer
LV_ModifyCol(4, "Integer")
SetFormat, Float, 0.2  ; Limit number of decimal places in percentages to two.

Loop  ; For each mixer number that exists in the system, query its capabilities.
{
    CurrMixer := A_Index
    SoundGet, Setting,,, %CurrMixer%
    if ErrorLevel = Can't Open Specified Mixer  ; Any error other than this indicates that the mixer exists.
        break

    ; For each component type that exists in this mixer, query its instances and control types:
    Loop, parse, ComponentTypes, `,
    {
        CurrComponent := A_LoopField
        ; First check if this component type even exists in the mixer:
        SoundGet, Setting, %CurrComponent%,, %CurrMixer%
        if ErrorLevel = Mixer Doesn't Support This Component Type
            continue  ; Start a new iteration to move on to the next component type.
        Loop  ; For each instance of this component type, query its control types.
        {
            CurrInstance := A_Index
            ; First check if this instance of this instance even exists in the mixer:
            SoundGet, Setting, %CurrComponent%:%CurrInstance%,, %CurrMixer%
            ; Checking for both of the following errors allows this script to run on older versions:
            if ErrorLevel in Mixer Doesn't Have That Many of That Component Type,Invalid Control Type or Component Type
                break  ; No more instances of this component type.
            ; Get the current setting of each control type that exists in this instance of this component:
            Loop, parse, ControlTypes, `,
            {
                CurrControl := A_LoopField
                SoundGet, Setting, %CurrComponent%:%CurrInstance%, %CurrControl%, %CurrMixer%
                ; Checking for both of the following errors allows this script to run on older versions:
                if ErrorLevel in Component Doesn't Support This Control Type,Invalid Control Type or Component Type
                    continue
                if ErrorLevel  ; Some other error, which is unexpected so show it in the results.
                    Setting := ErrorLevel
                ComponentString := CurrComponent
                if CurrInstance > 1
                    ComponentString = %ComponentString%:%CurrInstance%
                LV_Add("", ComponentString, CurrControl, Setting, CurrMixer)
            }  ; For each control type.
        }  ; For each component instance.
    }  ; For each component type.
}  ; For each mixer.

Loop % LV_GetCount("Col")  ; Auto-size each column to fit its contents.
    LV_ModifyCol(A_Index, "AutoHdr")

SplashTextOff
Gui, Show
return

GuiClose:
ExitApp
Once you have the headphone's id, run the following script and replace the '10' with the id of your headphones:

Code: Select all

x::
mwt_mic=10  ;instead of 10, type in the headphone id
SoundSet , +1 , N/A , Mute , %mwt_mic% ;this will MUTE the headphones
Return

z::
mwt_mic=10  ;instead of 10, type in the headphone id
SoundSet , 0 , N/A , Mute , %mwt_mic% ;this will UN-MUTE the headphones
Return
Hopefully this will work for you. Credits to who wrote the first script above are in the contents of the code.

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 09 Jan 2021, 18:32

Thank you for the reply! I kept the console's volume in the mixer at 18, and unfortunately the first script doesn't seem to have found it. I see 3 mixer numbers, two have both VOLUME and MUTE controls, one only has VOLUME controls. None of the volumes matched the 18 I set the console to, and just to be sure I tried using each of the mixer numbers in the second script, none of them muted anything in my mixer.

There are also far fewer entries in the gui from the first script than there are items in my volume mixer in general, which I suppose is something I can dig into to try to get this figured out.

Any further help would be greatly appreciated!

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 09 Jan 2021, 18:37

Some progress. Haven't found the console yet, but to anyone else looking to use the above scripts, you should comment out line 50 and uncomment line 49. The script as-is is only looking at component type N/A, switching which line is commented will instead look at all component types. And as expected, running the first script with that change finds a lot more stuff. At first glance still nothing that matches the 18 volume level though, but I'm going to go through and mute each of the new sources in turn and see if by chance anything works.

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 09 Jan 2021, 19:55

Still no success. I went through every audio device in the full list, eventually found my headphones but nothing corresponded to my console (as I'd guessed when I didn't see its audio mixer value in the list).

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 20:13

ok hang on let me take a look..

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 20:17

ok, this is working for me:

Code: Select all

x::
SoundSet, +1, line, mute
each time x is pressed, it mutes or un-mutes the line in depending on its current state. But I must admit I had to play around with my PC's audio settings quite a bit in order for it to finally work...

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 21:12

I think what was causing the confusion on my end was the 2 instances of 'line in' in the audio properties of Windows. I think I was targeting the wrong one here.

In the sound properties, in order for this to work, I go into the 'Playback' tab, not the 'Recording' tab.

And in the Playback tab, a green checkmark should indicate which device the PC is using for its main playback output. So with that device highlighted, I went to the 'Properties' button, and then to the 'Levels' tab. And in there I found 'Line In' and that is the slider which adjusts the volume of the line in.

I had incorrectly went into the 'Recording' tab in the initial step. And when going into that tab, we also see a 'Line In' option, which I think is not the one we need. This is where I wad scratching my head as to why nothing was happening when I pressed x.

Hope this works for you too.

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 09 Jan 2021, 23:07

Thank you so much for continuing to help. Unfortunately that new script also doesn't seem to mute my console. Like I mentioned, SoundGet shows absolutely nothing for LINE components, and the above script that pulls every audio device also shows no LINE components. I tried muting every component that the script found, and none of them muted my console. And I'm sure the console is in my Recording tab, I've changed the name and icon of the audio device so I can definitely see it there. I hear it in my headphones because I went into Properties -> Listen -> Listen to this device. I think part of the issue here is that this particular audio device is just not showing up anywhere in SoundGet, so it seems like AHK cannot find it.

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 23:19

try replacing the word 'line' with any of the following: MASTER, DIGITAL, LINE, MICROPHONE, SYNTH, CD, TELEPHONE, PCSPEAKER, WAVE, AUX, ANALOG, HEADPHONES, or N/A

the problem for me was that it didn't work at first either. It took a good half hour or so of messing around with the audio properties before I got it going

edit - sorry, I just saw that you already tried that.

What's the default device for your computer's sound playback? Is it Speakers or is it something else

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 09 Jan 2021, 23:27

Yeah my headphones are the default, which is what I want

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 23:27

don't worry about any of the previous scripts in this topic, the following one should work just fine once we solve the issue:

Code: Select all

x::
SoundSet, +1, line, mute

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 23:28

tatermakes wrote:
09 Jan 2021, 23:27
Yeah my headphones are the default, which is what I want
ok, I'm wondering if this might be the issue. Are you able to switch the default audio device to just 'speakers' for a minute?

We're looking for the 'Speakers' option which will reflect the soundcard that your computer has installed


for example, I have two 'Speakers' options, but the one I use is "Speakers 2-Realtek High Definition Audio"

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 09 Jan 2021, 23:34

Speakers are now my default. Running the script that scrapes for all audio devices doesn't show anything at 18 volume (where my console is in the mixer), though I just realized in the properties the console was at 85 volume and something does match that. Going to try muting that again and see if the console mutes in the properties pane.

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 09 Jan 2021, 23:38

THAT DID IT! When I tried muting every audio device listed I looked for the console being muted in the mixer, but it was being muted in the device Properties instead so I didn't see it.

Thank you so much for bearing with me. One final question, do you know if should I expect that Mixer Number to change when I restart my computer or should it be static?

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 23:47

My mixer number changes every time I plug in/out some kind of a USB device. All the numbers shift when this happens. I don't know if it happens when plugging in/unplugging audio cables into the back of the PC (line in, line out, etc) - I don't think that affects it though. Just USB devices.

But remember - I dont think any of the initial scripts in this topic are necessary now. This simple 2 lines of code should work:

Code: Select all

x::
SoundSet, +1, line, mute
But I'm thinking that in order for this to work, your default audio device has to be your soundcard's 'Speakers' option. Then once that is set as default, hit the 'Properties' button and then go to the 'Levels' tab and in there you should see Line In. It should have a slider beside it and the mute/unmute button. This is the one that the above code will affect. But If your default audio device is set to your headphones, then when you hit the 'Properties' button and again go to the levels tab, the 'Line In' option will not be there and that's why nothing happens when pressing x. So I guess in retrospect the above code may only work when using the soundcard's Speakers option as the default sound output device. All I know is that on my end, nothing in the 'Recording' tab had anything to do with it, it was all to do with everything under the 'Playback' tab

But hey if you got it working using the scripts earlier in this topic then that's great!

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 09 Jan 2021, 23:53

Unfortunately it's not a LINE device, it's under master, mixer number 9. I modified the script to get it to work.

Code: Select all

SoundSet, +1, MASTER, mute, 9
It also still works after going back to my headphones.

scriptor2016
Posts: 863
Joined: 21 Dec 2015, 02:34

Re: Help With SoundGet/SoundSet

Post by scriptor2016 » 09 Jan 2021, 23:57

alright awesome haha.

yeah just remember as you plug in/out additional USB devices the mixer number will probably change so you'll have to go through the whole scenario again to retrieve that ID number.

tatermakes
Posts: 12
Joined: 08 Jan 2021, 22:57

Re: Help With SoundGet/SoundSet

Post by tatermakes » 10 Jan 2021, 00:01

But unfortunately that means I might have to contend with a changing mixer number. But I'll cross that when I get to it.

For anyone looking for a TL:DR for this thread:
The issue I was trying to solve was that polling all audio devices (using the first script scriptor2016 posted above) didn't find any device with the same volume level as the device I was looking for.
The mistake I was making was that I was looking for the value that it was set to in the mixer, but when polling devices with SoundGet it was showing me the volume it was set to in Control Panel -> Sound and Hardware -> Sound -> [device you're looking for] -> Device Properties -> Levels.
E.G. my console was set to 18 in the mixer, but 85 in Device Property Levels. Printing out every device SoundGet could find showed nothing at 18 volume, but it did have something at 85 volume.

Post Reply

Return to “Ask for Help (v1)”