fix: formula for dbspl and cent values

This commit is contained in:
Jan
2024-10-06 17:59:36 +02:00
parent 70bfd06fdc
commit c1d3138b04
3 changed files with 75 additions and 69 deletions

View File

@ -115,14 +115,40 @@ unsigned int GetAliasSubListCount(const unsigned int startRow, const ParsedCsv&
return count;
}
double DecibelsToAmp(double decibels)
float DbsplToLinear(const float dbsplValue)
{
return std::pow(10.0, decibels / 20.0);
return std::pow(10.0f, (dbsplValue - 100.0f) / 20.0f);
}
double CentsToHertz(double cents)
float CentToHertz(const float cents)
{
return std::numeric_limits<int16_t>::max() * std::pow(2, cents / 1200.0);
return std::pow(2.0f, cents / 1200.0f);
}
bool ReadColumnVolumeDbspl(const float dbsplValue, const char* colName, const unsigned rowIndex, uint16_t& value)
{
if (dbsplValue < 0.0f || dbsplValue > 100.0f)
{
std::cerr << std::format("Invalid value for row {} col '{}' - {} [0.0, 100.0]\n", rowIndex + 1, colName, dbsplValue);
return false;
}
value = static_cast<uint16_t>(DbsplToLinear(dbsplValue) * static_cast<float>(std::numeric_limits<uint16_t>::max()));
return true;
}
bool ReadColumnPitchCents(const float centValue, const char* colName, const unsigned rowIndex, uint16_t& value)
{
if (centValue < -2400.0f || centValue > 1200.0f)
{
std::cerr << std::format("Invalid value for row {} col '{}' - {} [-2400.0, 1200.0]\n", rowIndex + 1, colName, centValue);
return false;
}
value = static_cast<uint16_t>(CentToHertz(centValue) * static_cast<float>(std::numeric_limits<uint16_t>::max()));
return true;
}
bool LoadSoundAlias(MemoryManager* memory, SndAlias* alias, const ParsedCsvRow& row, const unsigned int rowNum)
@ -153,49 +179,17 @@ bool LoadSoundAlias(MemoryManager* memory, SndAlias* alias, const ParsedCsvRow&
alias->duck = Common::SND_HashName(row.GetValue("duck").data());
const auto volMinDecibels = row.GetValueFloat("vol_min");
if (volMinDecibels < 0.0f || volMinDecibels > 100.0f)
{
std::cerr << std::format("Invalid value for row {} col 'vol_min' - {} [0.0, 100.0]\n", rowNum + 1, volMinDecibels);
if (!ReadColumnVolumeDbspl(row.GetValueFloat("vol_min"), "vol_min", rowNum, alias->volMin))
return false;
}
const auto volMinAmp = static_cast<int>(std::round(DecibelsToAmp(volMinDecibels) / T6::Common::AMP_RATIO));
alias->volMin = volMinAmp;
const auto volMaxDecibels = row.GetValueFloat("vol_max");
if (volMaxDecibels < 0.0f || volMaxDecibels > 100.0f)
{
std::cerr << std::format("Invalid value for row {} col 'vol_max' - {} [0.0, 100.0]\n", rowNum + 1, volMaxDecibels);
if (!ReadColumnVolumeDbspl(row.GetValueFloat("vol_max"), "vol_max", rowNum, alias->volMax))
return false;
}
const auto volMaxAmp = static_cast<int>(std::round(DecibelsToAmp(volMaxDecibels) / T6::Common::AMP_RATIO));
alias->volMax = volMaxAmp;
const auto pitchMinCents = row.GetValueFloat("pitch_min");
if (pitchMinCents < -2400.0f || pitchMinCents > 1200.0f)
{
std::cerr << std::format("Invalid value for row {} col 'pitch_min' - {} [-2400.0, 1200.0]\n", rowNum + 1, pitchMinCents);
if (!ReadColumnVolumeDbspl(row.GetValueFloat("pitch_min"), "pitch_min", rowNum, alias->pitchMin))
return false;
}
const auto pitchMinHertz = static_cast<int>(std::round(CentsToHertz(pitchMinCents)));
alias->pitchMin = pitchMinHertz;
const auto pitchMaxCents = row.GetValueFloat("pitch_max");
if (pitchMaxCents < -2400.0f || pitchMaxCents > 1200.0f)
{
std::cerr << std::format("Invalid value for row {} col 'pitch_max' - {} [-2400.0, 1200.0]\n", rowNum + 1, pitchMaxCents);
if (!ReadColumnVolumeDbspl(row.GetValueFloat("pitch_max"), "pitch_max", rowNum, alias->pitchMax))
return false;
}
const auto pitchMaxHertz = static_cast<int>(std::round(CentsToHertz(pitchMaxCents)));
alias->pitchMax = pitchMaxHertz;
alias->distMin = row.GetValueInt<decltype(alias->distMin)>("dist_min");
alias->distMax = row.GetValueInt<decltype(alias->distMax)>("dist_max");
@ -208,11 +202,19 @@ bool LoadSoundAlias(MemoryManager* memory, SndAlias* alias, const ParsedCsvRow&
alias->maxPriorityThreshold = row.GetValueInt<decltype(alias->maxPriorityThreshold)>("max_priority_threshold");
alias->probability = row.GetValueInt<decltype(alias->probability)>("probability");
alias->startDelay = row.GetValueInt<decltype(alias->startDelay)>("start_delay");
alias->reverbSend = row.GetValueInt<decltype(alias->reverbSend)>("reverb_send");
alias->centerSend = row.GetValueInt<decltype(alias->centerSend)>("center_send");
if (!ReadColumnVolumeDbspl(row.GetValueFloat("reverb_send"), "reverb_send", rowNum, alias->reverbSend))
return false;
if (!ReadColumnVolumeDbspl(row.GetValueFloat("center_send"), "center_send", rowNum, alias->centerSend))
return false;
alias->envelopMin = row.GetValueInt<decltype(alias->envelopMin)>("envelop_min");
alias->envelopMax = row.GetValueInt<decltype(alias->envelopMax)>("envelop_max");
alias->envelopPercentage = row.GetValueInt<decltype(alias->envelopPercentage)>("envelop_percentage");
if (!ReadColumnVolumeDbspl(row.GetValueFloat("envelop_percentage"), "envelop_percentage", rowNum, alias->envelopPercentage))
return false;
alias->occlusionLevel = row.GetValueInt<decltype(alias->occlusionLevel)>("occlusion_level");
alias->fluxTime = row.GetValueInt<decltype(alias->fluxTime)>("move_time");
alias->futzPatch = row.GetValueInt<decltype(alias->futzPatch)>("futz");