音频设备操作(静音,音量调节,麦克风音量)
// GTVolumeMute.h: interface for the CGTVolumeMute class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_GTVOLUMEMUTE_H__AFD7D44E_54A3_48B6_A87E_7CFD0A17D148__INCLUDED_)
#define AFX_GTVOLUMEMUTE_H__AFD7D44E_54A3_48B6_A87E_7CFD0A17D148__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <mmsystem.h>
#include <mmreg.h>
class CGTVolumeMute
{
public:
CGTVolumeMute();
virtual ~CGTVolumeMute();
public:
unsigned WINAPI GetVolume(int dev);
bool WINAPI SetVolume(long dev,long vol);
bool WINAPI SetMute(long dev,bool vol);
bool WINAPI GetMute(long dev);
private:
bool GetVolumeControl(HMIXER hmixer,long componentType, long ctrlType,MIXERCONTROL* mxc);
long GetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc);
unsigned GetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc);
bool SetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc, bool mute);
bool SetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc, long volume);
};
#endif // !defined(AFX_GTVOLUMEMUTE_H__AFD7D44E_54A3_48B6_A87E_7CFD0A17D148__INCLUDED_)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GTVolumeMute.cpp: implementation of the CGTVolumeMute class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Dlg.h"
#include "GTVolumeMute.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGTVolumeMute::CGTVolumeMute()
{
}
CGTVolumeMute::~CGTVolumeMute()
{
}
bool CGTVolumeMute::GetVolumeControl(HMIXER hmixer ,long componentType,long ctrlType,MIXERCONTROL* mxc)
{
MIXERLINECONTROLS mxlc;
MIXERLINE mxl;
mxl.cbStruct = sizeof(mxl);
mxl.dwComponentType = componentType;
if(!mixerGetLineInfo((HMIXEROBJ)hmixer, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE))
{
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType = ctrlType;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(MIXERCONTROL);
mxlc.pamxctrl = mxc;
if(mixerGetLineControls((HMIXEROBJ)hmixer,&mxlc,MIXER_GETLINECONTROLSF_ONEBYTYPE))
return 0;
else
return 1;
}
return 0;
}
//---------------------------------------------------------------------------
long CGTVolumeMute::GetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_BOOLEAN mxcdMute;
mxcd.hwndOwner = 0;
mxcd.cbStruct = sizeof(mxcd);
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbDetails = sizeof(mxcdMute);
mxcd.paDetails = &mxcdMute;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
if (mixerGetControlDetails((HMIXEROBJ)hmixer, &mxcd,MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))
return -1;
return mxcdMute.fValue;
}
//---------------------------------------------------------------------------
unsigned CGTVolumeMute::GetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_UNSIGNED vol; vol.dwValue=0;
mxcd.hwndOwner = 0;
mxcd.cbStruct = sizeof(mxcd);
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbDetails = sizeof(vol);
mxcd.paDetails = &vol;
mxcd.cChannels = 1;
if(mixerGetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))
return -1;
return vol.dwValue;
}
//---------------------------------------------------------------------------
bool CGTVolumeMute::SetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc, bool mute)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_BOOLEAN mxcdMute;mxcdMute.fValue=mute;
mxcd.hwndOwner = 0;
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(mxcdMute);
mxcd.paDetails = &mxcdMute;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
if (mixerSetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))
return 0;
return 1;
}
//---------------------------------------------------------------------------
bool CGTVolumeMute::SetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc, long volume)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_UNSIGNED vol;vol.dwValue = volume;
mxcd.hwndOwner = 0;
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(vol);
mxcd.paDetails = &vol;
mxcd.cChannels = 1;
if(mixerSetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))
return 0;
return 1;
}
//---------------------------------------------------------------------------
unsigned WINAPI CGTVolumeMute::GetVolume(int dev)//µÃµ½É豸µÄÒôÁ¿dev=0Ö÷ÒôÁ¿£¬1WAVE ,2MIDI ,3 LINE IN
{
long device;unsigned rt=0;
MIXERCONTROL volCtrl;
HMIXER hmixer;
switch (dev)
{
case 1:
device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;
case 2:
device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;
case 3:
// device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break; // cd ÒôÁ¿
// device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; break; //Âó¿Ë·çÒôÁ¿
// device=MIXERLINE_COMPONENTTYPE_SRC_LINE; break; //PC ÑïÉùÆ÷ÒôÁ¿
device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;
case 4:
device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;break;
default:
device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}
if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;
if(!GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))
return 0;
rt=GetVolumeValue(hmixer,&volCtrl)*100/volCtrl.Bounds.lMaximum;
mixerClose(hmixer);
return rt;
}
//---------------------------------------------------------------------------
bool WINAPI CGTVolumeMute::SetVolume(long dev,long vol)
{
// dev =0,1,2 ·Ö±ð±íʾÖ÷ÒôÁ¿,²¨ÐÎ,MIDI ,LINE IN
// vol=0-100 ±íʾÒôÁ¿µÄ´óС , ÉèÖÃÓë·µ»ØÒôÁ¿µÄÖµÓõÄÊǰٷֱȣ¬¼´ÒôÁ¿´Ó0 - 100£¬¶ø²»ÊÇÉ豸µÄ¾ø¶ÔÖµ
// retrun false ±íʾÉèÖÃÒôÁ¿µÄ´óСµÄ²Ù×÷²»³É¹¦
// retrun true ±íʾÉèÖÃÒôÁ¿µÄ´óСµÄ²Ù×÷³É¹¦
long device;
bool rc=false;
MIXERCONTROL volCtrl;
HMIXER hmixer;
switch (dev)
{
case 1:
device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;
case 2:
device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;
case 3:
device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;
case 4:
device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;break;
default:
device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}
if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;
if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))
{
vol=vol*volCtrl.Bounds.lMaximum/100;
if(SetVolumeValue(hmixer,&volCtrl,vol))
rc=true;
}
mixerClose(hmixer);
return rc;
}
//---------------------------------------------------------------------------
bool WINAPI CGTVolumeMute::SetMute(long dev,bool vol)//ÉèÖÃÉ豸¾²Òô
{
// dev =0,1,2 ·Ö±ð±íʾÖ÷ÒôÁ¿,²¨ÐÎ,MIDI ,LINE IN
// vol=0,1 ·Ö±ð±íʾȡÏû¾²Òô,ÉèÖþ²Òô
// retrun false ±íʾȡÏû»òÉèÖþ²Òô²Ù×÷²»³É¹¦
// retrun true ±íʾȡÏû»òÉèÖþ²Òô²Ù×÷³É¹¦
long device;
bool rc=false;
MIXERCONTROL volCtrl;
HMIXER hmixer;
switch (dev)
{
case 1:
device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;
case 2:
device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;
case 3:
device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;
case 4:
device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;break;
default:
device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}
if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;
if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_MUTE,&volCtrl))
if(SetMuteValue(hmixer,&volCtrl,(bool)vol))
rc=true;
mixerClose(hmixer);
return rc;
}
//---------------------------------------------------------------------------
bool WINAPI CGTVolumeMute::GetMute(long dev)
{
//dev =0,1,2 ·Ö±ð±íʾÖ÷ÒôÁ¿£¬²¨ÐÎ,MIDI ,LINE IN
// retrun false ±íʾûÓо²Òô
// retrun true ±íʾ¾²Òô
long device;
bool rc=false;
MIXERCONTROL volCtrl;
HMIXER hmixer;
switch (dev)
{
case 1:
device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;
case 2:
device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;
case 3:
device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;
case 4:
device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;break;
default:
device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}
if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;
if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_MUTE,&volCtrl))
rc = GetMuteValue(hmixer,&volCtrl) == 0 ? false : true;
mixerClose(hmixer);
return rc;
}