Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Changing default audio device


  • Please log in to reply
104 replies to this topic
flipswitch5point
  • Members
  • 31 posts
  • Last active: Jan 13 2012 07:08 PM
  • Joined: 11 May 2011
tjmonk15 & Guest1 - just tried it, seems to be working excellently! Thanks! :D

EDIT: Well, it gives a little trouble to start working, but it works.

(I get the "Cannot find either device" message a couple times first)

lunis
  • Guests
  • Last active:
  • Joined: --
Do I need a program to execute your code, or can I copy your code and paste it in a file? Which file type should it be?

Lunis
  • Guests
  • Last active:
  • Joined: --
I don't run external .exe files, for security reasons, so I've created a vbs file with a few .bat files to switch between my 3 output profiles (default sound card, usb card and bluetooth). I posted all the code at http://www.4shared.c...nd_scripts.html

Great. It works for me now. Any way to suppress the dialog: "New Sound Card: XXX XXXX XXX"??

LNDN
  • Guests
  • Last active:
  • Joined: --
Hi guys, So I created a script that switches the default sound card on open and runs a program on ASIO4all. When it closes it shuts down again. I want to a add a part that basically allows me to use the [ en ] keys to control the volume of the secondary soundcard (which is in use by ASIO4all) and that I CAN change the volume of.

Can anybody help me?

bitflow
  • Members
  • 5 posts
  • Last active: Dec 04 2011 07:48 PM
  • Joined: 08 Jan 2010
I have three endpoints I regularly switch between. Sometimes the USB device isn't plugged in, so all the techniques that depend on the device being in a particular position in the list ended up being unreliable when devices present changed.

Here's another take at it keying off a part of the endpoint identifier and based on all the great work in this post, plus a moving the window off-screen and a traytip showing activated device

ActivateSoundDeviceEndpoint(DeviceName) {
	; close sound panel if it's open
	IfWinExist Sound ahk_class #32770
		WinClose

	; open sound panel
	Run rundll32.exe shell32.dll`,Control_RunDLL mmsys.cpl`,`,0  ; similar to Run mmsys.cpl
	WinWait Sound ahk_class #32770

	; arrow-key movement fails if pointer is over window; get window out of the way
	WinMove, 0,8000

	; get, then iterate over list of sound device endpoints
	ControlGet, len, List, Count, SysListView321
	Loop %len%
	{
		; select first or next endpoint
		ControlSend, SysListView321, {Down}

		; get endpoint info
		ControlGet, Device_Info, List, Selected, SysListView321

		; endpoint contans our DeviceName?
		IfInString, Device_Info, %DeviceName%
		{
			; extract endpoint title and device it's on into MatchedTitle and MatchedDevice
			RegExMatch(Device_Info, "(?P<Title>.+)\t(?P<Device>.+)\t", Matched)

			; set this endpoint as system default (activate it)
			ControlClick, &Set Default
			ControlClick, OK
			WinWaitClose

			; play a sound on the newly activated device
			SoundPlay, *64  ; Asterisk (info)

			; show a tray tip with the new endpoint
			TrayTip %MatchedTitle%, %MatchedDevice% is now active, 2, 17 ; 2 seconds, info icon (1) without sound (+16)
			SetTimer, RemoveTrayTip, 2000  ; TrayTip durations under 10 seconds don't work, remove ourselves after 2 second timer

			; stop iterating
			Break
		}
	}
	return
}


RemoveTrayTip() {
   SetTimer, RemoveTrayTip, Off
   TrayTip  ; without parameters, removes displayed traytip
}

In my case, I wanted to bind to a single key, varying which sound endpoint activates by the number of times I press that key in succession. ins0mniaque's PatternHotKey provided the mechanism

; Activate sound device endpoint
;     '.'   (sinle short press)  activates Bluetooth headset
;     '..'  (double short press) activates Speakers
;     '...' (triple short press) activates USB headset
ScrollLock::PatternHotKey(".->ActivateSoundDeviceEndpoint(Bluetooth)"
                        , "..->ActivateSoundDeviceEndpoint(Klipsh)"
                        , "...->ActivateSoundDeviceEndpoint(Logitech)")


Jackinthex
  • Guests
  • Last active:
  • Joined: --
Can i assign this script to MUTE button on keyboard?
If so how would one do that.

bitflow
  • Members
  • 5 posts
  • Last active: Dec 04 2011 07:48 PM
  • Joined: 08 Jan 2010
To assign something to the mute key, look at the List of Keys, Mouse Buttons, and Joystick Controls to see that Autohotkey knows the Mute key as Volume_Mute.

Try replacing the part to the left of the :: with Volume_Mute, as in
Volume_Mute::PatternHotKey()


jackinthex
  • Guests
  • Last active:
  • Joined: --
Weird... cant get it to work from mute key

codydotmorton
  • Members
  • 1 posts
  • Last active: Jan 01 2012 02:18 PM
  • Joined: 01 Jan 2012
i'm not sure how to look for this, but is there any way to force the completion of the script made by tjmonk15 even if the mouse is on the 'Sound' window? Either that or minimize it so it's not seen?

Thanks and thank you for the code. You guys have helped me so much






Edit: Scratch that. i just changed the ControlClick,OK to Send {enter} and it works. The window pops up, does what i need, and closes, no matter if the mouse is on the window.

P.S. Figures that i will figure it out after i have posted. Just thought that i would post this for others who are looking to refine it so it doesn't stay open.

Japievb
  • Guests
  • Last active:
  • Joined: --
If you are running a Dutch version of Windows7 you could use the following script.

#g::
Run, mmsys.cpl
WinWait,Geluid
ControlSend,SysListView321,{Down}
ControlGet, isEnabled, Enabled,,&Als standaard instellen
if(!isEnabled)
{
ControlSend,SysListView321,{Down 2}
}
ControlClick,&Als standaard instellen
ControlClick,OK
WinWaitClose
return

Arujei
  • Members
  • 8 posts
  • Last active: Sep 06 2011 08:35 AM
  • Joined: 31 Oct 2005
I've taken what I found here for my use. This will cycle through all available devices starting from the last device.

aka: If the current default device is #2, it will start from #3 and work its way down till it finds a device it can enable. When it reaches bottom it will cycle back to the top. It stores the last device in a ini file.

There is much improvement and cleaning up that can be done to it. For now it just works :3.

#+a::
IfWinExist Sound
{
   WinKill Sound
}
Run, mmsys.cpl
WinWait,Sound
MouseGetPos,xpos,ypos
if (xpos<600) {
   WinMove,800,0
} else {
   WinMove,10,10
}
devicecnt = 0
IfExist, ahk.ini
{
IniRead, lastdevice, ahk.ini, Audio, LD
} else {
IniWrite, 1, ahk.ini, Audio, LD
lastdevice := 1
}

deviceloop := lastdevice
ControlGet, len, List, Count, SysListView321
Loop %len%
{
if lastdevice = %len%
{
    ControlSend,SysListView321,{Home}
    lastdevice :=
    deviceloop :=
    ++devicecnt
} 
else 
{
if deviceloop > 0 
{
Loop %deviceloop%
{
ControlSend,SysListView321,{Down}
--deviceloop
++devicecnt
}
deviceloop := 0
} else {

if devicecnt = %len%
{
    ControlSend,SysListView321,{Home}
	devicecnt := 1
}
else {
++devicecnt
    ControlSend,SysListView321,{Down}
}
}
}
    ControlGet, isEnabled, Enabled,,&Set Default
    if(isEnabled)
    {
        ControlClick,&Set Default
        Sleep 500
        ControlClick,OK
		IniWrite, %devicecnt%, ahk.ini, Audio, LD
	devicecnt :=
        Return
    }
}
Return


nalvin
  • Guests
  • Last active:
  • Joined: --
Mon script en modifiant les votres ^^

^!F11::
Run, mmsys.cpl
WinWait,Son
ControlSend,SysListView321,{Up 5}
if(!isEnabled)
{
ControlSend,SysListView321,{Down 1}
}
Send !{D}
ControlClick,OK
return

^!F12::
Run, mmsys.cpl
WinWait,Son
ControlSend,SysListView321,{Up 5}
Send !{D}
ControlClick,OK
return

borna
  • Guests
  • Last active:
  • Joined: --
I haven't found an AHK solution for changing audio device in Win 7 without sending commands to a GUI.

I did however find this non-AHK code that seems to solve that: http://www.daveament... ... windows-7/

But I'm not good enough at coding to be able to translate that into AHK code. Hopefully someone else here is.

tomchuk
  • Members
  • 2 posts
  • Last active: Feb 17 2012 10:55 PM
  • Joined: 15 Feb 2012
Ok, I've got something working very well thanks to borna's link. I've modified the code to switch the playback device for all roles (console, multimedia, communication). The original code would only switch the default sound device for console (and the previous default would be assigned multimedia and communication roles). I've also modified it so that the return value is the number of available devices when called with no arguments.

Here is the zip file with source code and binary. The exe is in the "Release" directory. If you don't trust me (and why would you), you can download the zip file, download and install Visual C++ Express (for free), and open the .sln file. Hit F7 and it will build a fresh release.

Move the EndPointController.exe file somewhere on your %PATH% (I put mine in C:\Windows\System32\). Run it from cmd to get a list of your devices with associated device IDs. And here's the AHK code:

; Cycle through playback devices
^!s::
if not TotalEndPoints {
	TotalEndPoints = 0
	RunWait, %comspec% /c EndPointController > %A_Temp%\endpoints.txt,, Hide
	Loop, Read, %A_Temp%\endpoints.txt
		TotalEndPoints++
	FileDelete, %A_Temp%\endpoints.txt
}
CurrentEndPoint++
CurrentEndPoint := Mod(CurrentEndPoint, TotalEndPoints)
RunWait, %comspec% /c EndPointController %CurrentEndPoint% ,, Hide
return

; Switch to playback device 0
^!0::
RunWait, %comspec% /c EndPointController 0 ,, Hide
return

; Switch to playback device 1
^!1::
RunWait, %comspec% /c EndPointController 1 ,, Hide
return

; Switch to playback device 2
^!2::
RunWait, %comspec% /c EndPointController 2 ,, Hide
return


Cragaha
  • Members
  • 265 posts
  • Last active: Jan 04 2016 02:24 AM
  • Joined: 19 Nov 2010
Very nice tomchuk :)
But cycling/^!s:: does nothing on my side.