Generating XML output

This commit is contained in:
Daniel Wolf
2015-11-25 22:00:24 +01:00
parent 2ef99119b0
commit 27ba3ef357
5 changed files with 58 additions and 14 deletions

View File

@@ -1,4 +1,7 @@
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <format.h>
#include "audio_input/WaveFileReader.h"
#include "phone_extraction.h"
#include "mouth_animation.h"
@@ -8,6 +11,11 @@ using std::exception;
using std::string;
using std::wstring;
using std::unique_ptr;
using std::map;
using std::chrono::duration;
using std::chrono::duration_cast;
using boost::filesystem::path;
using boost::property_tree::ptree;
string getMessage(const exception& e) {
string result(e.what());
@@ -20,7 +28,7 @@ string getMessage(const exception& e) {
return result;
}
unique_ptr<AudioStream> createAudioStream(boost::filesystem::path filePath) {
unique_ptr<AudioStream> createAudioStream(path filePath) {
try {
return unique_ptr<AudioStream>(new WaveFileReader(filePath));
} catch (...) {
@@ -28,6 +36,37 @@ unique_ptr<AudioStream> createAudioStream(boost::filesystem::path filePath) {
}
}
string formatDuration(duration<double> seconds) {
return fmt::format("{0:.2f}", seconds.count());
}
ptree createXmlTree(const path& filePath, const map<centiseconds, Phone>& phones, const map<centiseconds, Shape>& shapes) {
ptree tree;
// Add sound file path
tree.add("rhubarbResult.info.soundFile", filePath.string());
// Add phones
for (auto it = phones.cbegin(), itNext = ++phones.cbegin(); itNext != phones.cend(); ++it, ++itNext) {
auto pair = *it;
auto nextPair = *itNext;
ptree& phoneElement = tree.add("rhubarbResult.phones.phone", pair.second);
phoneElement.add("<xmlattr>.start", formatDuration(pair.first));
phoneElement.add("<xmlattr>.duration", formatDuration(nextPair.first - pair.first));
}
// Add mouth cues
for (auto it = shapes.cbegin(), itNext = ++shapes.cbegin(); itNext != shapes.cend(); ++it, ++itNext) {
auto pair = *it;
auto nextPair = *itNext;
ptree& mouthCueElement = tree.add("rhubarbResult.mouthCues.mouthCue", pair.second);
mouthCueElement.add("<xmlattr>.start", formatDuration(pair.first));
mouthCueElement.add("<xmlattr>.duration", formatDuration(nextPair.first - pair.first));
}
return tree;
}
int main(int argc, char *argv[]) {
try {
// Get sound file name
@@ -41,21 +80,18 @@ int main(int argc, char *argv[]) {
unique_ptr<AudioStream> audioStream = createAudioStream(soundFileName);
// Detect phones
std::map<centiseconds, Phone> phones = detectPhones(std::move(audioStream));
map<centiseconds, Phone> phones = detectPhones(std::move(audioStream));
// Generate mouth shapes
std::map<centiseconds, Shape> shapes = animate(phones);
map<centiseconds, Shape> shapes = animate(phones);
for (auto &pair : phones) {
std::cout << pair.first << ": " << pair.second << "\n";
}
for (auto &pair : shapes) {
std::cout << pair.first << ": " << pair.second << "\n";
}
// Print XML
boost::property_tree::ptree xmlTree = createXmlTree(soundFileName, phones, shapes);
boost::property_tree::write_xml(std::cout, xmlTree, boost::property_tree::xml_writer_settings<string>(' ', 2));
return 0;
} catch (const exception& e) {
std::cout << "An error occurred. " << getMessage(e);
std::cerr << "An error occurred. " << getMessage(e);
return 1;
}
}
}