bool configureAudioDevice(int bitDepth, int sampleRate, const QString& channel, QString& errorString)
{
HRESULT hr;
IMMDeviceEnumerator* deviceEnumerator = nullptr;
IMMDevice* defaultDevice = nullptr;
IAudioClient* audioClient = nullptr;
WAVEFORMATEX* closestMatch;
WAVEFORMATEX* nativeFormat;
// 初始化COM库
hr = CoInitialize(nullptr);
if (S_OK != hr) {
errorString = "COM library initialization failed.";
}
else
{
// 创建设备枚举器
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, IID_PPV_ARGS(&deviceEnumerator));
if (S_OK != hr) {
errorString = "Failed to create device enumerator instance.";
}
else
{
// 获取默认音频设备
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
if (S_OK != hr) {
errorString = "Failed to get default audio endpoint.";
}
else
{
// 激活音频客户端
hr = defaultDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&audioClient));
if (S_OK != hr) {
errorString = "Failed to activate audio client.";
}
else
{
// 获取当前音频格式
hr = audioClient->GetMixFormat(&nativeFormat);
if (S_OK != hr) {
errorString = "Failed to get mix format.";
}
else
{
nativeFormat->wFormatTag = static_cast<WORD>(65534);
nativeFormat->nChannels = static_cast<WORD>("mono" == channel ? 1 : 2);//单声道mono 双声道stereo
nativeFormat->wBitsPerSample = static_cast<WORD>(bitDepth*2);
nativeFormat->nSamplesPerSec = static_cast<DWORD>(sampleRate);
nativeFormat->nBlockAlign = static_cast<WORD>((nativeFormat->wBitsPerSample * nativeFormat->nChannels) / 8);
nativeFormat->nAvgBytesPerSec = static_cast<DWORD>(nativeFormat->nBlockAlign * nativeFormat->nSamplesPerSec);
// 独占模式
//WAVEFORMATEX *newFormat = nativeFormat;
//newFormat.wFormatTag = WAVE_FORMAT_PCM;
//newFormat.nChannels = static_cast<WORD>("mono" == channel ? 1 : 2);//单声道mono 双声道stereo
//newFormat.wBitsPerSample = static_cast<WORD>(bitDepth);
//newFormat.nSamplesPerSec = static_cast<DWORD>(sampleRate);
//newFormat.nBlockAlign = (newFormat.wBitsPerSample * newFormat.nChannels) / 8;
//newFormat.nAvgBytesPerSec = newFormat.nBlockAlign * newFormat.nSamplesPerSec;
hr = audioClient->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, nativeFormat, &closestMatch);
//独占模式
//hr = audioClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &newFormat, NULL);
if (S_OK != hr) {
errorString = "Requested format is not supported.";
}
else
{
hr = audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, nativeFormat, NULL);
//独占模式
//hr = audioClient->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE, 2, 100000, 100000, &newFormat, NULL);
if (S_OK != hr) {
errorString = "Failed to initialize audio client with the specified format";
}
else {
errorString = "Audio client initialized with the specified format";
}
}
}
}
}
}
}
// 清理COM对象
if (closestMatch) {
CoTaskMemFree(closestMatch);
}
if (nativeFormat) {
CoTaskMemFree(nativeFormat);
}
if (audioClient) {
audioClient->Release();
}
if (defaultDevice) {
defaultDevice->Release();
}
if (deviceEnumerator) {
deviceEnumerator->Release();
}
CoUninitialize();
return SUCCEEDED(hr);
}
【Windows API】设置声卡采样率、位深、通道数
最新推荐文章于 2024-10-04 10:45:54 发布