diff --git a/src/Common/Game/T6/CommonT6.cpp b/src/Common/Game/T6/CommonT6.cpp index 5a54bc8d..34121e0b 100644 --- a/src/Common/Game/T6/CommonT6.cpp +++ b/src/Common/Game/T6/CommonT6.cpp @@ -2,6 +2,8 @@ #include "Utils/Pack.h" +#include + using namespace T6; PackedTexCoords Common::Vec2PackTexCoords(const float (&in)[2]) @@ -33,3 +35,27 @@ void Common::Vec4UnpackGfxColor(const GfxColor& in, float (&out)[4]) { pack32::Vec4UnpackGfxColor(in.packed, out); } + +float Common::LinearToDbspl(const float linear) +{ + const auto db = 20.0f * std::log10(std::max(linear, 0.0000152879f)); + if (db > -95.0f) + return db + 100.0f; + + return 0; +} + +float Common::DbsplToLinear(const float dbsplValue) +{ + return std::pow(10.0f, (dbsplValue - 100.0f) / 20.0f); +} + +float Common::HertzToCents(const float hertz) +{ + return 1200.0f * std::log2(hertz); +} + +float Common::CentsToHertz(const float cents) +{ + return std::pow(2.0f, cents / 1200.0f); +} diff --git a/src/Common/Game/T6/CommonT6.h b/src/Common/Game/T6/CommonT6.h index f32c9d97..212a09d9 100644 --- a/src/Common/Game/T6/CommonT6.h +++ b/src/Common/Game/T6/CommonT6.h @@ -102,5 +102,9 @@ namespace T6 static void Vec2UnpackTexCoords(const PackedTexCoords& in, float (&out)[2]); static void Vec3UnpackUnitVec(const PackedUnitVec& in, float (&out)[3]); static void Vec4UnpackGfxColor(const GfxColor& in, float (&out)[4]); + static float LinearToDbspl(const float linear); + static float DbsplToLinear(const float dbsplValue); + static float HertzToCents(const float hertz); + static float CentsToHertz(const float cents); }; } // namespace T6 diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderSoundBank.cpp b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderSoundBank.cpp index ff16a1cf..1454fc2a 100644 --- a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderSoundBank.cpp +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderSoundBank.cpp @@ -76,16 +76,6 @@ namespace return 0; } - float DbsplToLinear(const float dbsplValue) - { - return std::pow(10.0f, (dbsplValue - 100.0f) / 20.0f); - } - - float CentsToHertz(const float cents) - { - return std::pow(2.0f, cents / 1200.0f); - } - bool ReadColumnString(const std::vector& row, const unsigned columnIndex, const char*& value, MemoryManager& memory) { const auto& cell = row[columnIndex]; @@ -117,7 +107,7 @@ namespace return false; } - value = static_cast(DbsplToLinear(dbsplValue) * static_cast(std::numeric_limits::max())); + value = static_cast(T6::Common::DbsplToLinear(dbsplValue) * static_cast(std::numeric_limits::max())); return true; } @@ -142,7 +132,7 @@ namespace return false; } - value = static_cast(CentsToHertz(centValue) * static_cast(std::numeric_limits::max())); + value = static_cast(T6::Common::CentsToHertz(centValue) * static_cast(std::numeric_limits::max())); return true; } diff --git a/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperSndBank.cpp b/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperSndBank.cpp index bf3d6f13..a691ee01 100644 --- a/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperSndBank.cpp +++ b/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperSndBank.cpp @@ -326,22 +326,6 @@ namespace } } - float LinearToDbspl(float linear) - { - linear = std::max(linear, 0.0000152879f); - - const auto db = 20.0f * std::log10(linear); - if (db > -95.0f) - return db + 100.0f; - - return 0; - } - - float HertzToCents(const float hertz) - { - return 1200.0f * std::log2(hertz); - } - void WriteColumnString(CsvOutputStream& stream, const std::string& stringValue) { stream.WriteColumn(stringValue); @@ -398,21 +382,62 @@ namespace void WriteColumnVolumeLinear(CsvOutputStream& stream, const uint16_t value) { const auto linear = static_cast(value) / static_cast(std::numeric_limits::max()); - const auto dbSpl = std::clamp(LinearToDbspl(linear), 0.0f, 100.0f); - stream.WriteColumn(std::format("{}", std::stof(std::format("{:.0f}", dbSpl)))); + const auto dbSpl = std::clamp(T6::Common::LinearToDbspl(linear), 0.0f, 100.0f); + + float dbSplRound; + for (auto i = 0; i <= 4; i++) + { + dbSplRound = std::stof(std::format("{:.{}f}", dbSpl, i)); + const auto dbSplRoundToValue = + static_cast(T6::Common::DbsplToLinear(dbSplRound) * static_cast(std::numeric_limits::max())); + + if (dbSplRoundToValue == value) + { + break; + } + } + + stream.WriteColumn(std::format("{}", dbSplRound)); } void WriteColumnPitchHertz(CsvOutputStream& stream, const uint16_t value) { const auto hertz = static_cast(value) / static_cast(std::numeric_limits::max()); - const auto cents = std::clamp(HertzToCents(hertz), -2400.0f, 1200.0f); - stream.WriteColumn(std::format("{}", std::stof(std::format("{:.0f}", cents)))); + const auto cents = std::clamp(T6::Common::HertzToCents(hertz), -2400.0f, 1200.0f); + + float centsRound; + for (auto i = 0; i <= 4; i++) + { + centsRound = std::stof(std::format("{:.{}f}", cents, i)); + const auto centsRoundToValue = + static_cast(T6::Common::CentsToHertz(centsRound) * static_cast(std::numeric_limits::max())); + + if (centsRoundToValue == value) + { + break; + } + } + + stream.WriteColumn(std::format("{}", centsRound)); } void WriteColumnNormByte(CsvOutputStream& stream, const uint8_t value) { const auto normValue = static_cast(value) / static_cast(std::numeric_limits::max()); - stream.WriteColumn(std::format("{}", std::stof(std::format("{:.2f}", normValue)))); + + float normValueRound; + for (auto i = 0; i <= 4; i++) + { + normValueRound = std::stof(std::format("{:.{}f}", normValue, i)); + const auto normValueRoundToValue = static_cast(normValueRound * static_cast(std::numeric_limits::max())); + + if (normValueRoundToValue == value) + { + break; + } + } + + stream.WriteColumn(std::format("{}", normValueRound)); } void WriteColumnWithKnownHashes(CsvOutputStream& stream, const std::unordered_map& knownValues, const unsigned value)