3

I need to change sample rate for microphone and speakers.

Advanced Speaker Properties

I know how to change volume levels for speakers and microphone using IMMDevice, but how can I set sample rate?

bool SoundDeviceControler::setVolume(EDataFlow dataFlow,float volume )
{
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    if (FAILED(hr))
        return false
    IMMDeviceEnumerator *deviceEnumerator = NULL;
    hr = CoCreateInstance( __uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&deviceEnumerator) );
    if (FAILED(hr))
        return false;
    IMMDeviceCollection *deviceCollection = NULL;
    hr = deviceEnumerator->EnumAudioEndpoints( dataFlow, DEVICE_STATE_ACTIVE | DEVICE_STATE_UNPLUGGED, &deviceCollection );
    if (FAILED(hr))
        return false;
    UINT deviceCount;
    hr = deviceCollection->GetCount(&deviceCount);
    if (FAILED(hr))
        return false;    
    IMMDevice *device = NULL;
    for (UINT i = 0 ; i < deviceCount ; i += 1)
    {
        device = NULL;
        hr = deviceCollection->Item(i, &device);
        if (FAILED(hr))
            return false;
        IAudioEndpointVolume *endpointVolume = NULL;
        hr = device->Activate( __uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<void **>(&endpointVolume) );
        if (FAILED(hr))
            retur false;
        hr=endpointVolume->SetMasterVolumeLevelScalar(volume,NULL);
        if (FAILED(hr))
            return false;   
        hr=endpointVolume->SetMute(false,NULL);
        if (FAILED(hr))
            return false;   
    }
    return true;
}   
Timur
  • 95
  • 2
  • 12
  • [This question](http://stackoverflow.com/questions/22616924/wasapi-choosing-a-wave-format-for-exclusive-output) shows how to get device format using `PKEY_AudioEngine_DeviceFormat`. You might want to try to change it and set your own. Also, have a look [there](https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/e2e9858f-9bf5-4400-8a4e-570fa3285aad/changing-audio-device-sample-rate-via-pkeyaudioenginedeviceformat-does-not-work?forum=windowssdk). – Roman R. Mar 21 '16 at 11:35
  • Why are you trying to change the **default** settings for **your** program? That is almost certainly a bug, and one that is very annoying to users. DO NOT ALTER SHARED SETTINGS ! – MSalters Mar 21 '16 at 13:36
  • @MSalters I use external sound card which plug in special for my software. And i will change settings only for external sound card – Timur Mar 23 '16 at 07:21

0 Answers0