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
|
|
@ -162,9 +162,6 @@ public:
|
|||
// test line
|
||||
void testHull (const Vector &start, const Vector &end, int ignoreFlags, int hullNumber, edict_t *ignoreEntity, TraceResult *ptr);
|
||||
|
||||
// get's the wave length
|
||||
float getWaveLen (const char *fileName);
|
||||
|
||||
// we are on dedicated server ?
|
||||
bool isDedicated ();
|
||||
|
||||
|
|
|
|||
|
|
@ -105,6 +105,9 @@ public:
|
|||
// get the current date and time as string
|
||||
String getCurrentDateTime ();
|
||||
|
||||
// get's the wave length
|
||||
float getWaveLength (StringRef filename);
|
||||
|
||||
public:
|
||||
|
||||
// re-show welcome after changelevel ?
|
||||
|
|
|
|||
|
|
@ -1170,7 +1170,7 @@ int Bot::pickBestWeapon (Array <int> &vec, int moneySave) {
|
|||
for (const auto &w : vec) {
|
||||
const auto &weapon = tab[w];
|
||||
|
||||
// if wea have enough money for weapon buy it
|
||||
// if weaзщт have enough money for weapon buy it
|
||||
if (weapon.price + moneySave < m_moneyAmount + rg.get (50, 200) && rg.chance (chance)) {
|
||||
return w;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -348,7 +348,7 @@ void BotConfig::loadChatterConfig () {
|
|||
|
||||
for (auto &sound : sentences) {
|
||||
sound.trim ().trim ("\"");
|
||||
auto duration = game.getWaveLen (sound.chars ());
|
||||
const auto duration = util.getWaveLength (sound.chars ());
|
||||
|
||||
if (duration > 0.0f) {
|
||||
m_chatter[event.code].emplace (cr::move (sound), event.repeat, duration);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -582,7 +582,7 @@ void BotManager::serverFill (int selection, int personality, int difficulty, int
|
|||
selection = 5;
|
||||
}
|
||||
constexpr char teams[6][12] = { "", {"Terrorists"}, {"CTs"}, "", "", {"Random"}, };
|
||||
auto toAdd = numToAdd == -1 ? maxClients - (getHumansCount () + getBotCount ()) : numToAdd;
|
||||
const auto toAdd = numToAdd == -1 ? maxClients - (getHumansCount () + getBotCount ()) : numToAdd;
|
||||
|
||||
for (int i = 0; i <= toAdd; ++i) {
|
||||
addbot ("", difficulty, personality, selection, -1, true);
|
||||
|
|
|
|||
|
|
@ -322,7 +322,7 @@ String BotStorage::buildPath (int32_t file, bool isMemoryLoad) {
|
|||
path.emplace (game.getRunningModName ());
|
||||
}
|
||||
|
||||
// allways append addons/product
|
||||
// always append addons/product
|
||||
path.emplace (folders.addons);
|
||||
path.emplace (folders.bot);
|
||||
|
||||
|
|
|
|||
|
|
@ -635,3 +635,83 @@ StringRef BotSupport::weaponIdToAlias (int32_t id) {
|
|||
}
|
||||
return none;
|
||||
}
|
||||
|
||||
|
||||
// helper class for reading wave header
|
||||
class WaveEndianessHelper final : 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) const {
|
||||
if (little && memcmp (format, "WAVE", 4) == 0) {
|
||||
return true;
|
||||
}
|
||||
return *reinterpret_cast <uint32_t *> (format) == 0x57415645;
|
||||
}
|
||||
};
|
||||
|
||||
float BotSupport::getWaveLength (StringRef 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 {};
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue