Click here to Skip to main content
11,412,833 members (75,659 online)
Click here to Skip to main content

Volume Manipulation Classes

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

Introduction

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->Enable();
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 == 
             MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE )
    {
        *((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.

Conclusion

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.

License

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

Share

About the Author

Alex Chmut
Web Developer
Ukraine Ukraine
No Biography provided

Comments and Discussions

 
SuggestionWindows 7 volume control sample Pin
M A V at 15-Dec-12 2:05
memberM A V15-Dec-12 2:05 
QuestionLicense? Pin
LuisVilla at 16-Feb-12 5:48
memberLuisVilla16-Feb-12 5:48 
AnswerRe: License? Pin
Alex Chmut at 16-Feb-12 11:15
memberAlex Chmut16-Feb-12 11:15 
GeneralControlling master volume in Win7 - IAudioEndpointVolume - Working sample code in MSDN Pin
kormoe at 15-Jan-12 6:39
memberkormoe15-Jan-12 6:39 
GeneralVolume Control in Windows7 Pin
Member 2640836 at 17-Aug-10 21:41
memberMember 264083617-Aug-10 21:41 
GeneralMaking compatible Pin
BazKhan at 18-Jan-09 23:49
memberBazKhan18-Jan-09 23:49 
QuestionWindows Vista Anyone??? Pin
{bill} at 1-Sep-08 10:41
member{bill}1-Sep-08 10:41 
AnswerRe: Windows Vista Anyone??? Pin
Steven Dong at 9-Nov-08 5:37
memberSteven Dong9-Nov-08 5:37 
I have same question!
QuestionCallback Function Pin
peene at 9-Aug-07 6:46
memberpeene9-Aug-07 6:46 
Questionhow can i control the sound of left and right speaker Pin
rajneshmalik at 1-Aug-07 22:49
memberrajneshmalik1-Aug-07 22:49 
GeneralProblem in 2000 Pin
lilesh at 26-Feb-07 23:56
memberlilesh26-Feb-07 23:56 
QuestionBegginer's question Pin
lnenad at 12-Nov-06 9:12
memberlnenad12-Nov-06 9:12 
QuestionHigh Definition Audio Pin
Saber001 at 19-Jul-06 1:09
memberSaber00119-Jul-06 1:09 
QuestionHow to use Volume Manipulation Classes Pin
castlezhen at 28-Apr-06 4:04
membercastlezhen28-Apr-06 4:04 
GeneralDiable Microphone Pin
anakia at 18-Apr-06 18:05
memberanakia18-Apr-06 18:05 
General[Q] Love your article and more... Pin
yoontet at 17-Aug-05 6:52
memberyoontet17-Aug-05 6:52 
GeneralRe: [Q] Love your article and more... Pin
yoontet at 17-Aug-05 7:23
memberyoontet17-Aug-05 7:23 
GeneralProblems with Microphone control Pin
Virendra Sharma at 26-May-05 0:29
memberVirendra Sharma26-May-05 0:29 
GeneralRe: Problems with Microphone control Pin
Fco Javier Lama at 4-May-06 0:24
memberFco Javier Lama4-May-06 0:24 
Generaldoesn t work with a USB headset Pin
bartouze at 22-Apr-05 4:09
memberbartouze22-Apr-05 4:09 
GeneralProblem passing callback function in... Pin
el davo at 4-Mar-04 14:48
memberel davo4-Mar-04 14:48 
GeneralRe: Problem passing callback function in... Pin
laduran at 27-Aug-04 13:55
memberladuran27-Aug-04 13:55 
GeneralRe: Problem passing callback function in... Pin
Vinod Vijayan at 29-Jan-05 1:06
memberVinod Vijayan29-Jan-05 1:06 
QuestionHow to determine whether volume is enabled? Pin
M A V at 22-Feb-04 10:54
memberM A V22-Feb-04 10:54 
GeneralMany Compile Errors - microphone implementation Pin
Bobboots at 12-Feb-04 20:19
memberBobboots12-Feb-04 20:19 
GeneralSolved Compile - microphone vol does not change... Pin
Anonymous at 13-Feb-04 17:40
sussAnonymous13-Feb-04 17:40 
Generalline-in playback Pin
Anonymous at 12-Feb-04 11:53
sussAnonymous12-Feb-04 11:53 
GeneralRe: line-in playback Pin
Saber001 at 19-Jul-06 1:13
memberSaber00119-Jul-06 1:13 
GeneralRe: line-in playback Pin
wanhaven at 23-Feb-07 2:38
memberwanhaven23-Feb-07 2:38 
GeneralDisplaying input audio level in progress bar Pin
Hridaynath Musale at 10-Feb-04 14:24
memberHridaynath Musale10-Feb-04 14:24 
GeneralFound bug. It should called the destructor. Pin
saintstone at 1-Feb-04 18:28
membersaintstone1-Feb-04 18:28 
GeneralThanks! Pin
sparky909 at 23-Oct-03 2:05
membersparky90923-Oct-03 2:05 
GeneralRe: Thanks! Pin
Anonymous at 8-Aug-04 5:33
sussAnonymous8-Aug-04 5:33 
GeneralmmSystem.h Pin
dietercools at 20-Sep-03 14:02
memberdietercools20-Sep-03 14:02 
GeneralRe: mmSystem.h Pin
sparky909 at 23-Oct-03 1:57
membersparky90923-Oct-03 1:57 
GeneralRe: mmSystem.h Pin
dietercools at 11-Feb-04 9:42
memberdietercools11-Feb-04 9:42 
GeneralRe: mmSystem.h Pin
Anonymous at 11-Feb-04 14:21
sussAnonymous11-Feb-04 14:21 
QuestionHow to support WinCE Pin
weblxj at 2-Jul-03 16:03
memberweblxj2-Jul-03 16:03 
AnswerRe: How to support WinCE Pin
Alex Chmut at 2-Jul-03 23:17
memberAlex Chmut2-Jul-03 23:17 
Generalstdafx.h Pin
M.Joshi at 5-Apr-03 6:44
memberM.Joshi5-Apr-03 6:44 
AnswerRe: stdafx.h Pin
Saber001 at 19-Jul-06 0:54
memberSaber00119-Jul-06 0:54 
GeneralRe: stdafx.h Pin
Saber001 at 19-Jul-06 1:05
memberSaber00119-Jul-06 1:05 
Generalto increse the volume Pin
xyz123 at 3-Feb-03 2:57
sussxyz1233-Feb-03 2:57 
GeneralRe: to increse the volume Pin
Anonymous at 20-Nov-03 23:26
sussAnonymous20-Nov-03 23:26 
GeneralDoes not work while device in use Pin
codermallu at 1-Aug-02 4:11
membercodermallu1-Aug-02 4:11 
GeneralRe: Does not work while device in use Pin
Peter B. at 22-May-03 11:55
memberPeter B.22-May-03 11:55 
GeneralHelp: compilation error for VolumeInXXX.cpp Pin
Annette Skaar at 7-Jul-02 18:51
memberAnnette Skaar7-Jul-02 18:51 
QuestionChanges the balance !?!? Pin
Lou at 4-May-02 14:03
memberLou4-May-02 14:03 
AnswerRe: Changes the balance !?!? Pin
KuZiKuZi at 6-Jan-05 21:04
memberKuZiKuZi6-Jan-05 21:04 
GeneralDoes not work with USB Audio Device Pin
Anonymous at 12-Feb-02 9:09
memberAnonymous12-Feb-02 9:09 

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.150427.1 | Last Updated 17 Nov 1999
Article Copyright 1999 by Alex Chmut
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid