Improved animation quality through new algorithm

Using "lazy" ruleset instead of 1:1 mapping from phones
This commit is contained in:
Daniel Wolf 2016-06-26 20:11:02 +02:00
parent 8c9466bcf3
commit 2a5ed95698
2 changed files with 129 additions and 55 deletions

View File

@ -12,7 +12,8 @@ enum class Shape {
D, // Mouth wide open (b[u]t, m[y], sh[ou]ld...) D, // Mouth wide open (b[u]t, m[y], sh[ou]ld...)
E, // h[ow] E, // h[ow]
F, // Pout ([o]ff, sh[ow]) F, // Pout ([o]ff, sh[ow])
G // F, V G, // F, V
EndSentinel
}; };
class ShapeConverter : public EnumConverter<Shape> { class ShapeConverter : public EnumConverter<Shape> {

View File

@ -1,74 +1,147 @@
#include "mouthAnimation.h" #include "mouthAnimation.h"
#include "logging.h" #include "logging.h"
#include <unordered_set>
#include <unordered_map>
#include <array>
using std::map; using std::map;
using std::unordered_set;
using std::unordered_map;
using std::vector;
using boost::optional;
Shape getShape(Phone phone) { using AnimationResult = Timeline<Shape>;
switch (phone) {
case Phone::P:
case Phone::B:
case Phone::M:
return Shape::A;
case Phone::Unknown: AnimationResult animateFixedSound(Shape shape, centiseconds duration) {
case Phone::IY: return AnimationResult{ {centiseconds::zero(), duration, shape} };
case Phone::T: }
case Phone::D:
case Phone::K:
case Phone::G:
case Phone::CH:
case Phone::JH:
case Phone::TH:
case Phone::DH:
case Phone::S:
case Phone::Z:
case Phone::SH:
case Phone::ZH:
case Phone::N:
case Phone::NG:
case Phone::R:
case Phone::Y:
return Shape::B;
case Phone::EH: // Diphtong vowels
case Phone::IH: AnimationResult animateDiphtong(Shape first, Shape second, centiseconds duration) {
case Phone::AH: return AnimationResult{
case Phone::EY: { centiseconds::zero(), duration, first },
case Phone::HH: { duration / 2, duration, second }
case Phone::L: };
return Shape::C; }
case Phone::AA: // P, B
case Phone::AE: AnimationResult animateBilabialStop(centiseconds duration, centiseconds leftDuration, optional<Shape> rightShape) {
case Phone::AY: Shape openShape = rightShape.value_or(Shape::B);
case Phone::AW: if (openShape == Shape::A) {
return Shape::D; openShape = Shape::B;
}
return AnimationResult{
{ -leftDuration / 4, centiseconds::zero(), Shape::A },
{ centiseconds::zero(), duration, openShape }
};
}
case Phone::AO: // Sounds with no fixed mouth position.
case Phone::UH: // Mapping specifies the shape to use for every right shape.
case Phone::OW: AnimationResult animateFlexibleSound(std::array<Shape, 7> mapping, centiseconds duration, optional<Shape> rightShape) {
case Phone::ER: constexpr int mapSize = std::tuple_size<decltype(mapping)>::value;
return Shape::E; static_assert(static_cast<int>(Shape::EndSentinel) == mapSize, "Shape definition has changed.");
case Phone::UW: Shape right = rightShape.value_or(Shape::A);
case Phone::OY: Shape shape = mapping[static_cast<int>(right)];
case Phone::W: return AnimationResult{ { centiseconds::zero(), duration, shape } };
return Shape::F; }
case Phone::F: AnimationResult animate(optional<Phone> phone, centiseconds duration, centiseconds leftDuration, optional<Shape> rightShape) {
case Phone::V: constexpr Shape A = Shape::A;
return Shape::G; constexpr Shape B = Shape::B;
constexpr Shape C = Shape::C;
constexpr Shape D = Shape::D;
constexpr Shape E = Shape::E;
constexpr Shape F = Shape::F;
constexpr Shape G = Shape::G;
default: if (!phone) {
throw std::runtime_error("Unexpected Phone value."); return animateFixedSound(A, duration);
}
switch (*phone) {
case Phone::Unknown: return animateFixedSound(B, duration);
case Phone::AO: return animateFixedSound(E, duration);
case Phone::AA: return animateFixedSound(D, duration);
case Phone::IY: return animateFixedSound(B, duration);
case Phone::UW: return animateFixedSound(F, duration);
case Phone::EH: return animateFixedSound(C, duration);
case Phone::IH: return animateFixedSound(B, duration);
case Phone::UH: return animateFixedSound(E, duration);
case Phone::AH: return animateFixedSound(C, duration);
case Phone::AE: return animateFixedSound(D, duration);
case Phone::EY: return animateDiphtong(C, B, duration);
case Phone::AY: return animateDiphtong(D, B, duration);
case Phone::OW: return animateDiphtong(E, F, duration);
case Phone::AW: return animateDiphtong(D, F, duration);
case Phone::OY: return animateDiphtong(E, B, duration);
case Phone::ER: return animateFixedSound(E, duration);
case Phone::P:
case Phone::B: return animateBilabialStop(duration, leftDuration, rightShape);
case Phone::T:
case Phone::D:
case Phone::K: return animateFlexibleSound({ B, B, B, B, B, F, B }, duration, rightShape);
case Phone::G: return animateFlexibleSound({ B, B, C, C, E, F, B }, duration, rightShape);
case Phone::CH:
case Phone::JH: return animateFlexibleSound({ B, B, B, B, B, F, B }, duration, rightShape);
case Phone::F:
case Phone::V: return animateFixedSound(G, duration);
case Phone::TH:
case Phone::DH:
case Phone::S:
case Phone::Z:
case Phone::SH:
case Phone::ZH: return animateFlexibleSound({ B, B, B, B, B, F, B }, duration, rightShape);
case Phone::HH: return animateFlexibleSound({ B, B, C, D, E, F, B }, duration, rightShape);
case Phone::M: return animateFixedSound(A, duration);
case Phone::N: return animateFlexibleSound({ B, B, C, C, C, F, B }, duration, rightShape);
case Phone::NG:
case Phone::L: return animateFlexibleSound({ B, B, C, D, E, F, B }, duration, rightShape);
case Phone::R: return animateFlexibleSound({ B, B, B, B, B, F, B }, duration, rightShape);
case Phone::Y: return animateFixedSound(B, duration);
case Phone::W: return animateFixedSound(F, duration);
default:
throw std::invalid_argument("Unexpected phone.");
} }
} }
ContinuousTimeline<Shape> animate(const BoundedTimeline<Phone> &phones) { ContinuousTimeline<Shape> animate(const BoundedTimeline<Phone> &phones) {
ContinuousTimeline<Shape> shapes(phones.getRange(), Shape::A); // Convert phones to continuous timeline so that silences show up when iterating
ContinuousTimeline<optional<Phone>> continuousPhones(phones.getRange(), boost::none);
for (const auto& timedPhone : phones) { for (const auto& timedPhone : phones) {
Timed<Shape> timedShape(timedPhone.getTimeRange(), getShape(timedPhone.getValue())); continuousPhones.set(timedPhone.getTimeRange(), timedPhone.getValue());
shapes.set(timedShape); }
ContinuousTimeline<Shape> shapes(phones.getRange(), Shape::A);
// Iterate phones in *reverse* order so we can access the right-hand result
optional<Shape> lastShape;
centiseconds cutoff = shapes.getRange().getEnd();
for (auto it = continuousPhones.rbegin(); it != continuousPhones.rend(); ++it) {
// Animate one phone
optional<Phone> phone = it->getValue();
centiseconds duration = it->getTimeRange().getLength();
centiseconds leftDuration = std::next(it) != continuousPhones.rend()
? std::next(it)->getTimeRange().getLength()
: centiseconds::zero();
Timeline<Shape> result = animate(phone, duration, leftDuration, lastShape);
// Result timing is relative to phone. Make absolute.
result.shift(it->getStart());
// New shapes must not overwrite existing shapes
result.clear(cutoff, shapes.getRange().getEnd());
// Copy to target timeline
for (const auto& timedShape : result) {
shapes.set(timedShape);
}
lastShape = result.empty() ? optional<Shape>() : result.begin()->getValue();
if (!result.empty()) {
cutoff = result.begin()->getStart();
}
} }
for (const auto& timedShape : shapes) { for (const auto& timedShape : shapes) {