How to Control the Volume of Sound Files from Visual Basic

ID: Q118377


The information in this article applies to:


SUMMARY

The Visual Basic MCI Control doesn't allow volume control of a Windows sound file. The volume can be controlled through the Multimedia API by using the waveOutSetVolume function. You can also use the function waveOutGetVolume() to read the current volume settings.


MORE INFORMATION

The waveOutSetVolume() function sets the volume of a waveform output device. With this function, you can control the right- and left-channel volume settings independently.

Declaration Syntax

Use one the following Declare statements to declare the waveOutGetVolume() Windows API function in your Visual Basic program:

Enter each Declare statement as one, single line:

Declare Function waveOutSetVolume Lib "MMSYSTEM.DLL"
   (ByVal wDeviceID as Integer, ByVal dwVolume as Long) as Integer
Declare Function waveOutGetVolume Lib "MMSYSTEM.DLL"
   (ByVal wDeviceID as Integer, dwVolume as Long) as Integer 

As an alternative, you can declare the waveOutSetVolume function by passing the dwVolume parameter as two separate integers, one for the left-channel setting and one for the right-channel setting. Pass the high-order word first and then the low-order word.

Enter the following Declare statement as one, single line:

Declare Function waveOutSetVolume Lib "MMSYSTEM.DLL"
   (ByVal wDeviceID as Integer, ByVal dwVolumeRight as Integer
   ByVal dwVolumeLeft as Integer) as Integer 

Parameters Explained


wDeviceID   This parameter identifies the waveform-output device. If
            you were to play one wave file, you would set this
            parameter to 0. If  you were to play multiple wave files, you
            would set this parameter to 1 for the second, 2 for the third,
            and so on.

dwVolume    For the waveOutSetVloume function, this parameter specifies
            the new volume setting. For the waveOutGetVolume
            function, it specifies a far pointer to a location that will be
            filled with the current volume setting.

            The low-order word contains the left-channel volume setting,
            and the high-order word contains the right-channel volume
            setting.

            A value of &HFFFF represents full volume, and a value of
            &H0000 represents no volume.

            If a device does not support both left and right volume
            control, the low-order word of dwVolume specifies the volume
            level, and the high-order word is ignored. 

Return Value

?? returns zero if the function is successful. Otherwise, it returns an error number. Possible return values are:
NOTE: Not all devices support volume changes or volume control on both the left and right channels. Most devices do not support the full 16 bits of volume-level control and will not use the high-order bits.

Step-by-Step Example

This example demonstrates how to use the waveOutSetVolume(), waveOutGetVolume(), and sndPlaySound Multimedia API functions to increase or decrease the sound in either the left or right channels:
  1. Start a new project in Visual Basic; Form1 is created by default.


  2. Add the following controls to Form1 and set their properties as indicated:
    
       Control           Property    Setting
       ----------------------------------------
    
       Command Button    Name        PlaySound
                         Caption     Play Sound
    
       Command Button    Name        LeftUp
                         Caption     Left Up
    
       Command Button    Name        LeftDown
                         Caption     Left Down
    
       Command Button    Name        RightUp
                         Caption     Right Up
    
       Command Button    Name        RightDown
                         Caption     Right Down
    
       Label             Name        LeftVol
    
       Label             Name        RightVol
     


  3. Add the following code to the General Declarations section of Form1:

    ' Enter each of the following Declare statements as one, single line:
    
       Declare Function sndPlaySound Lib "MMSYSTEM.DLL"
          (ByVal lpszSoundName As String, ByVal wFlags As Integer) As Integer
       Declare Function waveoutSetVolume Lib "mmsystem.dll"
          (ByVal wDeviceID As Integer, ByVal dwVolumeRight As Integer,
          ByVal dwVolumeLeft As Integer) As Integer
       Declare Function waveOutGetVolume Lib "MMSYSTEM.DLL"
          (ByVal wDeviceID As Integer, lpdwvolume As Long) As Integer
    
       Const SND_ASYNC = &H1
       Const SND_NODEFAULT = &H2
    
       Dim CurrentVolLeft As Long
       Dim CurrentVolRight As Long 


  4. Add the sample code shown below to the appropriate Form or Control event procedure.


  5. Save your work, and run the program.


  6. You will see the current channel setting in the labels. Choose the Play Sound button to hear the tada wave file play.


  7. Click one of the other command buttons to change either the left- or right-channel setting, and then click the Play Sound command button again. The volume level has changed.


Sample Code


Sub Form_Load ()
   Dim x As Integer
   Dim BothVolumes As Long

 ' Note that the waveid is 0 indicating the first wave output device.
 ' If you were to play multiple wavefiles on multiple wave output devices
 ' you would use 1 for the second wave output device, 2 for the third and
 ' so on.
 ' This code will retrieve the current volume setting

   x = waveOutGetVolume(0, BothVolumes)

   ' This code isolates the low-order word.
   ' Note that the value &HFFFF& is a Long Integer, which is the same
   ' as 0000FFFF, but because Visual Basic would automatically
   ' truncate this to FFFF, you must force the logical operation to use
   ' a four-byte Long Integer (0000FFFF) rather than a two-byte Integer
   ' (FFFF). This is accomplished by using the type casting
   ' character (&).
   CurrentVolLeft = BothVolumes And &HFFFF&

   ' This code isolates the high-order word.
   ' Enter the following two lines as one, single line:
   CurrentVolRight = ((BothVolumes And &HFFFF0000) / &H10000)
     And &HFFFF&

   LeftVol.Caption = Hex$(CurrentVolLeft)   ' Update the label with the
   RightVol.Caption = Hex$(CurrentVolRight) ' current volume settings.
End Sub

Sub PlaySound_Click ()
   Dim x As Integer
   Dim wFlags As Integer
   Dim SoundName As String
   SoundName = "C:\WINDOWS\MSREMIND.WAV"    ' Pick any wave file.
   wFlags = SND_ASYNC Or SND_NODEFAULT
   x = sndPlaySound(SoundName$, wFlags%)    ' Play the wave file.
End Sub

Sub LeftUp_Click ()
   ' Increase the left sound channel setting:
   Dim x As Integer
   CurrentVolLeft = CurrentVolLeft + &H1000&
   ' Prevent the channel setting from exceeding the maximum limit:
   If CurrentVolLeft > &HFFFF& Then CurrentVolLeft = &HFFFF&
   LeftVol.Caption = Format$(Hex$(CurrentVolLeft))
   ' Enter the following two lines as one, single line:
   x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)),
      CInt("&H" + Hex$(CurrentVolLeft)))
End Sub

Sub LeftDown_Click ()
   ' Decrease the left sound channel setting:
   Dim x As Integer
   CurrentVolLeft = CurrentVolLeft - &H1000&
   ' Prevent the channel setting from dropping below the minimum limit:
   If CurrentVolLeft < &H0& Then CurrentVolLeft = &H0&
   LeftVol.Caption = Hex$(CurrentVolLeft)
   ' Enter the following two lines as one, single line:
   x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)),
      CInt("&H" + Hex$(CurrentVolLeft)))
End Sub

Sub RightUp_Click ()
   ' Increase the right sound channel setting:
   Dim x As Integer
   CurrentVolRight = CurrentVolRight + &H1000&
   ' Prevent the channel setting from exceeding the maximum limit.
   If CurrentVolRight > &HFFFF& Then CurrentVolRight = &HFFFF&
   RightVol.Caption = Hex$(CurrentVolRight)
   ' Enter the following two lines as one, single line:
   x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)),
      CInt("&H" + Hex$(CurrentVolLeft)))
End Sub

Sub RightDown_Click ()
   ' Decrease the right sound channel setting:
   Dim x As Integer
   CurrentVolRight = CurrentVolRight - &H1000&
   ' Prevent the channel setting from dropping below the minimum limit:
   If CurrentVolRight < 0 Then CurrentVolRight = 0
   RightVol.Caption = Hex$(CurrentVolRight)
   ' Enter the following two lines as one, single line:
   x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)),
      CInt("&H" + Hex$(CurrentVolLeft)))
End Sub 


REFERENCES

For more detailed information on these and other Multimedia-related functions, please see the "Multimedia Programmer's Reference Guide" included with the Microsoft Windows version 3.1 Software Development Kit.

For information on other Visual Basic declarations and constant values for Multimedia-related functions, see the WINMMSYS.TXT text file. This file comes as part of the Professional edition of Visual Basic. Assuming default installation, you can find this file in the C:\VB\WINAPI\ directory.

Additional query words: 3.00


Keywords          : kbcode 
Version           : 3.00
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: May 28, 1999