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>
|
|
|
|
#include <format.h>
|
2015-11-18 19:59:03 +00:00
|
|
|
#include "audio_input/WaveFileReader.h"
|
|
|
|
#include "phone_extraction.h"
|
2015-11-20 21:20:19 +00:00
|
|
|
#include "mouth_animation.h"
|
2015-11-19 20:17:35 +00:00
|
|
|
#include "platform_tools.h"
|
2015-09-28 19:19:39 +00:00
|
|
|
|
2015-11-19 17:32:14 +00:00
|
|
|
using std::exception;
|
|
|
|
using std::string;
|
|
|
|
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
|
|
|
|
|
|
|
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 {
|
2015-11-19 20:17:35 +00:00
|
|
|
return unique_ptr<AudioStream>(new 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
|
|
|
}
|
|
|
|
|
2015-11-25 21:00:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-19 17:32:14 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
try {
|
2015-11-19 20:17:35 +00:00
|
|
|
// Get sound file name
|
2015-11-25 21:04:48 +00:00
|
|
|
if (argc != 2) {
|
2015-11-19 20:17:35 +00:00
|
|
|
throw std::runtime_error("Invalid command line arguments. Call with sound file name as sole argument.");
|
|
|
|
}
|
2015-11-25 21:04:48 +00:00
|
|
|
string soundFileName = argv[1];
|
2015-11-19 20:17:35 +00:00
|
|
|
|
|
|
|
// Create audio streams
|
|
|
|
unique_ptr<AudioStream> audioStream = createAudioStream(soundFileName);
|
2015-09-10 19:31:25 +00:00
|
|
|
|
2015-11-19 20:17:35 +00:00
|
|
|
// Detect phones
|
2015-11-25 21:00:24 +00:00
|
|
|
map<centiseconds, Phone> phones = detectPhones(std::move(audioStream));
|
2015-11-19 17:32:14 +00:00
|
|
|
|
2015-11-20 21:20:19 +00:00
|
|
|
// Generate mouth shapes
|
2015-11-25 21:00:24 +00:00
|
|
|
map<centiseconds, Shape> shapes = animate(phones);
|
2015-11-20 21:20:19 +00:00
|
|
|
|
2015-11-25 21:00:24 +00:00
|
|
|
// 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));
|
2015-11-19 17:32:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} catch (const exception& e) {
|
2015-11-25 21:00:24 +00:00
|
|
|
std::cerr << "An error occurred. " << getMessage(e);
|
2015-11-19 17:32:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-11-25 21:00:24 +00:00
|
|
|
}
|