mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
Improve sound bank code
This commit is contained in:
parent
20af6c4ba5
commit
d29dc082e2
@ -1,6 +1,6 @@
|
|||||||
#include "SoundBank.h"
|
#include "SoundBank.h"
|
||||||
|
|
||||||
#include "Utils/FileUtils.h"
|
#include "Utils/ObjStream.h"
|
||||||
#include "zlib.h"
|
#include "zlib.h"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -114,7 +114,7 @@ SoundBankEntryInputStream::SoundBankEntryInputStream()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SoundBankEntryInputStream::SoundBankEntryInputStream(std::unique_ptr<std::istream> stream, SoundAssetBankEntry entry)
|
SoundBankEntryInputStream::SoundBankEntryInputStream(std::unique_ptr<std::istream> stream, const SoundAssetBankEntry& entry)
|
||||||
: m_stream(std::move(stream)),
|
: m_stream(std::move(stream)),
|
||||||
m_entry(entry)
|
m_entry(entry)
|
||||||
{
|
{
|
||||||
@ -122,7 +122,7 @@ SoundBankEntryInputStream::SoundBankEntryInputStream(std::unique_ptr<std::istrea
|
|||||||
|
|
||||||
bool SoundBankEntryInputStream::IsOpen() const
|
bool SoundBankEntryInputStream::IsOpen() const
|
||||||
{
|
{
|
||||||
return m_stream.get() != nullptr;
|
return static_cast<bool>(m_stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SoundBank::ReadHeader()
|
bool SoundBank::ReadHeader()
|
||||||
@ -134,15 +134,15 @@ bool SoundBank::ReadHeader()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_header.magic != MAGIC)
|
if (m_header.magic != sndbank_consts::MAGIC)
|
||||||
{
|
{
|
||||||
std::cout << "Invalid sndbank magic 0x" << std::hex << m_header.magic << std::endl;
|
std::cout << "Invalid sndbank magic 0x" << std::hex << m_header.magic << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_header.version != VERSION)
|
if (m_header.version != sndbank_consts::VERSION)
|
||||||
{
|
{
|
||||||
std::cout << "Unsupported sndbank version " << m_header.version << " (should be " << VERSION << ")" << std::endl;
|
std::cout << "Unsupported sndbank version " << m_header.version << " (should be " << sndbank_consts::VERSION << ")" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,13 +159,14 @@ bool SoundBank::ReadHeader()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_header.entryCount && (m_header.entryOffset <= 0 || m_header.entryOffset + sizeof(SoundAssetBankEntry) * m_header.entryCount > m_file_size))
|
if (m_header.entryCount
|
||||||
|
&& (m_header.entryOffset <= 0 || m_header.entryOffset + static_cast<int64_t>(sizeof(SoundAssetBankEntry) * m_header.entryCount) > m_file_size))
|
||||||
{
|
{
|
||||||
std::cout << "Invalid sndbank entry offset " << m_header.entryOffset << " (filesize is " << m_file_size << ")" << std::endl;
|
std::cout << "Invalid sndbank entry offset " << m_header.entryOffset << " (filesize is " << m_file_size << ")" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_header.checksumOffset <= 0 || m_header.checksumOffset + sizeof(SoundAssetBankChecksum) * m_header.entryCount > m_file_size)
|
if (m_header.checksumOffset <= 0 || m_header.checksumOffset + static_cast<int64_t>(sizeof(SoundAssetBankChecksum) * m_header.entryCount) > m_file_size)
|
||||||
{
|
{
|
||||||
std::cout << "Invalid sndbank checksum offset " << m_header.checksumOffset << " (filesize is " << m_file_size << ")" << std::endl;
|
std::cout << "Invalid sndbank checksum offset " << m_header.checksumOffset << " (filesize is " << m_file_size << ")" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
|
@ -3,10 +3,7 @@
|
|||||||
#include "ObjContainer/ObjContainerReferenceable.h"
|
#include "ObjContainer/ObjContainerReferenceable.h"
|
||||||
#include "ObjContainer/ObjContainerRepository.h"
|
#include "ObjContainer/ObjContainerRepository.h"
|
||||||
#include "ObjContainer/SoundBank/SoundBankTypes.h"
|
#include "ObjContainer/SoundBank/SoundBankTypes.h"
|
||||||
#include "SearchPath/ISearchPath.h"
|
|
||||||
#include "Utils/ClassUtils.h"
|
#include "Utils/ClassUtils.h"
|
||||||
#include "Utils/FileUtils.h"
|
|
||||||
#include "Utils/ObjStream.h"
|
|
||||||
#include "Zone/Zone.h"
|
#include "Zone/Zone.h"
|
||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
@ -14,20 +11,42 @@
|
|||||||
class SoundBankEntryInputStream
|
class SoundBankEntryInputStream
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<std::istream> m_stream;
|
|
||||||
SoundAssetBankEntry m_entry;
|
|
||||||
|
|
||||||
SoundBankEntryInputStream();
|
SoundBankEntryInputStream();
|
||||||
SoundBankEntryInputStream(std::unique_ptr<std::istream> stream, SoundAssetBankEntry entry);
|
SoundBankEntryInputStream(std::unique_ptr<std::istream> stream, const SoundAssetBankEntry& entry);
|
||||||
|
|
||||||
_NODISCARD bool IsOpen() const;
|
_NODISCARD bool IsOpen() const;
|
||||||
|
|
||||||
|
std::unique_ptr<std::istream> m_stream;
|
||||||
|
SoundAssetBankEntry m_entry;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SoundBank final : public ObjContainerReferenceable
|
class SoundBank final : public ObjContainerReferenceable
|
||||||
{
|
{
|
||||||
static constexpr uint32_t MAGIC = FileUtils::MakeMagic32('2', 'U', 'X', '#');
|
bool ReadHeader();
|
||||||
static constexpr uint32_t VERSION = 14u;
|
bool ReadEntries();
|
||||||
|
bool ReadChecksums();
|
||||||
|
|
||||||
|
public:
|
||||||
|
SoundBank(std::string fileName, std::unique_ptr<std::istream> stream, int64_t fileSize);
|
||||||
|
~SoundBank() override = default;
|
||||||
|
SoundBank(const SoundBank& other) = delete;
|
||||||
|
SoundBank(SoundBank&& other) noexcept = default;
|
||||||
|
SoundBank& operator=(const SoundBank& other) = delete;
|
||||||
|
SoundBank& operator=(SoundBank&& other) noexcept = default;
|
||||||
|
|
||||||
|
static std::string GetFileNameForDefinition(bool streamed, const char* zone, const char* language);
|
||||||
|
|
||||||
|
std::string GetName() override;
|
||||||
|
|
||||||
|
bool Initialize();
|
||||||
|
_NODISCARD const std::vector<std::string>& GetDependencies() const;
|
||||||
|
|
||||||
|
_NODISCARD bool VerifyChecksum(const SoundAssetBankChecksum& checksum) const;
|
||||||
|
_NODISCARD SoundBankEntryInputStream GetEntryStream(unsigned int id) const;
|
||||||
|
|
||||||
|
static ObjContainerRepository<SoundBank, Zone> Repository;
|
||||||
|
|
||||||
|
private:
|
||||||
std::string m_file_name;
|
std::string m_file_name;
|
||||||
std::unique_ptr<std::istream> m_stream;
|
std::unique_ptr<std::istream> m_stream;
|
||||||
int64_t m_file_size;
|
int64_t m_file_size;
|
||||||
@ -38,28 +57,4 @@ class SoundBank final : public ObjContainerReferenceable
|
|||||||
std::vector<SoundAssetBankEntry> m_entries;
|
std::vector<SoundAssetBankEntry> m_entries;
|
||||||
std::vector<SoundAssetBankChecksum> m_checksums;
|
std::vector<SoundAssetBankChecksum> m_checksums;
|
||||||
std::unordered_map<unsigned int, size_t> m_entries_by_id;
|
std::unordered_map<unsigned int, size_t> m_entries_by_id;
|
||||||
|
|
||||||
bool ReadHeader();
|
|
||||||
bool ReadEntries();
|
|
||||||
bool ReadChecksums();
|
|
||||||
|
|
||||||
public:
|
|
||||||
static ObjContainerRepository<SoundBank, Zone> Repository;
|
|
||||||
|
|
||||||
static std::string GetFileNameForDefinition(bool streamed, const char* zone, const char* language);
|
|
||||||
|
|
||||||
SoundBank(std::string fileName, std::unique_ptr<std::istream> stream, int64_t fileSize);
|
|
||||||
~SoundBank() override = default;
|
|
||||||
SoundBank(const SoundBank& other) = delete;
|
|
||||||
SoundBank(SoundBank&& other) noexcept = default;
|
|
||||||
SoundBank& operator=(const SoundBank& other) = delete;
|
|
||||||
SoundBank& operator=(SoundBank&& other) noexcept = default;
|
|
||||||
|
|
||||||
std::string GetName() override;
|
|
||||||
|
|
||||||
bool Initialize();
|
|
||||||
_NODISCARD const std::vector<std::string>& GetDependencies() const;
|
|
||||||
|
|
||||||
_NODISCARD bool VerifyChecksum(const SoundAssetBankChecksum& checksum) const;
|
|
||||||
_NODISCARD SoundBankEntryInputStream GetEntryStream(unsigned int id) const;
|
|
||||||
};
|
};
|
||||||
|
@ -1,30 +1,32 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Utils/FileUtils.h"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
class SoundBankConsts
|
namespace sndbank_consts
|
||||||
{
|
{
|
||||||
SoundBankConsts() = default;
|
static constexpr uint32_t MAGIC = FileUtils::MakeMagic32('2', 'U', 'X', '#');
|
||||||
|
static constexpr uint32_t VERSION = 14u;
|
||||||
|
|
||||||
public:
|
|
||||||
static constexpr unsigned OFFSET_DATA_START = 0x800;
|
static constexpr unsigned OFFSET_DATA_START = 0x800;
|
||||||
};
|
}; // namespace sndbank_consts
|
||||||
|
|
||||||
struct SoundAssetBankChecksum
|
struct SoundAssetBankChecksum
|
||||||
{
|
{
|
||||||
char checksumBytes[16];
|
uint8_t checksumBytes[16];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SoundAssetBankHeader
|
struct SoundAssetBankHeader
|
||||||
{
|
{
|
||||||
unsigned int magic; // + 0x0
|
uint32_t magic; // + 0x0
|
||||||
unsigned int version; // + 0x4
|
uint32_t version; // + 0x4
|
||||||
unsigned int entrySize; // + 0x8
|
uint32_t entrySize; // + 0x8
|
||||||
unsigned int checksumSize; // + 0xC
|
uint32_t checksumSize; // + 0xC
|
||||||
unsigned int dependencySize; // + 0x10
|
uint32_t dependencySize; // + 0x10
|
||||||
unsigned int entryCount; // + 0x14
|
uint32_t entryCount; // + 0x14
|
||||||
unsigned int dependencyCount; // + 0x18
|
uint32_t dependencyCount; // + 0x18
|
||||||
unsigned int pad32; // + 0x1C
|
uint32_t pad32; // + 0x1C
|
||||||
int64_t fileSize; // + 0x20
|
int64_t fileSize; // + 0x20
|
||||||
int64_t entryOffset; // + 0x28
|
int64_t entryOffset; // + 0x28
|
||||||
int64_t checksumOffset; // + 0x30
|
int64_t checksumOffset; // + 0x30
|
||||||
@ -34,12 +36,12 @@ struct SoundAssetBankHeader
|
|||||||
|
|
||||||
struct SoundAssetBankEntry
|
struct SoundAssetBankEntry
|
||||||
{
|
{
|
||||||
unsigned int id;
|
uint32_t id;
|
||||||
unsigned int size;
|
uint32_t size;
|
||||||
unsigned int offset;
|
uint32_t offset;
|
||||||
unsigned int frameCount;
|
uint32_t frameCount;
|
||||||
unsigned char frameRateIndex;
|
uint8_t frameRateIndex;
|
||||||
unsigned char channelCount;
|
uint8_t channelCount;
|
||||||
unsigned char looping;
|
uint8_t looping;
|
||||||
unsigned char format;
|
uint8_t format;
|
||||||
};
|
};
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "Sound/WavWriter.h"
|
#include "Sound/WavWriter.h"
|
||||||
#include "Utils/ClassUtils.h"
|
#include "Utils/ClassUtils.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user