2015-09-28 19:19:39 +00:00
|
|
|
#include <iostream>
|
2015-11-25 21:00:24 +00:00
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
#include <boost/property_tree/xml_parser.hpp>
|
2016-02-09 21:08:11 +00:00
|
|
|
#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>
|
2016-03-08 17:21:17 +00:00
|
|
|
#include "audio/WaveFileReader.h"
|
2015-12-29 15:26:01 +00:00
|
|
|
#include "phoneExtraction.h"
|
|
|
|
#include "mouthAnimation.h"
|
|
|
|
#include "appInfo.h"
|
2016-01-06 19:45:04 +00:00
|
|
|
#include "NiceCmdLineOutput.h"
|
2016-01-08 09:53:35 +00:00
|
|
|
#include "ProgressBar.h"
|
2016-02-29 20:00:38 +00:00
|
|
|
#include "logging.h"
|
|
|
|
#include <gsl_util.h>
|
2016-03-01 20:57:05 +00:00
|
|
|
#include <tools.h>
|
2016-04-09 20:07:25 +00:00
|
|
|
#include <Timeline.h>
|
2015-09-28 19:19:39 +00:00
|
|
|
|
2015-11-19 17:32:14 +00:00
|
|
|
using std::exception;
|
|
|
|
using std::string;
|
2016-03-08 21:59:44 +00:00
|
|
|
using std::vector;
|
2015-11-19 17:32:14 +00:00
|
|
|
using std::unique_ptr;
|
2015-11-25 21:00:24 +00:00
|
|
|
using std::map;
|
|
|
|
using std::chrono::duration;
|
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using boost::filesystem::path;
|
|
|
|
using boost::property_tree::ptree;
|
2015-11-19 17:32:14 +00:00
|
|
|
|
2016-03-08 21:59:44 +00:00
|
|
|
namespace tclap = TCLAP;
|
|
|
|
|
2015-11-19 17:32:14 +00:00
|
|
|
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
|
|
|
|
2015-11-19 17:32:14 +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) {
|
2015-11-19 17:32:14 +00:00
|
|
|
try {
|
2016-04-09 20:07:25 +00:00
|
|
|
return std::make_unique<WaveFileReader>(filePath);
|
2015-11-19 17:32:14 +00:00
|
|
|
} catch (...) {
|
|
|
|
std::throw_with_nested(std::runtime_error("Could not open sound file.") );
|
2015-09-10 19:31:25 +00:00
|
|
|
}
|
2015-11-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 20:07:25 +00:00
|
|
|
ptree createXmlTree(const path& filePath, const Timeline<Phone>& phones, const Timeline<Shape>& shapes) {
|
2015-11-25 21:00:24 +00:00
|
|
|
ptree tree;
|
|
|
|
|
|
|
|
// Add sound file path
|
|
|
|
tree.add("rhubarbResult.info.soundFile", filePath.string());
|
|
|
|
|
|
|
|
// Add phones
|
2016-04-09 20:07:25 +00:00
|
|
|
for (auto& timedPhone : phones) {
|
|
|
|
ptree& phoneElement = tree.add("rhubarbResult.phones.phone", timedPhone.getValue());
|
|
|
|
phoneElement.add("<xmlattr>.start", formatDuration(timedPhone.getStart()));
|
|
|
|
phoneElement.add("<xmlattr>.duration", formatDuration(timedPhone.getLength()));
|
2015-11-25 21:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add mouth cues
|
2016-04-09 20:07:25 +00:00
|
|
|
for (auto& timedShape : shapes) {
|
|
|
|
ptree& mouthCueElement = tree.add("rhubarbResult.mouthCues.mouthCue", timedShape.getValue());
|
|
|
|
mouthCueElement.add("<xmlattr>.start", formatDuration(timedShape.getStart()));
|
|
|
|
mouthCueElement.add("<xmlattr>.duration", formatDuration(timedShape.getLength()));
|
2015-11-25 21:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2016-03-08 21:59:44 +00:00
|
|
|
// Tell TCLAP how to handle our types
|
2016-02-09 21:08:11 +00:00
|
|
|
namespace TCLAP {
|
|
|
|
template<>
|
2016-03-08 21:59:44 +00:00
|
|
|
struct ArgTraits<LogLevel> {
|
|
|
|
typedef ValueLike ValueCategory;
|
2016-02-09 21:08:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-11-19 17:32:14 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2016-03-08 21:59:44 +00:00
|
|
|
auto pausableStderrSink = addPausableStderrSink(LogLevel::Warning);
|
|
|
|
pausableStderrSink->pause();
|
2016-02-29 20:00:38 +00:00
|
|
|
|
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);
|
2016-01-06 19:45:04 +00:00
|
|
|
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);
|
|
|
|
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
|
|
|
|
2016-01-06 19:45:04 +00:00
|
|
|
try {
|
2016-02-29 20:00:38 +00:00
|
|
|
auto resumeLogging = gsl::finally([&]() {
|
|
|
|
std::cerr << std::endl << std::endl;
|
2016-03-08 21:59:44 +00:00
|
|
|
pausableStderrSink->resume();
|
2016-02-29 20:00:38 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
});
|
|
|
|
|
2015-12-29 10:44:55 +00:00
|
|
|
// Parse command line
|
|
|
|
cmd.parse(argc, argv);
|
2015-11-19 20:17:35 +00:00
|
|
|
|
2016-03-08 21:59:44 +00:00
|
|
|
// Set up log file
|
|
|
|
if (logFileName.isSet()) {
|
|
|
|
addFileSink(path(logFileName.getValue()), logLevel.getValue());
|
|
|
|
}
|
|
|
|
|
2015-11-19 20:17:35 +00:00
|
|
|
// 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;
|
2016-02-09 21:08:11 +00:00
|
|
|
phones = detectPhones(
|
2016-03-07 20:28:31 +00:00
|
|
|
createAudioStream(inputFileName.getValue()),
|
2016-03-08 21:59:44 +00:00
|
|
|
dialog.isSet() ? dialog.getValue() : boost::optional<string>(),
|
2016-02-09 21:08:11 +00:00
|
|
|
progressBar);
|
2016-01-08 09:53:35 +00:00
|
|
|
}
|
|
|
|
std::cerr << "Done" << std::endl;
|
2015-11-19 17:32:14 +00:00
|
|
|
|
2015-11-20 21:20:19 +00:00
|
|
|
// 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;
|
2015-11-20 21:20:19 +00:00
|
|
|
|
2015-11-25 21:00:24 +00:00
|
|
|
// Print XML
|
2015-12-29 10:44:55 +00:00
|
|
|
ptree xmlTree = createXmlTree(inputFileName.getValue(), phones, shapes);
|
2015-11-25 21:00:24 +00:00
|
|
|
boost::property_tree::write_xml(std::cout, xmlTree, boost::property_tree::xml_writer_settings<string>(' ', 2));
|
2015-11-19 17:32:14 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-03-08 21:59:44 +00:00
|
|
|
} catch (tclap::ArgException& e) {
|
2016-01-06 19:45:04 +00:00
|
|
|
// Error parsing command-line args.
|
|
|
|
cmd.getOutput()->failure(cmd, e);
|
2016-02-29 20:00:38 +00:00
|
|
|
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&) {
|
2016-01-06 19:45:04 +00:00
|
|
|
// A built-in TCLAP command (like --help) has finished. Exit application.
|
2016-02-29 20:00:38 +00:00
|
|
|
std::cerr << std::endl;
|
2016-01-06 19:45:04 +00:00
|
|
|
return 0;
|
2015-11-19 17:32:14 +00:00
|
|
|
} catch (const exception& e) {
|
2016-01-06 19:45:04 +00:00
|
|
|
// Generic error
|
2016-02-29 20:00:38 +00:00
|
|
|
std::cerr << "An error occurred. " << getMessage(e) << std::endl;
|
2015-11-19 17:32:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-11-25 21:00:24 +00:00
|
|
|
}
|