2016-05-03 01:16:26 +00:00
|
|
|
#include "amuse/AudioGroup.hpp"
|
2019-08-25 10:28:43 +00:00
|
|
|
|
2018-08-25 08:34:04 +00:00
|
|
|
#include <regex>
|
2019-08-25 10:28:43 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "amuse/AudioGroupData.hpp"
|
|
|
|
|
2018-08-25 08:34:04 +00:00
|
|
|
#include <athena/FileReader.hpp>
|
2019-07-20 04:23:25 +00:00
|
|
|
#include <fmt/ostream.h>
|
2019-08-25 10:28:43 +00:00
|
|
|
|
2018-08-25 08:34:04 +00:00
|
|
|
|
|
|
|
using namespace std::literals;
|
2016-05-03 01:16:26 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
namespace amuse {
|
2016-05-03 01:16:26 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void AudioGroup::assign(const AudioGroupData& data) {
|
|
|
|
m_pool = AudioGroupPool::CreateAudioGroupPool(data);
|
|
|
|
m_proj = AudioGroupProject::CreateAudioGroupProject(data);
|
|
|
|
m_sdir = AudioGroupSampleDirectory::CreateAudioGroupSampleDirectory(data);
|
|
|
|
m_samp = data.getSamp();
|
2018-07-18 07:39:26 +00:00
|
|
|
}
|
2021-06-30 18:15:40 +00:00
|
|
|
void AudioGroup::assign(std::string_view groupPath) {
|
2018-12-08 05:20:09 +00:00
|
|
|
/* Reverse order when loading intermediates */
|
|
|
|
m_groupPath = groupPath;
|
|
|
|
m_sdir = AudioGroupSampleDirectory::CreateAudioGroupSampleDirectory(groupPath);
|
|
|
|
m_pool = AudioGroupPool::CreateAudioGroupPool(groupPath);
|
|
|
|
m_proj = AudioGroupProject::CreateAudioGroupProject(groupPath);
|
|
|
|
m_samp = nullptr;
|
2018-07-18 07:39:26 +00:00
|
|
|
}
|
2021-06-30 18:15:40 +00:00
|
|
|
void AudioGroup::assign(const AudioGroup& data, std::string_view groupPath) {
|
2018-12-08 05:20:09 +00:00
|
|
|
/* Reverse order when loading intermediates */
|
|
|
|
m_groupPath = groupPath;
|
|
|
|
m_sdir = AudioGroupSampleDirectory::CreateAudioGroupSampleDirectory(groupPath);
|
|
|
|
m_pool = AudioGroupPool::CreateAudioGroupPool(groupPath);
|
|
|
|
m_proj = AudioGroupProject::CreateAudioGroupProject(data.getProj());
|
|
|
|
m_samp = nullptr;
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
2016-05-28 02:28:59 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
const SampleEntry* AudioGroup::getSample(SampleId sfxId) const {
|
|
|
|
auto search = m_sdir.m_entries.find(sfxId);
|
|
|
|
if (search == m_sdir.m_entries.cend())
|
|
|
|
return nullptr;
|
|
|
|
return search->second.get();
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 18:15:40 +00:00
|
|
|
std::string AudioGroup::getSampleBasePath(SampleId sfxId) const {
|
|
|
|
return m_groupPath + '/' + SampleId::CurNameDB->resolveNameFromId(sfxId).data();
|
2018-07-31 08:04:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
std::pair<ObjToken<SampleEntryData>, const unsigned char*> AudioGroup::getSampleData(SampleId sfxId,
|
|
|
|
const SampleEntry* sample) const {
|
|
|
|
if (sample->m_data->m_looseData) {
|
2021-06-30 18:15:40 +00:00
|
|
|
std::string basePath = getSampleBasePath(sfxId);
|
2018-12-08 05:20:09 +00:00
|
|
|
const_cast<SampleEntry*>(sample)->loadLooseData(basePath);
|
|
|
|
return {sample->m_data, sample->m_data->m_looseData.get()};
|
|
|
|
}
|
|
|
|
return {sample->m_data, m_samp + sample->m_data->m_sampleOff};
|
2018-07-16 07:41:15 +00:00
|
|
|
}
|
2018-07-31 08:04:43 +00:00
|
|
|
|
2021-06-30 18:15:40 +00:00
|
|
|
SampleFileState AudioGroup::getSampleFileState(SampleId sfxId, const SampleEntry* sample, std::string* pathOut) const {
|
2018-12-08 05:20:09 +00:00
|
|
|
if (sample->m_data->m_looseData) {
|
2021-06-30 18:15:40 +00:00
|
|
|
std::string basePath = getSampleBasePath(sfxId);
|
2018-12-08 05:20:09 +00:00
|
|
|
return sample->getFileState(basePath, pathOut);
|
|
|
|
}
|
|
|
|
if (sample->m_data->isFormatDSP() || sample->m_data->getSampleFormat() == SampleFormat::N64)
|
|
|
|
return SampleFileState::MemoryOnlyCompressed;
|
|
|
|
return SampleFileState::MemoryOnlyWAV;
|
2018-07-31 08:04:43 +00:00
|
|
|
}
|
2018-08-03 03:45:48 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void AudioGroup::patchSampleMetadata(SampleId sfxId, const SampleEntry* sample) const {
|
|
|
|
if (sample->m_data->m_looseData) {
|
2021-06-30 18:15:40 +00:00
|
|
|
std::string basePath = getSampleBasePath(sfxId);
|
2018-12-08 05:20:09 +00:00
|
|
|
sample->patchSampleMetadata(basePath);
|
|
|
|
}
|
2018-08-03 03:45:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void AudioGroup::makeWAVVersion(SampleId sfxId, const SampleEntry* sample) const {
|
|
|
|
if (sample->m_data->m_looseData) {
|
|
|
|
m_sdir._extractWAV(sfxId, *sample->m_data, m_groupPath, sample->m_data->m_looseData.get());
|
|
|
|
}
|
2018-08-03 03:45:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void AudioGroup::makeCompressedVersion(SampleId sfxId, const SampleEntry* sample) const {
|
|
|
|
if (sample->m_data->m_looseData) {
|
|
|
|
m_sdir._extractCompressed(sfxId, *sample->m_data, m_groupPath, sample->m_data->m_looseData.get(), true);
|
|
|
|
}
|
2018-08-03 03:45:48 +00:00
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void AudioGroupDatabase::renameSample(SampleId id, std::string_view str) {
|
2021-06-30 18:15:40 +00:00
|
|
|
std::string oldBasePath = getSampleBasePath(id);
|
2018-12-08 05:20:09 +00:00
|
|
|
SampleId::CurNameDB->rename(id, str);
|
2021-06-30 18:15:40 +00:00
|
|
|
std::string newBasePath = getSampleBasePath(id);
|
|
|
|
Rename((oldBasePath + ".wav").c_str(), (newBasePath + ".wav").c_str());
|
|
|
|
Rename((oldBasePath + ".dsp").c_str(), (newBasePath + ".dsp").c_str());
|
|
|
|
Rename((oldBasePath + ".vadpcm").c_str(), (newBasePath + ".vadpcm").c_str());
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void AudioGroupDatabase::deleteSample(SampleId id) {
|
2021-06-30 18:15:40 +00:00
|
|
|
std::string basePath = getSampleBasePath(id);
|
|
|
|
Unlink((basePath + ".wav").c_str());
|
|
|
|
Unlink((basePath + ".dsp").c_str());
|
|
|
|
Unlink((basePath + ".vadpcm").c_str());
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 18:15:40 +00:00
|
|
|
void AudioGroupDatabase::copySampleInto(const std::string& basePath, const std::string& newBasePath) {
|
|
|
|
Copy((basePath + ".wav").c_str(), (newBasePath + ".wav").c_str());
|
|
|
|
Copy((basePath + ".dsp").c_str(), (newBasePath + ".dsp").c_str());
|
|
|
|
Copy((basePath + ".vadpcm").c_str(), (newBasePath + ".vadpcm").c_str());
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioGroupDatabase::_recursiveRenameMacro(SoundMacroId id, std::string_view str, int& macroIdx,
|
2018-12-08 05:20:09 +00:00
|
|
|
std::unordered_set<SoundMacroId>& renamedIds) {
|
|
|
|
if (renamedIds.find(id) != renamedIds.cend())
|
|
|
|
return;
|
|
|
|
if (const SoundMacro* macro = getPool().soundMacro(id)) {
|
|
|
|
if (!strncmp(SoundMacroId::CurNameDB->resolveNameFromId(id).data(), "macro", 5)) {
|
|
|
|
std::string macroName("macro"sv);
|
2019-07-20 04:23:25 +00:00
|
|
|
if (macroIdx)
|
2020-04-11 22:49:30 +00:00
|
|
|
macroName += fmt::format(FMT_STRING("{}"), macroIdx);
|
2018-12-08 05:20:09 +00:00
|
|
|
macroName += '_';
|
|
|
|
macroName += str;
|
|
|
|
++macroIdx;
|
|
|
|
SoundMacroId::CurNameDB->rename(id, macroName);
|
|
|
|
renamedIds.insert(id);
|
|
|
|
int sampleIdx = 0;
|
|
|
|
for (const auto& cmd : macro->m_cmds) {
|
|
|
|
switch (cmd->Isa()) {
|
|
|
|
case SoundMacro::CmdOp::StartSample: {
|
|
|
|
SoundMacro::CmdStartSample* ss = static_cast<SoundMacro::CmdStartSample*>(cmd.get());
|
|
|
|
if (!strncmp(SampleId::CurNameDB->resolveNameFromId(ss->sample.id).data(), "sample", 6)) {
|
|
|
|
std::string sampleName("sample"sv);
|
2019-07-20 04:23:25 +00:00
|
|
|
if (sampleIdx)
|
2020-04-11 22:49:30 +00:00
|
|
|
sampleName += fmt::format(FMT_STRING("{}"), sampleIdx);
|
2018-12-08 05:20:09 +00:00
|
|
|
sampleName += '_';
|
|
|
|
sampleName += macroName;
|
|
|
|
++sampleIdx;
|
|
|
|
renameSample(ss->sample.id, sampleName);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SoundMacro::CmdOp::SplitKey:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdSplitKey*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
case SoundMacro::CmdOp::SplitVel:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdSplitVel*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
case SoundMacro::CmdOp::Goto:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdGoto*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
case SoundMacro::CmdOp::PlayMacro:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdPlayMacro*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
case SoundMacro::CmdOp::SplitMod:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdSplitMod*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
case SoundMacro::CmdOp::SplitRnd:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdSplitRnd*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
case SoundMacro::CmdOp::GoSub:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdGoSub*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
case SoundMacro::CmdOp::TrapEvent:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdTrapEvent*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
case SoundMacro::CmdOp::SendMessage:
|
|
|
|
_recursiveRenameMacro(static_cast<SoundMacro::CmdSendMessage*>(cmd.get())->macro, str, macroIdx, renamedIds);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
static const std::regex DefineGRPEntry(R"(#define\s+GRP(\S+)\s+(\S+))", std::regex::ECMAScript | std::regex::optimize);
|
|
|
|
static const std::regex DefineSNGEntry(R"(#define\s+SNG(\S+)\s+(\S+))", std::regex::ECMAScript | std::regex::optimize);
|
|
|
|
static const std::regex DefineSFXEntry(R"(#define\s+SFX(\S+)\s+(\S+))", std::regex::ECMAScript | std::regex::optimize);
|
2018-08-25 08:34:04 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
void AudioGroupDatabase::importCHeader(std::string_view header) {
|
|
|
|
std::match_results<std::string_view::const_iterator> dirMatch;
|
|
|
|
auto begin = header.cbegin();
|
|
|
|
auto end = header.cend();
|
|
|
|
while (std::regex_search(begin, end, dirMatch, DefineGRPEntry)) {
|
|
|
|
std::string key = dirMatch[1].str();
|
|
|
|
std::string value = dirMatch[2].str();
|
|
|
|
char* endPtr;
|
|
|
|
amuse::ObjectId id;
|
|
|
|
id.id = strtoul(value.c_str(), &endPtr, 0);
|
|
|
|
if (endPtr == value.c_str())
|
|
|
|
continue;
|
|
|
|
GroupId::CurNameDB->rename(id, key);
|
|
|
|
begin = dirMatch.suffix().first;
|
|
|
|
}
|
|
|
|
begin = header.cbegin();
|
|
|
|
end = header.cend();
|
|
|
|
while (std::regex_search(begin, end, dirMatch, DefineSNGEntry)) {
|
|
|
|
std::string key = dirMatch[1].str();
|
|
|
|
std::string value = dirMatch[2].str();
|
|
|
|
char* endPtr;
|
|
|
|
amuse::ObjectId id;
|
|
|
|
id.id = strtoul(value.c_str(), &endPtr, 0);
|
|
|
|
if (endPtr == value.c_str())
|
|
|
|
continue;
|
|
|
|
SongId::CurNameDB->rename(id, key);
|
|
|
|
begin = dirMatch.suffix().first;
|
|
|
|
}
|
|
|
|
begin = header.cbegin();
|
|
|
|
end = header.cend();
|
|
|
|
std::unordered_set<SoundMacroId> renamedMacroIDs;
|
|
|
|
while (std::regex_search(begin, end, dirMatch, DefineSFXEntry)) {
|
|
|
|
std::string key = dirMatch[1].str();
|
|
|
|
std::string value = dirMatch[2].str();
|
|
|
|
char* endPtr;
|
|
|
|
amuse::ObjectId id;
|
|
|
|
id.id = strtoul(value.c_str(), &endPtr, 0);
|
|
|
|
if (endPtr == value.c_str())
|
|
|
|
continue;
|
|
|
|
SFXId::CurNameDB->rename(id, key);
|
|
|
|
int macroIdx = 0;
|
|
|
|
for (auto& sfxGrp : getProj().sfxGroups()) {
|
|
|
|
for (auto& sfx : sfxGrp.second->m_sfxEntries) {
|
|
|
|
if (sfx.first == id) {
|
|
|
|
ObjectId sfxObjId = sfx.second.objId;
|
|
|
|
if (sfxObjId == ObjectId() || sfxObjId.id & 0xc000)
|
2018-08-25 08:34:04 +00:00
|
|
|
continue;
|
2018-12-08 05:20:09 +00:00
|
|
|
_recursiveRenameMacro(sfxObjId, key, macroIdx, renamedMacroIDs);
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
begin = dirMatch.suffix().first;
|
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 04:23:25 +00:00
|
|
|
static void WriteDefineLine(std::stringstream& ret, std::string_view typeStr, std::string_view name, ObjectId id) {
|
2020-04-11 22:49:30 +00:00
|
|
|
fmt::print(ret, FMT_STRING("#define {}{} 0x{}\n"), typeStr, name, id);
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
std::string AudioGroupDatabase::exportCHeader(std::string_view projectName, std::string_view groupName) const {
|
2019-07-20 04:23:25 +00:00
|
|
|
std::stringstream ret;
|
|
|
|
ret <<
|
2018-12-08 05:20:09 +00:00
|
|
|
"/* Auto-generated Amuse Defines\n"
|
|
|
|
" *\n"
|
|
|
|
" * Project: "sv;
|
2019-07-20 04:23:25 +00:00
|
|
|
ret << projectName;
|
|
|
|
ret <<
|
2018-12-08 05:20:09 +00:00
|
|
|
"\n"
|
|
|
|
" * Subproject: "sv;
|
2019-07-20 04:23:25 +00:00
|
|
|
ret << groupName;
|
|
|
|
ret <<
|
2018-12-08 05:20:09 +00:00
|
|
|
"\n"
|
|
|
|
" * Date: "sv;
|
|
|
|
time_t curTime = time(nullptr);
|
2018-08-26 07:58:04 +00:00
|
|
|
#ifndef _WIN32
|
2018-12-08 05:20:09 +00:00
|
|
|
struct tm curTm;
|
|
|
|
localtime_r(&curTime, &curTm);
|
|
|
|
char curTmStr[26];
|
|
|
|
asctime_r(&curTm, curTmStr);
|
2018-08-26 07:58:04 +00:00
|
|
|
#else
|
2018-12-08 05:20:09 +00:00
|
|
|
struct tm curTm;
|
|
|
|
localtime_s(&curTm, &curTime);
|
|
|
|
char curTmStr[26];
|
|
|
|
asctime_s(curTmStr, &curTm);
|
2018-08-26 07:58:04 +00:00
|
|
|
#endif
|
2018-12-08 05:20:09 +00:00
|
|
|
if (char* ch = strchr(curTmStr, '\n'))
|
|
|
|
*ch = '\0';
|
2019-07-20 04:23:25 +00:00
|
|
|
ret << curTmStr;
|
|
|
|
ret <<
|
2018-12-08 05:20:09 +00:00
|
|
|
"\n"
|
|
|
|
" */\n\n\n"sv;
|
2018-08-25 08:34:04 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
bool addLF = false;
|
|
|
|
for (const auto& sg : SortUnorderedMap(getProj().songGroups())) {
|
|
|
|
auto name = amuse::GroupId::CurNameDB->resolveNameFromId(sg.first);
|
|
|
|
WriteDefineLine(ret, "GRP"sv, name, sg.first);
|
|
|
|
addLF = true;
|
|
|
|
}
|
|
|
|
for (const auto& sg : SortUnorderedMap(getProj().sfxGroups())) {
|
|
|
|
auto name = amuse::GroupId::CurNameDB->resolveNameFromId(sg.first);
|
|
|
|
WriteDefineLine(ret, "GRP"sv, name, sg.first);
|
|
|
|
addLF = true;
|
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
if (addLF)
|
2019-07-20 04:23:25 +00:00
|
|
|
ret << "\n\n"sv;
|
2018-12-08 05:20:09 +00:00
|
|
|
addLF = false;
|
2018-08-25 08:34:04 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
std::unordered_set<amuse::SongId> songIds;
|
|
|
|
for (const auto& sg : getProj().songGroups())
|
|
|
|
for (const auto& song : sg.second->m_midiSetups)
|
|
|
|
songIds.insert(song.first);
|
|
|
|
for (amuse::SongId id : SortUnorderedSet(songIds)) {
|
|
|
|
auto name = amuse::SongId::CurNameDB->resolveNameFromId(id);
|
|
|
|
WriteDefineLine(ret, "SNG"sv, name, id);
|
|
|
|
addLF = true;
|
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
if (addLF)
|
2019-07-20 04:23:25 +00:00
|
|
|
ret << "\n\n"sv;
|
2018-12-08 05:20:09 +00:00
|
|
|
addLF = false;
|
2018-08-25 08:34:04 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
for (const auto& sg : SortUnorderedMap(getProj().sfxGroups())) {
|
|
|
|
for (const auto& sfx : SortUnorderedMap(sg.second.get()->m_sfxEntries)) {
|
|
|
|
auto name = amuse::SFXId::CurNameDB->resolveNameFromId(sfx.first);
|
|
|
|
WriteDefineLine(ret, "SFX"sv, name, sfx.first.id);
|
|
|
|
addLF = true;
|
2018-08-25 08:34:04 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
}
|
2018-08-25 08:34:04 +00:00
|
|
|
|
2018-12-08 05:20:09 +00:00
|
|
|
if (addLF)
|
2019-07-20 04:23:25 +00:00
|
|
|
ret << "\n\n"sv;
|
2018-09-03 00:43:05 +00:00
|
|
|
|
2019-07-20 04:23:25 +00:00
|
|
|
return ret.str();
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
2018-12-08 05:20:09 +00:00
|
|
|
} // namespace amuse
|