mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-05-09 14:04:57 +00:00
Increase rounded sound alias float value precision until it is equal to initial value
This commit is contained in:
parent
93a35f5dcf
commit
ed06700f99
@ -2,6 +2,8 @@
|
||||
|
||||
#include "Utils/Pack.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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<CsvCell>& row, const unsigned columnIndex, const char*& value, MemoryManager& memory)
|
||||
{
|
||||
const auto& cell = row[columnIndex];
|
||||
@ -117,7 +107,7 @@ namespace
|
||||
return false;
|
||||
}
|
||||
|
||||
value = static_cast<uint16_t>(DbsplToLinear(dbsplValue) * static_cast<float>(std::numeric_limits<uint16_t>::max()));
|
||||
value = static_cast<uint16_t>(T6::Common::DbsplToLinear(dbsplValue) * static_cast<float>(std::numeric_limits<uint16_t>::max()));
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -142,7 +132,7 @@ namespace
|
||||
return false;
|
||||
}
|
||||
|
||||
value = static_cast<uint16_t>(CentsToHertz(centValue) * static_cast<float>(std::numeric_limits<int16_t>::max()));
|
||||
value = static_cast<uint16_t>(T6::Common::CentsToHertz(centValue) * static_cast<float>(std::numeric_limits<int16_t>::max()));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -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<float>(value) / static_cast<float>(std::numeric_limits<uint16_t>::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<uint16_t>(T6::Common::DbsplToLinear(dbSplRound) * static_cast<float>(std::numeric_limits<uint16_t>::max()));
|
||||
|
||||
if (dbSplRoundToValue == value)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
stream.WriteColumn(std::format("{}", dbSplRound));
|
||||
}
|
||||
|
||||
void WriteColumnPitchHertz(CsvOutputStream& stream, const uint16_t value)
|
||||
{
|
||||
const auto hertz = static_cast<float>(value) / static_cast<float>(std::numeric_limits<int16_t>::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<uint16_t>(T6::Common::CentsToHertz(centsRound) * static_cast<float>(std::numeric_limits<int16_t>::max()));
|
||||
|
||||
if (centsRoundToValue == value)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
stream.WriteColumn(std::format("{}", centsRound));
|
||||
}
|
||||
|
||||
void WriteColumnNormByte(CsvOutputStream& stream, const uint8_t value)
|
||||
{
|
||||
const auto normValue = static_cast<float>(value) / static_cast<float>(std::numeric_limits<uint8_t>::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<uint8_t>(normValueRound * static_cast<float>(std::numeric_limits<uint8_t>::max()));
|
||||
|
||||
if (normValueRoundToValue == value)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
stream.WriteColumn(std::format("{}", normValueRound));
|
||||
}
|
||||
|
||||
void WriteColumnWithKnownHashes(CsvOutputStream& stream, const std::unordered_map<unsigned, std::string>& knownValues, const unsigned value)
|
||||
|
Loading…
x
Reference in New Issue
Block a user