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);
}
Example #2
0
// Public method
//   Sets 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 SetCaptureListDeviceVolume(DeviceInfo *pDeviceInfo, int index, float fVolume)
{
	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;

    // EventContext is used to identify the action owner when client receive volume change notification.
	// See the details in MSSDK/Samples/MultiMedia/Audio/WinAudio/player.cpp
    hr = pAudioVolume->SetMasterVolumeLevelScalar(fVolume, &g_EventContext); 

errors:
    SAFE_RELEASE(pAudioVolume);
    SAFE_RELEASE(pDevice);
	return hr;
}
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;
}
Example #4
0
bool SetMute(bool mute) {
  HRESULT hr;
  CoInitialize(NULL);
  IAudioEndpointVolume *endpointVolume = GetEndpointVolume();
  hr = endpointVolume->SetMute((bool)(mute), NULL);
  endpointVolume->Release();
  CoUninitialize();
  return 0;
}
Example #5
0
bool SetMasterVolume(float level) {
  HRESULT hr;
  CoInitialize(NULL);
  IAudioEndpointVolume *endpointVolume = GetEndpointVolume();
  hr = endpointVolume->SetMasterVolumeLevelScalar((float)(level), NULL);
  endpointVolume->Release();
  CoUninitialize();
  return 0;
}
Example #6
0
bool GetMute() {
  HRESULT hr;
  CoInitialize(NULL);
  IAudioEndpointVolume *endpointVolume = GetEndpointVolume();
  BOOL currentMute = false;
  hr = endpointVolume->GetMute(&currentMute);
  endpointVolume->Release();
  CoUninitialize();
  return (bool)currentMute;
}
Example #7
0
float GetMasterVolume() {
  HRESULT hr;
  CoInitialize(NULL);
  IAudioEndpointVolume *endpointVolume = GetEndpointVolume();
  float currentVolume = 0;
  hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
  endpointVolume->Release();
  CoUninitialize();
  return currentVolume;
}
Example #8
0
void SetMuteStatus()
{
    HRESULT hr;

    CoInitialize(NULL);
    IMMDeviceEnumerator *deviceEnumerator = NULL;
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
    if (!SUCCEEDED(hr)) 
    {
        CoUninitialize();
    }

    IMMDevice *defaultDevice = NULL;

    hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
    deviceEnumerator->Release();
    deviceEnumerator = NULL;
    if (!SUCCEEDED(hr))
    {
        CoUninitialize();
    }

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

    if (!SUCCEEDED(hr)) 
    {
        CoUninitialize();
    }

    // -------------------------
	BOOL b;

	endpointVolume->GetMute(&b);
	b=!b;
	endpointVolume->SetMute(b, NULL);
	endpointVolume->Release();
    CoUninitialize();
}
Example #9
0
// This method will set the specified channel volume.
// float fVolume: the range of the value from 0.0 to 1.0
AUDIO_DEVICE_API HRESULT SetCaptureListDeviceChannelVolume(DeviceInfo *pDeviceInfo, int index, float fVolume, UINT iChannel)
{
	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->SetChannelVolumeLevelScalar(iChannel, fVolume, &g_EventContext);

errors:
    SAFE_RELEASE(pAudioVolume);
    SAFE_RELEASE(pDevice);
	return hr;
}
Example #10
0
// This method will get the number of channels.
AUDIO_DEVICE_API HRESULT GetCaptureListDeviceChannelCount(DeviceInfo *pDeviceInfo, int index, UINT *iCount)
{
	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->GetChannelCount(iCount);

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;
}
Example #12
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;
}
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();
}
/**********************************************************************************

Function Name = LeapDesktopAppFull::volumeManipulation

Descriptive Name = Increase/decrease/mute/unmute the volume

Function =

    This functions is responsible for performing the actions of increasing/
    decreasing/muting/unmuting the volume. Makes use of the available functions on 
    windows to construct and initialize the interface of audio devices for communication.

Dependencies =

    None

Restrictions =

    Must have audio devices connected, running and capable of being accessed through the 
    IMMDeviceEnumerator interface.

Input =
    
    string controlOption - The volume action to be performed, supports the following options:
                                VOLUME_STEP_UP      - This option will increase the volume by 1
                                VOLUME_STEP_DOWN    - This option will decrease the volume by 1
                                VOLUME_MUTE_UNMUTE  - This option will mute or unmute based on 
                                                      what the state of the volume currently is.

Output =
   
   Currently there are no outputs from this functions as it only performs the action. 
   Future TODO is to make sure errors are handled and appropriate response is returned.

Normal Return =
    
    N/A

Error Return =

    N/A

******************************************************************************/
void LeapDesktopAppFull::volumeManipulation ( string controlOption )
{
    // Variable Declaration
    HRESULT              volumeManipulationResults = NULL ;  // Stores detailed information for the volume communication interface
    IMMDeviceEnumerator  *deviceEnumerator         = NULL ;  // Stores the interface pointer of enumerating audio devices
    IMMDevice            *defaultDevice            = NULL ;  // Stores all the default audio devices resources
    IAudioEndpointVolume *endpointVolume           = NULL ;  // Stores the volume controls on the audio streams 
    BOOL                 pbMute                    = FALSE ; //

    // Initialize the COM library on the current thread
    CoInitialize ( NULL ) ;

    // Create an instance of a single uninitialized object of audio devices, a success instance creation will return "S_OK"
    volumeManipulationResults = CoCreateInstance ( __uuidof( MMDeviceEnumerator ),    // The CLSID associated with the data and code that is used to create the object
                                                   NULL,                              // Set to NULL to identify that object is not being created as an aggregate
                                                   CLSCTX_INPROC_SERVER,              // Identifies how the new object will run, in this case it is a DLL that runs in the same process
                                                   __uuidof( IMMDeviceEnumerator ),   // The identifier of the interface that will be used to communicate with the object
                                                   ( LPVOID * ) &deviceEnumerator     // Pointer that will store the interface pointer that was requested
                                                 ) ;

    // Using the device enumerator interface get the default audio endpoint for the provided data-flow direction
    volumeManipulationResults = deviceEnumerator->GetDefaultAudioEndpoint ( eRender,          // Set to eRender to represent an audio rendering stream
                                                                            eConsole,         // Set to eConsole to represent the role that is assigned to the endpoint device
                                                                            &defaultDevice    // Pointer to which the device address is stored of the endpoint object
                                                                          ) ;

    // No longer need the pointer for the device enumerator interface so release it to free up memory
    deviceEnumerator->Release () ;
    deviceEnumerator = NULL ;  // Force un-initialization

    // Activate the device that was retrieved above, a success activation will return "S_OK"
    volumeManipulationResults = defaultDevice->Activate ( __uuidof( IAudioEndpointVolume ), // The interface identifier, in this case this is an audio endpoint device
                                                          CLSCTX_INPROC_SERVER,             // Identifies how the new object will run, in this case it is a DLL that runs in the same process
                                                          NULL,                             // Set to NULL to activate an IAudioEndpointVolume interface on an audio endpoint device
                                                          ( LPVOID * ) &endpointVolume      // Pointer which stores the address of the IAudioEndpointVolume interface
                                                        ) ;

    // No longer need the pointer for the device interface so release it to free up memory
    defaultDevice->Release () ;
    defaultDevice = NULL ; // Force un-initialization

    // From the endpointVolume interface get the mute status
    // GetMute returns TRUE for muted and FALSE for the stream not muted
    endpointVolume->GetMute ( &pbMute ) ;

    // Based on the mute status perform the action of muting or unmuting the stream
    if ( ( pbMute && controlOption == VOLUME_MUTE_UNMUTE ) )
    {
        // Found that the stream is already currently muted so un-mute the stream
        volumeManipulationResults = endpointVolume->SetMute ( 0, NULL ) ;

        // Display user feed back image based on the action performed
        createUserFeedBackWindow ( loadResource ( RES_KEYTAP_IMAGE ), 150, 131 ) ;
    }
    else if ( !pbMute && controlOption == VOLUME_MUTE_UNMUTE )
    {
        // Found that the stream is not mute so mute the stream
        volumeManipulationResults = endpointVolume->SetMute ( 1, NULL ) ;

        // Display user feed back image based on the action performed
        createUserFeedBackWindow ( loadResource ( RES_KEYTAPRELEASE_IMAGE ), 150, 131 ) ;
    }

    // Based on the volume increase/decrease option, perform the appropriate action
    if ( controlOption == VOLUME_STEP_UP )
    {
        // Increase the volume on the stream by one, this also will update any connected system notifications
        volumeManipulationResults = endpointVolume->VolumeStepUp ( NULL );
        volumeManipulationResults = endpointVolume->VolumeStepUp ( NULL );
        volumeManipulationResults = endpointVolume->VolumeStepUp ( NULL );
        volumeManipulationResults = endpointVolume->VolumeStepUp ( NULL );
    }
    else if ( controlOption == VOLUME_STEP_DOWN )
    {
        // Decrease the volume on the stream by one, this also will update any connected system notifications
        volumeManipulationResults = endpointVolume->VolumeStepDown ( NULL );
        volumeManipulationResults = endpointVolume->VolumeStepDown ( NULL );
        volumeManipulationResults = endpointVolume->VolumeStepDown ( NULL );
        volumeManipulationResults = endpointVolume->VolumeStepDown ( NULL );
    }

    // No longer need the endpointVolume interface so release it to free up memory
    endpointVolume->Release ();

    // All the actions that are to be performed on the end point device are done so un-initialize the instance
    CoUninitialize ();
}