Add dumping of iw4 loaded sounds

This commit is contained in:
Jan 2020-10-18 17:41:14 +02:00
parent ac022ee78a
commit 019e772cd2
6 changed files with 130 additions and 3 deletions

View File

@ -0,0 +1,30 @@
#pragma once
#include <cstdint>
#include "Utils/FileUtils.h"
constexpr uint32_t WAV_WAVE_ID = MakeMagic32('W', 'A', 'V', 'E');
constexpr uint32_t WAV_CHUNK_ID_RIFF = MakeMagic32('R', 'I', 'F', 'F');
constexpr uint32_t WAV_CHUNK_ID_FMT = MakeMagic32('f', 'm', 't', ' ');
constexpr uint32_t WAV_CHUNK_ID_DATA = MakeMagic32('d', 'a', 't', 'a');
struct WavChunkHeader
{
uint32_t chunkID;
uint32_t chunkSize;
};
enum class WavFormat : int16_t
{
PCM = 1
};
struct WavFormatChunkPcm
{
WavFormat wFormatTag;
uint16_t nChannels;
uint32_t nSamplesPerSec;
uint32_t nAvgBytesPerSec;
uint16_t nBlockAlign;
uint16_t wBitsPerSample;
};

View File

@ -0,0 +1,70 @@
#include "AssetDumperLoadedSound.h"
#include "Sound/WavTypes.h"
using namespace IW4;
bool AssetDumperLoadedSound::ShouldDump(LoadedSound* asset)
{
return true;
}
std::string AssetDumperLoadedSound::GetFileNameForAsset(Zone* zone, LoadedSound* asset)
{
return "sound/" + std::string(asset->name);
}
void AssetDumperLoadedSound::DumpWavPcm(Zone* zone, LoadedSound* asset, FileAPI::File* out)
{
const uint32_t riffMasterChunkSize = sizeof WAV_CHUNK_ID_RIFF
+ sizeof uint32_t
+ sizeof WAV_WAVE_ID
+ sizeof WavChunkHeader
+ sizeof WavFormatChunkPcm
+ sizeof WavChunkHeader
+ sizeof asset->sound.info.data_len;
out->Write(&WAV_CHUNK_ID_RIFF, sizeof WAV_CHUNK_ID_RIFF, 1);
out->Write(&riffMasterChunkSize, sizeof riffMasterChunkSize, 1);
out->Write(&WAV_WAVE_ID, sizeof WAV_WAVE_ID, 1);
const WavChunkHeader formatChunkHeader
{
WAV_CHUNK_ID_FMT,
sizeof WavFormatChunkPcm
};
out->Write(&formatChunkHeader, sizeof formatChunkHeader, 1);
WavFormatChunkPcm formatChunk
{
WavFormat::PCM,
static_cast<uint16_t>(asset->sound.info.channels),
asset->sound.info.rate,
asset->sound.info.rate * asset->sound.info.channels * asset->sound.info.bits / 8,
static_cast<uint16_t>(asset->sound.info.block_size),
static_cast<uint16_t>(asset->sound.info.bits)
};
out->Write(&formatChunk, sizeof formatChunk, 1);
const WavChunkHeader dataChunkHeader
{
WAV_CHUNK_ID_DATA,
asset->sound.info.data_len
};
out->Write(&dataChunkHeader, sizeof dataChunkHeader, 1);
out->Write(asset->sound.data, 1, asset->sound.info.data_len);
}
void AssetDumperLoadedSound::DumpAsset(Zone* zone, LoadedSound* asset, FileAPI::File* out)
{
switch (static_cast<WavFormat>(asset->sound.info.format))
{
case WavFormat::PCM:
DumpWavPcm(zone, asset, out);
break;
default:
printf("Unknown format %i for loaded sound: %s\n", asset->sound.info.format, asset->name);
break;
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "Dumping/AbstractAssetDumper.h"
#include "Game/IW4/IW4.h"
namespace IW4
{
class AssetDumperLoadedSound final : public AbstractAssetDumper<LoadedSound>
{
static void DumpWavPcm(Zone* zone, LoadedSound* asset, FileAPI::File* out);
protected:
bool ShouldDump(LoadedSound* asset) override;
std::string GetFileNameForAsset(Zone* zone, LoadedSound* asset) override;
void DumpAsset(Zone* zone, LoadedSound* asset, FileAPI::File* out) override;
};
}

View File

@ -7,6 +7,7 @@
#include "AssetDumpers/AssetDumperStringTable.h"
#include "AssetDumpers/AssetDumperLocalizeEntry.h"
#include "AssetDumpers/AssetDumperGfxImage.h"
#include "AssetDumpers/AssetDumperLoadedSound.h"
using namespace IW4;
@ -38,7 +39,6 @@ bool ZoneDumper::DumpZone(Zone* zone, const std::string& basePath) const
DUMP_ASSET_POOL(AssetDumperGfxImage, m_image);
// DUMP_ASSET_POOL(AssetDumpersnd_alias_list_t, m_sound);
// DUMP_ASSET_POOL(AssetDumperSndCurve, m_sound_curve);
// DUMP_ASSET_POOL(AssetDumperLoadedSound, m_loaded_sound);
// DUMP_ASSET_POOL(AssetDumperclipMap_t, m_clip_map);
// DUMP_ASSET_POOL(AssetDumperComWorld, m_com_world);
// DUMP_ASSET_POOL(AssetDumperGameWorldSp, m_game_world_sp);
@ -62,6 +62,7 @@ bool ZoneDumper::DumpZone(Zone* zone, const std::string& basePath) const
// DUMP_ASSET_POOL(AssetDumperTracerDef, m_tracer);
// DUMP_ASSET_POOL(AssetDumperVehicleDef, m_vehicle);
// DUMP_ASSET_POOL(AssetDumperAddonMapEnts, m_addon_map_ents);
DUMP_ASSET_POOL(AssetDumperLoadedSound, m_loaded_sound)
return true;

View File

@ -0,0 +1,10 @@
#pragma once
#include <cstdint>
constexpr uint32_t MakeMagic32(const char ch0, const char ch1, const char ch2, const char ch3)
{
return static_cast<uint32_t>(ch0)
| static_cast<uint32_t>(ch1) << 8
| static_cast<uint32_t>(ch2) << 16
| static_cast<uint32_t>(ch3) << 24;
}

View File

@ -876,7 +876,7 @@ namespace IW4
StringTableCell* values;
};
struct _AILSOUNDINFO
struct AILSOUNDINFO
{
int format;
const void* data_ptr;
@ -891,7 +891,7 @@ namespace IW4
struct MssSound
{
_AILSOUNDINFO info;
AILSOUNDINFO info;
char* data;
};