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
  • Guests
  • Last active:
  • Joined: --

Here's another approach. Still a little clunky, but this will scroll to the next sound card in Windows 7 and loop back to the top from the bottom. You can uncomment the msgbox toward the end if you want the selected card to be displayed; and/or comment out the SoundPlay line if you don't want an audio alert on the new card.

There is probably a way to make this a little more efficient; ideas welcome.

^+`::
Run,mmsys.cpl
WinWait,Sound
IfWinNotActive,Sound WinActivate,Sound
WinWaitActive,Sound
ControlSend,SysListView321,{PgUp}  
x := 1
ControlGet, isEnabled, Enabled,,&Set Default
ControlGet, max, List, Count, SysListView321
while (isEnabled AND x <= max) {
   ControlSend,SysListView321,{Down}  
   ControlGet, isEnabled, Enabled,,&Set Default
   x := x+1
}
if (x >= max) {
   x := 1
} else {
   x := x+1
}
sleep 50
ControlSend,SysListView321,{PgUp}  
while (x > 1) {
  sleep 50
  ControlSend,SysListView321,{Down}  
  x := x-1
}
ControlGet, Current, List, Selected, SysListView321
; msgbox %Current%
ControlClick,&Set Default
ControlClick,OK
SoundPlay *48

return
[/code]


This script seems to work (great work). However in my setup three playback devices are listed: Speakers Logitech, Speakers SB X-Fi and SPDIF-Out SB X-Fi.

This seems to mess up the counting in the script, any idea on how I could make it toggle between the first to devices only?

AngelsNecropolis
  • Guests
  • Last active:
  • Joined: --

I just updated to windows 7 and needed a toggle version of this script. I mixed and matched what I had with the above posts and came up with this.

This will set the first device active if it isn't already.
If the first device is active then it will activate the 3rd device.

Additionally once a device is picked it plays a sound so you know which device is selected.

^+`::
Run, mmsys.cpl
WinWait,Sound
ControlSend,SysListView321,{Down}
ControlGet, isEnabled, Enabled,,&Set Default
if(!isEnabled)
{
	ControlSend,SysListView321,{Down 2}
}
ControlClick,&Set Default
ControlClick,OK
WinWaitClose
SoundPlay, *-1
return


Just found this site and program tonight so pardon my ignorance. I use this script to successfully toggle between my Speakers and Headset for playback. However, I also want to Enable/Disable the default recording device. I use a G330 gaming headset and basically just want to easily enable and disable is as I login and out of my games.

Thanks for the help.

dan112123
  • Members
  • 8 posts
  • Last active: Sep 18 2010 08:12 AM
  • Joined: 23 Dec 2008
I looked at a couple of replies here and decided to fix up the bugs in the posts here and improve some things.

Here is what I did:

* Fixed looping error so now if the selection is already at the bottom it will properly circle back up
* Fixed a problem where if you have too any devices PgUp wouldn't go all the way up
* Fixed a problem with the Sound window already being up and {pgup} button going to the next tab effectively switching out of this tab
* other minor fixes
+ Added better logic that finds avaliable devices
+ Added SPDIF output skipping

; -------------------------------------------------------------
; Current Default Playback Device Switcher
; -------------------------------------------------------------
*#8::
    IfWinExist Sound
    {
        WinKill Sound
    }
    Run rundll32.exe shell32.dll`,Control_RunDLL mmsys.cpl`,`,0 

    WinWait,Sound 
    IfWinNotActive,Sound WinActivate,Sound 
    WinWaitActive,Sound
 
    ControlSend,SysListView321,{Home}  
    x := 1
    devNum := 0
    isSecondLoop := false
    isEnabled := false
;    ControlGet, isEnabled, Enabled,,&Set Default 
    ControlGet, devNum, List, Count, SysListView321 
; isEnabled AND 
    StringCaseSense Off
    loop 
    { 
        ControlGet, Current, List, Selected, SysListView321
        if(InStr(Current, "default device") OR x > devNum)
        {
            break
        }
        x := x + 1
        ControlSend,SysListView321,{Down}  
    } 

    if (x >= devNum) 
    {
        ControlSend,SysListView321,{Home}
        x := 1 
    } 

    loop 
    { 
        ControlGet, Current, List, Selected, SysListView321
        ControlGet, isEnabled, Enabled,,&Set Default 
        ;msgbox %isEnabled% %Current%

        ; exclude S/PDIF cards
        if(InStr(Current, "PDIF"))
        {
            isEnabled := false
        }

        if (x > devNum AND not isEnabled)
        {
            ControlSend,SysListView321,{Home}
            x := 1
            isSecondLoop := true
        }
        if (x > devNum OR isEnabled)
        {
            ;msgbox breaking now %x% %devNum% %isEnabled% %isSecondLoop%
            break
        }
        if (x > devNum AND isSecondLoop)
        {
            ; infinite loop breaker if no devices avaliable
            ;msgbox breaking sec %x% %devNum% %isEnabled% %isSecondLoop%
        }

        ControlSend,SysListView321,{Down}
        x := x + 1
    } 


;ControlGet, Current, List, Selected, SysListView321 
;msgbox %Current% 
    Sleep 100
    ControlClick,&Set Default 
    ControlClick,OK 
    SoundPlay *48 
    WinKill Sound

return 


tjmonk15
  • Guests
  • Last active:
  • Joined: --
Just figured I'd post mine since I just spent an hour redoing it.


#+a::
Run, mmsys.cpl
WinWait,Sound

device1 = VIA High Definition Audio
device2 = Creative USB Headset

ControlGet, len, List, Count, SysListView321
Loop %len%
{
    ControlSend,SysListView321,{Down}
    ControlGet, List, List, Selected, SysListView321
    Loop, Parse, List, `n  ; Rows are delimited by linefeeds (`n).
    {
        strDevice = ''
        RowNumber := A_Index
        Loop, Parse, A_LoopField, %A_Tab%
        {
            if A_Index = 2
            {
                strDevice=%A_LoopField%
                break
            }
        }

        if(strDevice = device1)
        {
            ControlGet, isEnabled, Enabled,,&Set Default
            if(isEnabled)
            {
                ControlClick,&Set Default
                ControlClick,OK
                WinWaitClose
                SoundPlay, *-1
                return
            }
        }
        if(strDevice = device2)
        {
            ControlGet, isEnabled, Enabled,,&Set Default
            if(isEnabled)
            {
                ControlClick,&Set Default
                ControlClick,OK
                WinWaitClose
                SoundPlay, *-1
                return
            }
        }
    }
}

MsgBox Did not find either device.

return

Change device1 and device2 at the top to the 1st subtitle under the 2 devices you want to switch to.

As an example, my Headset says the following
Headset
Creative USB Headset <--- Use this
Ready

and My speakers say
Speakers
VIA High Definition Audio <--- Use this
Default Device

Guest1
  • Guests
  • Last active:
  • Joined: --

Just figured I'd post mine since I just spent an hour redoing it.


#+a::
Run, mmsys.cpl
WinWait,Sound

device1 = VIA High Definition Audio
device2 = Creative USB Headset

ControlGet, len, List, Count, SysListView321
Loop %len%
{
    ControlSend,SysListView321,{Down}
    ControlGet, List, List, Selected, SysListView321
    Loop, Parse, List, `n  ; Rows are delimited by linefeeds (`n).
    {
        strDevice = ''
        RowNumber := A_Index
        Loop, Parse, A_LoopField, %A_Tab%
        {
            if A_Index = 2
            {
                strDevice=%A_LoopField%
                break
            }
        }

        if(strDevice = device1)
        {
            ControlGet, isEnabled, Enabled,,&Set Default
            if(isEnabled)
            {
                ControlClick,&Set Default
                ControlClick,OK
                WinWaitClose
                SoundPlay, *-1
                return
            }
        }
        if(strDevice = device2)
        {
            ControlGet, isEnabled, Enabled,,&Set Default
            if(isEnabled)
            {
                ControlClick,&Set Default
                ControlClick,OK
                WinWaitClose
                SoundPlay, *-1
                return
            }
        }
    }
}

MsgBox Did not find either device.

return

Change device1 and device2 at the top to the 1st subtitle under the 2 devices you want to switch to.

As an example, my Headset says the following
Headset
Creative USB Headset <--- Use this
Ready

and My speakers say
Speakers
VIA High Definition Audio <--- Use this
Default Device


This works great for me, except the second line of my audio devices is the same. By changing if A_Index = 2 to if A_Index = 1 then I can use the top line. i.e.

Speakers / Headphones <--- Use this
SigmaTel High Definition Audio CODEC
Ready

and

HDMI <--- Use this
SigmaTel High Definition Audio CODEC
Default Device

kensiko
  • Guests
  • Last active:
  • Joined: --
Hello,

I've been struggling to get this code to work since yesterday. I'm french and this is for my girlfriend who just got a USB headset and wants to switch sound easily.

The code is not working in french. I first changed the text "Sound" for the window by "Son". After that, I can't get to "Set default" button, the name in french is "Par défaut" and the accent is causing problems with AutoHotkey. I tried "Config" = Configurer button, "Prop" = Propriété button (properties), they work just fine, but NOT PAR DÉFAUT ! I tried "Pa", "FAUT", I tried changing the SetTitleMatchMode to 2, not good.

I didn't find any VBscript or VB code that seems to work either on changing default sound device.

Any help would be appreciated.

kensiko
  • Members
  • 2 posts
  • Last active: Mar 16 2011 10:52 AM
  • Joined: 22 Feb 2011
Flag

skoos
  • Guests
  • Last active:
  • Joined: --
Dear ALL!

I'm absolutely new in this, however, I'd need 2(two) exe (or whatever) files to change from the current audio device to the other one.

I need two, beacuse my father is going to use it, and I'd like to have 2 simple buttons, one for each sound device, so I'd name the exe files like HEADSET and SPEAKERS

For WinXP home, nothing special.

How could i do that? Please help me solving this!

Thanks a LOT!

haltux
  • Members
  • 5 posts
  • Last active: Dec 30 2009 02:49 PM
  • Joined: 29 Dec 2009
There is a general problem that have not been solved so far, even though someone has already pointed it out: all this scripts do not work when the mouse is over the window which is poping up. Here is my (ugly) solution:

WinWait,Sound
MouseGetPos,xpos,ypos
if (xpos<600) {
	WinMove,800,0
} else {
	WinMove,0,0
}


haltux
  • Members
  • 5 posts
  • Last active: Dec 30 2009 02:49 PM
  • Joined: 29 Dec 2009

Dear ALL!

I'm absolutely new in this, however, I'd need 2(two) exe (or whatever) files to change from the current audio device to the other one.

I need two, beacuse my father is going to use it, and I'd like to have 2 simple buttons, one for each sound device, so I'd name the exe files like HEADSET and SPEAKERS

For WinXP home, nothing special.

How could i do that? Please help me solving this!

Thanks a LOT!


Install AutoHotKey and create one XXX.ahk file (with any text editor).
In these ahk file, you can put something like that:

IfWinExist Sound
{
   WinKill Sound
}
Run, mmsys.cpl
WinWait,Sound
MouseGetPos,xpos,ypos
if (xpos<800) {
	WinMove,1000,0
} else {
	WinMove,0,0
}
ControlSend,SysListView321,{Up 6}
ControlSend,SysListView321,{Down [color=red]0[/color]}
ControlGet, isEnabled, Enabled,,&Set Default
ControlClick,&Set Default
ControlClick,OK
WinWaitClose
SoundPlay, *-1
return

Instead of 0, you put the position of the device you want to set with the corresponding script in the sound device list (starting at 0).

When you double click on the ahk file it should do what you expect.

haltux
  • Members
  • 5 posts
  • Last active: Dec 30 2009 02:49 PM
  • Joined: 29 Dec 2009

Hello,

After that, I can't get to "Set default" button, the name in french is "Par défaut" and the accent is causing problems with AutoHotkey. I tried "Config" = Configurer button, "Prop" = Propriété button (properties), they work just fine, but NOT PAR DÉFAUT ! I tried "Pa", "FAUT", I tried changing the SetTitleMatchMode to 2, not good.


I have just tested WinWait with a window with an accent (some of my applications are in french but my Windows is in english), it works fine.

Check that your script is ok (try mine just over this answer) and try with another text editor, it could be a character encoding pb. I used notepad.

kensiko
  • Members
  • 2 posts
  • Last active: Mar 16 2011 10:52 AM
  • Joined: 22 Feb 2011
Thanks, but I ended up using a ExeFile from a gadget for Vista: SetVistaDefaultAudio and VBScript to change the audio. I save the previous default device and I don't check which one is default.

FuBaSh
  • Guests
  • Last active:
  • Joined: --
; Swap Default Recording Device for Win7



*F5::

    Run, mmsys.cpl

    WinWait,Sound

    ControlSend,SysListView321,{Control down}{Tab}{Control up}

    ControlSend,SysListView321,{Down 5}

    ControlClick,&Set Default

    ControlClick,OK

    return



*F6::

    Run, mmsys.cpl

    WinWait,Sound

    ControlSend,SysListView321,{Control down}{Tab}{Control up}

    ControlSend,SysListView321,{Down 6}

    ControlClick,&Set Default

    ControlClick,OK

    return


NewJerseyGuy
  • Guests
  • Last active:
  • Joined: --
Great job tjmonk15 and Guest 1 - this works on my Conexant 20561 SmartAudio HD as well. Guest1's fix to change the row index worked since the line 2 description was the same for both devices.

IanM
  • Guests
  • Last active:
  • Joined: --
Thank you everyone for some great examples of how to take control of audio device switching in Windows 7. Here's my take on selecting from 3 devices, with volume OSD and screen sleep also.

Cheers!
Ian MacKenzie

; IM_AudioPlus.ahk     May 25, 2011
;
; This is a modification of dan112123's audio device switcher with
; mrfantastic's on-screen volume display added in to create a
; complete device & volume package.
;
; It selects a specific device instead of switching back
; and forth between only two devices. When each device is 
; activated, an initial volume and custom volume increment are set.
; The current device and volume increment are written to an .ini file
; for retrieval on startup.
;
; Desired_Device must match be the display name from Sound Playback
; menu, or the device will not be found.


#NoEnv
#SingleInstance force
SetBatchLines -1
COM_Init()
IniRead, Current_Device, AudioPlus_Storage.ini, APS, CD
IniRead, Vol_Increment, AudioPlus_Storage.ini, APS, VI


; -------------------
; START CONFIGURATION
; -------------------
; The amount by which to raise or lower the volume each time
; 1 unit yields 2% change
; Vol_Increment = 1
; How long to display the volume level bar graphs (in milliseconds)
vol_DisplayTime = 1250
; Transparency of window (0-255)
vol_TransValue = 180
; Bar's background colour
vol_CW = 000000   
vol_Width = 250  ; width of bar
vol_Thick = 15   ; thickness of bar
; Bar's screen position
vol_PosX := A_ScreenWidth - vol_Width - 30
vol_PosY := A_ScreenHeight - vol_Thick - 100
; --------------------
; END OF CONFIGURATION
; --------------------
vol_BarOptionsMaster = 1:B1 ZH%vol_Thick% ZX8 ZY4 W%vol_Width% X%vol_PosX% Y%vol_PosY% CW%vol_CW%
return


^SC15D::
Run, mmsys.cpl
WinWait,Sound
Return


^Up::
Desired_Device = USB Hifi
Vol_Initial = 92
Vol_Increment = 1 
GoSub, Device_Selector
Return


^Down::
Desired_Device = Speakers
Vol_Initial = 40
Vol_Increment = 2 
GoSub, Device_Selector
Return


^Left::
Desired_Device = HDMI
Vol_Initial = 90
Vol_Increment = 1 
GoSub, Device_Selector
Return


^WheelUp::
SC130::
{
Send {Volume_up %Vol_Increment%}
Gosub, vol_ShowBars
}
return


^WheelDown::
SC12E::
{
Send {Volume_down %Vol_Increment%}
Gosub, vol_ShowBars
}
return


~$Volume_mute::
Gosub, vol_ShowBars
return


;MediaCenter launch button - turns screen off
SC112::
Sleep, 200
SendMessage,0x112,0xF170,2,,Program Manager
return


;~$Volume_up::
;~$Volume_down::
vol_ShowBars:
; Get volumes in case the user or an external program changed them:
Sleep 10
vol_Master := VA_GetMasterVolume()
StringTrimRight, vol_OSD, vol_Master, 4
vol_Mute := VA_GetMasterMute()
if vol_Mute <> 1
{
  vol_Colour = White   
  vol_Text := Current_Device . "  " . vol_OSD . "%" 
}
else
{
  vol_Colour = Red     
  vol_Text := Current_Device . " (muted)"
}
; To prevent the "flashing" effect, only create the bar window if it doesn't already exist:
IfWinNotExist, VolumeOSDxyz
{
    Progress, %vol_BarOptionsMaster% CB%vol_Colour% CT%vol_Colour%, , %vol_Text%, VolumeOSDxyz
    WinSet, Transparent, %vol_TransValue%, VolumeOSDxyz
}
Progress, 1:%vol_Master%, , %vol_Text%
SetTimer, vol_BarOff, %vol_DisplayTime%
return


vol_BarOff:
SetTimer, vol_BarOff, off
Progress, 1:Off
return


Device_Selector:
IfWinExist Sound
{
    WinKill Sound
}
Run, mmsys.cpl
WinWait,Sound
ControlGet, len, List, Count, SysListView321
Loop %len%
{
    ControlSend,SysListView321,{Down}
    ControlGet, Device_Info, List, Selected, SysListView321
    IfInString, Device_Info, %Desired_Device%
    {
        ControlClick,&Set Default
        Current_Device = %Desired_Device%
        VA_SetMasterVolume(Vol_Initial)
        IniWrite, %Current_Device%, AudioPlus_Storage.ini, APS, CD
        IniWrite, %Vol_Increment%, AudioPlus_Storage.ini, APS, VI
        Sleep 1500
        ControlClick,OK
        WinWaitClose
        Return
    }
}
MsgBox, The device %Desired_Device% was not found.
Return