rhubarb-lip-sync/src/main.cpp

147 lines
4.3 KiB
C++
Raw Normal View History

2015-09-28 19:19:39 +00:00
#include <iostream>
#include <boost/optional.hpp>
2015-11-25 21:00:24 +00:00
#include <format.h>
2015-12-29 10:44:55 +00:00
#include <tclap/CmdLine.h>
#include "audio/WaveFileReader.h"
#include "phoneExtraction.h"
#include "mouthAnimation.h"
#include "appInfo.h"
#include "NiceCmdLineOutput.h"
2016-01-08 09:53:35 +00:00
#include "ProgressBar.h"
#include "logging.h"
#include <gsl_util.h>
#include "Timeline.h"
#include "Exporter.h"
2015-09-28 19:19:39 +00:00
using std::exception;
using std::string;
2016-03-08 21:59:44 +00:00
using std::vector;
using std::unique_ptr;
using std::make_unique;
2015-11-25 21:00:24 +00:00
using std::map;
using std::chrono::duration;
using std::chrono::duration_cast;
using boost::filesystem::path;
2016-03-08 21:59:44 +00:00
namespace tclap = TCLAP;
string getMessage(const exception& e) {
string result(e.what());
try {
std::rethrow_if_nested(e);
} catch(const exception& innerException) {
result += "\n" + getMessage(innerException);
} catch(...) {}
2015-09-10 19:31:25 +00:00
return result;
}
2015-09-10 19:31:25 +00:00
2015-11-25 21:00:24 +00:00
unique_ptr<AudioStream> createAudioStream(path filePath) {
try {
2016-04-09 20:07:25 +00:00
return std::make_unique<WaveFileReader>(filePath);
} catch (...) {
std::throw_with_nested(std::runtime_error(fmt::format("Could not open sound file '{0}'.", filePath.string())));
2015-09-10 19:31:25 +00:00
}
}
2016-03-08 21:59:44 +00:00
// Tell TCLAP how to handle our types
namespace TCLAP {
template<>
2016-03-08 21:59:44 +00:00
struct ArgTraits<LogLevel> {
typedef ValueLike ValueCategory;
};
template<>
struct ArgTraits<ExportFormat> {
typedef ValueLike ValueCategory;
};
}
int main(int argc, char *argv[]) {
2016-03-08 21:59:44 +00:00
auto pausableStderrSink = addPausableStderrSink(LogLevel::Warning);
pausableStderrSink->pause();
2015-12-29 10:44:55 +00:00
// Define command-line parameters
const char argumentValueSeparator = ' ';
2016-03-08 21:59:44 +00:00
tclap::CmdLine cmd(appName, argumentValueSeparator, appVersion);
2015-12-29 10:44:55 +00:00
cmd.setExceptionHandling(false);
cmd.setOutput(new NiceCmdLineOutput());
2016-03-08 21:59:44 +00:00
auto logLevels = vector<LogLevel>(getEnumValues<LogLevel>());
tclap::ValuesConstraint<LogLevel> logLevelConstraint(logLevels);
tclap::ValueArg<LogLevel> logLevel("", "logLevel", "The minimum log level to log", false, LogLevel::Debug, &logLevelConstraint, cmd);
tclap::ValueArg<string> logFileName("", "logFile", "The log file path.", false, string(), "string", cmd);
tclap::ValueArg<string> dialog("d", "dialog", "The text of the dialog.", false, string(), "string", cmd);
auto exportFormats = vector<ExportFormat>(getEnumValues<ExportFormat>());
tclap::ValuesConstraint<ExportFormat> exportFormatConstraint(exportFormats);
tclap::ValueArg<ExportFormat> exportFormat("f", "exportFormat", "The export format.", false, ExportFormat::TSV, &exportFormatConstraint, cmd);
2016-03-08 21:59:44 +00:00
tclap::UnlabeledValueArg<string> inputFileName("inputFile", "The input file. Must be a sound file in WAVE format.", true, "", "string", cmd);
2015-12-29 10:44:55 +00:00
try {
auto resumeLogging = gsl::finally([&]() {
std::cerr << std::endl << std::endl;
2016-03-08 21:59:44 +00:00
pausableStderrSink->resume();
std::cerr << std::endl;
});
2015-12-29 10:44:55 +00:00
// Parse command line
cmd.parse(argc, argv);
2016-03-08 21:59:44 +00:00
// Set up log file
if (logFileName.isSet()) {
addFileSink(path(logFileName.getValue()), logLevel.getValue());
}
// Detect phones
2016-01-08 09:53:35 +00:00
const int columnWidth = 30;
std::cerr << std::left;
std::cerr << std::setw(columnWidth) << "Analyzing input file";
2016-04-09 20:07:25 +00:00
Timeline<Phone> phones{};
2016-01-08 09:53:35 +00:00
{
ProgressBar progressBar;
phones = detectPhones(
createAudioStream(inputFileName.getValue()),
2016-03-08 21:59:44 +00:00
dialog.isSet() ? dialog.getValue() : boost::optional<string>(),
progressBar);
2016-01-08 09:53:35 +00:00
}
std::cerr << "Done" << std::endl;
// Generate mouth shapes
2016-01-08 09:53:35 +00:00
std::cerr << std::setw(columnWidth) << "Generating mouth shapes";
2016-04-09 20:07:25 +00:00
Timeline<Shape> shapes = animate(phones);
2016-01-08 09:53:35 +00:00
std::cerr << "Done" << std::endl;
std::cerr << std::endl;
// Export
unique_ptr<Exporter> exporter;
switch (exportFormat.getValue()) {
case ExportFormat::TSV:
exporter = make_unique<TSVExporter>();
break;
case ExportFormat::XML:
exporter = make_unique<XMLExporter>();
break;
case ExportFormat::JSON:
exporter = make_unique<JSONExporter>();
break;
default:
throw std::runtime_error("Unknown export format.");
}
exporter->exportShapes(path(inputFileName.getValue()), shapes, std::cout);
return 0;
2016-03-08 21:59:44 +00:00
} catch (tclap::ArgException& e) {
// Error parsing command-line args.
cmd.getOutput()->failure(cmd, e);
std::cerr << std::endl;
2015-12-29 10:44:55 +00:00
return 1;
2016-03-08 21:59:44 +00:00
} catch (tclap::ExitException&) {
// A built-in TCLAP command (like --help) has finished. Exit application.
std::cerr << std::endl;
return 0;
} catch (const exception& e) {
// Generic error
std::cerr << "An error occurred.\n" << getMessage(e) << std::endl;
return 1;
}
2015-11-25 21:00:24 +00:00
}