Sign in to follow this  
Followers 0
sky

2 sound cards output on my computer

5 posts in this topic

my comp has 2 sound cards,and i wanna know how to switch sound device with au3? thx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I have looked at this as well, but didn't find any cleaner way than just simulating user input (lots of ControlClicks).

It doesn't seem there is anything available along the lines of a COM interface, dll call etc. (This is on Win7)

I'd love to be proven wrong though.

Edit:

I once again got realy frustrated with the lack of a scriptable interface to change audio devices, but thought this might still be usefull to someone:

#include <array.au3>

Global $hWnd = _SoundCards_Open()
Global $aCards = _SoundsCards_List($hWnd) ;If needed you can check the status of each device before picking one. (For instance to retrieve the current default)
_ArrayDisplay($aCards)
_SoundCards_SetDefault($hWnd,0) ;Set the topmost device as default
WinClose($hWnd) ;close window.


;opens the sound harware configuration window and retuns the handle
;Parameters:
; $iTimeOut - time to wait for a matching window to appear
; $sTitle[Optional] - Title of the expected configuration window.
;
;   Success: Returns handle to the window for use with other functions
;   Failure: Returns 0 and sets @error
;   @error values:
;       0 successfull
;       1 sound properties window not found before timeout

Func _SoundCards_Open($iTimeOut = 2000, $sTitle = "")
    ;TODO: Check if the window is already open first.
    ;Run hidden (risky as the window will stay hidden if the script doesn't close it, leaving a normal user unable to manually open it.)
    Local $PID = Run(@SystemDir & "\rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,0")
    Local $iTimer = TimerInit()
    Local $aHWnd
    While TimerDiff($iTimer) < 2000
        $aHWnd = WinList($sTitle)
        For $i = 1 To $aHWnd[0][0]
            If WinGetProcess($aHWnd[$i][1]) = $PID Then
                If ControlGetHandle($aHWnd[$i][1],"","SysListView321") Then Return $aHWnd[$i][1]
            EndIf
        Next
    WEnd
    Return SetError(1,0,0)
EndFunc

;returns the information about soundcards that are currently visible
;Parameters:
; $hWnd - handle to the sound configuration window
;
;   Success: Returns a 2d array of devices and their status
;   Failure: Returns 0 and sets @error
;   @error values:
;       0 successfull
;       1 sound properties window not found
;       2 listview not found
;       3 no audio devices found in listview
Func _SoundsCards_List($hWnd)
    If Not WinExists($hWnd) Then Return SetError(1,0,0)

    Local $hLVCtrl = ControlGetHandle($hWnd,"","SysListView321")
    If Not $hLVCtrl Then Return SetError(2,0,0)

    Local $iCount = ControlListView($hWnd,"",$hLVCtrl,"GetItemCount")
    If Not $iCount Then Return SetError(3,0,0)

    Local $aTemp, $aReturn[$iCount][3]
    For $i = 0 To $iCount - 1
        For $j = 0 To 2
            $aReturn[$i][$j] = ControlListView($hWnd,"",$hLVCtrl,"GetText",$i,$j)
        Next
    Next
    Return $aReturn
EndFunc

;Set a default output device.
;Parameters:
; $hWnd - handle to the sound configuration window
; $iDevice - index of the device to set as default
;
;   Success: returns 1
;   Failure: returns 0 and sets @error
;   @error values:
;       0 successfull
;       1 sound properties window not found
;       2 listview not found
;       3 $iDevice too low
;       4 $iDevice too high
;       5 ControlClick failed. (devise is probably already default)
Func _SoundCards_SetDefault($hWnd,$iDevice)
    If Not WinExists($hWnd) Then Return SetError(1,0,0)

    Local $hLVCtrl = ControlGetHandle($hWnd,"","SysListView321")
    $iDevice = Number($iDevice)
    If Not $hLVCtrl Then Return SetError(2,0,0)

    If $iDevice < 0 Then Return SetError(3,0,0)
    If $iDevice + 1 > ControlListView($hWnd,"",$hLVCtrl,"GetItemCount") Then Return SetError(4,0,0)
    ControlListView($hWnd,"",$hLVCtrl,"Select", $iDevice)

    If Not ControlClick($hWnd,"","[CLASSNN:Button2]") Then Return SetError(5,0,0)
    Return 1
EndFunc

I didn't want to depend on the window title, out of fear for false positives and because the title is localisation dependant, but using the window title would make it easier to find out if the window is already open.

For other versions of windows it might require some changes.

Edited by Tvern

Share this post


Link to post
Share on other sites

I joined the forum to try and find an answer to selecting the sound output device (not change the windows default output device) and haven't found a solution. Several posts asking the same thing. After hours of Googling, my script is at a standstill, I have just about given up.

<sound.au3> works well and solved some Vista issues I had with playback, but sadly, it doesn't support sound card selection.

Hopeing someone will chime in with a relevant forum post.

Cheers

Laurie


.... GOTOs? We don't need no stinkin' GOTOs! ....

Share this post


Link to post
Share on other sites

I am looking for exactly the same thing. Just want a small program that can play a sound effect to a specific output device.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This is a little scary. I just searched through my content to find this topic because I misplaced the code I posted here. (this is an hour ago and it's the first time I came back to this topic)

I still havn't found a way to directly send sounds to a soundcard, other than the windows default, or a better way to change the default output device though.

Edit: "littel" should be "little"

Edited by Tvern

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0