Click here to Skip to main content
11,804,008 members (79,381 online)
Click here to Skip to main content

Volume Manipulation Classes

, 16 Nov 1999 310.1K 4.5K 83
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

GeneralSolved Compile - microphone vol does not change... Pin
Anonymous13-Feb-04 16:40
sussAnonymous13-Feb-04 16:40 
Generalline-in playback Pin
Anonymous12-Feb-04 10:53
sussAnonymous12-Feb-04 10:53 
GeneralRe: line-in playback Pin
Saber00119-Jul-06 0:13
memberSaber00119-Jul-06 0:13 
GeneralRe: line-in playback Pin
wanhaven23-Feb-07 1:38
memberwanhaven23-Feb-07 1:38 
GeneralDisplaying input audio level in progress bar Pin
Hridaynath Musale10-Feb-04 13:24
memberHridaynath Musale10-Feb-04 13:24 
GeneralFound bug. It should called the destructor. Pin
saintstone1-Feb-04 17:28
membersaintstone1-Feb-04 17:28 
GeneralThanks! Pin
sparky90923-Oct-03 1:05
membersparky90923-Oct-03 1:05 
GeneralRe: Thanks! Pin
Anonymous8-Aug-04 4:33
sussAnonymous8-Aug-04 4:33 
GeneralmmSystem.h Pin
dietercools20-Sep-03 13:02
memberdietercools20-Sep-03 13:02 
GeneralRe: mmSystem.h Pin
sparky90923-Oct-03 0:57
membersparky90923-Oct-03 0:57 
GeneralRe: mmSystem.h Pin
dietercools11-Feb-04 8:42
memberdietercools11-Feb-04 8:42 
GeneralRe: mmSystem.h Pin
Anonymous11-Feb-04 13:21
sussAnonymous11-Feb-04 13:21 
QuestionHow to support WinCE Pin
weblxj2-Jul-03 15:03
memberweblxj2-Jul-03 15:03 
AnswerRe: How to support WinCE Pin
Alex Chmut2-Jul-03 22:17
memberAlex Chmut2-Jul-03 22:17 
Generalstdafx.h Pin
M.Joshi5-Apr-03 5:44
memberM.Joshi5-Apr-03 5:44 
AnswerRe: stdafx.h Pin
Saber00118-Jul-06 23:54
memberSaber00118-Jul-06 23:54 
GeneralRe: stdafx.h Pin
Saber00119-Jul-06 0:05
memberSaber00119-Jul-06 0:05 
Generalto increse the volume Pin
xyz1233-Feb-03 1:57
sussxyz1233-Feb-03 1:57 
GeneralRe: to increse the volume Pin
Anonymous20-Nov-03 22:26
sussAnonymous20-Nov-03 22:26 
GeneralDoes not work while device in use Pin
codermallu1-Aug-02 3:11
membercodermallu1-Aug-02 3:11 
GeneralRe: Does not work while device in use Pin
Peter B.22-May-03 10:55
memberPeter B.22-May-03 10:55 
GeneralHelp: compilation error for VolumeInXXX.cpp Pin
Annette Skaar7-Jul-02 17:51
memberAnnette Skaar7-Jul-02 17:51 
QuestionChanges the balance !?!? Pin
Lou4-May-02 13:03
memberLou4-May-02 13:03 
AnswerRe: Changes the balance !?!? Pin
KuZiKuZi6-Jan-05 20:04
memberKuZiKuZi6-Jan-05 20:04 
GeneralDoes not work with USB Audio Device Pin
Anonymous12-Feb-02 8:09
memberAnonymous12-Feb-02 8:09 
GeneralRe: Does not work with USB Audio Device Pin
NR19-Feb-02 14:03
memberNR19-Feb-02 14:03 
GeneralRe: Does not work with USB Audio Device Pin
Peter B.23-Dec-02 3:31
memberPeter B.23-Dec-02 3:31 
GeneralHere is my solution... Pin
pengok25-Nov-04 19:43
memberpengok25-Nov-04 19:43 
GeneralSample project required Pin
santu27-Nov-01 23:23
membersantu27-Nov-01 23:23 
GeneralRe: Sample project required Pin
Atlantys11-Feb-02 5:03
memberAtlantys11-Feb-02 5:03 
GeneralRe: Sample project required Pin
lordsoth998-Jul-04 9:50
memberlordsoth998-Jul-04 9:50 
GeneralRecording Pin
yoavadler5-Nov-01 22:58
memberyoavadler5-Nov-01 22:58 
GeneralRe: Recording Pin
Yong23-Nov-01 6:05
memberYong23-Nov-01 6:05 
GeneralThanks! But I have a question. Pin
hoonja23-Oct-01 1:16
memberhoonja23-Oct-01 1:16 
GeneralRe: Thanks! But I have a question. Pin
Yong23-Nov-01 6:14
memberYong23-Nov-01 6:14 
QuestionpLineInfo initialization? Pin
Anonymous22-Sep-01 5:27
memberAnonymous22-Sep-01 5:27 
Questionwhy mute volume in invalid in win2000? Pin
jadeking2-Sep-01 20:04
memberjadeking2-Sep-01 20:04 
AnswerRe: why mute volume in invalid in win2000? Pin
Yong23-Nov-01 6:03
memberYong23-Nov-01 6:03 
GeneralStdafx.h Pin
_Nico_13-Aug-01 3:44
member_Nico_13-Aug-01 3:44 
GeneralRe: Stdafx.h Pin
Anonymous9-Aug-02 2:21
sussAnonymous9-Aug-02 2:21 
GeneralStdafx.h Pin
_Nico_13-Aug-01 3:44
member_Nico_13-Aug-01 3:44 
Generalgood class Pin
Anonymous19-Jul-01 5:17
memberAnonymous19-Jul-01 5:17 
QuestionWhy It Cost So Much Memory? Pin
cookie2k20-May-01 20:55
membercookie2k20-May-01 20:55 
AnswerRe: Why It Cost So Much Memory? Pin
DavidCrow12-Aug-03 10:43
memberDavidCrow12-Aug-03 10:43 
GeneralEqualizer Pin
tatdat8-May-01 15:00
membertatdat8-May-01 15:00 
GeneralWhy? I can't execute in win98 Pin
Powenko15-Feb-01 14:24
memberPowenko15-Feb-01 14:24 
GeneralRe: Why? I can't execute in win98 Pin
Alex Chmut17-Feb-01 3:24
memberAlex Chmut17-Feb-01 3:24 
QuestionWhy do you need the global g_pThis? Pin
Patrick Koorevaar14-Dec-00 2:36
memberPatrick Koorevaar14-Dec-00 2:36 
Generalmore audio board Pin
Antonio Salomone9-Nov-00 10:36
memberAntonio Salomone9-Nov-00 10:36 
GeneralRe: more audio board Pin
Alex Chmut9-Nov-00 22:58
memberAlex Chmut9-Nov-00 22:58 

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

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

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