1. header file (mixer. h)
// Mixer. h: interface for the cmixer class. <br/> ////////////////////////////// //////////////////////////////////////// </P> <p> # If! Defined (afx_mixer_h%7%baa5_012e_467e_ad1e_a383e4474c58%included _) <br/> # define customization _ <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 ); // check whether the device is muted <br/> bool setmute (mixerdeice Dev, bool vol); // set the device to mute <br/> bool setvolume (mixerdeice Dev, long vol ); // set the device volume <br/> unsigned getvolume (mixerdeice Dev); // obtain the device volume Dev = 0 main volume, 1 wave, 2 Midi, 3 line in </P> <p> PRIVATE: <br/> bool getvolumecontrol (hmixer, Long componenttype, long ctrltype, mixercontrol * mxc); <br/> bool setvolumevalue (hmixer, mixercontrol * mxc, long volume); <br/> bool setmutevalue (hmixer, mixercontrol * mxc, bool mute); <br/> unsigned getvolumevalue (hmixer, mixercontrol * mxc); <br/> long getmutevalue (hmixer, mixercontrol * mxc ); </P> <p> long m_volrange; <br/>}; </P> <p> # endif //! Defined (afx_mixer_h%7%baa5_012e_467e_ad1e_a383e4474c58%included _)
2. cpp file (mixer. cpp)
// Mixer. CPP: Implementation of the cmixer class. <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/> // -------------------------- set the volume limit </P> <p> bool cmixer :: getvolumecontrol (hmixer, long componenttype, long ctrltype, mixercontrol * mxc) <br/>{< br/> mixerlinecontrols mxlc; <br/> mixerline mxl; <br/> bool exist = false; <br/> mxl. cbstruct = sizeof (M XL); <br/> mxl. dwcomponenttype = componenttype; </P> <p> If (componenttype = mixerline_componenttype_src_microphone) <br/>{// obtain the recording microphone device <br/> mxl. dwcomponenttype = established; <br/> // obtain the number of connections in the recording bus <br/> mixergetlineinfo (hmixerobj) hmixer, & mxl, <br/> mixer_objectf_hmixer | mixer_getlineinfof_componenttype ); <br/> // Save the number of connections <br/> DWORD dwconnections = mxl. cconnections; <br/> // Obtain the ID of the microphone device. <br/> DWORD dwlineid = 0; <br/> for (DWORD I = 0; I <dwconnections; I ++) <br/>{< br/> // enumerate each device. When the source ID is equal to the current iteration count <br/> mxl. dwsource = I; <br/> // obtain the connection information based on the sourceid <br/> mmresult mr = mixergetlineinfo (hmixerobj) hmixer, & mxl, <br/> mixer_objectf_hmixer | mixer_getlineinfof_source); <br/> // judge whether a function execution error occurs. <br/> If (Mr! = 0) <br/>{< br/> break; <br/>}< br/> // if the current device type is microphone, the loop is displayed. <Br/> If (mxl. dwcomponenttype = <br/> mixerline_componenttype_src_microphone) <br/> {<br/> exist = true; <br/> break; <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/> // compile <br/> long cmixer: getmutevalue (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 | signature) <br/> return-1; <br/> return mxcdmute. fvalue; <br/>}</P> <p> //------------------------------------------------------------------ --------- <Br/> unsigned cmixer: getvolumevalue (hmixer, mixercontrol * mxc) <br/>{< br/> mixercontroldetails mxcd; <br/> limit 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> // signature <br/> bool cmixer: setmutevalue (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) <br/> return 0; <br/> return 1; <br/>}</P> <p> //-------------- Compile </P> <p> bool cmixer: setvolumevalue (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. PADET Ails = & Vol; <br/> mxcd. cchannels = 1; <br/> If (mixersetcontroldetails (hmixerobj) hmixer, & mxcd, mixer_objectf_hmixer | mixer) <br/> return 0; <br/> return 1; <br/>}</P> <p> // signature <br/> unsigned/* winapi */cmixer: getvolume (mixerdeice Dev) // obtain the device volume Dev = 0 main volume, 1 wave, 2 Midi, 3 line in <br/>{</P> <p> long device; <B R/> unsigned RT = 0; <br/> mixercontrol volctrl; <br/> hmixer; <br/> switch (Dev) <br/> {<br/> case waveout: <br/> device = mixerline_componenttype_src_waveout; break; <br/> case synthesizer: <br/> device = signature; break; <br/> case microphone: <br/> // device = mixerline_componenttype_src_compactdisc; break; // CD volume <br/> device = mixerline_componenttype_src_microphone; Break; // microphone volume <br/> // device = mixerline_componenttype_src_line; break; // PC speaker volume <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> // signature </P> <p> bool/* winapi */cmixer: setvolume (mixerdeice Dev, long vol) // set the device volume <br/>{< br/> // Dev =, 2 represents the main volume, waveform, Midi, line in <br/> // Vol = 0-m_volrange indicates the volume. The percentage is used for setting and returning the volume, that is, the volume starts from 0 to m_volrange, instead of the absolute value of the device <br/> // retrun false indicates that the operation to set the volume is not successful <br/> // retrun true indicates that the operation to set the volume is successful </P> <p> long device; <br/> bool rc = false; <br/> mixercontrol volctrl; <br/> hmixer; <br/> switch (Dev) <br/> {<br/> case waveout: <br/> device = mixerline_componenttype_src_waveout; break; <br/> case synthesizer: <br/> device = signature; break; <br/> case microphone: </P> <p> // device = mixerline_componenttype_src_compactdisc; break; // CD volume <br/> device = mixerline_componenttype_src_microphone; break; // microphone volume <br/> // device = mixerline_componenttype_src_line; break; // PC speaker volume <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> // signature </P> <p> bool/* winapi */cmixer: setmute (mixerdeice Dev, /* long */bool vol) // set the device to mute <br/> {<br/> // Dev = 0, 1, 2, indicating the main volume, waveform, Midi, line in <br/> // Vol = indicates that the mute is canceled, set mute <br/> // retrun false indicates that the operation is canceled or set to mute failed <br/> // retrun true indicates that the operation is canceled or set to mute successfully </P> <p> long device; <br/> bool rc = false; <br/> mixercontrol volctrl; <br/> hmixer; <br/> switch (Dev) <br/> {<br/> case waveout: <br/> device = mixerline_componenttype_src_waveout; break; <br/> case synthesizer: <br/> device = signature; break; <br/> case microphone: <br/> // device = mixerline_componenttype_src_compactdisc; break; // CD volume <br/> device = mixerline_componenttype_src_microphone; break; // microphone volume <br/> // device = mixerline_componenttype_src_line; break; // PC speaker volume <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> // signature </P> <p> bool/* winapi */cmixer: getmute (mixerdeice Dev) // check whether the device is muted <br/>{< br/> // Dev =, 2 indicates the main volume, waveform, Midi, line in <br/> // retrun false indicates no mute <br/> // retrun true indicates mute <br/> long device; <br/> bool rc = false; <br/> mixercontrol volctrl; <br/> 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 volume <br/> // device = mixerline_componenttype_src_microphone; break; // microphone volume <br/> // device = mixerline_componenttype_src_line; break; // PC speaker volume <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) 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>}