AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SetVolume function

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
jballi



Joined: 01 Oct 2005
Posts: 385
Location: Texas, USA

PostPosted: Sat Apr 15, 2006 11:18 pm    Post subject: SetVolume function Reply with quote

This is a general purpose function to set or incrementally change the volume.

Yes, another fracken routine to change the volume! Haven't we had enough already?! Wink

I wrote this routine because I wanted the ability to change the volume both slowly (small increments) and quickly (larger increments) without having to use different hotkeys to make it happen. This function is a result of that requirement.

The function also includes a sample OSD, just in case you don't have your own.

As per my usual, the function includes (too much) documenation with examples.

I hope someone finds this useful.

Code:
;********************
;*                  *
;*    Set Volume    *
;*                  *
;********************
;
;
;   Decription:
;   ===========
;   This is a general purpose function to set or incrementally change the volume
;   of the standard sound components.  In addition, this function supports
;   "Dynamic Volume Change" and includes a sample On-Screen Display (OSD).
;   
;   
;   Parameters
;   ==========
;
;       Name                Decryption
;       ----                ----------
;       p_Command           Volume command. [Required]  Valid commands are "Up",
;                           "Down", "Set", and "Mute". 
;
;       p_ComponetType      Component Type. [Optional]  Default is "Master". 
;
;       p_Value             Setting value. [Optional]  Default is 1.
;                           
;                           If P_Command is "Up" or "Down", this parameter
;                           contains the volume increment change. 
;
;                           If p_Command is "Set", this parameter contains the
;                           exact volume setting (from 0 to 100).
;
;                           If p_Command is "Mute", any positive number will
;                           turn Mute ON and a zero will turn mute OFF.
;                           However, if the number begins with a plus or minus
;                           sign (the entire parameter must be in quotes for
;                           this option), the mute setting will be toggled (set
;                           to the opposite of its current state).
;
;       ---------------
;       For the following parameters, See the "Dynamic Volume Change" section
;       for more information.
;       --------------- 
;
;       p_Increment         Volume increment. [Optional]  Default is 1.
;
;       p_MaxValue          Maximum value. [Optional]  Default is 4.
;
;       p_MinValue          Minimum value. [Optional]  Default is 0.05.
;
;       p_ETT               Elapsed Time Threshold (in milliseconds). [Optional]
;                           Default is 200. 
;
;
;
;   Dynamic Volume Change
;   =====================
;
;       Synopsis
;       --------
;       "Dynamic Volume Change" is a term I coined to describe changing the
;       volume based upon how quickly and often the function is called.
;
;
;       Definitions
;       -----------
;       For the purposes of this function, the following definitions will be
;       used:
;
;           "Elapsed Time" is is the amount time (in milliseconds) that has
;           passed since the function was last called.
;       
;           The "Elapsed Time Threshold" (ETT) is reached if the "Elapsed
;           Time" is less than or equal to the value of p_ETT.

;
;       Parameters
;       ----------
;       The following parameters are used to support "Dynamic Volume Change":
;
;           p_Increment
;           p_MaxValue
;           p_MinValue
;           p_ETT
;
;
;       Parameter Values
;       ----------------
;       Each time the "Elapsed Time Threshold" is reached, p_Increment is
;       cumulatively added to p_Value which determines the volume change
;       increment.  To insure that the volume doesn't change too quickly or too
;       slowly, the cumulative volume increment is checked against p_MaxValue
;       and p_MinValue and is clipped accordingly.
;
;       To change the volume very quickly, set p_Increment to 2 or higher.  If
;       desired, change p_MaxValue to a larger upper limit.  For example:
;
;           SetVolume("Up","Wave",2,2,10)
;
;       To change the volume slowly, set p_Value and p_Increment to a value less
;       than 1 and set p_MaxValue to a small value (2 or less). For example:
;
;           SetVolume("Down","Microphone",0.2,0.1,2)
;
;       To reduce the speed of a volume change, set p_Value to a reasonable
;       value (2 or 3) and set p_Increment to a negative value (-0.2 for
;       example).  If desired, change the value of p_MinValue.  For example:
;   
;           SetVolume("Up","Master",3,-0.2,3,0.2)
;
;       To disable or "turn off" the "Dynamic Volume Change" feature, set
;       p_Increment to 0.  For example:
;
;           SetVolume("Down","Telephone",1,0)
;
;
;       Reset
;       -----
;       "Dynamic Volume Change" variables are automatically reset to defaults
;       when any of the following conditions are true:
;
;           1)  The values of p_Command or p_ComponetType change.
;           2)  p_Command is not equal to "Up" or "Down"
;           3)  The "Elapsed Time Threshold" has not been reached.
;       
;           
;
;   Notes
;   =====
;
;       On-Screen Display (OSD)
;       -----------------
;       The function includes a sample (and operational) code for an OSD.  If
;       you don't want or need an OSD, delete or comment out this code.  If you
;       have your own OSD, add a call to your OSD or call your OSD immediately
;       after calling this function.
;
;       
;       Parameter Defaults
;       ------------------
;       The parameter defaults were set based upon personal experience but they
;       may not make sense for all hardware and configurations.  If you have
;       sluggish or hypersensitive hardware, you might want to change the
;       defaults to suit your needs.
;
;
;       Device
;       ------
;       This function only controls the the first sound device.
;
;
;
;   Examples Of Use
;   ===============
;   The following are examples (not recommendations) of use.
;   
;   ;-- Keyboard
;   #Up::SetVolume("Up")
;   #Down::SetVolume("Down")
;
;   ^#Up::SetVolume("Up","Wave")
;   ^#Down::SetVolume("Down","Wave")
;
;   ;-- Set
;   ^#!Up::SetVolume("Set","Master",100)
;   ^#!Down::SetVolume("Set","Master",10)
;
;   ;-- Mute
;   ^+!Up::SetVolume("Mute","",0)
;   ^+!Down::SetVolume("Mute","",1)
;
;   ;-- Starts very slow and speeds up gradually
;   ^+!Up::SetVolume("Up","",0.2,0.2,6)
;   ^+!Down::SetVolume("Down","",0.2,0.2,6)
;
;   ;-- Mouse Wheel
;   #WheelUp::SetVolume("Up","Master",1,3,5)
;   #WheelDown::SetVolume("Down","Master",1,3,5)
;
;   ^#WheelUp::SetVolume("Up","Wave",1,3,5)
;   ^#WheelDown::SetVolume("Down","Wave",1,3,5)

;-------------------------------------------------------------------------------
SetVolume(p_Command
         ,p_ComponentType=""
         ,p_Value=1
         ,p_Increment=1
         ,p_MaxValue=4
         ,p_MinValue=0.05
         ,p_ETT=200)
    {
    static s_Command
    static s_ComponentType
    static s_Value
    static s_TickCount

    ;[==============]
    ;[  Initialize  ]
    ;[==============]
    l_OSD:=true   ;-- Set to false to turn off sample OSD
    l_ReturnCode:=false
    l_ControlType=Volume

    ;[===========]
    ;[  Process  ]
    ;[===========]
    gosub SetVolume_SoundSet
    gosub SetVolume_OSD

    ;[====================]
    ;[  Return to sender  ]
    ;[====================]
    return l_ReturnCode



    ;***************************************
    ;*                                     *
    ;*                                     *
    ;*                                     *
    ;*                                     *
    ;*        SetVolume Subroutines        *
    ;*                                     *
    ;*                                     *
    ;*                                     *
    ;*                                     *
    ;***************************************
    ;******************
    ;*                *
    ;*    SoundSet    *
    ;*                *
    ;******************
    SetVolume_SoundSet:

    ;[===============]
    ;[   Parameters  ]
    ;[===============]
    p_Command=%p_Command%              ;-- AutoTrim
    p_ComponentType=%p_ComponentType%  ;-- AutoTrim
    if not p_ComponentType
        p_ComponentType=Master

    if (p_MaxValue<p_Value)
        p_MaxValue:=p_Value+0

    ;[==============]
    ;[  First run?  ]
    ;[==============]
    if not s_Command
        s_TickCount=600000

    ;[===================]
    ;[  Determine value  ]
    ;[===================]
    if p_Command not in Set,Mute
        {
        if (p_Command=s_Command
       and  p_ComponentType=s_ComponentType
       and  (A_TickCount-s_TickCount)<=p_ETT)
            {
            s_Value:=s_Value+p_Increment
            if (s_Value>p_MaxValue)
                s_Value:=p_MaxValue+0
             else
                if (s_Value<p_MinValue)
                    s_Value:=p_MinValue+0
            }
         else
            s_Value:=p_Value+0
        }
       
    ;[==================================]
    ;[  Identify/Define volume setting  ]
    ;[==================================]
    if p_Command in +,U,Up
        l_NewSetting=+%s_Value%
     else
        if p_Command in -,D,Down
            l_NewSetting=-%s_Value%
         else
            if p_Command=Set
                l_NewSetting:=abs(p_Value)
             else
                {
                l_ControlType=Mute
                l_NewSetting:=p_Value
                }

    ;[==============]
    ;[  Set Volume  ]
    ;[==============]
    SoundSet %l_NewSetting%,%p_ComponentType%,%l_ControlType%
    l_ReturnCode:=ErrorLevel

    ;[=================]
    ;[  Update static  ]
    ;[=================]
    s_Command      :=p_Command
    s_ComponentType:=p_ComponentType
    s_TickCount    :=A_TickCount

    ;-- Return to sender
    return



    ;*************
    ;*           *
    ;*    OSD    *
    ;*           *
    ;*************
    SetVolume_OSD:

    if not l_OSD
        return

    ;----- Begin Sample OSD -----
    SoundGet l_CurrentVolume,%p_ComponentType%,Volume
    l_CurrentVolume:=round(l_CurrentVolume)
    progress 7:%l_CurrentVolume%
            ,%l_CurrentVolume%`%
            ,%p_ComponentType% Volume %p_Command%
            ,SetVolume OSD
    SetTimer SetVolume_OSDTimer,2000
    return

    SetVolume_OSDTimer:
    SetTimer SetVolume_OSDTimer,off
    progress 7:off
    return
    ;----- End Sample OSD -----
    }
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group