Click here to Skip to main content
13,346,065 members (58,583 online)
Click here to Skip to main content
Add your own
alternative version


83 bookmarked
Posted 16 Nov 1999

Volume Manipulation Classes

, 16 Nov 1999
Rate this:
Please Sign up or sign in to vote.
Volume manipulation classes.


Many times my applications required audio volume manipulation. To make the volume-enabled application development easier, I decided to create a few C++ classes that would allow me to easily regulate and track the changes of such volume controls as Output Master Volume, WaveOut Volume and Input (WaveIn) Volume. Here I provide such classes that share a common interface (defined in IVolume.h):

  • bool IsAvailable() - Says whether the volume controlling is possible.
  • void Enable() - Enables the line of the volume control.
  • void Disable() - Disables the line of the volume control.
  • DWORD GetVolumeMetric() - Retrieves the granularity of volume.
  • DWORD GetMinimalVolume() - Retrieves the minimal volume that can be set.
  • DWORD GetMaximalVolume() - Retrieves the maximal volume that can be set.
  • DWORD GetCurrentVolume() - Retrieves the current volume.
  • void SetCurrentVolume( DWORD dwValue ) - Sets the volume.

And the last function allows to register a user-implemented callback that will be called as a notification of volume changes:

void RegisterNotificationSink( PONMICVOULUMECHANGE, DWORD )

This interface is implemented by CVolumeOutMaster (VolumeOutMaster.h/cpp), CVolumeOutWave (VolumeOutWave.h/cpp) and CVolumeInXXX (VolumeInXXX.h/cpp) classes. The usage of the classes is very simple:

In your StdAfx.h, include "mmSystem.h" and make sure you link to the "winmm.lib" (#pragma comment(lib, "winmm.lib")). Then, if you are going to use Output Mater volume control, include "VolumeOutMaster.h", say, to the StdAfx.h.

The IVolume.h, VolumeInXXX.h, VolumeInXXX.cpp are to be inserted as your project files.

void CALLBACK MasterVolumeChanged( DWORD dwCurrentVolume, DWORD dwUserValue );
// Volume control Initialization
IVolume* pMasterVolume = (IVolume*)new CVolumeOutMaster();
if ( !pMasterVolume || !pMasterVolume->IsAvailable() )
    // handle error
pMasterVolume->RegisterNotificationSink( MasterVolumeChanged, dwAnyUserValue );
pMasterVolume->SetCurrentVolume( dwVolumeToSet );
DWORD dwCurrentVolume = pMasterVolume->SetCurrentVolume();
void CALLBACK MasterVolumeChanged( DWORD dwCurrentVolume, DWORD dwUserValue )
   // handle the volume change

Very simple, isn't it? Yet, the CVolumeInXXX class requires more explanation. In order to manipulate the Input volume, the source line index is to be passed to the constructor. Confused? Please, be not. CVolumeInXXX class provides a static function to enumerate those lines:

bool EnumerateInputLines( PINPUTLINEPROC, DWORD dwUserValue );

This allows you to manipulate the volume of any WaveIn-based lines. Say, you want to manipulate the microphone volume:

bool CALLBACK EnumInputLineProc( UINT uLineIndex, 
              MIXERLINE* pLineInfo, DWORD dwUserValue );
// Initialization
UINT uMicrophoneLineIndex = (UINT)-1;
if ( !CVolumeInXXX::EnumerateInputLines( EnumInputLineProc, 
                           (DWORD)&uMicrophoneLineIndex ) )
   // handle error
if ( uMicrophoneLineIndex == (UINT)-1 )
        // Error: mic volume'ing is not available.
IVolume* pMicrophoneVolume = 
         (IVolume*)new CVolumeInXXX( uMicrophoneLineIndex );
if ( !pMicrophoneVolume || !pMicrophoneVolume->IsAvailable() )
   // handle error
// Go on and use pMicrophoneVolume to manipulate the volume
bool CALLBACK EnumInputLineProc( UINT uLineIndex, 
              MIXERLINE* pLineInfo, DWORD dwUserValue )
    if ( pLineInfo->dwComponentType == 
        *((UINT*)dwUserValue) = uLineIndex;
        return false;
    return true;

Be aware, that for performance reasons it is better to have a single instance of a given class per application. So don't rush to create lots of CVolumeInXXX objects, better share the only one through your code.


The proposed classes do not encapsulate all the abilities exposed by the mixers. However, working with a mixer just to add a pretty simple functionality is quite boring. That's why, as I think, the proposed classes might be of some help to you.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alex Chmut
Web Developer
Ukraine Ukraine
No Biography provided

You may also be interested in...

Comments and Discussions

Generalto increse the volume Pin
xyz1233-Feb-03 2:57
sussxyz1233-Feb-03 2:57 
GeneralRe: to increse the volume Pin
Anonymous20-Nov-03 23:26
sussAnonymous20-Nov-03 23:26 
GeneralDoes not work while device in use Pin
codermallu1-Aug-02 4:11
membercodermallu1-Aug-02 4:11 
I created a small console utility in win nt using your class.
But I am finding that it does not work when the audio device
is being used, only when it is not in use.

Any help is appreciated


GeneralRe: Does not work while device in use Pin
Peter B.22-May-03 11:55
memberPeter B.22-May-03 11:55 
GeneralHelp: compilation error for VolumeInXXX.cpp Pin
Annette Skaar7-Jul-02 18:51
memberAnnette Skaar7-Jul-02 18:51 
QuestionChanges the balance !?!? Pin
Lou4-May-02 14:03
memberLou4-May-02 14:03 
AnswerRe: Changes the balance !?!? Pin
KuZiKuZi6-Jan-05 21:04
memberKuZiKuZi6-Jan-05 21:04 
GeneralDoes not work with USB Audio Device Pin
Anonymous12-Feb-02 9:09
memberAnonymous12-Feb-02 9:09 
GeneralRe: Does not work with USB Audio Device Pin
NR19-Feb-02 15:03
memberNR19-Feb-02 15:03 
GeneralRe: Does not work with USB Audio Device Pin
Peter B.23-Dec-02 4:31
memberPeter B.23-Dec-02 4:31 
GeneralHere is my solution... Pin
pengok25-Nov-04 20:43
memberpengok25-Nov-04 20:43 
GeneralSample project required Pin
santu28-Nov-01 0:23
membersantu28-Nov-01 0:23 
GeneralRe: Sample project required Pin
Atlantys11-Feb-02 6:03
memberAtlantys11-Feb-02 6:03 
GeneralRe: Sample project required Pin
lordsoth998-Jul-04 10:50
memberlordsoth998-Jul-04 10:50 
GeneralRecording Pin
yoavadler5-Nov-01 23:58
memberyoavadler5-Nov-01 23:58 
GeneralRe: Recording Pin
Yong23-Nov-01 7:05
memberYong23-Nov-01 7:05 
GeneralThanks! But I have a question. Pin
hoonja23-Oct-01 2:16
memberhoonja23-Oct-01 2:16 
GeneralRe: Thanks! But I have a question. Pin
Yong23-Nov-01 7:14
memberYong23-Nov-01 7:14 
QuestionpLineInfo initialization? Pin
Anonymous22-Sep-01 6:27
memberAnonymous22-Sep-01 6:27 
Questionwhy mute volume in invalid in win2000? Pin
jadeking2-Sep-01 21:04
memberjadeking2-Sep-01 21:04 
AnswerRe: why mute volume in invalid in win2000? Pin
Yong23-Nov-01 7:03
memberYong23-Nov-01 7:03 
GeneralStdafx.h Pin
_Nico_13-Aug-01 4:44
member_Nico_13-Aug-01 4:44 
GeneralRe: Stdafx.h Pin
Anonymous9-Aug-02 3:21
sussAnonymous9-Aug-02 3:21 
GeneralStdafx.h Pin
_Nico_13-Aug-01 4:44
member_Nico_13-Aug-01 4:44 
Generalgood class Pin
Anonymous19-Jul-01 6:17
memberAnonymous19-Jul-01 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 17 Nov 1999
Article Copyright 1999 by Alex Chmut
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid