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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRe: stdafx.hmemberSaber00118 Jul '06 - 23:54 
GeneralRe: stdafx.hmemberSaber00119 Jul '06 - 0:05 
Generalto increse the volumesussxyz1233 Feb '03 - 1:57 
GeneralRe: to increse the volumesussAnonymous20 Nov '03 - 22:26 
GeneralDoes not work while device in usemembercodermallu1 Aug '02 - 3:11 
GeneralRe: Does not work while device in usememberPeter B.22 May '03 - 10:55 
GeneralHelp: compilation error for VolumeInXXX.cppmemberAnnette Skaar7 Jul '02 - 17:51 
HI,
 
I can't compile the classes in my project. I have an MFC application, and I have included IVolume.h, VolumeInXXX.h and VolumeInXXX.cpp in my project. I have included mmSystem.h and VolumeOutMaster.h in my StdAfx.h file and I have linked to winmm.lib. When compiling; I get over 100 errors for VolumeInXXX.cpp (here are some of them):
 
:\Code\Volume Control\Volume\VolumeInXXX.cpp(24) : error C2065: 'WAVEFORMATEX' : undeclared identifier
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(24) : error C2065: 'pwfe' : undeclared identifier
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(25) : error C2448: '' : function-style initializer appears to be a function definition
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(78) : error C2065: 'mixerGetNumDevs' : undeclared identifier
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(81) : error C2065: 'HWAVEIN' : undeclared identifier
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(81) : error C2146: syntax error : missing ';' before identifier 'hwaveIn'
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(81) : error C2065: 'hwaveIn' : undeclared identifier
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(82) : error C2065: 'MMRESULT' : undeclared identifier
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(82) : error C2146: syntax error : missing ';' before identifier 'mmResult'
C:\Code\Volume Control\Volume\VolumeInXXX.cpp(82) : error C2065: 'mmResult' : undeclared identifier
.
.
.
etc.
 
Does anyone know why? Any help is very appreciated!
 
Regards,
Annette Skaar
QuestionChanges the balance !?!?memberLou4 May '02 - 13:03 
AnswerRe: Changes the balance !?!?memberKuZiKuZi6 Jan '05 - 20:04 
GeneralDoes not work with USB Audio DevicememberAnonymous12 Feb '02 - 8:09 
GeneralRe: Does not work with USB Audio DevicememberNR19 Feb '02 - 14:03 
GeneralRe: Does not work with USB Audio DevicememberPeter B.23 Dec '02 - 3:31 
GeneralHere is my solution...memberpengok25 Nov '04 - 19:43 
GeneralSample project requiredmembersantu27 Nov '01 - 23:23 
GeneralRe: Sample project requiredmemberAtlantys11 Feb '02 - 5:03 
GeneralRe: Sample project requiredmemberlordsoth998 Jul '04 - 9:50 
GeneralRecordingmemberyoavadler5 Nov '01 - 22:58 
GeneralRe: RecordingmemberYong23 Nov '01 - 6:05 
GeneralThanks! But I have a question.memberhoonja23 Oct '01 - 1:16 
GeneralRe: Thanks! But I have a question.memberYong23 Nov '01 - 6:14 
QuestionpLineInfo initialization?memberAnonymous22 Sep '01 - 5:27 
Questionwhy mute volume in invalid in win2000?memberjadeking2 Sep '01 - 20:04 
AnswerRe: why mute volume in invalid in win2000?memberYong23 Nov '01 - 6:03 
GeneralStdafx.hmember_Nico_13 Aug '01 - 3:44 
GeneralRe: Stdafx.hsussAnonymous9 Aug '02 - 2:21 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 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