void printVolume()
{
	HRESULT hr;
	bool decibels = false;
	bool scalar = false;
	double newVolume = 10;
	// -------------------------
	CoInitialize(NULL);
	IMMDeviceEnumerator *deviceEnumerator = NULL;
	hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
	IMMDevice *defaultDevice = NULL;

	hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
	deviceEnumerator->Release();
	deviceEnumerator = NULL;

	IAudioEndpointVolume *endpointVolume = NULL;
	hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
	defaultDevice->Release();
	defaultDevice = NULL;

	// -------------------------
	float currentVolume = 0;
	endpointVolume->GetMasterVolumeLevel(&currentVolume);
	printf("Current volume in dB is: %f\n", currentVolume);

	hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
	printf("Current volume as a scalar is: %f\n", currentVolume);
}
bool GetWin7AudioState(const VolumeAction action)
{
	IMMDevice * pEndpoint = 0;
	IAudioEndpointVolume * pEndptVol = 0;
	bool success = false;

	if (InitCom())
	{
		if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK)
		{
			if (pEndpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, 0, (void**)&pEndptVol) == S_OK)
			{
				if (pEndptVol->GetMute(&is_mute) == S_OK && action == TOGGLE_MUTE)
				{
					success = pEndptVol->SetMute(is_mute == TRUE ? FALSE : TRUE, 0) == S_OK;
				}
				// get current volume
				float vol = 0.0f;
				if (action != TOGGLE_MUTE && pEndptVol->GetMasterVolumeLevelScalar(&vol) == S_OK)
				{
					master_volume = vol;
				}
			}
		}
	}

	SAFE_RELEASE(pEndptVol)
	SAFE_RELEASE(pEndpoint)
	UnInitCom();
	return success;
}
float GetMasterVolume() {
  HRESULT hr;
  CoInitialize(NULL);
  IAudioEndpointVolume *endpointVolume = GetEndpointVolume();
  float currentVolume = 0;
  hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
  endpointVolume->Release();
  CoUninitialize();
  return currentVolume;
}
Exemple #4
0
bool ChangeVolume(double nVolume,bool bScalar)
{
 
    HRESULT hr=NULL;
    bool decibels = false;
    bool scalar = false;
    double newVolume=nVolume;
 
    CoInitialize(NULL);

		IMMDeviceEnumerator *deviceEnumerator = NULL;
		hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, 
		                      __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
		IMMDevice *defaultDevice = NULL;
 
		hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
		deviceEnumerator->Release();
		deviceEnumerator = NULL;
 
		IAudioEndpointVolume *endpointVolume = NULL;
		hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), 
		     CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
		defaultDevice->Release();
		defaultDevice = NULL;
 
		// -------------------------
		float currentVolume = 0;
		endpointVolume->GetMasterVolumeLevel(&currentVolume);
		//printf("Current volume in dB is: %f\n", currentVolume);

		hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
		//CString strCur=L"";
		//strCur.Format(L"%f",currentVolume);
		//AfxMessageBox(strCur);

		// printf("Current volume as a scalar is: %f\n", currentVolume);
		if (bScalar==false)
		{
		    hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
		}
		else if (bScalar==true)
		{
		    hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
		}
		endpointVolume->Release();
 
    CoUninitialize();
 
    return FALSE;
}
Exemple #5
0
// Public method
//   Gets the volume level of the default audio session
//   of the currently selected endpoint rendering device.
// float fVolume: the range of the value from 0.0 to 1.0
AUDIO_DEVICE_API HRESULT GetCaptureListDeviceVolume(DeviceInfo *pDeviceInfo, int index, float *pfVolume)
{
	InternalDeviceInfo *pInternalDeviceInfo = (InternalDeviceInfo *) pDeviceInfo;

    HRESULT hr;
	IMMDevice *pDevice = NULL;
	IAudioEndpointVolume *pAudioVolume = NULL;

    if (pInternalDeviceInfo->pCaptureCollection != NULL) {
        hr = pInternalDeviceInfo->pCaptureCollection->Item(index, &pDevice);
    }
	if (hr != S_OK) goto errors;

    hr = pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pAudioVolume);
	if (hr != S_OK) goto errors;

    hr = pAudioVolume->GetMasterVolumeLevelScalar(pfVolume);

errors:
    SAFE_RELEASE(pAudioVolume);
    SAFE_RELEASE(pDevice);
	return hr;
}
bool SetWin7Volume(UINT volume, int offset = 0)
{
	IMMDevice * pEndpoint = 0;
	IAudioEndpointVolume * pEndptVol = 0;
	bool success = false;

	if (InitCom())
	{
		if (pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint) == S_OK)
		{
			if (pEndpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, 0, (void**)&pEndptVol) == S_OK)
			{
				pEndptVol->SetMute(FALSE, 0);
				float vol = 0.0f;
				if (offset != 0) // change master volume + offset
				{
					float off = static_cast<float>(offset) / 100.0f;
					vol = master_volume + off;
					vol = (vol < 0.0f) ? 0.0f : ((vol > 1.0f) ? 1.0f : vol);
				}
				else
				{
					vol = (float)volume / 100.0f;
				}
				// set to volume
				success = pEndptVol->SetMasterVolumeLevelScalar(vol, 0) == S_OK;
				if (success) success = pEndptVol->GetMasterVolumeLevelScalar(&vol) == S_OK;
				if (success) master_volume = vol;
			}
		}
	}

	SAFE_RELEASE(pEndptVol)
	SAFE_RELEASE(pEndpoint)
	UnInitCom();
	return success;
}
void modifyVolume(char ch)
{
	HRESULT hr;
	bool decibels = false;
	bool scalar = true;
	double newVolume = 10;
	// -------------------------
	CoInitialize(NULL);
	IMMDeviceEnumerator *deviceEnumerator = NULL;
	hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
	IMMDevice *defaultDevice = NULL;

	hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &defaultDevice);
	deviceEnumerator->Release();
	deviceEnumerator = NULL;

	IAudioEndpointVolume *endpointVolume = NULL;
	hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
	defaultDevice->Release();
	defaultDevice = NULL;

	// -------------------------
	float currentVolume = 0;
	endpointVolume->GetMasterVolumeLevel(&currentVolume);
	//printf("Current volume in dB is: %f\n", currentVolume);

	hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);

	float min, max, inc;
	endpointVolume->GetVolumeRange(&min, &max, &inc);
	//printf("Current volume as a scalar is: %f\n", currentVolume);

	if (ch == 'u')
		newVolume = currentVolume + 0.1;
	else if (ch == 'd')
		newVolume = currentVolume - 0.1;

	if (ch == 'm')
	{
		endpointVolume->SetMute(TRUE, NULL);
	}
	else if (ch == 'n')
	{
		endpointVolume->SetMute(FALSE, NULL);
	}
	else
	{
		if (decibels)
		{
			hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
			//endpointVolume->VolumeStepUp(NULL);
		}
		else if (scalar)
		{
			hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
			//endpointVolume->VolumeStepUp(NULL);
		}
	}


	endpointVolume->Release();

	CoUninitialize();
}