📄 volumeapi.cpp
字号:
}
*pVolume = 0;
//if the ringer volume is the active volume now, get true value from waveOut api
if (PHGetActiveVolume() == RingerVolumeMode)
{
return GetWaveOutVolume(pVolume);
}
//otherwise, it is not applicable to get live ringer volume, return E_FAIL
return E_FAIL;
}
/*------------------------------------------------------------------------------
SetRingerVolume
Set ringer volume
------------------------------------------------------------------------------*/
HRESULT
SetRingerVolume(
DWORD UIVolume
)
{
if (UIVolume < 0 || UIVolume > c_MaxUIVolume)
{
return E_INVALIDARG;
}
//apply it to waveout api when ringer volume is the active volume
if (PHGetActiveVolume() == RingerVolumeMode)
{
return SetWaveOutVolume(UIVolume);
}
return S_OK;
}
/*------------------------------------------------------------------------------
GetSpeakerVolume
Get current speaker volume (UI volume ranges from 0 - c_MaxUIVolume)
------------------------------------------------------------------------------*/
HRESULT
GetSpeakerVolume(
__out DWORD* pVolume
)
{
if (pVolume == NULL)
{
return E_INVALIDARG;
}
*pVolume = 0;
//if we speaker volume is the active volume now, get true value from waveOut api
if (PHGetActiveVolume() == SpeakerVolumeMode)
{
return GetWaveOutVolume(pVolume);
}
//otherwise, it is not applicable to get Live speaker volume, return E_FAIL
return E_FAIL;
}
/*------------------------------------------------------------------------------
SetSpeakerVolume
Set current speaker volume
------------------------------------------------------------------------------*/
HRESULT
SetSpeakerVolume(
DWORD UIVolume
)
{
if (UIVolume < 0 || UIVolume > c_MaxUIVolume)
{
return E_INVALIDARG;
}
//apply it to wave out api when it is the active volume currently
if (PHGetActiveVolume() == SpeakerVolumeMode)
{
return SetWaveOutVolume(UIVolume);
}
return S_OK;
}
/*------------------------------------------------------------------------------
GetMicrophoneVolume
Get current microphone volume
------------------------------------------------------------------------------*/
HRESULT
GetMicrophoneVolume(
__out DWORD* pVolume
)
{
if (pVolume == NULL)
{
return E_INVALIDARG;
}
*pVolume = 0;
Mixer_t Mixer;
HRESULT hr = Mixer.Open();
if (FAILED(hr))
{
return E_UNEXPECTED;
}
hr = Mixer.GetSourceVolume(pVolume);
if (FAILED(hr))
{
return E_UNEXPECTED;
}
*pVolume = AudioVolumeToUIVolume(*pVolume);
return S_OK;
}
/*------------------------------------------------------------------------------
SetMicrophoneVolume
Set current microphone volume
------------------------------------------------------------------------------*/
HRESULT
SetMicrophoneVolume(
DWORD UIVolume
)
{
if (UIVolume < 0 || UIVolume > c_MaxUIVolume)
{
return E_INVALIDARG;
}
Mixer_t Mixer;
HRESULT hr = Mixer.Open();
if (FAILED(hr))
{
return hr;
}
DWORD Volume = UIVolumeToAudioVolume(UIVolume, false);
hr = Mixer.SetSourceVolume(Volume);
if (FAILED(hr))
{
return hr;
}
return hr;
}
/*------------------------------------------------------------------------------
GetLiveVolumeIfApplicable
Get UI volume for given device
------------------------------------------------------------------------------*/
HRESULT
GetLiveVolumeIfApplicable(
PHVolumeSetting Type,
__out DWORD* pVolume
)
{
switch (Type)
{
case phvsMicrophoneVolume:
return GetMicrophoneVolume(pVolume);
case phvsSpeakerVolume:
return GetSpeakerVolume(pVolume);
case phvsRingerVolume:
return GetRingerVolume(pVolume);
default:
ASSERT(FALSE);
return E_INVALIDARG;
}
}
/*------------------------------------------------------------------------------
SetLiveVolumeIfApplicable
Set UI volume for given device
------------------------------------------------------------------------------*/
HRESULT
SetLiveVolumeIfApplicable(
PHVolumeSetting Type,
DWORD Value
)
{
switch (Type)
{
case phvsMicrophoneVolume:
return SetMicrophoneVolume(Value);
case phvsSpeakerVolume:
return SetSpeakerVolume(Value);
case phvsRingerVolume:
return SetRingerVolume(Value);
default:
ASSERT(FALSE);
return E_INVALIDARG;
}
}
inline
bool
GetVolumeSettingRegPath(
PHVolumeSetting Setting,
__deref_out_opt HKEY* pRoot,
__deref_out_opt const WCHAR** ppPath,
__deref_out_opt const WCHAR** ppValue
)
{
switch (Setting)
{
case phvsHandsetVolume:
*pRoot = SN_VOIP_HANDSETVOLUME_ROOT;
*ppPath = SN_VOIP_HANDSETVOLUME_PATH;
*ppValue = SN_VOIP_HANDSETVOLUME_VALUE;
break;
case phvsSpeakerVolume:
*pRoot = SN_VOIP_SPEAKERVOLUME_ROOT;
*ppPath = SN_VOIP_SPEAKERVOLUME_PATH;
*ppValue = SN_VOIP_SPEAKERVOLUME_VALUE;
break;
case phvsRingerVolume:
*pRoot = SN_VOIP_RINGERVOLUME_ROOT;
*ppPath = SN_VOIP_RINGERVOLUME_PATH;
*ppValue = SN_VOIP_RINGERVOLUME_VALUE;
break;
case phvsMicrophoneVolume:
*pRoot = SN_VOIP_MICROPHONEVOLUME_ROOT;
*ppPath = SN_VOIP_MICROPHONEVOLUME_PATH;
*ppValue = SN_VOIP_MICROPHONEVOLUME_VALUE;
break;
default:
ASSERT(FALSE);
return false;
}
return true;
}
EXTERN_C
DWORD
PHGetVolumeSetting(
PHVolumeSetting Setting
)
{
//for volume, we should first try to get live volume from either waveOut or Mixer if that is applicable
if (Setting == phvsMicrophoneVolume ||
Setting == phvsSpeakerVolume ||
Setting == phvsRingerVolume)
{
DWORD CurrentVolume = 0;
if (SUCCEEDED(GetLiveVolumeIfApplicable(Setting, &CurrentVolume)))
{
return CurrentVolume;
}
//if we failed/not applicable at getting volume, continue to get volume from registry
}
HKEY Root;
const WCHAR* pPath = NULL;
const WCHAR* pValue = NULL;
if (! GetVolumeSettingRegPath(
Setting,
&Root,
&pPath,
&pValue
))
{
COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Invalid argument to PHGetSetting: %d", Setting));
return 0;
}
DWORD Data = 0;
HRESULT hr = RegistryGetDWORD(
Root,
pPath,
pValue,
&Data
);
if (SUCCEEDED(hr))
{
return Data;
}
//
// Setting is not present in the registry.
// Set the value to half of the max value.
//
if (Setting == phvsSpeakerVolume ||
Setting == phvsRingerVolume ||
Setting == phvsHandsetVolume ||
Setting == phvsMicrophoneVolume
)
{
hr = PHSetVolume (
Setting,
c_MaxUIVolume/2
);
if (SUCCEEDED(hr))
{
return c_MaxUIVolume/2;
}
}
return 0;
}
EXTERN_C
HRESULT
PHSetVolume(
PHVolumeSetting Setting,
DWORD Value
)
{
HKEY Root;
const WCHAR* pPath = NULL;
const WCHAR* pValue = NULL;
if (!GetVolumeSettingRegPath(
Setting,
&Root,
&pPath,
&pValue
))
{
COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Invalid argument to PHEnableSetting: %d", Setting));
return E_INVALIDARG;
}
HRESULT hr = RegistrySetDWORD(
Root,
pPath,
pValue,
Value
);
if (FAILED(hr))
{
COMMON_RETAILMSG(ZONE_COMMON_ERROR, (L"Failed writing the registry value 0x%x", hr));
return hr;
}
//we need to apply volumes to live after we save them to registry if that is applicable
if (Setting == phvsSpeakerVolume ||
Setting == phvsRingerVolume ||
Setting == phvsMicrophoneVolume)
{
return SetLiveVolumeIfApplicable(
Setting,
Value
);
}
return S_OK;
}
/*------------------------------------------------------------------------------
PHSetActiveVolume
Set Active volume mode, if the new mode is different than the old one, we apply the new volume
to waveOut API
------------------------------------------------------------------------------*/
EXTERN_C
HRESULT
PHSetActiveVolume(
PHActiveVolumeMode Mode
)
{
//if the mode is the same as old one, bail out
if (PHGetActiveVolume() == Mode)
{
return S_FALSE;
}
//get the volume value for new volume mode
DWORD Volume;
if (Mode == RingerVolumeMode)
{
Volume = PHGetVolumeSetting(phvsRingerVolume);
}
else if (Mode == SpeakerVolumeMode)
{
Volume = PHGetVolumeSetting(phvsSpeakerVolume);
}
else
{
return E_INVALIDARG;
}
//set the registry first
HRESULT hr = RegistrySetDWORD(
SN_VOIP_ACTIVEVOLUMEMODE_ROOT,
SN_VOIP_ACTIVEVOLUMEMODE_PATH,
SN_VOIP_ACTIVEVOLUMEMODE_VALUE,
Mode
);
if (FAILED(hr))
{
//bail out if we failed
return hr;
}
//apply the new volume to live
return SetWaveOutVolume(Volume);
}
/*------------------------------------------------------------------------------
PHGetActiveVolume
Get the active volume mode
------------------------------------------------------------------------------*/
EXTERN_C
PHActiveVolumeMode
PHGetActiveVolume (
void
)
{
DWORD Data;
HRESULT hr = RegistryGetDWORD(
SN_VOIP_ACTIVEVOLUMEMODE_ROOT,
SN_VOIP_ACTIVEVOLUMEMODE_PATH,
SN_VOIP_ACTIVEVOLUMEMODE_VALUE,
&Data
);
if (FAILED(hr) || Data >= LastVolumeMode)
{
Data = 0;
}
return static_cast<PHActiveVolumeMode>(Data);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -