Volume Manipulation Classes

来源:互联网 发布:北风大数据教程百度云 编辑:程序博客网 时间:2024/05/16 17:23

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:

Collapse Copy Code
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.

Collapse Copy Code
...void CALLBACK MasterVolumeChanged( DWORD dwCurrentVolume, DWORD dwUserValue );...// Volume control InitializationIVolume* 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:

Collapse Copy Code
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:

Collapse Copy Code
...bool CALLBACK EnumInputLineProc( UINT uLineIndex,               MIXERLINE* pLineInfo, DWORD dwUserValue );...// InitializationUINT 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.

 

 

Form:  http://www.codeproject.com/audio/volumeclasses.asp

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 慢性肾炎感冒了血尿了怎么办 连接不可用红叉怎么办 鼻咽癌放疗后失去味觉怎么办 感冒会引起肾疼怎么办 iga患者老被传染感冒怎么办 iga肾病感冒了要怎么办 感冒了腰特别疼怎么办 运动后头晕恶心想吐怎么办 3岁儿童自闭症无语言怎么办? 2岁智力发育迟缓怎么办 法国第一年语言没有通过怎么办 听力42天未通过怎么办 7岁儿童叛逆期怎么办 被忽视型幼儿该怎么办 言语发育障碍脑神经损伤怎么办 5岁幼儿胆子小怎么办 幼儿20天胆子小怎么办 两岁宝宝胆子小怎么办 两岁的宝宝胆子小怎么办 孕中期发烧38度怎么办 小班社会交往教案有垃圾怎么办 两个月婴幼儿哭闹入睡难怎么办 8岁儿童睡觉困难怎么办 做精细动作手抖怎么办 15个月宝宝缺锌怎么办 八个月宝宝有蛔虫怎么办 两个月婴儿缺锌怎么办 婴儿天热出汗多尿少怎么办 婴儿5个月不主动抓东西怎么办 打疫苗吃了辣怎么办 四个月宝宝枕秃怎么办 四个月的孩子缺钙怎么办 小孩张牙齿会发烧怎么办 对节白蜡叶子干了怎么办 刷机字库坏了怎么办 魅蓝返回键失灵怎么办 牙蛀严重且牙根松动怎么办 蛀牙伤到神经了怎么办 给客户发错邮件怎么办外贸 cf没有枪声和脚步声怎么办 穿越火线fps太低怎么办