Supporting multiple export formats

- Simplified XML export format
- Added TSV and JSON formats
- Using TSV as standard export format
This commit is contained in:
Daniel Wolf
2016-04-12 20:49:06 +02:00
parent 90e1375f1b
commit fd6b3b1e2f
5 changed files with 194 additions and 35 deletions

View File

@@ -1,6 +1,4 @@
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/optional.hpp>
#include <format.h>
#include <tclap/CmdLine.h>
@@ -12,18 +10,18 @@
#include "ProgressBar.h"
#include "logging.h"
#include <gsl_util.h>
#include <tools.h>
#include <Timeline.h>
#include "Timeline.h"
#include "Exporter.h"
using std::exception;
using std::string;
using std::vector;
using std::unique_ptr;
using std::make_unique;
using std::map;
using std::chrono::duration;
using std::chrono::duration_cast;
using boost::filesystem::path;
using boost::property_tree::ptree;
namespace tclap = TCLAP;
@@ -42,41 +40,20 @@ unique_ptr<AudioStream> createAudioStream(path filePath) {
try {
return std::make_unique<WaveFileReader>(filePath);
} catch (...) {
std::throw_with_nested(std::runtime_error(fmt::format("Could not open sound file {0}.", filePath)));
std::throw_with_nested(std::runtime_error(fmt::format("Could not open sound file '{0}'.", filePath.string())));
}
}
ptree createXmlTree(const path& filePath, const Timeline<Phone>& phones, const Timeline<Shape>& shapes) {
ptree tree;
// Add sound file path
tree.put("rhubarbResult.info.soundFile", filePath.string());
// Add phones
tree.put("rhubarbResult.phones", "");
for (auto& timedPhone : phones) {
ptree& phoneElement = tree.add("rhubarbResult.phones.phone", timedPhone.getValue());
phoneElement.put("<xmlattr>.start", formatDuration(timedPhone.getStart()));
phoneElement.put("<xmlattr>.duration", formatDuration(timedPhone.getLength()));
}
// Add mouth cues
tree.put("rhubarbResult.mouthCues", "");
for (auto& timedShape : shapes) {
ptree& mouthCueElement = tree.add("rhubarbResult.mouthCues.mouthCue", timedShape.getValue());
mouthCueElement.put("<xmlattr>.start", formatDuration(timedShape.getStart()));
mouthCueElement.put("<xmlattr>.duration", formatDuration(timedShape.getLength()));
}
return tree;
}
// Tell TCLAP how to handle our types
namespace TCLAP {
template<>
struct ArgTraits<LogLevel> {
typedef ValueLike ValueCategory;
};
template<>
struct ArgTraits<ExportFormat> {
typedef ValueLike ValueCategory;
};
}
int main(int argc, char *argv[]) {
@@ -93,6 +70,9 @@ int main(int argc, char *argv[]) {
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);
tclap::UnlabeledValueArg<string> inputFileName("inputFile", "The input file. Must be a sound file in WAVE format.", true, "", "string", cmd);
try {
@@ -131,9 +111,22 @@ int main(int argc, char *argv[]) {
std::cerr << std::endl;
// Print XML
ptree xmlTree = createXmlTree(inputFileName.getValue(), phones, shapes);
boost::property_tree::write_xml(std::cout, xmlTree, boost::property_tree::xml_writer_settings<string>(' ', 2));
// 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;
} catch (tclap::ArgException& e) {