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(gameengine): read audio length from the file, not from the device

Olcay Seygan3dd1fea75ac1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a0e6edc28..56f2baf0f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -379,6 +379,7 @@ found and fixed — EA's own, not port damage.**
- A replay that never recorded which seat was yours plays instead of crashing before the map loads.
- Alt-tabbing away with an arrow key held no longer leaves the camera sliding that way when you come back. The key release goes to whatever you switched to, so the game never heard it; losing focus now ends the scroll, the middle-button pan and the ALT pitch drag along with it.
- Locking Windows mid-battle no longer eats the game. Nothing that renders runs while the screen is locked, and the particle bookkeeping had been filed under rendering, so every explosion while you were away was created and never cleared up. A hundred rocket buggies firing at the ground for a minute is enough to come back to a game that has run out of memory or simply stopped responding.
+- A mission script that waits for a line of speech to finish waits the same length of time on every machine. It used to ask the sound hardware how long the file was, so a player whose sound card was not working, or was turned off, got the answer zero and walked straight through waits that everyone else sat out. That is a different game on two screens, and a replay that stops matching the machine that recorded it. The length now comes out of the file itself: measured on one line of speech, a silent machine said 0 milliseconds and a machine with sound said 3,285. Both say 3,285 now.
- Laying a foundation costs less the further up the map you build. Flattening the ground under a new building scanned every row of the map from the bottom edge up to the building, threw all of them away bar the ones actually under it, and did that once per column. It works out the rows it needs now. The ground comes out identical, down to the last unit of height.
- EVA announcements that were queued behind another one no longer go quiet. A request waits its turn and expires if its turn never comes, and the sum that worked out when that was could wrap round to zero, retiring the announcement on the frame it was asked for.
- Tank tracks no longer crash the game. The renderer has to borrow the buffer it writes them into, and when that request failed, which is what a graphics device pulled out from under the game does, the tracks were written to address zero anyway. Two more of the same shape went with it: the screenshot path and the texture size lookup both used a pointer that the failing call had never filled in. Three of those texture lookups also leaked the surface they asked about, once per procedural texture, for the life of the process.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GameAudio.h b/GeneralsMD/Code/GameEngine/Include/Common/GameAudio.h
index 954290e5e..277b64c83 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GameAudio.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GameAudio.h
@@ -285,6 +285,7 @@ class AudioManager : public SubsystemInterface
// For Scripting
virtual Real getAudioLengthMS( const AudioEventRTS *event );
virtual Real getFileLengthMS( AsciiString strToLoad ) const = 0;
+ Real getWaveFileLengthMS( AsciiString strToLoad ) const; ///< from the file's own header, with no audio device involved
// For the file cache to know when to remove files.
virtual void closeAnySamplesUsingFile( const void *fileToClose ) = 0;
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
index 4ff913367..765826c3c 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
@@ -51,6 +51,7 @@
#include "Common/AudioHandleSpecialValues.h"
#include "Common/AudioRequest.h"
#include "Common/AudioSettings.h"
+#include "Common/File.h"
#include "Common/FileSystem.h"
#include "Common/GameEngine.h"
#include "Common/GameMusic.h"
@@ -940,6 +941,87 @@ void AudioManager::refreshCachedVariables()
m_surroundSpeakers = isCurrentSpeakerTypeSurroundSound();
}
+//-------------------------------------------------------------------------------------------------
+/**
+ * How long a RIFF/WAVE file runs, in milliseconds, read out of its own header. Zero for anything
+ * that is not one, or that cannot be read.
+ *
+ * This exists because getAudioLengthMS is not a client question. Two script conditions are built on
+ * it - "has this speech finished" and "has this sound finished" - and both run inside GameLogic, so
+ * the answer is part of the simulation. The way it used to be answered was not: Miles was asked to
+ * open the file, and a machine with no working sound device has no handle to open it with, which
+ * made the length zero. So did every -headless run, since headless turns the audio off. Those
+ * machines walked straight past a wait that everyone else sat through - a mismatch in a network
+ * game, and a replay that diverges from the machine that recorded it.
+ *
+ * Every sound and every line of speech the game ships is a .wav, and a .wav says how long it is in
+ * its own header: the byte rate in the format chunk and the size of the data chunk. No device, same
+ * answer everywhere.
+ */
+Real AudioManager::getWaveFileLengthMS( AsciiString strToLoad ) const
+{
+ if (strToLoad.isEmpty())
+ return 0.0f;
+
+ File *file = TheFileSystem->openFile( strToLoad.str(), File::READ | File::BINARY );
+ if (file == NULL)
+ return 0.0f;
+
+ Real lengthMS = 0.0f;
+
+ // RIFF <size> WAVE, then a run of <fourcc><size><payload> chunks.
+ char header[12];
+ if (file->read( header, sizeof(header) ) == sizeof(header)
+ && ::memcmp( header, "RIFF", 4 ) == 0
+ && ::memcmp( header + 8, "WAVE", 4 ) == 0)
+ {
+ UnsignedInt bytesPerSecond = 0;
+ UnsignedInt dataBytes = 0;
+
+ // 'fmt ' can follow 'data' in a legal file, so read to the end rather than stopping at
+ // whichever of the two turns up first.
+ for (;;)
+ {
+ char chunkID[4];
+ UnsignedInt chunkSize = 0;
+ if (file->read( chunkID, sizeof(chunkID) ) != sizeof(chunkID))
+ break;
+ if (file->read( &chunkSize, sizeof(chunkSize) ) != sizeof(chunkSize))
+ break;
+
+ if (::memcmp( chunkID, "fmt ", 4 ) == 0 && chunkSize >= 16)
+ {
+ // wFormatTag, nChannels, nSamplesPerSec, nAvgBytesPerSec, ...
+ char fmt[16];
+ if (file->read( fmt, sizeof(fmt) ) != sizeof(fmt))
+ break;
+ ::memcpy( &bytesPerSecond, fmt + 8, sizeof(bytesPerSecond) );
+ if (chunkSize > sizeof(fmt))
+ file->seek( chunkSize - sizeof(fmt), File::CURRENT );
+ }
+ else if (::memcmp( chunkID, "data", 4 ) == 0)
+ {
+ dataBytes = chunkSize;
+ if (file->seek( chunkSize, File::CURRENT ) < 0)
+ break;
+ }
+ else if (file->seek( chunkSize, File::CURRENT ) < 0)
+ {
+ break;
+ }
+
+ if (chunkSize & 1)
+ file->seek( 1, File::CURRENT ); // chunks are word aligned
+ }
+
+ if (bytesPerSecond > 0 && dataBytes > 0)
+ lengthMS = (Real)dataBytes * 1000.0f / (Real)bytesPerSecond;
+ }
+
+ file->close();
+ return lengthMS;
+}
+
//-------------------------------------------------------------------------------------------------
Real AudioManager::getAudioLengthMS( const AudioEventRTS *event )
{
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
index 05a5e4202..7d531698f 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
@@ -2646,6 +2646,16 @@ Real MilesAudioManager::getFileLengthMS( AsciiString strToLoad ) const
return 0.0f;
}
+ //
+ // The file's own header first, because two script conditions are built on this answer and a
+ // machine with no sound device - a -headless run is one - cannot ask Miles anything. See
+ // AudioManager::getWaveFileLengthMS. Miles still answers for whatever is not a .wav.
+ //
+ const Real fromHeader = getWaveFileLengthMS(strToLoad);
+ if (fromHeader > 0.0f) {
+ return fromHeader;
+ }
+
// Load it as a stream to get the file info without actually opening the file.
HSTREAM stream = AIL_open_stream(m_digitalHandle, strToLoad.str(), 0);
if (!stream) {