Click here to Skip to main content
Click here to Skip to main content

Volume Manipulation Classes

By , 16 Nov 1999
 

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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionWindows 7 volume control sample PinmemberM A V15 Dec '12 - 1:05 
QuestionLicense? PinmemberLuisVilla16 Feb '12 - 4:48 
GeneralControlling master volume in Win7 - IAudioEndpointVolume - Working sample code in MSDN Pinmemberkormoe15 Jan '12 - 5:39 
GeneralVolume Control in Windows7 PinmemberMember 264083617 Aug '10 - 20:41 
GeneralMaking compatible PinmemberBazKhan18 Jan '09 - 22:49 
QuestionWindows Vista Anyone??? Pinmember{bill}1 Sep '08 - 9:41 
QuestionCallback Function Pinmemberpeene9 Aug '07 - 5:46 
Questionhow can i control the sound of left and right speaker Pinmemberrajneshmalik1 Aug '07 - 21:49 
GeneralProblem in 2000 Pinmemberlilesh26 Feb '07 - 22:56 
QuestionBegginer's question Pinmemberlnenad12 Nov '06 - 8:12 
QuestionHigh Definition Audio PinmemberSaber00119 Jul '06 - 0:09 
QuestionHow to use Volume Manipulation Classes Pinmembercastlezhen28 Apr '06 - 3:04 
GeneralDiable Microphone Pinmemberanakia18 Apr '06 - 17:05 
General[Q] Love your article and more... Pinmemberyoontet17 Aug '05 - 5:52 
GeneralProblems with Microphone control PinmemberVirendra Sharma25 May '05 - 23:29 
Generaldoesn t work with a USB headset Pinmemberbartouze22 Apr '05 - 3:09 
GeneralProblem passing callback function in... Pinmemberel davo4 Mar '04 - 13:48 
QuestionHow to determine whether volume is enabled? PinmemberM A V22 Feb '04 - 9:54 
GeneralMany Compile Errors - microphone implementation PinmemberBobboots12 Feb '04 - 19:19 
Generalline-in playback PinsussAnonymous12 Feb '04 - 10:53 
GeneralDisplaying input audio level in progress bar PinmemberHridaynath Musale10 Feb '04 - 13:24 
GeneralFound bug. It should called the destructor. Pinmembersaintstone1 Feb '04 - 17:28 
GeneralThanks! Pinmembersparky90923 Oct '03 - 1:05 
GeneralmmSystem.h Pinmemberdietercools20 Sep '03 - 13:02 
QuestionHow to support WinCE Pinmemberweblxj2 Jul '03 - 15:03 
Generalstdafx.h PinmemberM.Joshi5 Apr '03 - 5:44 
Generalto increse the volume Pinsussxyz1233 Feb '03 - 1:57 
GeneralDoes not work while device in use Pinmembercodermallu1 Aug '02 - 3:11 
GeneralHelp: compilation error for VolumeInXXX.cpp PinmemberAnnette Skaar7 Jul '02 - 17:51 
QuestionChanges the balance !?!? PinmemberLou4 May '02 - 13:03 
GeneralDoes not work with USB Audio Device PinmemberAnonymous12 Feb '02 - 8:09 
GeneralSample project required Pinmembersantu27 Nov '01 - 23:23 
GeneralRecording Pinmemberyoavadler5 Nov '01 - 22:58 
GeneralThanks! But I have a question. Pinmemberhoonja23 Oct '01 - 1:16 
QuestionpLineInfo initialization? PinmemberAnonymous22 Sep '01 - 5:27 
Questionwhy mute volume in invalid in win2000? Pinmemberjadeking2 Sep '01 - 20:04 
GeneralStdafx.h Pinmember_Nico_13 Aug '01 - 3:44 
GeneralStdafx.h Pinmember_Nico_13 Aug '01 - 3:44 
Generalgood class PinmemberAnonymous19 Jul '01 - 5:17 
QuestionWhy It Cost So Much Memory? Pinmembercookie2k20 May '01 - 20:55 
GeneralEqualizer Pinmembertatdat8 May '01 - 15:00 
GeneralWhy? I can't execute in win98 PinmemberPowenko15 Feb '01 - 14:24 
QuestionWhy do you need the global g_pThis? PinmemberPatrick Koorevaar14 Dec '00 - 2:36 
Generalmore audio board PinmemberAntonio Salomone9 Nov '00 - 10:36 
GeneralExactly what I was looking for PinsussMiezoo24 Oct '00 - 22:46 
GeneralDamn good class !!! PinsussMarlboro15 May '00 - 15:19 
GeneralThanks! PinsussKiran13 Apr '00 - 13:51 

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

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