mirror of
https://github.com/mackron/miniaudio.git
synced 2026-04-23 16:54:03 +02:00
DirectSound: Improve the way APIs are loaded.
APIs are now per-context rather then per-device. This is leftover from the no-context-API days.
This commit is contained in:
@@ -783,6 +783,10 @@ struct mal_context
|
|||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
/*HMODULE*/ mal_handle hDSoundDLL;
|
/*HMODULE*/ mal_handle hDSoundDLL;
|
||||||
|
mal_proc DirectSoundCreate;
|
||||||
|
mal_proc DirectSoundEnumerateA;
|
||||||
|
mal_proc DirectSoundCaptureCreate;
|
||||||
|
mal_proc DirectSoundCaptureEnumerateA;
|
||||||
} dsound;
|
} dsound;
|
||||||
#endif
|
#endif
|
||||||
#ifdef MAL_SUPPORT_WINMM
|
#ifdef MAL_SUPPORT_WINMM
|
||||||
@@ -1153,7 +1157,6 @@ struct mal_device
|
|||||||
#ifdef MAL_SUPPORT_DSOUND
|
#ifdef MAL_SUPPORT_DSOUND
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
/*HMODULE*/ mal_handle hDSoundDLL;
|
|
||||||
/*LPDIRECTSOUND*/ mal_ptr pPlayback;
|
/*LPDIRECTSOUND*/ mal_ptr pPlayback;
|
||||||
/*LPDIRECTSOUNDBUFFER*/ mal_ptr pPlaybackPrimaryBuffer;
|
/*LPDIRECTSOUNDBUFFER*/ mal_ptr pPlaybackPrimaryBuffer;
|
||||||
/*LPDIRECTSOUNDBUFFER*/ mal_ptr pPlaybackBuffer;
|
/*LPDIRECTSOUNDBUFFER*/ mal_ptr pPlaybackBuffer;
|
||||||
@@ -4767,22 +4770,21 @@ static void mal_get_channels_from_speaker_config__dsound(DWORD speakerConfig, WO
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static HMODULE mal_open_dsound_dll()
|
|
||||||
{
|
|
||||||
return LoadLibraryW(L"dsound.dll");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mal_close_dsound_dll(HMODULE hModule)
|
|
||||||
{
|
|
||||||
FreeLibrary(hModule);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mal_result mal_context_init__dsound(mal_context* pContext)
|
mal_result mal_context_init__dsound(mal_context* pContext)
|
||||||
{
|
{
|
||||||
mal_assert(pContext != NULL);
|
mal_assert(pContext != NULL);
|
||||||
|
|
||||||
(void)pContext;
|
pContext->dsound.hDSoundDLL = mal_dlopen("dsound.dll");
|
||||||
|
if (pContext->dsound.hDSoundDLL == NULL) {
|
||||||
|
return MAL_API_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
pContext->dsound.DirectSoundCreate = mal_dlsym(pContext->dsound.hDSoundDLL, "DirectSoundCreate");
|
||||||
|
pContext->dsound.DirectSoundEnumerateA = mal_dlsym(pContext->dsound.hDSoundDLL, "DirectSoundEnumerateA");
|
||||||
|
pContext->dsound.DirectSoundCaptureCreate = mal_dlsym(pContext->dsound.hDSoundDLL, "DirectSoundCaptureCreate");
|
||||||
|
pContext->dsound.DirectSoundCaptureEnumerateA = mal_dlsym(pContext->dsound.hDSoundDLL, "DirectSoundCaptureEnumerateA");
|
||||||
|
|
||||||
return MAL_SUCCESS;
|
return MAL_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4791,7 +4793,8 @@ mal_result mal_context_uninit__dsound(mal_context* pContext)
|
|||||||
mal_assert(pContext != NULL);
|
mal_assert(pContext != NULL);
|
||||||
mal_assert(pContext->backend == mal_backend_dsound);
|
mal_assert(pContext->backend == mal_backend_dsound);
|
||||||
|
|
||||||
(void)pContext;
|
mal_dlclose(pContext->dsound.hDSoundDLL);
|
||||||
|
|
||||||
return MAL_SUCCESS;
|
return MAL_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4844,25 +4847,11 @@ static mal_result mal_enumerate_devices__dsound(mal_context* pContext, mal_devic
|
|||||||
enumData.infoCount = infoSize;
|
enumData.infoCount = infoSize;
|
||||||
enumData.pInfo = pInfo;
|
enumData.pInfo = pInfo;
|
||||||
|
|
||||||
HMODULE dsoundDLL = mal_open_dsound_dll();
|
|
||||||
if (dsoundDLL == NULL) {
|
|
||||||
return MAL_NO_BACKEND;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == mal_device_type_playback) {
|
if (type == mal_device_type_playback) {
|
||||||
mal_DirectSoundEnumerateAProc pDirectSoundEnumerateA = (mal_DirectSoundEnumerateAProc)GetProcAddress(dsoundDLL, "DirectSoundEnumerateA");
|
((mal_DirectSoundEnumerateAProc)pContext->dsound.DirectSoundEnumerateA)(mal_enum_devices_callback__dsound, &enumData);
|
||||||
if (pDirectSoundEnumerateA) {
|
|
||||||
pDirectSoundEnumerateA(mal_enum_devices_callback__dsound, &enumData);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
mal_DirectSoundCaptureEnumerateAProc pDirectSoundCaptureEnumerateA = (mal_DirectSoundCaptureEnumerateAProc)GetProcAddress(dsoundDLL, "DirectSoundCaptureEnumerateA");
|
((mal_DirectSoundCaptureEnumerateAProc)pContext->dsound.DirectSoundCaptureEnumerateA)(mal_enum_devices_callback__dsound, &enumData);
|
||||||
if (pDirectSoundCaptureEnumerateA) {
|
|
||||||
pDirectSoundCaptureEnumerateA(mal_enum_devices_callback__dsound, &enumData);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mal_close_dsound_dll(dsoundDLL);
|
|
||||||
|
|
||||||
*pCount = enumData.deviceCount;
|
*pCount = enumData.deviceCount;
|
||||||
return MAL_SUCCESS;
|
return MAL_SUCCESS;
|
||||||
@@ -4872,7 +4861,6 @@ static void mal_device_uninit__dsound(mal_device* pDevice)
|
|||||||
{
|
{
|
||||||
mal_assert(pDevice != NULL);
|
mal_assert(pDevice != NULL);
|
||||||
|
|
||||||
if (pDevice->dsound.hDSoundDLL != NULL) {
|
|
||||||
if (pDevice->dsound.pNotify) {
|
if (pDevice->dsound.pNotify) {
|
||||||
IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)pDevice->dsound.pNotify);
|
IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)pDevice->dsound.pNotify);
|
||||||
}
|
}
|
||||||
@@ -4902,9 +4890,6 @@ static void mal_device_uninit__dsound(mal_device* pDevice)
|
|||||||
if (pDevice->dsound.pPlayback != NULL) {
|
if (pDevice->dsound.pPlayback != NULL) {
|
||||||
IDirectSound_Release((LPDIRECTSOUND)pDevice->dsound.pPlayback);
|
IDirectSound_Release((LPDIRECTSOUND)pDevice->dsound.pPlayback);
|
||||||
}
|
}
|
||||||
|
|
||||||
mal_close_dsound_dll((HMODULE)pDevice->dsound.hDSoundDLL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static mal_result mal_device_init__dsound(mal_context* pContext, mal_device_type type, mal_device_id* pDeviceID, const mal_device_config* pConfig, mal_device* pDevice)
|
static mal_result mal_device_init__dsound(mal_context* pContext, mal_device_type type, mal_device_id* pDeviceID, const mal_device_config* pConfig, mal_device* pDevice)
|
||||||
@@ -4922,11 +4907,6 @@ static mal_result mal_device_init__dsound(mal_context* pContext, mal_device_type
|
|||||||
mal_assert(pDevice != NULL);
|
mal_assert(pDevice != NULL);
|
||||||
mal_zero_object(&pDevice->dsound);
|
mal_zero_object(&pDevice->dsound);
|
||||||
|
|
||||||
pDevice->dsound.hDSoundDLL = (mal_handle)mal_open_dsound_dll();
|
|
||||||
if (pDevice->dsound.hDSoundDLL == NULL) {
|
|
||||||
return MAL_NO_BACKEND;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that we have a valid format.
|
// Check that we have a valid format.
|
||||||
GUID subformat;
|
GUID subformat;
|
||||||
switch (pConfig->format)
|
switch (pConfig->format)
|
||||||
@@ -4967,13 +4947,7 @@ static mal_result mal_device_init__dsound(mal_context* pContext, mal_device_type
|
|||||||
|
|
||||||
// Unfortunately DirectSound uses different APIs and data structures for playback and catpure devices :(
|
// Unfortunately DirectSound uses different APIs and data structures for playback and catpure devices :(
|
||||||
if (type == mal_device_type_playback) {
|
if (type == mal_device_type_playback) {
|
||||||
mal_DirectSoundCreateProc pDirectSoundCreate = (mal_DirectSoundCreateProc)GetProcAddress((HMODULE)pDevice->dsound.hDSoundDLL, "DirectSoundCreate");
|
if (FAILED(((mal_DirectSoundCreateProc)pContext->dsound.DirectSoundCreate)((pDeviceID == NULL) ? NULL : (const GUID*)pDeviceID->dsound, (LPDIRECTSOUND*)&pDevice->dsound.pPlayback, NULL))) {
|
||||||
if (pDirectSoundCreate == NULL) {
|
|
||||||
mal_device_uninit__dsound(pDevice);
|
|
||||||
return mal_post_error(pDevice, "[DirectSound] Could not find DirectSoundCreate().", MAL_API_NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FAILED(pDirectSoundCreate((pDeviceID == NULL) ? NULL : (const GUID*)pDeviceID->dsound, (LPDIRECTSOUND*)&pDevice->dsound.pPlayback, NULL))) {
|
|
||||||
mal_device_uninit__dsound(pDevice);
|
mal_device_uninit__dsound(pDevice);
|
||||||
return mal_post_error(pDevice, "[DirectSound] DirectSoundCreate() failed for playback device.", MAL_DSOUND_FAILED_TO_CREATE_DEVICE);
|
return mal_post_error(pDevice, "[DirectSound] DirectSoundCreate() failed for playback device.", MAL_DSOUND_FAILED_TO_CREATE_DEVICE);
|
||||||
}
|
}
|
||||||
@@ -5104,13 +5078,7 @@ static mal_result mal_device_init__dsound(mal_context* pContext, mal_device_type
|
|||||||
pDevice->bufferSizeInFrames *= 2; // <-- Might need to fiddle with this to find a more ideal value. May even be able to just add a fixed amount rather than scaling.
|
pDevice->bufferSizeInFrames *= 2; // <-- Might need to fiddle with this to find a more ideal value. May even be able to just add a fixed amount rather than scaling.
|
||||||
}
|
}
|
||||||
|
|
||||||
mal_DirectSoundCaptureCreateProc pDirectSoundCaptureCreate = (mal_DirectSoundCaptureCreateProc)GetProcAddress((HMODULE)pDevice->dsound.hDSoundDLL, "DirectSoundCaptureCreate");
|
if (FAILED(((mal_DirectSoundCaptureCreateProc)pContext->dsound.DirectSoundCaptureCreate)((pDeviceID == NULL) ? NULL : (const GUID*)pDeviceID->dsound, (LPDIRECTSOUNDCAPTURE*)&pDevice->dsound.pCapture, NULL))) {
|
||||||
if (pDirectSoundCaptureCreate == NULL) {
|
|
||||||
mal_device_uninit__dsound(pDevice);
|
|
||||||
return mal_post_error(pDevice, "[DirectSound] Could not find DirectSoundCreate().", MAL_API_NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FAILED(pDirectSoundCaptureCreate((pDeviceID == NULL) ? NULL : (const GUID*)pDeviceID->dsound, (LPDIRECTSOUNDCAPTURE*)&pDevice->dsound.pCapture, NULL))) {
|
|
||||||
mal_device_uninit__dsound(pDevice);
|
mal_device_uninit__dsound(pDevice);
|
||||||
return mal_post_error(pDevice, "[DirectSound] DirectSoundCaptureCreate() failed for capture device.", MAL_FAILED_TO_OPEN_BACKEND_DEVICE);
|
return mal_post_error(pDevice, "[DirectSound] DirectSoundCaptureCreate() failed for capture device.", MAL_FAILED_TO_OPEN_BACKEND_DEVICE);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user