Solved

How do I set the master volume level?

Posted on 1998-03-27
3
403 Views
Last Modified: 2013-12-03
I'd like to be able to programmatically set the master volume level -- i.e. the equivalent of using the volume control in the system tray. I've found how to set the wave volume, and the midi volume looks similar, but how do I set the overall master volume? Thanks!
0
Comment
Question by:mccluney
3 Comments
 
LVL 7

Expert Comment

by:kamall
ID: 1412354
Try this code (Visual Basic):

Attribute VB_Name = "MIXER"
'****************************************************************
'     ************
'* This constant holds the value of the Highest Custom volume set
'     ting. The *
'     '* lowest value will always be zero.*
'****************************************************************
'     ************
Public Const HIGHEST_VOLUME_SETTING = 12
'     'Put these into a module
'     ' device ID for aux device mapper
Public Const AUX_MAPPER = -1&
Public Const MAXPNAMELEN = 32

Type AUXCAPS
       wMid As Integer
       wPid As Integer
       vDriverVersion As Long
       szPname As String * MAXPNAMELEN
       wTechnology As Integer
       dwSupport As Long
End Type

'     ' flags for wTechnology field in AUXCAPS structure
Public Const AUXCAPS_CDAUDIO = 1 ' audio from internal CD-ROM drive
Public Const AUXCAPS_AUXIN = 2 ' audio from auxiliary input jacks
'     ' flags for dwSupport field in AUXCAPS structure
Public Const AUXCAPS_VOLUME = &H1 ' supports volume control
Public Const AUXCAPS_LRVOLUME = &H2 ' separate left-right volume control

Declare Function auxGetNumDevs Lib "winmm.dll" () As Long

Declare Function auxGetDevCaps Lib "winmm.dll" Alias "auxGetDevCapsA" (ByVal uDeviceID As Long, lpCaps As AUXCAPS, ByVal uSize As Long) As Long

Declare Function auxSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long

Declare Function auxGetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByRef lpdwVolume As Long) As Long

Declare Function auxOutMessage Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal msg As Long, ByVal dw1 As Long, ByVal dw2 As Long) As Long
       '****************************************************************
       '     ************
       '     '* Possible Return values from auxGetVolume, auxSetVolume*
       '****************************************************************
       '     ************
       Public Const MMSYSERR_NOERROR = 0
       Public Const MMSYSERR_BASE = 0
       Public Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2)
       '****************************************************************
       '     ************
       '     '* Use the CopyMemory function from the Windows API *
       '****************************************************************
       '     ************

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
       '****************************************************************
       '     ************
       '     '* Use this structure to break the Long into two Integers*
       '****************************************************************
       '     ************

Public Type VolumeSetting
       LeftVol As Integer
       RightVol As Integer
End Type


Sub lCrossFader()

       '     'Vol1 = 100 - Slider1.Value ' Left
       '     'Vol2 = 100 - Slider5.Value ' Right
       '     'E = CrossFader.Value
       '     'F = 100 - E
       '     'If Check4.Value = 1 Then ' Half Fader Check
       '     'LVol = (F * Val(Vol1) / 100) * 2
       '     'RVol = (E * Val(Vol2) / 100) * 2
       '     'If LVol > (50 * Val(Vol1) / 100) * 2 Then
       '     'LVol = (50 * Val(Vol1) / 100) * 2
       '     'End If
       '     'If RVol > (50 * Val(Vol2) / 100) * 2 Then
       '     'RVol = (50 * Val(Vol2) / 100) * 2
       '     'End If
       '     'Else
       '     'LVol = (F * Val(Vol1) / 100)
       '     'RVol = (E * Val(Vol2) / 100)
       '     'End If
       'Label1.Caption = "Fader: " + LTrim$(Str$(LVol)) + " x " + LTrim$
       '     (Str$(RVol))
       '     '
End Sub


Public Function lSetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long

       '****************************************************************
       '     ************
       '* This function sets the current Windows volume settings to the
       '     specified *
       '* device using two Custom numbers from 0 to HIGHEST_VOLUME_SETTI
       '     NG for the *
       '     '* right and left volume settings. *
       '     '* *
       '* The return value of this function is the Return value of the a
       '     uxGetVolume*
       '     '* Windows API call.*
       '****************************************************************
       '     ************
       Dim bReturnValue As Boolean ' Return Value from Function
       Dim Volume As VolumeSetting ' Type structure used to convert a long to/from
       '     ' two Integers.
       Dim lAPIReturnVal As Long' Return value from API Call
       Dim lBothVolumes As Long' The API passed value of the Combined Volumes
       '****************************************************************
       '     ************
       '     '* Calculate the Integers*
       '****************************************************************
       '     ************
       Volume.LeftVol = nSigned(lLeftVol * 65535 / HIGHEST_VOLUME_SETTING)
       Volume.RightVol = nSigned(lRightVol * 65535 / HIGHEST_VOLUME_SETTING)
       '****************************************************************
       '     ************
       '     '* Combine the Integers into a Long to be Passed to the API *
       '****************************************************************
       '     ************
       lDataLen = Len(Volume)
       CopyMemory lBothVolumes, Volume.LeftVol, lDataLen
       '****************************************************************
       '     ************
       '     '* Set the Value to the API*
       '****************************************************************
       '     ************
       lAPIReturnVal = auxSetVolume(lDeviceID, lBothVolumes)
       lSetVolume = lAPIReturnVal
End Function


Public Function lGetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long

       '****************************************************************
       '     ************
       '* This function reads the current Windows volume settings from t
       '     he *
       '     '* specified device, and returns two numbers from 0 to *
       '* HIGHEST_VOLUME_SETTING for the right and left volume settings.
       '     *
       '     '* *
       '* The return value of this function is the Return value of the a
       '     uxGetVolume*
       '     '* Windows API call.*
       '****************************************************************
       '     ************
       Dim bReturnValue As Boolean ' Return Value from Function
       Dim Volume As VolumeSetting ' Type structure used to convert a long to/from
       '     ' two Integers.
       Dim lAPIReturnVal As Long' Return value from API Call
       Dim lBothVolumes As Long' The API Return of the Combined Volumes
       '****************************************************************
       '     ************
       '     '* Get the Value from the API*
       '****************************************************************
       '     ************
       lAPIReturnVal = auxGetVolume(lDeviceID, lBothVolumes)
       '****************************************************************
       '     ************
       '     '* Split the Long value returned from the API into to Integers *
       '****************************************************************
       '     ************
       lDataLen = Len(Volume)
       CopyMemory Volume.LeftVol, lBothVolumes, lDataLen
       '****************************************************************
       '     ************
       '     '* Calculate the Return Values. *
       '****************************************************************
       '     ************
       lLeftVol = HIGHEST_VOLUME_SETTING * lUnsigned(Volume.LeftVol) / 65535
       lRightVol = HIGHEST_VOLUME_SETTING * lUnsigned(Volume.RightVol) / 65535
       lGetVolume = lAPIReturnVal
End Function


Public Function nSigned(ByVal lUnsignedInt As Long) As Integer

       Dim nReturnVal As Integer ' Return value from Function

              If lUnsignedInt > 65535 Or lUnsignedInt < 0 Then
                     MsgBox "Error in conversion from Unsigned to nSigned Integer"
                     nSignedInt = 0
                     Exit Function
              End If


              If lUnsignedInt > 32767 Then
                     nReturnVal = lUnsignedInt - 65536
              Else
                     nReturnVal = lUnsignedInt
              End If

       nSigned = nReturnVal
End Function


Public Function lUnsigned(ByVal nSignedInt As Integer) As Long

       Dim lReturnVal As Long ' Return value from Function

              If nSignedInt < 0 Then
                     lReturnVal = nSignedInt + 65536
              Else
                     lReturnVal = nSignedInt
              End If


              If lReturnVal > 65535 Or lReturnVal < 0 Then
                     MsgBox "Error in conversion from nSigned to Unsigned Integer"
                     lReturnVal = 0
              End If

       lUnsigned = lReturnVal
End Function

0
 

Author Comment

by:mccluney
ID: 1412355
This shows how to set the volume for a particular device, but how do I set the overall system volume? I.e. I want to mimic the master volume slider (which doesn't move the sliders for the individual channels, but does affect the volume of all the devices). Is there a device for the master volume?
0
 

Accepted Solution

by:
RLM earned 100 total points
ID: 1412356
Look for mixer line: MIXERLINE_COMPONENTTYPE_DST_SPEAKERS. Once you've found the matching line, look for control type MIXERCONTROL_CONTROLTYPE_VOLUME. There it is :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Table Size and Indexes in SQL Server 6 26
Microsoft Query from QuickBooks 9 72
Visual Studio 2015 locks debug executable 9 214
Windows Process running 3 58
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question