Skip to content

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.

fix(milesaudiodevice): sound reach and air stretch with the zoom

Olcay Seygan6200455e41c3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f1e4a4b1e..5730edd35 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -53,7 +53,8 @@ found and fixed, EA's own, not port damage.**
- A hill or a big building between you and a fight muffles it. Guns firing behind a cliff lose their crack altogether, their middle comes through ten decibels down and their thump four and a half, and the same guns heard across open ground sound as they always did. It eases in over a quarter of a second as the camera moves. Unit replies and the interface are never muffled.
- A big battle no longer fills every channel with gunfire. At most 16 battle sounds play at once, and when a seventeenth arrives the quietest, furthest one gives way, or the new one is dropped if it matters less than all of them. The same shot fired by several tanks side by side in the same moment is one louder sound instead of a stack. On a 290 unit crowd fight the busiest second went from 25 voices to 17, with the same peak level. Replies, EVA, the interface and looping sounds are never cut for it.
- Sounds that travel before they are heard now obey the same limits as the ones that start at once. A rocket's launch heard a second late used to ignore the cap on how many copies of it may play, and a sound limited to one at a time could refuse itself and go silent. Both are EA's, both are fixed.
-- Zoom out and the fight takes a step back. Unit and weapon sounds drop to 60 percent while the camera is high, and the wind, birds and city hum around them do not, so the whole map still sounds like a place.
+- Zoom out and the fight takes a step back. Unit and weapon sounds drop to 75 percent while the camera is high, and the wind, birds and city hum around them do not, so the whole map still sounds like a place.
+- A fight anywhere on the screen is heard now, zoomed out as well. The camera goes nearly three times further out than the original's, but every sound still carried only as far as it did at the original's zoom, so at full zoom the side of the screen was 700 units from the microphone and its top 1000, past the 800 most sounds reach: the top quarter of the screen was silent and a fight at the edge came through 14 decibels down with nothing above 5 kHz, against 9 decibels down for one in the middle. Every sound's reach and the air now stretch with the zoom, so the screen falls off from its middle to its edges the way it does zoomed in: at full zoom a fight in the middle plays 2.5 decibels down and one at the edge 3, still bright. The air is also thinner, so at the normal zoom a fight at the edge of the screen keeps everything up to about 13 kHz where it used to lose all above 10, and one just off it up to about 9 kHz where it used to lose all above 6.
- Every map has a bed of ambience now: snow, town, desert or field, chosen from the terrain the map is made of and played from the game's own ambient loops. It sits under the battle and swells as you zoom out.
- The music crossfade is real. The old fade counted drawn frames, so it ran at the speed of your graphics card; it now runs on game time, two seconds, with no dip in the middle.
- A mod's music tracks can say whether they are calm, tense or battle music, and for which side, so Music Follows the Fight works with a mod's own soundtrack.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/AudioSettings.h b/GeneralsMD/Code/GameEngine/Include/Common/AudioSettings.h
index ef970e142..cea6485a0 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/AudioSettings.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/AudioSettings.h
@@ -49,7 +49,7 @@ struct AudioSettings
m_maxSoundDelay( 1000.0f ),
m_dopplerScale( 3.0f ),
m_dopplerFastSpeed( 100.0f ),
- m_airAbsorptionDistance( 300.0f ),
+ m_airAbsorptionDistance( 450.0f ),
m_reverbLevel( 1.0f ),
m_duckVolume( 0.15f ),
m_duckTime( 4000.0f ),
@@ -68,7 +68,7 @@ struct AudioSettings
m_occlusionRays( 16 ),
m_zoomMixNearHeight( 300.0f ),
m_zoomMixFarHeight( 900.0f ),
- m_zoomMixDetailVolume( 0.6f ),
+ m_zoomMixDetailVolume( 0.75f ),
m_ambienceBedNearVolume( 0.3f ),
m_ambienceBedFarVolume( 0.8f ),
m_ambienceBedSnow( "Amb_WinterNatureWindLoop" ),
@@ -119,7 +119,9 @@ struct AudioSettings
* times its speed and a tank at 30 at 3. Zero keeps one scale for everything.
* `AirAbsorptionDistance`, game units: where air has taken the top end down from 20 kHz to 10,
* falling with the square of the distance past it, through two stages. Zero is no air. A snow
- * map takes it at 60%.
+ * map takes it at 60%, and a zoom past MaxCameraHeight stretches it with the ranges. 450: at
+ * 300 a fight at the edge of the screen at EA's zoom (about 300 units off) lost all above 10 kHz
+ * and one just past it (460) all above 6 kHz.
* `ReverbLevel`: the room's send, as a share of what the map asks for. Zero is no room.
* `DuckVolume` and `DuckTime` (milliseconds): when one of `DuckSounds` starts within earshot,
* everything else dips to that volume and comes back over that time. 100% is no duck. */
@@ -174,7 +176,9 @@ struct AudioSettings
* at, the 3D sounds that are not ambience (weapons, engines, explosions, voices on the field) fall
* to `ZoomMixDetailVolume` of their level, while ambience keeps its own; zoomed back in, the
* detail returns. 100% leaves the mix as it was. EA's own zoom bonus (`ZoomSoundVolume...`)
- * still applies on top, to both.
+ * still applies on top, to both. 75%, 2.5 dB: 60% took 4.4 dB off a zoomed out
+ * fight, on top of EA's 1.9. The ranges and the air stretch with the zoom as well
+ * (AudioManager::getRangeScale), which is what keeps the edges of a zoomed out screen in earshot.
*
* The map's ambience bed (MilesAudioManager::updateAmbienceBed): one looping 2D sound chosen
* from the map when it starts, `AmbienceBedSnow` on a snow map, `AmbienceBedTown` on a built-up
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GameAudio.h b/GeneralsMD/Code/GameEngine/Include/Common/GameAudio.h
index 234ea0e74..c54759977 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GameAudio.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GameAudio.h
@@ -316,6 +316,9 @@ class AudioManager : public SubsystemInterface
Real getZoomVolume() const { return m_zoomVolume; }
/// how far out the camera is between ZoomMixNearHeight (0) and ZoomMixFarHeight (1)
Real getZoomOut() const { return m_zoomOut; }
+ /// what every world sound's MinRange, MaxRange and the air are multiplied by: the camera's zoom past
+ /// MaxCameraHeight, so the screen sounds as far across at any zoom as at the one the ranges were made for
+ Real getRangeScale() const { return m_rangeScale; }
// Birds, wind, water, a town's murmur: a looping world sound effect at the lowest priority that
// is not hidden by the shroud. These answer to the Ambient slider instead of the effects one.
@@ -390,6 +393,7 @@ class AudioManager : public SubsystemInterface
Real m_zoomVolume;
Real m_zoomOut; ///< see getZoomOut
Real m_zoomDetailVolume; ///< ZoomMixDetailVolume as far as the camera is out, on every 3D sound but ambience
+ Real m_rangeScale; ///< see getRangeScale
AudioEventRTS *m_silentAudioEvent;
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
index ecc7ab221..73dba6da2 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
@@ -214,6 +214,7 @@ AudioManager::AudioManager() :
m_zoomVolume = 1.0f;
m_zoomOut = 0.0f;
m_zoomDetailVolume = 1.0f;
+ m_rangeScale = 1.0f;
m_volumeHasChanged = FALSE;
//
@@ -452,6 +453,14 @@ void AudioManager::update()
m_zoomOut = MIN( 1.0f, MAX( 0.0f, m_zoomOut ) );
m_zoomDetailVolume = 1.0f - (1.0f - settings->m_zoomMixDetailVolume) * m_zoomOut;
+ // The sounds' ranges were made for EA's zoom, MaxCameraHeight; the wheel goes nearly three times
+ // further out, where the side of the screen stood 700 units from the microphone and its top 1000,
+ // past nearly every sound's MaxRange of 800, so only the middle of the screen was heard. Past
+ // MaxCameraHeight the ranges grow with the zoom, and the screen falls off from its middle to its
+ // edges as it does at EA's zoom. Legacy sound keeps retail's ranges.
+ m_rangeScale = TheGlobalData->m_legacySound ? 1.0f
+ : MAX( 1.0f, TheTacticalView->getHeightAboveGround() / TheGlobalData->m_maxCameraHeight );
+
set3DVolumeAdjustment( m_zoomVolume );
m_music->update();
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameSounds.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameSounds.cpp
index 444996e6f..4f50d8900 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameSounds.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameSounds.cpp
@@ -227,7 +227,7 @@ Bool SoundManager::canPlayNow( AudioEventRTS *event )
if (pos)
{
distance.sub(pos);
- if (distance.length() >= event->getAudioEventInfo()->m_maxDistance)
+ if (distance.length() >= event->getAudioEventInfo()->m_maxDistance * TheAudio->getRangeScale())
{
#ifdef INTENSIVE_AUDIO_DEBUG
DEBUG_LOG(("- culled due to distance (%.2f).\n", distance.length()));
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h b/GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h
index 9369687e7..517d379f5 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h
@@ -269,6 +269,8 @@ class MilesAudioManager : public AudioManager
Bool refuseOverBudget( AudioEventRTS *event ) const;
Real getLoudness( AudioEventRTS *event ) const;
void logMix( void );
+ void noteReach( const PlayingAudio *playing, const Coord3D *pos ) const;
+ void setSampleDistances( H3DSAMPLE sample3D, const AudioEventRTS *event ) const;
// Occlusion: the line from the microphone to a world sound, through the ground and the buildings.
Real lookForOcclusion( const Coord3D *pos ) const;
@@ -384,6 +386,8 @@ class MilesAudioManager : public AudioManager
UnsignedInt m_clockMovedAt; ///< Clock_Milliseconds when the logic clock last moved on
AsciiString m_roomMap; ///< the map setRoomFromMap last set the room for
Bool m_legacySound; ///< what applyLegacySound last put into m_audioSettings
+ Real m_roomAbsorption; ///< the air's absorption distance the room asks for, before the range scale
+ Real m_airScale; ///< the range scale the backend's air was last given with, 0 to give it again
AudioSettings m_engineSettings; ///< AudioSettings.ini as init read it, for Legacy sound turned off again
Bool m_askingOnArrival; ///< checkForSample is asking canPlayNow about a sound that waited out its distance
@@ -469,6 +473,10 @@ class MilesAudioManager : public AudioManager
Int started3D, merged, stolen, overBudget, repliesLet, repliesHeld, looks;
UnsignedInt peak3D, peakBattle;
Real occludedPeak;
+ // the AUDIO REACH line (noteReach): battle sounds started in the middle of the screen, on the
+ // rest of it and off it, their summed distance, gain in dB and cutoff in Hz
+ Int reachCount[3];
+ Real reachDistance[3], reachGainDb[3], reachCutoff[3];
};
mutable MixCounts m_mixCounts;
UnsignedInt m_mixLoggedAt;
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
index d8637d8d6..3b8f8d40e 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
@@ -146,6 +146,8 @@ MilesAudioManager::MilesAudioManager() :
m_clockElapsedMs(MSEC_PER_LOGICFRAME_REAL),
m_clockMovedAt(0),
m_legacySound(FALSE),
+ m_roomAbsorption(0.0f),
+ m_airScale(0.0f),
m_askingOnArrival(false),
m_bedHandle(AHSV_Error),
m_bedStartedAt(0),
@@ -507,6 +509,7 @@ void MilesAudioManager::init()
{
AudioManager::init();
m_engineSettings = *m_audioSettings;
+ m_roomAbsorption = m_audioSettings->m_airAbsorptionDistance;
#ifdef INTENSE_DEBUG
DEBUG_LOG(("Sound has temporarily been disabled in debug builds only. jkmcd\n"));
// for now, _DEBUG builds only should have no sound. ask jkmcd or srj about this.
@@ -630,6 +633,12 @@ void MilesAudioManager::update()
m_roomMap = TheGlobalData->m_mapName;
setRoomFromMap();
}
+ // the air, as far as the room has it, stretched with the sounds' ranges (AudioManager::getRangeScale)
+ if (m_airScale != getRangeScale())
+ {
+ m_airScale = getRangeScale();
+ AIL_ex_set_3D_air(m_audioSettings->m_speedOfSound, m_roomAbsorption * m_airScale);
+ }
if (TheGameLogic->isInGame() && !TheGameLogic->isLoadingMap())
{
buildBlockers();
@@ -1664,6 +1673,55 @@ void MilesAudioManager::initFilters3D( PlayingAudio *audio, const Coord3D *pos )
updateOcclusion(audio, pos, TRUE); // the first pass hears its hill from the start
}
applyOcclusion(audio);
+ noteReach(audio, pos);
+}
+
+//-------------------------------------------------------------------------------------------------
+/** What a battle sound starts at, for the AUDIO REACH line: where on the screen it is (the middle
+ * half of the width and height, the rest of the screen, off it), its distance from the microphone,
+ * its gain before the sliders (distance, the hill in the way, the zoom mix) and its low-pass cutoff
+ * (air and occlusion, AIL_ex_3D_cutoff; a snow map's thinner air is not counted). */
+void MilesAudioManager::noteReach( const PlayingAudio *playing, const Coord3D *pos ) const
+{
+ const AudioEventInfo *info = playing->m_audioEventRTS->getAudioEventInfo();
+ if (info->m_soundType != AT_SoundEffect || BitTest(info->m_type, ST_VOICE | ST_GLOBAL) || isAmbientSound(info)
+ || !TheGameLogic->isInGame() || TheGameLogic->isInShellGame()) {
+ return;
+ }
+ Int bucket = 2;
+ ICoord2D screen;
+ if (TheTacticalView->worldToScreenTriReturn(pos, &screen) == View::WTS_INSIDE_FRUSTUM) {
+ Int originX, originY;
+ TheTacticalView->getOrigin(&originX, &originY);
+ const Int width = TheTacticalView->getWidth();
+ const Int height = TheTacticalView->getHeight();
+ bucket = abs(2 * (screen.x - originX) - width) < width / 2 && abs(2 * (screen.y - originY) - height) < height / 2 ? 0 : 1;
+ }
+ Coord3D toMicrophone = *pos;
+ toMicrophone.sub(&m_listenerPosition);
+ const Real distance = toMicrophone.length();
+ const AudioSettings *settings = m_audioSettings;
+ const Real occlusionGain = 1.0f - (1.0f - settings->m_occlusionVolume) * playing->m_occlusion;
+ const Real gain = AIL_ex_3D_distance_gain(distance, info->m_minDistance * getRangeScale(), info->m_maxDistance * getRangeScale(),
+ settings->m_rangeVolumeFade) * occlusionGain * m_zoomDetailVolume;
+ const Real occlusion = max(playing->m_offscreenOcclusion, playing->m_occlusion * settings->m_occlusionStrength);
+ ++m_mixCounts.reachCount[bucket];
+ m_mixCounts.reachDistance[bucket] += distance;
+ m_mixCounts.reachGainDb[bucket] += 20.0f * log10f(max(gain, 0.001f));
+ m_mixCounts.reachCutoff[bucket] += AIL_ex_3D_cutoff(distance, settings->m_airAbsorptionDistance * getRangeScale(), occlusion);
+}
+
+//-------------------------------------------------------------------------------------------------
+/** A 3D sample's range, as the zoom has it (getRangeScale): set as it starts and again every pass, so
+ * a loop playing while the camera zooms follows it. 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. */
+void MilesAudioManager::setSampleDistances( H3DSAMPLE sample3D, const AudioEventRTS *event ) const
+{
+ const Bool global = BitTest(event->getAudioEventInfo()->m_type, ST_GLOBAL);
+ const Real minRange = global ? (Real)m_audioSettings->m_globalMinRange : event->getAudioEventInfo()->m_minDistance;
+ const Real maxRange = global ? (Real)m_audioSettings->m_globalMaxRange : event->getAudioEventInfo()->m_maxDistance;
+ AIL_set_3D_sample_distances(sample3D, maxRange * getRangeScale(), minRange * getRangeScale());
}
//-------------------------------------------------------------------------------------------------
@@ -2541,6 +2599,33 @@ void MilesAudioManager::logMix( void )
"%d replies let %d held, %d looks, most muffled %.2f\n", frame, m_mixCounts.started3D, m_mixCounts.peak3D,
m_mixCounts.peakBattle, m_mixCounts.merged, m_mixCounts.stolen, m_mixCounts.overBudget, m_mixCounts.repliesLet,
m_mixCounts.repliesHeld, m_mixCounts.looks, m_mixCounts.occludedPeak));
+
+ // How far the microphone is from the ground at the middle of the screen's side and top and bottom
+ // edges, against which the battle sounds started this second in each part of the screen.
+ Int originX, originY;
+ TheTacticalView->getOrigin(&originX, &originY);
+ const Int width = TheTacticalView->getWidth();
+ const Int height = TheTacticalView->getHeight();
+ const ICoord2D edges[3] = { { originX, originY + height / 2 }, { originX + width / 2, originY }, { originX + width / 2, originY + height - 1 } };
+ Real edgeDistance[3] = { -1.0f, -1.0f, -1.0f };
+ for (Int i = 0; i < 3; ++i) {
+ Coord3D ground;
+ if (TheTacticalView->screenToTerrain(&edges[i], &ground)) {
+ ground.sub(&m_listenerPosition);
+ edgeDistance[i] = ground.length();
+ }
+ }
+ const char *bucketNames[3] = { "middle", "edge", "off" };
+ AsciiString reach;
+ for (Int i = 0; i < 3; ++i) {
+ const Int n = m_mixCounts.reachCount[i];
+ AsciiString part;
+ part.format(", %s %d at %.0f units %.1f dB %.0f Hz", bucketNames[i], n, n ? m_mixCounts.reachDistance[i] / n : 0.0f,
+ n ? m_mixCounts.reachGainDb[i] / n : 0.0f, n ? m_mixCounts.reachCutoff[i] / n : 0.0f);
+ reach.concat(part);
+ }
+ DEBUG_LOG(("AUDIO REACH frame %u: side %.0f, top %.0f, bottom %.0f units away, zoom out %.2f%s\n", frame,
+ edgeDistance[0], edgeDistance[1], edgeDistance[2], getZoomOut(), reach.str()));
memset(&m_mixCounts, 0, sizeof(m_mixCounts));
}
@@ -3078,6 +3163,7 @@ void MilesAudioManager::processPlayingList( void )
{
updateOcclusion( playing, pos, lookAgain );
trackVelocity( playing, pos );
+ setSampleDistances( playing->m_3DSample, playing->m_audioEventRTS );
Real x = pos->x;
Real y = pos->y;
Real z = pos->z;
@@ -3495,7 +3581,8 @@ void MilesAudioManager::applyLegacySound( void )
m_legacySound = TheGlobalData->m_legacySound;
setSoundProcessing( m_audioSettings, m_legacySound ? NULL : &m_engineSettings );
AIL_ex_set_limiter( !m_legacySound );
- AIL_ex_set_3D_air( m_audioSettings->m_speedOfSound, m_audioSettings->m_airAbsorptionDistance );
+ m_roomAbsorption = m_audioSettings->m_airAbsorptionDistance;
+ m_airScale = 0.0f;
removeAudioEvent( m_bedHandle );
m_bedEvent.clear();
m_roomMap.clear();
@@ -3557,7 +3644,8 @@ void MilesAudioManager::setRoomFromMap( void )
slap = 0.0f;
absorption *= SNOW_AIR_SHARE;
}
- AIL_ex_set_3D_air(m_audioSettings->m_speedOfSound, absorption);
+ m_roomAbsorption = absorption;
+ m_airScale = 0.0f; // update() hands it down with the zoom's range scale
AIL_ex_set_reverb(wet * m_audioSettings->m_reverbLevel, decay, damping, echo, slap);
DEBUG_LOG(("AUDIO: room for %s: %.2f%% built over, town %.2f%s, send %.2f, decay %.2f s, damping %.2f, "
"echo %d ms at %.2f, air %.0f\n", m_roomMap.str(), cover * 100.0f, town, snowy ? " in snow" : "",
@@ -3693,8 +3781,8 @@ void MilesAudioManager::duckForBlast( AudioEventRTS *event, H3DSAMPLE sample3D )
toMicrophone.sub(&m_listenerPosition);
const Real distance = toMicrophone.length();
const Real nearness = sqrtf(AIL_ex_3D_distance_gain(distance,
- global ? (Real)settings->m_globalMinRange : info->m_minDistance,
- global ? (Real)settings->m_globalMaxRange : info->m_maxDistance, settings->m_rangeVolumeFade));
+ (global ? (Real)settings->m_globalMinRange : info->m_minDistance) * getRangeScale(),
+ (global ? (Real)settings->m_globalMaxRange : info->m_maxDistance) * getRangeScale(), settings->m_rangeVolumeFade));
if (nearness <= 0.0f)
{
DEBUG_LOG(("AUDIO: duck by %s at %.0f units: out of earshot, none\n", event->getEventName().str(), distance));
@@ -3932,7 +4020,7 @@ Real MilesAudioManager::getEffectiveVolume(AudioEventRTS *event) const
// 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. 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,
+ volume *= AIL_ex_3D_distance_gain( objDistance, objMinDistance * getRangeScale(), objMaxDistance * getRangeScale(),
TheAudio->getAudioSettings()->m_rangeVolumeFade );
}
}
@@ -4049,12 +4137,8 @@ void *MilesAudioManager::playSample3D( PlayingAudio *audio )
// 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_globalMaxRange, TheAudio->getAudioSettings()->m_globalMinRange );
- } else {
- AIL_set_3D_sample_distances(sample3D, event->getAudioEventInfo()->m_maxDistance, event->getAudioEventInfo()->m_minDistance );
- }
-
+ setSampleDistances(sample3D, event);
+
// Set the position of the sample here
Real x = pos->x;
Real y = pos->y;