Added sample rate when exporting

This commit is contained in:
KiritoDv
2024-11-21 21:55:34 -06:00
parent a9662a158f
commit 79e1721ebc
6 changed files with 211 additions and 11 deletions
+2
View File
@@ -1,6 +1,7 @@
#include "AudioContext.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include "Utils/StringHelper.h"
std::unordered_map<AudioTableType, std::unordered_map<uint32_t, AudioTableEntry>> AudioContext::tables;
std::unordered_map<AudioTableType, std::vector<uint8_t>> AudioContext::data;
@@ -62,6 +63,7 @@ TunedSample AudioContext::LoadTunedSample(LUS::BinaryReader& reader, uint32_t pa
node["offset"] = parent + sampleAddr;
node["tuning"] = tuning;
node["sampleBankId"] = sampleBankId;
node["symbol"] = StringHelper::Sprintf("Sample_P_%X_O_%X_B_%d", parent, parent + sampleAddr, sampleBankId);
Companion::Instance->AddAsset(node);
return { parent + sampleAddr, sampleBankId, tuning };
+13 -11
View File
@@ -88,18 +88,20 @@ void AudioConverter::SampleToAIFC(NSampleData* sample, LUS::BinaryWriter &out) {
std::vector<uint8_t> data(sampleData, sampleData + sample->size);
uint32_t num_frames = data.size() * 16 / 9;
uint32_t sample_rate = 0;
uint32_t sample_rate = sample->sampleRate;
if (sample->tuning <= 0.5f) {
sample_rate = 16000;
} else if (sample->tuning <= 1.0f) {
sample_rate = 32000;
} else if (sample->tuning <= 1.5f) {
sample_rate = 48000;
} else if (sample->tuning <= 2.5f) {
sample_rate = 80000;
} else {
sample_rate = 16000 * sample->tuning;
if(sample_rate == 0){
if (sample->tuning <= 0.5f) {
sample_rate = 16000;
} else if (sample->tuning <= 1.0f) {
sample_rate = 32000;
} else if (sample->tuning <= 1.5f) {
sample_rate = 48000;
} else if (sample->tuning <= 2.5f) {
sample_rate = 80000;
} else {
sample_rate = 16000 * sample->tuning;
}
}
int16_t num_channels = 1;
@@ -62,6 +62,7 @@ std::optional<std::shared_ptr<IParsedData>> NSampleFactory::parse(std::vector<ui
auto offset = GetSafeNode<uint32_t>(node, "offset");
auto parent = GetSafeNode<uint32_t>(node, "parent");
auto tuning = GetSafeNode<float>(node, "tuning", 1.0f);
auto sampleRate = GetSafeNode<uint32_t>(node, "sampleRate", 0);
auto sampleBankId = GetSafeNode<uint32_t>(node, "sampleBankId");
auto entry = AudioContext::tables[AudioTableType::FONT_TABLE].at(parent);
+3
View File
@@ -13,7 +13,10 @@ public:
uint32_t sampleAddr;
uint32_t loop;
uint32_t book;
// Custom fields
uint32_t sampleBankId;
uint32_t sampleRate;
float tuning;
};
+160
View File
@@ -0,0 +1,160 @@
#include "StringHelper.h"
#if (_MSC_VER)
#pragma optimize("2", on)
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _MSC_VER
#define vsprintf_s vsprintf
#endif
std::vector<std::string> StringHelper::Split(std::string s, const std::string& delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}
res.push_back(s.substr(pos_start));
return res;
}
std::vector<std::string_view> StringHelper::Split(std::string_view s, const std::string& delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string_view token;
std::vector<std::string_view> res;
while ((pos_end = s.find(delimiter, pos_start)) != std::string_view::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}
res.push_back(s.substr(pos_start));
return res;
}
std::string StringHelper::Strip(std::string s, const std::string& delimiter) {
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
s.erase(pos, pos + delimiter.length());
}
return s;
}
std::string StringHelper::Replace(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
while (start_pos != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos = str.find(from);
}
return str;
}
void StringHelper::ReplaceOriginal(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
while (start_pos != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos = str.find(from);
}
}
bool StringHelper::StartsWith(const std::string& s, const std::string& input) {
#if __cplusplus >= 202002L
return s.starts_with(input.c_str());
#else
return s.rfind(input, 0) == 0;
#endif
}
bool StringHelper::Contains(const std::string& s, const std::string& input) {
return s.find(input) != std::string::npos;
}
bool StringHelper::EndsWith(const std::string& s, const std::string& input) {
size_t inputLen = strlen(input.c_str());
return s.rfind(input) == (s.size() - inputLen);
}
std::string StringHelper::Sprintf(const char* format, ...) {
char buffer[32768];
// char buffer[2048];
std::string output;
va_list va;
va_start(va, format);
vsprintf_s(buffer, format, va);
va_end(va);
output = buffer;
return output;
}
std::string StringHelper::Implode(std::vector<std::string>& elements, const char* const separator) {
return "";
// return std::accumulate(std::begin(elements), std::end(elements), std::string(),
//[separator](std::string& ss, std::string& s) {
// return ss.empty() ? s : ss + separator + s;
//});
}
int64_t StringHelper::StrToL(const std::string& str, int32_t base) {
return std::strtoull(str.c_str(), nullptr, base);
}
std::string StringHelper::BoolStr(bool b) {
return b ? "true" : "false";
}
bool StringHelper::HasOnlyDigits(const std::string& str) {
return std::all_of(str.begin(), str.end(), ::isdigit);
}
// Validate a hex string based on the c89 standard
// https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Integer-Constants
bool StringHelper::IsValidHex(std::string_view str) {
if (str.length() < 3) {
return false;
}
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
return std::all_of(str.begin() + 2, str.end(), ::isxdigit);
}
return false;
}
bool StringHelper::IsValidHex(const std::string& str) {
return IsValidHex(std::string_view(str.c_str()));
}
bool StringHelper::IsValidOffset(std::string_view str) {
if (str.length() == 1) {
// 0 is a valid offset
return isdigit(str[0]);
}
return IsValidHex(str);
}
bool StringHelper::IsValidOffset(const std::string& str) {
if (str.length() == 1) {
// 0 is a valid offset
return isdigit(str[0]);
}
return IsValidHex(str);
}
bool StringHelper::IEquals(const std::string& a, const std::string& b) {
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char a, char b) { return tolower(a) == tolower(b); });
}
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include <algorithm>
#include <cstdarg>
#include <cstring>
#include <numeric>
#include <string>
#include <string_view>
#include <vector>
#include <cstdint>
class StringHelper {
public:
static std::vector<std::string> Split(std::string s, const std::string& delimiter);
static std::vector<std::string_view> Split(std::string_view s, const std::string& delimiter);
static std::string Strip(std::string s, const std::string& delimiter);
static std::string Replace(std::string str, const std::string& from, const std::string& to);
static void ReplaceOriginal(std::string& str, const std::string& from, const std::string& to);
static bool StartsWith(const std::string& s, const std::string& input);
static bool Contains(const std::string& s, const std::string& input);
static bool EndsWith(const std::string& s, const std::string& input);
static std::string Sprintf(const char* format, ...);
static std::string Implode(std::vector<std::string>& elements, const char* const separator);
static int64_t StrToL(const std::string& str, int32_t base = 10);
static std::string BoolStr(bool b);
static bool HasOnlyDigits(const std::string& str);
static bool IsValidHex(std::string_view str);
static bool IsValidHex(const std::string& str);
static bool IsValidOffset(std::string_view str);
static bool IsValidOffset(const std::string& str);
static bool IEquals(const std::string& a, const std::string& b);
};