refactor: move wave length calculation to util class
This commit is contained in:
parent
659db9b063
commit
5fddfed5ce
8 changed files with 87 additions and 86 deletions
|
|
@ -269,85 +269,6 @@ void Game::testHull (const Vector &start, const Vector &end, int ignoreFlags, in
|
|||
engfuncs.pfnTraceHull (start, end, !!(ignoreFlags & TraceIgnore::Monsters), hullNumber, ignoreEntity, ptr);
|
||||
}
|
||||
|
||||
// helper class for reading wave header
|
||||
class WaveEndianessHelper : public NonCopyable {
|
||||
private:
|
||||
#if defined (CR_ARCH_CPU_BIG_ENDIAN)
|
||||
bool little { false };
|
||||
#else
|
||||
bool little { true };
|
||||
#endif
|
||||
|
||||
public:
|
||||
uint16_t read16 (uint16_t value) {
|
||||
return little ? value : static_cast <uint16_t> ((value >> 8) | (value << 8));
|
||||
}
|
||||
|
||||
uint32_t read32 (uint32_t value) {
|
||||
return little ? value : (((value & 0x000000ff) << 24) | ((value & 0x0000ff00) << 8) | ((value & 0x00ff0000) >> 8) | ((value & 0xff000000) >> 24));
|
||||
}
|
||||
|
||||
bool isWave (char *format) {
|
||||
if (little && memcmp (format, "WAVE", 4) == 0) {
|
||||
return true;
|
||||
}
|
||||
return *reinterpret_cast <uint32_t *> (format) == 0x57415645;
|
||||
}
|
||||
};
|
||||
|
||||
float Game::getWaveLen (const char *fileName) {
|
||||
auto filePath = strings.joinPath (cv_chatter_path.str (), strings.format ("%s.wav", fileName));
|
||||
|
||||
MemFile fp (filePath);
|
||||
|
||||
// we're got valid handle?
|
||||
if (!fp) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
// else fuck with manual search
|
||||
struct WavHeader {
|
||||
char riff[4];
|
||||
uint32_t chunkSize;
|
||||
char wave[4];
|
||||
char fmt[4];
|
||||
uint32_t subchunk1Size;
|
||||
uint16_t audioFormat;
|
||||
uint16_t numChannels;
|
||||
uint32_t sampleRate;
|
||||
uint32_t byteRate;
|
||||
uint16_t blockAlign;
|
||||
uint16_t bitsPerSample;
|
||||
char dataChunkId[4];
|
||||
uint32_t dataChunkLength;
|
||||
} header {};
|
||||
|
||||
WaveEndianessHelper weh;
|
||||
|
||||
if (fp.read (&header, sizeof (WavHeader)) == 0) {
|
||||
logger.error ("Wave File %s - has wrong or unsupported format", filePath);
|
||||
return 0.0f;
|
||||
}
|
||||
fp.close ();
|
||||
|
||||
if (!weh.isWave (header.wave)) {
|
||||
logger.error ("Wave File %s - has wrong wave chunk id", filePath);
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
if (weh.read32 (header.dataChunkLength) == 0) {
|
||||
logger.error ("Wave File %s - has zero length!", filePath);
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
const auto length = static_cast <float> (weh.read32 (header.dataChunkLength));
|
||||
const auto bps = static_cast <float> (weh.read16 (header.bitsPerSample)) / 8;
|
||||
const auto channels = static_cast <float> (weh.read16 (header.numChannels));
|
||||
const auto rate = static_cast <float> (weh.read32 (header.sampleRate));
|
||||
|
||||
return length / bps / channels / rate;
|
||||
}
|
||||
|
||||
bool Game::isDedicated () {
|
||||
// return true if server is dedicated server, false otherwise
|
||||
static const bool dedicated = engfuncs.pfnIsDedicatedServer () > 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue