1、標頭檔(Mixer.h)
// Mixer.h: interface for the CMixer class.<br />//<br />////////////////////////////////////////////////////////////////////// </p><p>#if !defined(AFX_MIXER_H__7369BAA5_012E_467E_AD1E_A383E4474C58__INCLUDED_)<br />#define AFX_MIXER_H__7369BAA5_012E_467E_AD1E_A383E4474C58__INCLUDED_<br />#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers </p><p>#include "windows.h"<br />#include <mmsystem.h><br />#if _MSC_VER > 1000<br />#pragma once<br />#endif // _MSC_VER > 1000 </p><p>class CMixer<br />{<br />public:<br />CMixer();<br />CMixer(const int VolRange);<br />virtual ~CMixer();<br />enum MixerDeice{<br />SPEAKERS=0,<br />WAVEOUT,<br />SYNTHESIZER,<br />MICROPHONE<br />}; </p><p>bool GetMute(MixerDeice dev); //檢查裝置是否靜音<br />bool SetMute(MixerDeice dev,bool vol); //設定裝置靜音<br />bool SetVolume(MixerDeice dev,long vol); //設定裝置的音量<br />unsigned GetVolume(MixerDeice dev); //得到裝置的音量dev=0主音量,1WAVE ,2MIDI ,3 LINE IN </p><p>private:<br />bool GetVolumeControl(HMIXER hmixer ,long componentType,long ctrlType,MIXERCONTROL* mxc);<br />bool SetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc, long volume);<br />bool SetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc, bool mute);<br />unsigned GetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc);<br />long GetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc); </p><p>long m_VolRange ;<br />}; </p><p>#endif // !defined(AFX_MIXER_H__7369BAA5_012E_467E_AD1E_A383E4474C58__INCLUDED_)
2、cpp檔案(Mixer.cpp)
// Mixer.cpp: implementation of the CMixer class.<br />//<br />//#include "stdwx.h"<br />#include "Mixer.h" </p><p>//////////////////////////////////////////////////////////////////////<br />// Construction/Destruction<br />////////////////////////////////////////////////////////////////////// </p><p>CMixer::CMixer()<br />{<br />m_VolRange = 100;<br />} </p><p>CMixer::~CMixer()<br />{ </p><p>} </p><p>CMixer::CMixer(const int VolRange)<br />{<br />m_VolRange = VolRange;<br />}<br />//----------------------------設定音量--------------------------------------- </p><p>bool CMixer::GetVolumeControl(HMIXER hmixer ,long componentType,long ctrlType,MIXERCONTROL* mxc)<br />{<br />MIXERLINECONTROLS mxlc;<br />MIXERLINE mxl;<br />bool exist = false;<br />mxl.cbStruct = sizeof(mxl);<br />mxl.dwComponentType = componentType; </p><p>if (componentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE)<br />{//擷取錄音麥克風裝置<br /> mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;<br /> // 得到錄製匯流排中的串連數<br /> mixerGetLineInfo( (HMIXEROBJ)hmixer, &mxl,<br /> MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE );<br /> // 將串連數儲存<br /> DWORD dwConnections = mxl.cConnections;<br /> // 準備擷取麥克風裝置的ID<br /> DWORD dwLineID = 0;<br /> for ( DWORD i = 0; i < dwConnections; i++ )<br /> {<br /> // 枚舉每一個裝置,當Source的ID等於當前的迭代記數<br /> mxl.dwSource = i;<br /> // 根據SourceID獲得串連的資訊<br /> MMRESULT mr = mixerGetLineInfo( (HMIXEROBJ)hmixer, &mxl,<br /> MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_SOURCE );<br /> // 判斷函數執行錯誤<br /> if ( mr != 0 )<br /> {<br /> break;<br /> }<br /> // 如果當前裝置類型是麥克風,則跳出迴圈。<br /> if ( mxl.dwComponentType ==<br /> MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE )<br /> {<br /> exist = true;<br /> break;<br /> }<br /> }<br />}else if(!mixerGetLineInfo((HMIXEROBJ)hmixer, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE))<br />{<br /> exist = true;<br />} </p><p>if (exist)<br />{<br /> mxlc.cbStruct = sizeof(mxlc);<br /> mxlc.dwLineID = mxl.dwLineID;<br /> mxlc.dwControlType = ctrlType;<br /> mxlc.cControls = 1;<br /> mxlc.cbmxctrl = sizeof(MIXERCONTROL);<br /> mxlc.pamxctrl = mxc;<br /> if(mixerGetLineControls((HMIXEROBJ)hmixer,&mxlc,MIXER_GETLINECONTROLSF_ONEBYTYPE))<br /> return 0;<br /> else<br /> return 1;<br />}<br />return 0;<br />}<br />//---------------------------------------------------------------------------<br />long CMixer::GetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc)<br />{<br />MIXERCONTROLDETAILS mxcd;<br />MIXERCONTROLDETAILS_BOOLEAN mxcdMute;<br />mxcd.hwndOwner = 0;<br />mxcd.cbStruct = sizeof(mxcd);<br />mxcd.dwControlID = mxc->dwControlID;<br />mxcd.cbDetails = sizeof(mxcdMute);<br />mxcd.paDetails = &mxcdMute;<br />mxcd.cChannels = 1;<br />mxcd.cMultipleItems = 0;<br />if (mixerGetControlDetails((HMIXEROBJ)hmixer, &mxcd,MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))<br /> return -1;<br />return mxcdMute.fValue;<br />} </p><p>//---------------------------------------------------------------------------<br />unsigned CMixer::GetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc)<br />{<br />MIXERCONTROLDETAILS mxcd;<br />MIXERCONTROLDETAILS_UNSIGNED vol;<br />vol.dwValue=0;<br />mxcd.hwndOwner = 0;<br />mxcd.cbStruct = sizeof(mxcd);<br />mxcd.dwControlID = mxc->dwControlID;<br />mxcd.cbDetails = sizeof(vol);<br />mxcd.paDetails = &vol;<br />mxcd.cChannels = 1;<br />if(mixerGetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))<br /> return -1;<br />return vol.dwValue;<br />} </p><p>//---------------------------------------------------------------------------<br />bool CMixer::SetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc, bool mute)<br />{<br />MIXERCONTROLDETAILS mxcd;<br />MIXERCONTROLDETAILS_BOOLEAN mxcdMute;<br />mxcdMute.fValue=mute;<br />mxcd.hwndOwner = 0;<br />mxcd.dwControlID = mxc->dwControlID;<br />mxcd.cbStruct = sizeof(mxcd);<br />mxcd.cbDetails = sizeof(mxcdMute);<br />mxcd.paDetails = &mxcdMute;<br />mxcd.cChannels = 1;<br />mxcd.cMultipleItems = 0;<br />if (mixerSetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))<br /> return 0;<br />return 1;<br />} </p><p>//--------------------------------------------------------------------------- </p><p>bool CMixer::SetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc, long volume)<br />{<br />MIXERCONTROLDETAILS mxcd;<br />MIXERCONTROLDETAILS_UNSIGNED vol;<br />vol.dwValue = volume;<br />mxcd.hwndOwner = 0;<br />mxcd.dwControlID = mxc->dwControlID;<br />mxcd.cbStruct = sizeof(mxcd);<br />mxcd.cbDetails = sizeof(vol);<br />mxcd.paDetails = &vol;<br />mxcd.cChannels = 1;<br />if(mixerSetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))<br /> return 0;<br />return 1;<br />} </p><p>//---------------------------------------------------------------------------<br />unsigned /*WINAPI */CMixer::GetVolume(MixerDeice dev)//得到裝置的音量dev=0主音量,1WAVE ,2MIDI ,3 LINE IN<br />{ </p><p>long device;<br />unsigned rt=0;<br />MIXERCONTROL volCtrl;<br />HMIXER hmixer;<br />switch (dev)<br />{<br /> case WAVEOUT:<br /> device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;<br /> case SYNTHESIZER:<br /> device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;<br /> case MICROPHONE:<br /> // device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break; // cd 音量<br /> device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; break; //麥克風音量<br /> // device=MIXERLINE_COMPONENTTYPE_SRC_LINE; break; //PC 喇叭音量<br /> //device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break; </p><p> default:<br /> device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;<br />} </p><p>if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;<br />if(!GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))<br /> { </p><p>mixerClose(hmixer); </p><p>return 0; </p><p>}<br />rt=GetVolumeValue(hmixer,&volCtrl)*m_VolRange /volCtrl.Bounds.lMaximum;<br />mixerClose(hmixer);<br />return rt; </p><p>} </p><p>//--------------------------------------------------------------------------- </p><p>bool /*WINAPI*/ CMixer::SetVolume(MixerDeice dev,long vol)//設定裝置的音量<br />{<br />// dev =0,1,2 分別表示主音量,波形,MIDI ,LINE IN<br />// vol=0-m_VolRange 表示音量的大小 , 設定與返迴音量的值用的是百分比,即音量從0 - m_VolRange ,而不是裝置的絕對值<br />// retrun false 表示設定音量的大小的操作不成功<br />// retrun true 表示設定音量的大小的操作成功 </p><p>long device;<br />bool rc=false;<br />MIXERCONTROL volCtrl;<br />HMIXER hmixer;<br />switch (dev)<br />{<br />case WAVEOUT:<br /> device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;<br />case SYNTHESIZER:<br /> device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;<br />case MICROPHONE: </p><p> // device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break; // cd 音量<br /> device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; break; //麥克風音量<br /> // device=MIXERLINE_COMPONENTTYPE_SRC_LINE; break; //PC 喇叭音量<br /> //device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;<br />default:<br /> device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;<br />} </p><p>if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0; </p><p>if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))<br />{<br /> vol=vol*volCtrl.Bounds.lMaximum/m_VolRange ;<br /> if(SetVolumeValue(hmixer,&volCtrl,vol))<br /> rc=true;<br />}<br />mixerClose(hmixer);<br />return rc;<br />} </p><p>//--------------------------------------------------------------------------- </p><p>bool /*WINAPI*/ CMixer::SetMute(MixerDeice dev,/*long*/bool vol)//設定裝置靜音<br />{<br />// dev =0,1,2 分別表示主音量,波形,MIDI ,LINE IN<br />// vol=0,1 分別表示取消靜音,設定靜音<br />// retrun false 表示取消或設定靜音操作不成功<br />// retrun true 表示取消或設定靜音操作成功 </p><p>long device;<br />bool rc=false;<br />MIXERCONTROL volCtrl;<br />HMIXER hmixer;<br />switch (dev)<br />{<br />case WAVEOUT:<br /> device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;<br />case SYNTHESIZER:<br /> device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;<br />case MICROPHONE:<br /> // device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break; // cd 音量<br /> device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; break; //麥克風音量<br /> // device=MIXERLINE_COMPONENTTYPE_SRC_LINE; break; //PC 喇叭音量<br /> //device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break; </p><p>default:<br /> device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;<br />} </p><p>if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;<br />if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_MUTE,&volCtrl))<br /> if(SetMuteValue(hmixer,&volCtrl,(bool)vol))<br /> rc=true;<br /> mixerClose(hmixer);<br /> return rc;<br />} </p><p>//--------------------------------------------------------------------------- </p><p>bool /*WINAPI */CMixer::GetMute(MixerDeice dev)//檢查裝置是否靜音<br />{<br />//dev =0,1,2 分別表示主音量,波形,MIDI ,LINE IN<br />// retrun false 表示沒有靜音<br />// retrun true 表示靜音<br />long device;<br />bool rc=false;<br />MIXERCONTROL volCtrl;<br />HMIXER hmixer;<br />switch (dev)<br />{<br />case WAVEOUT:<br /> device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;<br />case SYNTHESIZER:<br /> device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;<br />case MICROPHONE:<br /> // device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break; // cd 音量<br /> //device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; break; //麥克風音量<br /> // device=MIXERLINE_COMPONENTTYPE_SRC_LINE; break; //PC 喇叭音量<br /> //device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;<br /> device=MIXERLINE_COMPONENTTYPE_DST_WAVEIN; break;<br />default:<br /> device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;<br />} </p><p>if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0; </p><p>if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_MUTE,&volCtrl))<br /> rc=GetMuteValue(hmixer,&volCtrl);<br />mixerClose(hmixer);<br />return rc; </p><p>}