Commit cb596d54 authored by David Reid's avatar David Reid

WASAPI: Simplify device initialization.

parent c0105c71
...@@ -6498,9 +6498,9 @@ mal_result mal_device_init_internal__wasapi(mal_context* pContext, mal_device_ty ...@@ -6498,9 +6498,9 @@ mal_result mal_device_init_internal__wasapi(mal_context* pContext, mal_device_ty
mal_result result = MAL_SUCCESS; mal_result result = MAL_SUCCESS;
const char* errorMsg = ""; const char* errorMsg = "";
MAL_AUDCLNT_SHAREMODE shareMode = MAL_AUDCLNT_SHAREMODE_SHARED; MAL_AUDCLNT_SHAREMODE shareMode = MAL_AUDCLNT_SHAREMODE_SHARED;
WAVEFORMATEXTENSIBLE* pBestFormatTemp = NULL;
MAL_REFERENCE_TIME bufferDurationInMicroseconds; MAL_REFERENCE_TIME bufferDurationInMicroseconds;
mal_bool32 wasInitializedUsingIAudioClient3 = MAL_FALSE; mal_bool32 wasInitializedUsingIAudioClient3 = MAL_FALSE;
WAVEFORMATEXTENSIBLE wf;
#ifdef MAL_WIN32_DESKTOP #ifdef MAL_WIN32_DESKTOP
mal_IMMDevice* pMMDevice = NULL; mal_IMMDevice* pMMDevice = NULL;
...@@ -6539,25 +6539,6 @@ mal_result mal_device_init_internal__wasapi(mal_context* pContext, mal_device_ty ...@@ -6539,25 +6539,6 @@ mal_result mal_device_init_internal__wasapi(mal_context* pContext, mal_device_ty
} }
WAVEFORMATEXTENSIBLE wf;
mal_zero_object(&wf);
wf.Format.cbSize = sizeof(wf);
wf.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wf.Format.nChannels = (WORD)pData->channelsIn;
wf.Format.nSamplesPerSec = (DWORD)pData->sampleRateIn;
wf.Format.wBitsPerSample = (WORD)mal_get_bytes_per_sample(pData->formatIn)*8;
wf.Format.nBlockAlign = (wf.Format.nChannels * wf.Format.wBitsPerSample) / 8;
wf.Format.nAvgBytesPerSec = wf.Format.nBlockAlign * wf.Format.nSamplesPerSec;
wf.Samples.wValidBitsPerSample = /*(pDevice->format == mal_format_s24_32) ? 24 :*/ wf.Format.wBitsPerSample;
wf.dwChannelMask = mal_channel_map_to_channel_mask__win32(pData->channelMapIn, pData->channelsIn);
if (pData->formatIn == mal_format_f32) {
wf.SubFormat = MAL_GUID_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
} else {
wf.SubFormat = MAL_GUID_KSDATAFORMAT_SUBTYPE_PCM;
}
// Here is where we try to determine the best format to use with the device. If the client if wanting exclusive mode, first try finding the best format for that. If this fails, fall back to shared mode. // Here is where we try to determine the best format to use with the device. If the client if wanting exclusive mode, first try finding the best format for that. If this fails, fall back to shared mode.
result = MAL_FORMAT_NOT_SUPPORTED; result = MAL_FORMAT_NOT_SUPPORTED;
if (pData->shareMode == mal_share_mode_exclusive) { if (pData->shareMode == mal_share_mode_exclusive) {
...@@ -6594,42 +6575,18 @@ mal_result mal_device_init_internal__wasapi(mal_context* pContext, mal_device_ty ...@@ -6594,42 +6575,18 @@ mal_result mal_device_init_internal__wasapi(mal_context* pContext, mal_device_ty
// Fall back to shared mode if necessary. // Fall back to shared mode if necessary.
if (result != MAL_SUCCESS) { if (result != MAL_SUCCESS) {
// In shared mode we are always using the format reported by the operating system.
WAVEFORMATEXTENSIBLE* pNativeFormat = NULL; WAVEFORMATEXTENSIBLE* pNativeFormat = NULL;
hr = mal_IAudioClient_GetMixFormat((mal_IAudioClient*)pData->pAudioClient, (WAVEFORMATEX**)&pNativeFormat); hr = mal_IAudioClient_GetMixFormat((mal_IAudioClient*)pData->pAudioClient, (WAVEFORMATEX**)&pNativeFormat);
if (hr == S_OK) { if (hr != S_OK) {
if (pData->usingDefaultFormat) { result = MAL_FORMAT_NOT_SUPPORTED;
wf.Format.wBitsPerSample = pNativeFormat->Format.wBitsPerSample;
wf.Format.nBlockAlign = pNativeFormat->Format.nBlockAlign;
wf.Format.nAvgBytesPerSec = pNativeFormat->Format.nAvgBytesPerSec;
wf.Samples.wValidBitsPerSample = pNativeFormat->Samples.wValidBitsPerSample;
wf.SubFormat = pNativeFormat->SubFormat;
}
if (pData->usingDefaultChannels) {
wf.Format.nChannels = pNativeFormat->Format.nChannels;
}
if (pData->usingDefaultSampleRate) {
wf.Format.nSamplesPerSec = pNativeFormat->Format.nSamplesPerSec;
}
if (pData->usingDefaultChannelMap) {
wf.dwChannelMask = pNativeFormat->dwChannelMask;
}
mal_CoTaskMemFree(pContext, pNativeFormat);
pNativeFormat = NULL;
}
hr = mal_IAudioClient_IsFormatSupported((mal_IAudioClient*)pData->pAudioClient, MAL_AUDCLNT_SHAREMODE_SHARED, (WAVEFORMATEX*)&wf, (WAVEFORMATEX**)&pBestFormatTemp);
if (hr != S_OK && hr != S_FALSE) {
hr = mal_IAudioClient_GetMixFormat((mal_IAudioClient*)pData->pAudioClient, (WAVEFORMATEX**)&pBestFormatTemp);
if (hr != S_OK) {
result = MAL_FORMAT_NOT_SUPPORTED;
} else {
result = MAL_SUCCESS;
}
} else { } else {
mal_copy_memory(&wf, pNativeFormat, sizeof(wf));
result = MAL_SUCCESS; result = MAL_SUCCESS;
} }
mal_CoTaskMemFree(pContext, pNativeFormat);
shareMode = MAL_AUDCLNT_SHAREMODE_SHARED; shareMode = MAL_AUDCLNT_SHAREMODE_SHARED;
} }
...@@ -6639,12 +6596,6 @@ mal_result mal_device_init_internal__wasapi(mal_context* pContext, mal_device_ty ...@@ -6639,12 +6596,6 @@ mal_result mal_device_init_internal__wasapi(mal_context* pContext, mal_device_ty
goto done; goto done;
} }
if (pBestFormatTemp != NULL) {
mal_copy_memory(&wf, pBestFormatTemp, sizeof(wf));
mal_CoTaskMemFree(pContext, pBestFormatTemp);
}
pData->formatOut = mal_format_from_WAVEFORMATEX((WAVEFORMATEX*)&wf); pData->formatOut = mal_format_from_WAVEFORMATEX((WAVEFORMATEX*)&wf);
pData->channelsOut = wf.Format.nChannels; pData->channelsOut = wf.Format.nChannels;
pData->sampleRateOut = wf.Format.nSamplesPerSec; pData->sampleRateOut = wf.Format.nSamplesPerSec;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment