amuse/driver/amuserender.cpp

450 lines
14 KiB
C++
Raw Normal View History

2016-07-06 21:30:46 +00:00
#include "amuse/amuse.hpp"
#include "amuse/BooBackend.hpp"
#include "athena/FileReader.hpp"
#include "boo/boo.hpp"
#include "boo/audiodev/IAudioVoiceEngine.hpp"
#include "logvisor/logvisor.hpp"
2019-06-12 02:02:52 +00:00
#include <optional>
2017-12-29 07:57:22 +00:00
#include <cstdio>
#include <cstring>
2016-07-07 04:10:02 +00:00
#include <signal.h>
2016-07-06 21:30:46 +00:00
#include <thread>
#include <map>
2016-07-07 04:19:40 +00:00
#include <set>
2016-07-06 21:30:46 +00:00
#include <vector>
#include <unordered_map>
2017-12-29 07:57:22 +00:00
#include <cstdarg>
2016-07-06 21:30:46 +00:00
static logvisor::Module Log("amuserender");
#if _WIN32
#include <DbgHelp.h>
#pragma comment(lib, "Dbghelp.lib")
#include <signal.h>
2018-12-08 05:20:09 +00:00
static void abortHandler(int signum) {
unsigned int i;
void* stack[100];
unsigned short frames;
SYMBOL_INFO* symbol;
HANDLE process;
process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);
frames = CaptureStackBackTrace(0, 100, stack, NULL);
symbol = (SYMBOL_INFO*)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
for (i = 0; i < frames; i++) {
SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol);
printf("%i: %s - 0x%0llX", frames - i - 1, symbol->Name, symbol->Address);
DWORD dwDisplacement;
IMAGEHLP_LINE64 line;
SymSetOptions(SYMOPT_LOAD_LINES);
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if (SymGetLineFromAddr64(process, (DWORD64)(stack[i]), &dwDisplacement, &line)) {
// SymGetLineFromAddr64 returned success
printf(" LINE %d\n", line.LineNumber);
} else {
printf("\n");
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
}
2016-07-06 21:30:46 +00:00
2018-12-08 05:20:09 +00:00
free(symbol);
2016-07-06 21:30:46 +00:00
2018-12-08 05:20:09 +00:00
// If you caught one of the above signals, it is likely you just
// want to quit your program right now.
system("PAUSE");
exit(signum);
2016-07-06 21:30:46 +00:00
}
#endif
/* SIGINT will gracefully break write loop */
static bool g_BreakLoop = false;
2016-07-14 04:54:46 +00:00
static void SIGINTHandler(int sig) { g_BreakLoop = true; }
2016-07-06 21:30:46 +00:00
#if _WIN32
int wmain(int argc, const boo::SystemChar** argv)
#else
int main(int argc, const boo::SystemChar** argv)
#endif
{
2018-12-08 05:20:09 +00:00
logvisor::RegisterConsoleLogger();
std::vector<boo::SystemString> m_args;
m_args.reserve(argc);
double rate = NativeSampleRate;
int chCount = 2;
double volume = 1.0;
for (int i = 1; i < argc; ++i) {
2016-07-06 21:45:41 +00:00
#if _WIN32
2018-12-08 05:20:09 +00:00
if (!wcsncmp(argv[i], L"-r", 2)) {
if (argv[i][2])
rate = wcstod(&argv[i][2], nullptr);
else if (argc > (i + 1)) {
rate = wcstod(argv[i + 1], nullptr);
++i;
}
} else if (!wcsncmp(argv[i], L"-c", 2)) {
if (argv[i][2])
chCount = wcstoul(&argv[i][2], nullptr, 0);
else if (argc > (i + 1)) {
chCount = wcstoul(argv[i + 1], nullptr, 0);
++i;
}
} else if (!wcsncmp(argv[i], L"-v", 2)) {
if (argv[i][2])
volume = wcstod(&argv[i][2], nullptr);
else if (argc > (i + 1)) {
volume = wcstod(argv[i + 1], nullptr);
++i;
}
} else
m_args.push_back(argv[i]);
2016-07-06 21:45:41 +00:00
#else
2018-12-08 05:20:09 +00:00
if (!strncmp(argv[i], "-r", 2)) {
if (argv[i][2])
rate = strtod(&argv[i][2], nullptr);
else if (argc > (i + 1)) {
rate = strtod(argv[i + 1], nullptr);
++i;
}
} else if (!strncmp(argv[i], "-c", 2)) {
if (argv[i][2])
chCount = strtoul(&argv[i][2], nullptr, 0);
else if (argc > (i + 1)) {
chCount = strtoul(argv[i + 1], nullptr, 0);
++i;
}
} else if (!strncmp(argv[i], "-v", 2)) {
if (argv[i][2])
volume = strtod(&argv[i][2], nullptr);
else if (argc > (i + 1)) {
volume = strtod(argv[i + 1], nullptr);
++i;
}
} else
m_args.push_back(argv[i]);
2016-07-06 21:45:41 +00:00
#endif
2018-12-08 05:20:09 +00:00
}
/* Load data */
if (m_args.size() < 1) {
Log.report(logvisor::Error,
2020-04-11 22:49:30 +00:00
FMT_STRING("Usage: amuserender <group-file> [<songs-file>] [-r <sample-rate>] [-c <channel-count>] [-v <volume "
2019-07-20 04:23:25 +00:00
"0.0-1.0>]"));
2018-12-08 05:20:09 +00:00
return 1;
}
amuse::ContainerRegistry::Type cType = amuse::ContainerRegistry::DetectContainerType(m_args[0].c_str());
if (cType == amuse::ContainerRegistry::Type::Invalid) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("invalid/no data at path argument"));
2018-12-08 05:20:09 +00:00
return 1;
}
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Info, FMT_STRING(_SYS_STR("Found '{}' Audio Group data")), amuse::ContainerRegistry::TypeToName(cType));
2018-12-08 05:20:09 +00:00
std::vector<std::pair<amuse::SystemString, amuse::IntrusiveAudioGroupData>> data =
amuse::ContainerRegistry::LoadContainer(m_args[0].c_str());
if (data.empty()) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("invalid/no data at path argument"));
2018-12-08 05:20:09 +00:00
return 1;
}
int m_groupId = -1;
int m_setupId = -1;
const amuse::SystemString* m_groupName = nullptr;
const amuse::SystemString* m_songName = nullptr;
amuse::ContainerRegistry::SongData* m_arrData = nullptr;
bool m_sfxGroup = false;
std::list<amuse::AudioGroupProject> m_projs;
2019-07-20 04:23:25 +00:00
std::map<amuse::GroupId, std::pair<std::pair<amuse::SystemString, amuse::IntrusiveAudioGroupData>*,
amuse::ObjToken<amuse::SongGroupIndex>>>
2018-12-08 05:20:09 +00:00
allSongGroups;
2019-07-20 04:23:25 +00:00
std::map<amuse::GroupId, std::pair<std::pair<amuse::SystemString, amuse::IntrusiveAudioGroupData>*,
amuse::ObjToken<amuse::SFXGroupIndex>>>
2018-12-08 05:20:09 +00:00
allSFXGroups;
size_t totalGroups = 0;
for (auto& grp : data) {
/* Load project to assemble group list */
m_projs.push_back(amuse::AudioGroupProject::CreateAudioGroupProject(grp.second));
amuse::AudioGroupProject& proj = m_projs.back();
totalGroups += proj.sfxGroups().size() + proj.songGroups().size();
for (auto it = proj.songGroups().begin(); it != proj.songGroups().end(); ++it)
allSongGroups[it->first] = std::make_pair(&grp, it->second);
for (auto it = proj.sfxGroups().begin(); it != proj.sfxGroups().end(); ++it)
allSFXGroups[it->first] = std::make_pair(&grp, it->second);
}
/* Attempt loading song */
std::vector<std::pair<amuse::SystemString, amuse::ContainerRegistry::SongData>> songs;
if (m_args.size() > 1)
songs = amuse::ContainerRegistry::LoadSongs(m_args[1].c_str());
else
songs = amuse::ContainerRegistry::LoadSongs(m_args[0].c_str());
if (songs.size()) {
bool play = true;
if (m_args.size() <= 1) {
bool prompt = true;
while (true) {
if (prompt) {
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("Render Song? (Y/N): "));
2018-12-08 05:20:09 +00:00
prompt = false;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
char userSel;
if (scanf("%c", &userSel) <= 0 || userSel == '\n')
continue;
userSel = tolower(userSel);
if (userSel == 'n')
play = false;
else if (userSel != 'y') {
prompt = true;
continue;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
break;
}
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
if (play) {
/* Get song selection from user */
if (songs.size() > 1) {
/* Ask user to specify which song */
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("Multiple Songs discovered:\n"));
2018-12-08 05:20:09 +00:00
int idx = 0;
for (const auto& pair : songs) {
const amuse::ContainerRegistry::SongData& sngData = pair.second;
int16_t grpId = sngData.m_groupId;
int16_t setupId = sngData.m_setupId;
if (sngData.m_groupId == -1 && sngData.m_setupId != -1) {
for (const auto& pair : allSongGroups) {
for (const auto& setup : pair.second.second->m_midiSetups) {
if (setup.first == sngData.m_setupId) {
2019-07-20 04:23:25 +00:00
grpId = pair.first.id;
2018-12-08 05:20:09 +00:00
break;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
}
if (grpId != -1)
2016-07-06 21:30:46 +00:00
break;
2018-12-08 05:20:09 +00:00
}
}
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING(_SYS_STR(" {} {} (Group {}, Setup {})\n")), idx++, pair.first, grpId, setupId);
2016-07-06 21:30:46 +00:00
}
int userSel = 0;
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("Enter Song Number: "));
2018-12-08 05:20:09 +00:00
if (scanf("%d", &userSel) <= 0) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to parse prompt"));
2018-12-08 05:20:09 +00:00
return 1;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
if (userSel < songs.size()) {
m_arrData = &songs[userSel].second;
m_groupId = m_arrData->m_groupId;
m_setupId = m_arrData->m_setupId;
m_songName = &songs[userSel].first;
} else {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to find Song {}"), userSel);
2018-12-08 05:20:09 +00:00
return 1;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
} else if (songs.size() == 1) {
m_arrData = &songs[0].second;
m_groupId = m_arrData->m_groupId;
m_setupId = m_arrData->m_setupId;
m_songName = &songs[0].first;
}
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
}
/* Get group selection via setup search */
if (m_groupId == -1 && m_setupId != -1) {
for (const auto& pair : allSongGroups) {
for (const auto& setup : pair.second.second->m_midiSetups) {
if (setup.first == m_setupId) {
2019-07-20 04:23:25 +00:00
m_groupId = pair.first.id;
2018-12-08 05:20:09 +00:00
m_groupName = &pair.second.first->first;
break;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
}
if (m_groupId != -1)
break;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
}
2016-07-06 21:30:46 +00:00
2018-12-08 05:20:09 +00:00
/* Get group selection via user */
if (m_groupId != -1) {
2016-07-06 21:30:46 +00:00
auto songSearch = allSongGroups.find(m_groupId);
2018-12-08 05:20:09 +00:00
auto sfxSearch = allSFXGroups.find(m_groupId);
if (songSearch != allSongGroups.end()) {
m_sfxGroup = false;
m_groupName = &songSearch->second.first->first;
} else if (sfxSearch != allSFXGroups.end()) {
m_sfxGroup = true;
m_groupName = &sfxSearch->second.first->first;
} else {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to find Group {}"), m_groupId);
2018-12-08 05:20:09 +00:00
return 1;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
} else if (totalGroups > 1) {
/* Ask user to specify which group in project */
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("Multiple Audio Groups discovered:\n"));
2018-12-08 05:20:09 +00:00
for (const auto& pair : allSFXGroups) {
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING(_SYS_STR(" {} {} (SFXGroup) {} sfx-entries\n")), pair.first,
2019-07-20 04:23:25 +00:00
pair.second.first->first, pair.second.second->m_sfxEntries.size());
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
for (const auto& pair : allSongGroups) {
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING(_SYS_STR(" {} {} (SongGroup) {} normal-pages, {} drum-pages, {} MIDI-setups\n")),
2019-07-20 04:23:25 +00:00
pair.first, pair.second.first->first, pair.second.second->m_normPages.size(),
pair.second.second->m_drumPages.size(), pair.second.second->m_midiSetups.size());
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
int userSel = 0;
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("Enter Group Number: "));
2018-12-08 05:20:09 +00:00
if (scanf("%d", &userSel) <= 0) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to parse prompt"));
2018-12-08 05:20:09 +00:00
return 1;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
auto songSearch = allSongGroups.find(userSel);
auto sfxSearch = allSFXGroups.find(userSel);
if (songSearch != allSongGroups.end()) {
m_groupId = userSel;
m_groupName = &songSearch->second.first->first;
m_sfxGroup = false;
} else if (sfxSearch != allSFXGroups.end()) {
m_groupId = userSel;
m_groupName = &sfxSearch->second.first->first;
m_sfxGroup = true;
} else {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to find Group {}"), userSel);
2018-12-08 05:20:09 +00:00
return 1;
}
} else if (totalGroups == 1) {
/* Load one and only group */
if (allSongGroups.size()) {
const auto& pair = *allSongGroups.cbegin();
2019-07-20 04:23:25 +00:00
m_groupId = pair.first.id;
2018-12-08 05:20:09 +00:00
m_groupName = &pair.second.first->first;
m_sfxGroup = false;
} else {
const auto& pair = *allSFXGroups.cbegin();
2019-07-20 04:23:25 +00:00
m_groupId = pair.first.id;
2018-12-08 05:20:09 +00:00
m_groupName = &pair.second.first->first;
m_sfxGroup = true;
}
} else {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("empty project"));
2018-12-08 05:20:09 +00:00
return 1;
}
/* Make final group selection */
amuse::IntrusiveAudioGroupData* selData = nullptr;
amuse::ObjToken<amuse::SongGroupIndex> songIndex;
amuse::ObjToken<amuse::SFXGroupIndex> sfxIndex;
auto songSearch = allSongGroups.find(m_groupId);
if (songSearch != allSongGroups.end()) {
selData = &songSearch->second.first->second;
songIndex = songSearch->second.second;
2019-07-20 04:23:25 +00:00
std::set<amuse::SongId> sortSetups;
2018-12-08 05:20:09 +00:00
for (auto& pair : songIndex->m_midiSetups)
sortSetups.insert(pair.first);
if (m_setupId == -1) {
/* Ask user to specify which group in project */
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("Multiple MIDI Setups:\n"));
2019-07-20 04:23:25 +00:00
for (auto setup : sortSetups)
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING(" {}\n"), setup);
2018-12-08 05:20:09 +00:00
int userSel = 0;
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("Enter Setup Number: "));
2018-12-08 05:20:09 +00:00
if (scanf("%d", &userSel) <= 0) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to parse prompt"));
2018-05-28 20:26:55 +00:00
return 1;
2018-12-08 05:20:09 +00:00
}
m_setupId = userSel;
2016-07-06 21:30:46 +00:00
}
2018-12-08 05:20:09 +00:00
if (sortSetups.find(m_setupId) == sortSetups.cend()) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to find setup {}"), m_setupId);
2018-12-08 05:20:09 +00:00
return 1;
}
} else {
auto sfxSearch = allSFXGroups.find(m_groupId);
if (sfxSearch != allSFXGroups.end()) {
selData = &sfxSearch->second.first->second;
sfxIndex = sfxSearch->second.second;
}
}
if (!selData) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to select audio group data"));
2018-12-08 05:20:09 +00:00
return 1;
}
if (m_sfxGroup) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("amuserender is currently only able to render SongGroups"));
2018-12-08 05:20:09 +00:00
return 1;
}
/* WAV out path */
2020-04-11 22:49:30 +00:00
amuse::SystemString pathOut = fmt::format(FMT_STRING(_SYS_STR("{}-{}.wav")), *m_groupName, *m_songName);
Log.report(logvisor::Info, FMT_STRING(_SYS_STR("Writing to {}")), pathOut);
2018-12-08 05:20:09 +00:00
/* Build voice engine */
2019-07-20 04:23:25 +00:00
std::unique_ptr<boo::IAudioVoiceEngine> voxEngine = boo::NewWAVAudioVoiceEngine(pathOut.c_str(), rate, chCount);
2018-12-08 05:20:09 +00:00
amuse::BooBackendVoiceAllocator booBackend(*voxEngine);
amuse::Engine engine(booBackend, amuse::AmplitudeMode::PerSample);
engine.setVolume(float(std::clamp(0.0, volume, 1.0)));
2018-12-08 05:20:09 +00:00
/* Load group into engine */
const amuse::AudioGroup* group = engine.addAudioGroup(*selData);
if (!group) {
2020-04-11 22:49:30 +00:00
Log.report(logvisor::Error, FMT_STRING("unable to add audio group"));
2018-12-08 05:20:09 +00:00
return 1;
}
/* Enter playback loop */
amuse::ObjToken<amuse::Sequencer> seq = engine.seqPlay(m_groupId, m_setupId, m_arrData->m_data.get(), false);
size_t wroteFrames = 0;
signal(SIGINT, SIGINTHandler);
do {
voxEngine->pumpAndMixVoices();
wroteFrames += voxEngine->get5MsFrames();
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("\rFrame {}"), wroteFrames);
2018-12-08 05:20:09 +00:00
fflush(stdout);
} while (!g_BreakLoop && (seq->state() == amuse::SequencerState::Playing || seq->getVoiceCount() != 0));
2020-04-11 22:49:30 +00:00
fmt::print(FMT_STRING("\n"));
2018-12-08 05:20:09 +00:00
return 0;
2016-07-06 21:30:46 +00:00
}
#if _WIN32
2016-09-11 01:25:32 +00:00
#include <shellapi.h>
2018-12-08 05:20:09 +00:00
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int) {
signal(SIGABRT, abortHandler);
signal(SIGSEGV, abortHandler);
signal(SIGILL, abortHandler);
signal(SIGFPE, abortHandler);
int argc = 0;
const boo::SystemChar** argv;
if (lpCmdLine[0])
argv = (const wchar_t**)(CommandLineToArgvW(lpCmdLine, &argc));
static boo::SystemChar selfPath[1024];
GetModuleFileNameW(nullptr, selfPath, 1024);
static const boo::SystemChar* booArgv[32] = {};
booArgv[0] = selfPath;
for (int i = 0; i < argc; ++i)
booArgv[i + 1] = argv[i];
logvisor::CreateWin32Console();
SetConsoleOutputCP(65001);
return wmain(argc + 1, booArgv);
2016-07-06 21:30:46 +00:00
}
#endif