From 45d6d5a2c3cdebee1f068451b3bf440ea469db4e Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 18 Nov 2022 21:20:05 +1000 Subject: [PATCH] Optimization for stereo sounds in the engine. In a stereo scenario, the listener will use a channel map of {SIDE_LEFT, SIDE_RIGHT}, but sounds will default to {FRONT_LEFT, FRONT_RIGHT}. As a result, channel mapping will be performed to do the conversion. By instead defaulting stereo sounds to be consistent with the listener, a passthrough path can be used instead, improving efficiency. --- miniaudio.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/miniaudio.h b/miniaudio.h index 975ec29d..d510cc7e 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -72382,6 +72382,7 @@ static ma_result ma_engine_node_get_heap_layout(const ma_engine_node_config* pCo ma_spatializer_config spatializerConfig; ma_uint32 channelsIn; ma_uint32 channelsOut; + ma_channel defaultStereoChannelMap[2] = {MA_CHANNEL_SIDE_LEFT, MA_CHANNEL_SIDE_RIGHT}; /* <-- Consistent with the default channel map of a stereo listener. Means channel conversion can run on a fast path. */ MA_ASSERT(pHeapLayout); @@ -72431,6 +72432,10 @@ static ma_result ma_engine_node_get_heap_layout(const ma_engine_node_config* pCo /* Spatializer. */ spatializerConfig = ma_engine_node_spatializer_config_init(&baseNodeConfig); + if (spatializerConfig.channelsIn == 2) { + spatializerConfig.pChannelMapIn = defaultStereoChannelMap; + } + result = ma_spatializer_get_heap_size(&spatializerConfig, &tempHeapSize); if (result != MA_SUCCESS) { return result; /* Failed to retrieve the size of the heap for the spatializer. */ @@ -72475,6 +72480,7 @@ MA_API ma_result ma_engine_node_init_preallocated(const ma_engine_node_config* p ma_panner_config pannerConfig; ma_uint32 channelsIn; ma_uint32 channelsOut; + ma_channel defaultStereoChannelMap[2] = {MA_CHANNEL_SIDE_LEFT, MA_CHANNEL_SIDE_RIGHT}; /* <-- Consistent with the default channel map of a stereo listener. Means channel conversion can run on a fast path. */ if (pEngineNode == NULL) { return MA_INVALID_ARGS; @@ -72554,6 +72560,10 @@ MA_API ma_result ma_engine_node_init_preallocated(const ma_engine_node_config* p spatializerConfig = ma_engine_node_spatializer_config_init(&baseNodeConfig); spatializerConfig.gainSmoothTimeInFrames = pEngineNode->pEngine->gainSmoothTimeInFrames; + if (spatializerConfig.channelsIn == 2) { + spatializerConfig.pChannelMapIn = defaultStereoChannelMap; + } + result = ma_spatializer_init_preallocated(&spatializerConfig, ma_offset_ptr(pHeap, heapLayout.spatializerOffset), &pEngineNode->spatializer); if (result != MA_SUCCESS) { goto error2;