#include "assets/sound_bank.hpp" #include "assets/rnc.hpp" #include "core/log.hpp" #include #include #include #include namespace top_octane::assets { namespace { inline uint16_t read_le16(const uint8_t* p) { return static_cast(p[0] | (static_cast(p[1]) << 8)); } inline int16_t read_le16s(const uint8_t* p) { return static_cast(read_le16(p)); } inline uint32_t read_le32(const uint8_t* p) { return static_cast(p[0]) | (static_cast(p[1]) << 8) | (static_cast(p[2]) << 16) | (static_cast(p[3]) << 24); } bool eq_icase(const std::string& a, const std::string& b) { if (a.size() != b.size()) return false; for (size_t i = 0; i < a.size(); ++i) { if (std::tolower(static_cast(a[i])) != std::tolower(static_cast(b[i]))) { return false; } } return true; } } // namespace bool decode_wav_bytes(const uint8_t* data, size_t size, SoundSample& out_sample) { if (!data || size < 44 || std::memcmp(data, "RIFF", 4) != 0 || std::memcmp(data + 8, "WAVE", 4) != 0) { return false; } uint16_t audio_fmt = 1; uint16_t channels = 1; uint32_t sample_rate = 22050; uint16_t bits_per_sample = 8; const uint8_t* pcm_ptr = nullptr; uint32_t pcm_size = 0; size_t pos = 12; while (pos + 8 <= size) { const uint8_t* cid = data + pos; uint32_t csz = read_le32(data + pos + 4); pos += 8; if (pos + csz > size) csz = static_cast(size - pos); if (std::memcmp(cid, "fmt ", 4) == 0 && csz >= 16) { audio_fmt = read_le16(data + pos + 0); channels = read_le16(data + pos + 2); sample_rate = read_le32(data + pos + 4); bits_per_sample = read_le16(data + pos + 14); } else if (std::memcmp(cid, "data", 4) == 0) { pcm_ptr = data + pos; pcm_size = csz; } pos += csz + (csz & 1u); } if (!pcm_ptr || audio_fmt != 1 || channels == 0 || sample_rate == 0) { return false; } out_sample.sample_rate = static_cast(sample_rate); out_sample.channels = static_cast(channels); out_sample.pcm_mono.clear(); if (bits_per_sample == 8) { size_t frames = pcm_size / channels; out_sample.pcm_mono.resize(frames); for (size_t i = 0; i < frames; ++i) { float sum = 0.0f; for (uint16_t ch = 0; ch < channels; ++ch) { sum += (static_cast(pcm_ptr[i * channels + ch]) - 128.0f) / 128.0f; } out_sample.pcm_mono[i] = sum / static_cast(channels); } } else if (bits_per_sample == 16) { size_t frames = pcm_size / (2u * channels); out_sample.pcm_mono.resize(frames); for (size_t i = 0; i < frames; ++i) { float sum = 0.0f; for (uint16_t ch = 0; ch < channels; ++ch) { sum += static_cast(read_le16s(pcm_ptr + (i * channels + ch) * 2u)) / 32768.0f; } out_sample.pcm_mono[i] = sum / static_cast(channels); } } else { return false; } if (!out_sample.pcm_mono.empty()) { double sum_sq = 0.0; float pk = 0.0f; for (float x : out_sample.pcm_mono) { sum_sq += static_cast(x) * static_cast(x); pk = std::max(pk, std::abs(x)); } out_sample.rms = static_cast(std::sqrt(sum_sq / static_cast(out_sample.pcm_mono.size()))); out_sample.peak = pk; } else { out_sample.rms = 0.0f; out_sample.peak = 0.0f; } return true; } bool SoundBank::load(const Vfs& vfs, const std::string& path) { std::vector raw; if (!vfs.read_file(path, raw) || raw.size() < 256) { LOG_ERROR("Failed to read sound archive: %s", path.c_str()); return false; } uint32_t fat_off = read_le32(raw.data() + raw.size() - 4); if (fat_off + 12 + 10 * 16 > raw.size()) { LOG_ERROR("Invalid SOUND.DAT tail FAT offset: %u", fat_off); return false; } auto load_bank = [&](int bank_idx, std::vector& out_list) -> bool { size_t b_off = static_cast(fat_off) + 12u + static_cast(bank_idx) * 16u; uint32_t off_fat = read_le32(raw.data() + b_off + 0); uint32_t off_data = read_le32(raw.data() + b_off + 4); uint32_t len_fat = read_le32(raw.data() + b_off + 8); uint32_t len_data = read_le32(raw.data() + b_off + 12); if (off_fat == 0xFFFFFFFFu || off_fat + len_fat > raw.size() || off_data >= raw.size()) { LOG_ERROR("Invalid bank %d offsets in SOUND.DAT", bank_idx); return false; } // Decompress Bank payload once if RNC-packed (Bank 3), else slice directly (Bank 9) std::vector unpacked; const uint8_t* bank_buf = nullptr; size_t bank_sz = 0; if (off_data + 18 <= raw.size() && std::memcmp(raw.data() + off_data, "RNC\x01", 4) == 0) { if (!rnc_unpack_bytes(raw.data() + off_data, raw.size() - off_data, unpacked)) { LOG_ERROR("Failed to RNC-unpack SOUND.DAT bank %d", bank_idx); return false; } bank_buf = unpacked.data(); bank_sz = unpacked.size(); } else { if (off_data + len_data > raw.size()) return false; bank_buf = raw.data() + off_data; bank_sz = len_data; } size_t count = len_fat / 32u; out_list.clear(); for (size_t i = 1; i < count; ++i) { const uint8_t* ent = raw.data() + off_fat + i * 32u; char name_buf[19]{}; std::memcpy(name_buf, ent, 18); uint32_t s_off = read_le32(ent + 18); uint32_t s_sz = read_le32(ent + 26); SoundSample smp; smp.name = name_buf; if (s_off + s_sz <= bank_sz && s_sz >= 44) { if (decode_wav_bytes(bank_buf + s_off, s_sz, smp)) { // Loudness-normalize 8-bit DOS SFX toward uniform ~0.18 RMS without clipping peaks if (smp.rms > 1e-4f) { float g = std::min(std::clamp(0.18f / smp.rms, 0.35f, 1.85f), 0.88f / std::max(smp.peak, 1e-4f)); for (float& x : smp.pcm_mono) { x = std::clamp(x * g, -0.90f, 0.90f); } smp.rms *= g; smp.peak = std::min(smp.peak * g, 0.90f); } } else { LOG_WARN("Failed to decode WAV %s in SOUND.DAT bank %d", smp.name.c_str(), bank_idx); } } out_list.push_back(std::move(smp)); } return !out_list.empty(); }; if (!load_bank(3, gameplay_sfx) || !load_bank(9, intro_sfx)) { return false; } return true; } const SoundSample* SoundBank::find_gameplay(const std::string& name) const { for (const auto& s : gameplay_sfx) { if (eq_icase(s.name, name)) return &s; } return nullptr; } const SoundSample* SoundBank::find_intro(const std::string& name) const { for (const auto& s : intro_sfx) { if (eq_icase(s.name, name)) return &s; } return nullptr; } } // namespace top_octane::assets