Click here to Skip to main content
Licence 
First Posted 16 Nov 1999
Views 251,405
Downloads 3,253
Bookmarked 81 times

Volume Manipulation Classes

By | 16 Nov 1999 | Article
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

About the Author

Alex Chmut

Web Developer

Ukraine Ukraine

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionLicense? PinmemberLuisVilla4:48 16 Feb '12  
AnswerRe: License? PinmemberAlex Chmut10:15 16 Feb '12  
GeneralControlling master volume in Win7 - IAudioEndpointVolume - Working sample code in MSDN Pinmemberkormoe5:39 15 Jan '12  
GeneralVolume Control in Windows7 PinmemberMember 264083620:41 17 Aug '10  
GeneralMaking compatible PinmemberBazKhan22:49 18 Jan '09  
QuestionWindows Vista Anyone??? Pinmember{bill}9:41 1 Sep '08  
AnswerRe: Windows Vista Anyone??? PinmemberSteven Dong4:37 9 Nov '08  
QuestionCallback Function Pinmemberpeene5:46 9 Aug '07  
Questionhow can i control the sound of left and right speaker Pinmemberrajneshmalik21:49 1 Aug '07  
GeneralProblem in 2000 Pinmemberlilesh22:56 26 Feb '07  
QuestionBegginer's question Pinmemberlnenad8:12 12 Nov '06  
QuestionHigh Definition Audio PinmemberSaber0010:09 19 Jul '06  
QuestionHow to use Volume Manipulation Classes Pinmembercastlezhen3:04 28 Apr '06  
GeneralDiable Microphone Pinmemberanakia17:05 18 Apr '06  
General[Q] Love your article and more... Pinmemberyoontet5:52 17 Aug '05  
GeneralRe: [Q] Love your article and more... Pinmemberyoontet6:23 17 Aug '05  
GeneralProblems with Microphone control PinmemberVirendra Sharma23:29 25 May '05  
GeneralRe: Problems with Microphone control PinmemberFco Javier Lama23:24 3 May '06  
Generaldoesn t work with a USB headset Pinmemberbartouze3:09 22 Apr '05  
GeneralProblem passing callback function in... Pinmemberel davo13:48 4 Mar '04  
GeneralRe: Problem passing callback function in... Pinmemberladuran12:55 27 Aug '04  
GeneralRe: Problem passing callback function in... PinmemberVinod Vijayan0:06 29 Jan '05  
QuestionHow to determine whether volume is enabled? PinmemberM A V9:54 22 Feb '04  
GeneralMany Compile Errors - microphone implementation PinmemberBobboots19:19 12 Feb '04  
GeneralSolved Compile - microphone vol does not change... PinsussAnonymous16:40 13 Feb '04  

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.

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