none
dynamic changing of audio sample rate and bit depth RRS feed

  • Question

  • I have tried of dynamic changing of audio sample rate and bit depth,it works fine for me for 16-bit 44K,48K but when i switch to 96K or 192K ,its giving failed to play audio tone. similarly for 24-bit case,all sample rates got failed to work. what need to be done to accomplish all the sample rate and bit depth to work fine.

    i have asked to upload here by a person from windows audio pro application forum,so that i can get a better solution.

    Below code is an application developed in visual studio,let me know the problem associated with this.

    WAVEFORMATEXTENSIBLE *deviceFormatProperties;
    
    	int main(int argc, _TCHAR* argv[]) 
    {
    
    HRESULT hr_READ,hr_WRITE;
    IMMDevice * pDevice_READ=NULL,*pDevice_WRITE = NULL;
    IMMDeviceEnumerator * pEnumerator_READ = NULL, *pEnumerator_WRITE = NULL;
    IPropertyStore* store_READ = nullptr, *store_WRITE = nullptr;
    IAudioClient   *audioClient = NULL;
    PROPVARIANT prop_READ,prop_WRITE;
    CoInitialize(NULL);
    // get the device enumerator
    hr_READ = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (LPVOID *)&pEnumerator_READ);
    // get default audio endpoint
    hr_READ = pEnumerator_READ->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice_READ);
    
    hr_READ = pDevice_READ->OpenPropertyStore(STGM_READ, &store_READ);
    if (FAILED(hr_READ)) 
    {
    std::cout << "OpenPropertyStore failed!" << std::endl;
    }
    
    hr_READ = store_READ->GetValue(PKEY_AudioEngine_DeviceFormat, &prop_READ);
    if (FAILED(hr_READ)) 
    {
    std::cout << "GetValue failed!" << std::endl;
    }
    
    deviceFormatProperties =(WAVEFORMATEXTENSIBLE *)prop_READ.blob.pBlobData;
    std::cout << "Channels    = " << deviceFormatProperties->Format.nChannels << std::endl;
    std::cout << "Sample rate = " << deviceFormatProperties->Format.nSamplesPerSec << std::endl;
    std::cout << "Bit depth   = " << deviceFormatProperties->Format.wBitsPerSample << std::endl;
    std::cout << "Format Tag   = " << deviceFormatProperties->Format.wFormatTag << std::endl;
    std::cout << "nBlockAlign   = " << deviceFormatProperties->Format.nBlockAlign << std::endl;
    std::cout << "Size   = " << deviceFormatProperties->Format.cbSize << std::endl;
    std::cout << "AvgBytesPerSec   = " << deviceFormatProperties->Format.nAvgBytesPerSec << std::endl;
    std::cout << "Samples.wReserved    = " << deviceFormatProperties->Samples.wReserved << std::endl;
    std::cout << "Samples.wSamplesPerBlock = " << deviceFormatProperties->Samples.wSamplesPerBlock << std::endl;
    std::cout << "Samples.wValidBitsPerSample   = " << deviceFormatProperties->Samples.wValidBitsPerSample << std::endl;
    std::cout << "dwChannelMask   = " << deviceFormatProperties->dwChannelMask << std::endl;
    deviceFormatProperties->Format.nChannels = 2;
    deviceFormatProperties->Format.nSamplesPerSec = 96000;
    deviceFormatProperties->Format.wBitsPerSample = 16;
    deviceFormatProperties->Samples.wValidBitsPerSample = deviceFormatProperties->Format.wBitsPerSample;
    deviceFormatProperties->Format.nBlockAlign = (deviceFormatProperties->Format.nChannels*deviceFormatProperties->Format.wBitsPerSample) / 8;
    deviceFormatProperties->Format.nAvgBytesPerSec = deviceFormatProperties->Format.nSamplesPerSec*deviceFormatProperties->Format.nBlockAlign;
    //deviceFormatProperties->Samples.wSamplesPerBlock = deviceFormatProperties->Samples.wValidBitsPerSample = 24;
    //deviceFormatProperties->Samples.wReserved = 24;
    deviceFormatProperties->dwChannelMask = 0x00000003;
    deviceFormatProperties->Format.cbSize = 22;
    hr_READ = pDevice_READ->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&audioClient);
    if (FAILED(hr_READ))
    {
    std::cout << "pDevice->Activate failed!" << std::endl;
    }
    		
    hr_READ = audioClient->IsFormatSupported(/*AUDCLNT_SHAREMODE_SHARED*/AUDCLNT_SHAREMODE_EXCLUSIVE, (PWAVEFORMATEX)&deviceFormatProperties->Format, NULL);
    if (FAILED(hr_READ)) 
    {
    std::cout << "IsFormatSupported failed" << std::endl;
    }
    hr_WRITE = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (LPVOID *)&pEnumerator_WRITE);
    
    // get default audio endpoint
    hr_WRITE = pEnumerator_WRITE->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice_WRITE);
    
    hr_WRITE= pDevice_WRITE->OpenPropertyStore(STGM_WRITE, &store_WRITE);
    if (FAILED(hr_WRITE)) 
    {
    std::cout << "OpenPropertyStore failed!" << std::endl;
    }
    hr_WRITE = store_WRITE->SetValue(PKEY_AudioEngine_DeviceFormat, prop_READ);
    if (FAILED(hr_WRITE)) 
    {
    std::cout << "SetValue failed!" << std::endl;
    }
    std::cout << "\nCHANGED VALUES\n";
    std::cout << "Channels    = " << deviceFormatProperties->Format.nChannels << std::endl;
    std::cout << "Sample rate = " << deviceFormatProperties->Format.nSamplesPerSec << std::endl;
    std::cout << "Bit depth   = " << deviceFormatProperties->Format.wBitsPerSample << std::endl;
    std::cout << "Format Tag   = " << deviceFormatProperties->Format.wFormatTag << std::endl;
    std::cout << "nBlockAlign   = " << deviceFormatProperties->Format.nBlockAlign << std::endl;
    std::cout << "Size   = " << deviceFormatProperties->Format.cbSize << std::endl;
    std::cout << "AvgBytesPerSec   = " << deviceFormatProperties->Format.nAvgBytesPerSec << std::endl;
    std::cout << "Samples.wReserved    = " << deviceFormatProperties->Samples.wReserved << std::endl;
    std::cout << "Samples.wSamplesPerBlock = " << deviceFormatProperties->Samples.wSamplesPerBlock << std::endl;
    std::cout << "Samples.wValidBitsPerSample   = " << deviceFormatProperties->Samples.wValidBitsPerSample << std::endl;
    std::cout << "dwChannelMask   = " << deviceFormatProperties->dwChannelMask << std::endl;
    hr_WRITE = store_WRITE->Commit();
    pDevice_WRITE->Release();
    pEnumerator_WRITE->Release();
    store_WRITE->Release();
    PropVariantClear(&prop_WRITE);
    pDevice_READ->Release();
    pEnumerator_READ->Release();
    store_READ->Release();
    pDevice_READ = nullptr;
    PropVariantClear(&prop_READ);
    CoUninitialize();
    system("pause");
    return 0;
    }

    Wednesday, September 9, 2015 1:14 PM