Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(milesaudio): fall world sounds off with distance from the listener
Olcay Seygan8c7195a10212
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a9d83307d..781d130ca 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1634,6 +1634,7 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same- Audio is real, through the audio library the retail game ships with.- Installs from outside Steam start again. Their copy of the game's data files differs from Steam's in a few places, and one of our fixes, to the Aurora's fuel-air bomb, looked for a piece that copy does not have and stopped the game at start with a parsing error. A fix that finds nothing to change now leaves that piece as it is and the game carries on.- A death cry plays to the end. So does "construction complete", and the tail of a gun. Telling a sound to stop used to cut it off that frame, where the 2003 game let it finish and only kept it from repeating; our own fix for the audio thread broke that, and it is put back. A looping sound caught in the pause between two plays can be stopped too, instead of carrying on after whatever made it is gone.+- Birdsong no longer drowns out the battle. Every sound on the map played at full volume all the way out to the edge of its range, because the game handed the sound library its near and far distances the wrong way round. A sound that carries a long way, like the birds and the wind, came through at full strength from across the map, while a gun firing just off screen had already been cut. Sounds get quieter with distance now, at the same rate the game uses to decide when one is too faint to keep playing, so what is close to the camera is what you hear.- A sound on the right of the screen comes out of the right speaker. Since the game went 64-bit its sound library worked left and right out the wrong way round, so a tank firing off to the right was heard on the left. Only a sound well off to one side swings far enough to notice, which is why it seemed to come and go.- Your units answer you over the battle. A reply plays at whichever of the effects and voice sliders is higher, lifted again on top of that, so "Moving out" is heard in the middle of an artillery duel. Nothing else is turned down to make room for it. EVA and briefings stay where the voice slider puts them.- Leaving the score screen no longer plays its music over the main menu theme. The score music was told to fade out over two seconds, but a fade only moves while the game updates its sound, and the menu's background battle was loading in that time with no sound update at all. The music came out of the load at full volume and faded on top of the menu's own track. It stops the moment you click now.diff --git a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cppindex d856039e3..a1129e10b 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp@@ -1689,6 +1689,7 @@ void MilesAudioManager::openDevice( void )// AIL_quick_startup should be replaced later with a call to actually pick which device to use, etcconst AudioSettings *audioSettings = getAudioSettings();+ AIL_ex_set_3D_linear_falloff(audioSettings->m_rangeVolumeFade);m_selectedSpeakerType = TheAudio->translateSpeakerTypeToUnsignedInt(m_prefSpeaker);retval = AIL_quick_startup(audioSettings->m_useDigital, audioSettings->m_useMidi, audioSettings->m_outputRate, audioSettings->m_outputBits, audioSettings->m_outputChannels);@@ -2610,7 +2611,7 @@ void MilesAudioManager::processPlayingList( void )else{Real volForConsideration = getEffectiveVolume(playing->m_audioEventRTS);- volForConsideration /= (m_sound3DVolume > 0.0f ? m_soundVolume : 1.0f);+ volForConsideration /= (m_sound3DVolume > 0.0f ? m_sound3DVolume : 1.0f);Bool playAnyways = BitTest( playing->m_audioEventRTS->getAudioEventInfo()->m_type, ST_GLOBAL) || playing->m_audioEventRTS->getAudioEventInfo()->m_priority == AP_CRITICAL;if( volForConsideration < m_audioSettings->m_minVolume && !playAnyways ){@@ -2741,7 +2742,9 @@ void MilesAudioManager::processFadingList( void )case PAT_3DSample:{- AIL_set_3D_sample_volume(playing->m_3DSample, volume);+ // Not volume: that carries the distance falloff, which the backend applies again.+ AIL_set_3D_sample_volume(playing->m_3DSample, getVoiceMixedVolume(playing->m_audioEventRTS, m_sound3DVolume)+ * (1.0f - 1.0f * playing->m_framesFaded / getAudioSettings()->m_fadeAudioFrames));break;}@@ -2993,22 +2996,10 @@ Real MilesAudioManager::getEffectiveVolume(AudioEventRTS *event) const// zero, so the hard cut at the maximum range makes a sound you are walking away// from stop dead instead of fading out. The linear one reaches zero exactly where// the cut is. Off by default: it changes how every 3D sound in the game attenuates,- // and that is a listening decision, not a bug fix.- if( TheAudio->getAudioSettings()->m_rangeVolumeFade &&- objMaxDistance > objMinDistance )- {- if( objDistance > objMinDistance )- volume *= 1.0f - (objDistance - objMinDistance) / (objMaxDistance - objMinDistance);- }- else if( objDistance > objMinDistance )- {- volume *= 1 / (objDistance / objMinDistance);- }-- if( objDistance >= objMaxDistance )- {- volume = 0.0f;- }+ // and that is a listening decision, not a bug fix. The curve is the audio backend's+ // own, so a sound is culled on the same curve it is heard at.+ volume *= AIL_ex_3D_distance_gain( objDistance, objMinDistance, objMaxDistance,+ TheAudio->getAudioSettings()->m_rangeVolumeFade );}}else@@ -3115,11 +3106,13 @@ void *MilesAudioManager::playSample3D( AudioEventRTS *event, H3DSAMPLE sample3D// Prep any sort of filtering, etc, hereAIL_register_3D_EOS_callback(sample3D, set3DSampleCompleted);- // Set the position values of the sample here+ // Set the position values of the sample here. Miles takes the maximum first; EA passed the+ // minimum first, and with the pair backwards every world sound played at full volume out to+ // its cut-off, so far-reaching ambient loops drowned the fighting next to the camera.if (event->getAudioEventInfo()->m_type & ST_GLOBAL) {- AIL_set_3D_sample_distances(sample3D, TheAudio->getAudioSettings()->m_globalMinRange, TheAudio->getAudioSettings()->m_globalMaxRange );+ AIL_set_3D_sample_distances(sample3D, TheAudio->getAudioSettings()->m_globalMaxRange, TheAudio->getAudioSettings()->m_globalMinRange );} else {- AIL_set_3D_sample_distances(sample3D, event->getAudioEventInfo()->m_minDistance, event->getAudioEventInfo()->m_maxDistance );+ AIL_set_3D_sample_distances(sample3D, event->getAudioEventInfo()->m_maxDistance, event->getAudioEventInfo()->m_minDistance );}// Set the position of the sample herediff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/include/MSS/MSS.h b/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/include/MSS/MSS.hindex 07e4e6674..82f844f5a 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/include/MSS/MSS.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/include/MSS/MSS.h@@ -263,6 +263,12 @@ void AILCALL AIL_quick_set_volume(HAUDIO audio, F32 volume, F32 extravol);S32 AILCALL AIL_ex_start_capture(const char *pathname);void AILCALL AIL_ex_stop_capture(void);+/* ---- distance falloff -------------------------------------------------- */+/* Not Miles. Nonzero makes every 3D sample fall off in a straight line from min_dist to max_dist+ instead of Miles' min_dist / distance; MilesAudioManager passes AudioSettings.ini's+ RangeVolumeFade, so what plays and what getEffectiveVolume culls follow one curve. */+void AILCALL AIL_ex_set_3D_linear_falloff(S32 linear);+/* ---- file format helpers ---------------------------------------------- */S32 AILCALL AIL_WAV_info(const void *data, AILSOUNDINFO *info);S32 AILCALL AIL_decompress_ADPCM(const AILSOUNDINFO *info, void **outdata, U32 *outsize);@@ -272,4 +278,19 @@ void AILCALL AIL_mem_free_lock(void *ptr);} /* extern "C" */#endif+/* Not Miles: the distance curve both backends play a 3D sample at and the one+ MilesAudioManager::getEffectiveVolume culls with, kept in one place so the two cannot drift. Full+ inside min_dist, then min_dist / distance (Miles' rolloff), or a straight line to zero when linear+ is set, and silent from max_dist on. */+static inline F32 AIL_ex_3D_distance_gain(F32 distance, F32 min_dist, F32 max_dist, S32 linear)+{+ if (distance >= max_dist)+ return 0.0f;+ if (distance <= min_dist)+ return 1.0f;+ if (linear && max_dist > min_dist)+ return 1.0f - (distance - min_dist) / (max_dist - min_dist);+ return min_dist / distance;+}+#endif /* MSS_H */diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/miniaudio/miles_miniaudio.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/miniaudio/miles_miniaudio.cppindex c546b76e0..c046b3779 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/miniaudio/miles_miniaudio.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/miniaudio/miles_miniaudio.cpp@@ -546,6 +546,7 @@ struct Engine};Engine g_engine;+std::atomic<S32> g_linearFalloff(0); // AIL_ex_set_3D_linear_falloffAILFILEOPENCB g_fileOpen;AILFILECLOSECB g_fileClose;@@ -582,14 +583,7 @@ void computeSpatialGain(const Sample &sample, float *volumeOut, float *panOut)const float dz = sample.positionZ - listener.positionZ;const float distance = (float)sqrt(dx * dx + dy * dy + dz * dz);- float attenuation = 1.0f;- if (sample.maxDistance > sample.minDistance) {- if (distance >= sample.maxDistance) {- attenuation = 0.0f;- } else if (distance > sample.minDistance) {- attenuation = (sample.maxDistance - distance) / (sample.maxDistance - sample.minDistance);- }- }+ float attenuation = AIL_ex_3D_distance_gain(distance, sample.minDistance, sample.maxDistance, g_linearFalloff);attenuation *= clampUnit(1.0f - sample.occlusion);float pan = DEFAULT_PAN;@@ -1740,6 +1734,12 @@ void AILCALL AIL_set_3D_sample_distances(H3DSAMPLE handle, F32 max_dist, F32 minrefreshSampleOutput(sample);}+void AILCALL AIL_ex_set_3D_linear_falloff(S32 linear)+{+ // A playing sample takes it at its next position update.+ g_linearFalloff.store(linear);+}+void AILCALL AIL_set_3D_sample_occlusion(H3DSAMPLE handle, F32 occlusion){Sample *sample = (Sample *)handle;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/xaudio2/miles_xaudio2.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/xaudio2/miles_xaudio2.cppindex be717490e..bd27b995d 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/xaudio2/miles_xaudio2.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/xaudio2/miles_xaudio2.cpp@@ -306,6 +306,7 @@ struct Engine};Engine g_engine;+S32 g_linearFalloff; // AIL_ex_set_3D_linear_falloffAILFILEOPENCB g_fileOpen;AILFILECLOSECB g_fileClose;@@ -359,14 +360,7 @@ void computeSpatialGain(const Sample &sample, float *volumeOut, float *panOut)const float dz = sample.positionZ - listener.positionZ;const float distance = (float)sqrt(dx * dx + dy * dy + dz * dz);- float attenuation = 1.0f;- if (sample.maxDistance > sample.minDistance) {- if (distance >= sample.maxDistance) {- attenuation = 0.0f;- } else if (distance > sample.minDistance) {- attenuation = (sample.maxDistance - distance) / (sample.maxDistance - sample.minDistance);- }- }+ float attenuation = AIL_ex_3D_distance_gain(distance, sample.minDistance, sample.maxDistance, g_linearFalloff);attenuation *= clampUnit(1.0f - sample.occlusion);float pan = DEFAULT_PAN;@@ -1486,6 +1480,13 @@ void AILCALL AIL_set_3D_sample_distances(H3DSAMPLE handle, F32 max_dist, F32 minLeaveCriticalSection(&g_engine.lock);}+void AILCALL AIL_ex_set_3D_linear_falloff(S32 linear)+{+ // No lock: it may come before AIL_startup has one, and a playing sample takes it at its next+ // position update.+ g_linearFalloff = linear;+}+void AILCALL AIL_set_3D_sample_occlusion(H3DSAMPLE handle, F32 occlusion){Sample *sample = (Sample *)handle;diff --git a/GeneralsMD/Code/Tests/miles_smoke.cpp b/GeneralsMD/Code/Tests/miles_smoke.cppindex 4be3d7dbf..c28e2f2b2 100644--- a/GeneralsMD/Code/Tests/miles_smoke.cpp+++ b/GeneralsMD/Code/Tests/miles_smoke.cpp@@ -156,6 +156,13 @@ int main(void)return 1;}+ /* The distance curve every 3D sample plays at, which needs no device: full inside the minimum,+ minimum / distance past it, a straight line with RangeVolumeFade, silent from the maximum. */+ CHECK(AIL_ex_3D_distance_gain(5.0f, 10.0f, 110.0f, 0) == 1.0f, "inside the minimum is not full volume");+ CHECK(fabs(AIL_ex_3D_distance_gain(60.0f, 10.0f, 110.0f, 0) - 10.0f / 60.0f) < 1e-6, "past the minimum is not minimum / distance");+ CHECK(fabs(AIL_ex_3D_distance_gain(60.0f, 10.0f, 110.0f, 1) - 0.5f) < 1e-6, "the linear falloff is not halfway at halfway");+ CHECK(AIL_ex_3D_distance_gain(110.0f, 10.0f, 110.0f, 0) == 0.0f, "the maximum is not silent");+AIL_startup();if (!AIL_quick_startup(1, 0, 44100, 16, 2)) {printf("skip: no audio device\n");diff --git a/GeneralsMD/Code/Tests/test_miles_miniaudio.cpp b/GeneralsMD/Code/Tests/test_miles_miniaudio.cppindex ad6317428..7ae63b426 100644--- a/GeneralsMD/Code/Tests/test_miles_miniaudio.cpp+++ b/GeneralsMD/Code/Tests/test_miles_miniaudio.cpp@@ -312,8 +312,8 @@ TEST(playback_rate_is_pitch)}// ----------------------------------------------------------------------------------------------// 3D: miles_xaudio2's linear falloff between the two distances, occlusion as a scale, and a pan-// along up x face.+// 3D: miles_xaudio2's min / distance falloff between the two distances (a straight line with+// AIL_ex_set_3D_linear_falloff), occlusion as a scale, and a pan along up x face.// ---------------------------------------------------------------------------------------------static Heard play3D(float x, float occlusion)@@ -357,11 +357,24 @@ static Heard play3D(float x, float occlusion)return heard;}-TEST(falloff_is_linear_between_the_distances)+TEST(falloff_is_the_minimum_over_the_distance){- // 60 units out, halfway from 10 to 110: half the volume, all of it on the right.+ // 60 units out with the minimum at 10: a sixth of the volume, all of it on the right. Full+ // volume here is the arguments swapped (minimum 110, maximum 10), which is how every world+ // sound in the game played until the call site was fixed.Heard heard = play3D(-60.0f, 0.0f);if (!audio()) return;+ CHECK(near(heard.right, TONE_RMS / 6.0));+ CHECK(heard.left < SILENT_RMS);+}++TEST(linear_falloff_is_a_straight_line_between_the_distances)+{+ // RangeVolumeFade: 60 units out, halfway from 10 to 110, is half the volume.+ AIL_ex_set_3D_linear_falloff(1);+ Heard heard = play3D(-60.0f, 0.0f);+ AIL_ex_set_3D_linear_falloff(0);+ if (!audio()) return;CHECK(near(heard.right, 0.5 * TONE_RMS));CHECK(heard.left < SILENT_RMS);}