Jump to content
Sign in to follow this  
acidfear

Reading Volume

Recommended Posts

acidfear

I want to write a little program that shows the current volume at all times, and changes when the volume is changed.

Is there a way to read what the current system Volume level is (Windows XP Pro) ?

Share this post


Link to post
Share on other sites
acidfear

Oops. I should clarify. I meant audio volume.

Hmm.. but you did give me a good idea to work on!

Edited by acidfear

Share this post


Link to post
Share on other sites
BrettF

You'll probably want to have a look in the help file at the Drive*() functions. I think you want DriveSpaceFree()?

I thought he wanted the sound volume???

If so....

This topic may have what you want.... :)

Share this post


Link to post
Share on other sites
acidfear

Hmm I couldn't find anything on reading volume levels. Maybe I was just too confused and didn't undetstand :)

Share this post


Link to post
Share on other sites
Valuater

maybe...

#include <GuiConstants.au3>

$Volume = _SoundGetWaveVolume()

GUICreate("MyGUI", 393, 125, -1, -1)

$Slider_1 = GUICtrlCreateSlider(20, 40, 360, 30)
GUICtrlSetLimit($Slider_1, 100, 0)
GUICtrlSetData($Slider_1, $Volume);
$Label_2 = GUICtrlCreateLabel("Volume Control", 30, 10, 140, 20)
$Close = GUICtrlCreateButton("Close", 150, 90, 60, 20)
SoundSetWaveVolume($Volume)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $Slider_1
            SoundSetWaveVolume(GUICtrlRead($Slider_1))
            ToolTip(GUICtrlRead($Slider_1))
            Sleep(300)
            ToolTip("")
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Close
            Exit
    EndSelect

WEnd
Exit

Func _SoundGetWaveVolume()
    Local $WaveVol = -1, $p, $ret
    Const $MMSYSERR_NOERROR = 0
    $p = DllStructCreate("dword")
    If @error Then
        SetError(2)
        Return -2
    EndIf
    $ret = DllCall("winmm.dll", "long", "waveOutGetVolume", "long", -1, "long", DllStructGetPtr($p))
    If ($ret[0] == $MMSYSERR_NOERROR) Then
        $WaveVol = Round(Dec(StringRight(Hex(DllStructGetData($p, 1), 8), 4)) / 0xFFFF * 100)
    Else
        SetError(1)
    EndIf
    $Struct = 0
    Return $WaveVol
EndFunc   ;==>_SoundGetWaveVolume

function from gafrost

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
acidfear

maybe...

#include <GuiConstants.au3>

$Volume = _SoundGetWaveVolume()

GUICreate("MyGUI", 393, 125, -1, -1)

$Slider_1 = GUICtrlCreateSlider(20, 40, 360, 30)
GUICtrlSetLimit($Slider_1, 100, 0)
GUICtrlSetData($Slider_1, $Volume);
$Label_2 = GUICtrlCreateLabel("Volume Control", 30, 10, 140, 20)
$Close = GUICtrlCreateButton("Close", 150, 90, 60, 20)
SoundSetWaveVolume($Volume)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $Slider_1
            SoundSetWaveVolume(GUICtrlRead($Slider_1))
            ToolTip(GUICtrlRead($Slider_1))
            Sleep(300)
            ToolTip("")
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Close
            Exit
    EndSelect

WEnd
Exit

Func _SoundGetWaveVolume()
    Local $WaveVol = -1, $p, $ret
    Const $MMSYSERR_NOERROR = 0
    $p = DllStructCreate("dword")
    If @error Then
        SetError(2)
        Return -2
    EndIf
    $ret = DllCall("winmm.dll", "long", "waveOutGetVolume", "long", -1, "long", DllStructGetPtr($p))
    If ($ret[0] == $MMSYSERR_NOERROR) Then
        $WaveVol = Round(Dec(StringRight(Hex(DllStructGetData($p, 1), 8), 4)) / 0xFFFF * 100)
    Else
        SetError(1)
    EndIf
    $Struct = 0
    Return $WaveVol
EndFunc   ;==>_SoundGetWaveVolume

function from gafrost

8)

Thank you!!

Share this post


Link to post
Share on other sites
Valuater

Thank you!!

Welcome

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
FreeFry

I was wondering one thing, this can only read/change the Wave volume, is there any way to read the Master volume?

I'm using a fix that I made to make my keyboard's volume buttons that change only the Master volume, to also affect the Wave volume, though this involves having a sndvol32.exe process(window) running, and using dll calls to read the value of the Master-volume-slider, and writing that value to the Wave-volume-slider.

This is the code I use for this:

Opt("WinTitleMatchMode", 4)

DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1) ; This was a workaround to make it work on Windows XP 64bit, though this have probably been fixed by now...

Global $pID
Global $WindowHandle
Global $ControlHandle1
Global $ControlHandle2

Global $Muted = False

Global Const $TBM_GETPOS = 0x400
Global Const $TBM_SETPOS = 0x405
Global Const $WM_VSCROLL = 0x115
Global Const $SB_LINEDOWN = 0x1
Global Const $SB_LINEUP = 0x0

$g_szVersion = "Volume Control Fixer v1.1"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)

While 1
    
    If Not WinExists($WindowHandle) Then
        _GetHandles()
    Else
        
        $data1 = DllCall("user32.dll", "int", "SendMessage", "hwnd", $ControlHandle1, "int", $TBM_GETPOS, "int", 0, "int", 0)
        $data2 = DllCall("user32.dll", "int", "SendMessage", "hwnd", $ControlHandle2, "int", $TBM_GETPOS, "int", 0, "int", 0)
        
        If $data1[0] <> $data2[0] Then
; Code below is outcommented because it was a little annoying...
;~          If $data1[0] > $data2[0] Then
;~              TrayTip("", "Volume @ " & Int(100-($data1[0]/5)) & "%", 0)
;~              DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $ControlHandle2, "int", $TBM_SETPOS, "int", 1, "int", $data1[0])
;~              DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $WindowHandle, "int", $WM_VSCROLL, "int", $SB_LINEDOWN, "int", $ControlHandle2)
;~          ElseIf $data1[0] < $data2[0] Then
;~              TrayTip("", "Volume @ " & Int(100-($data1[0]/5)) & "%", 0); works, but buggs out sometimes... annoying, therefore removed
;~              DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $ControlHandle2, "int", $TBM_SETPOS, "int", 1, "int", $data1[0])
;~              DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $WindowHandle, "int", $WM_VSCROLL, "int", $SB_LINEUP, "int", $ControlHandle2)
;~          EndIf
            
            DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $ControlHandle2, "int", $TBM_SETPOS, "int", True, "long", $data1[0])
            ; problem with the code above was that even though the $TBM_SETPOS message has the update parameter set to True, it did not update the new position 
            ; so the volume did not change, that's why the $WM_VSCROLL message is sent below. This made it update the window/change the volume..
            DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $WindowHandle, "int", $WM_VSCROLL, "int", $SB_LINEDOWN, "int", $ControlHandle2)
        Else
            If ControlCommand($WindowHandle, "", "Button2", "IsChecked") And Not $muted Then
                ControlCommand($WindowHandle, "", "Button4", "Check")
                TrayTip("Muted!", "Sound Muted!", 0)
                $muted = True
            ElseIf Not ControlCommand($WindowHandle, "", "Button2", "IsChecked") And $muted Then
                ControlCommand($WindowHandle, "", "Button4", "UnCheck")
                TrayTip("Unmuted!", "Sound Unmuted!", 0)
                $muted = False
            EndIf
        EndIf
    
    EndIf
    
    Sleep(100)
    
WEnd

Func OnAutoItExit()
    
    ; less reliable
;~  If WinExists($WindowHandle) Then
;~      WinClose($WindowHandle)
;~  EndIf
    
    ; more reliable/preferable..
    If ProcessExists($pID) Then
        ProcessClose($pID)
    EndIf
    
EndFunc   ;==>OnAutoItExit

Func _GetHandles()
    
    $pID = Run(@SystemDir & "\sndvol32.exe", @SystemDir, @SW_HIDE)
    
    If Not WinWait("classname=Volume Control", "", 7500) Then
        MsgBox(0, "Error!", "Volume Control could not be opened! Exiting..")
        Exit
    EndIf
    
    $WindowHandle = WinGetHandle("classname=Volume Control")
    $ControlHandle1 = ControlGetHandle($WindowHandle, "", 1001)
    $ControlHandle2 = ControlGetHandle($WindowHandle, "", 2001)
    
EndFunc

I would gladly like to know if anyone knows a 'proper' fix to the non-update problem I described in the code. :)

Edited by FreeFry

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.