WIP: Migrate to C++ modules

This commit is contained in:
Daniel Wolf
2025-10-14 14:10:17 +02:00
parent 8496e0ab4b
commit 36ea0deb3d
29 changed files with 218 additions and 221 deletions

View File

@@ -321,26 +321,23 @@ set_target_properties(vorbis PROPERTIES FOLDER lib)
include_directories("src")
# ... rhubarb-animation
add_library(
add_library(rhubarb-animation)
target_sources(
rhubarb-animation
src/animation/animation-rules.cpp
src/animation/animation-rules.h
src/animation/mouth-animation.cpp
src/animation/mouth-animation.h
src/animation/pause-animation.cpp
src/animation/pause-animation.h
src/animation/rough-animation.cpp
src/animation/rough-animation.h
src/animation/shape-rule.cpp
src/animation/shape-rule.h
src/animation/static-segments.cpp
src/animation/static-segments.h
src/animation/target-shape-set.cpp
src/animation/target-shape-set.h
src/animation/timing-optimization.cpp
src/animation/timing-optimization.h
src/animation/tweening.cpp
src/animation/tweening.h
PUBLIC
FILE_SET publicModules
TYPE CXX_MODULES
FILES
src/animation/animation.cppm
src/animation/animation-rules.cppm
src/animation/mouth-animation.cppm
src/animation/pause-animation.cppm
src/animation/rough-animation.cppm
src/animation/shape-rule.cppm
src/animation/static-segments.cppm
src/animation/target-shape-set.cppm
src/animation/timing-optimization.cppm
src/animation/tweening.cppm
)
target_include_directories(rhubarb-animation PRIVATE "src/animation")
target_link_libraries(rhubarb-animation rhubarb-core rhubarb-logging rhubarb-time)

View File

@@ -1,10 +1,14 @@
#include "animation-rules.h"
module;
#include <array>
#include <boost/algorithm/clamp.hpp>
#include <set>
#include "core/phone.h"
#include "core/shape.h"
#include "time/continuous-timeline.h"
#include "time/timeline.h"
export module rhubarb.animation:animation_rules;
using boost::optional;
using boost::algorithm::clamp;
@@ -18,18 +22,21 @@ using enum Shape;
constexpr size_t shapeValueCount = static_cast<size_t>(Shape::EndSentinel);
// Returns the basic shape (A-F) that most closely resembles the specified shape.
Shape getBasicShape(Shape shape) {
static constexpr array<Shape, shapeValueCount> basicShapes =
to_array({A, B, C, D, E, F, A, C, A});
return basicShapes[static_cast<size_t>(shape)];
}
// Returns the mouth shape that results from relaxing the specified shape.
Shape relax(Shape shape) {
static constexpr array<Shape, shapeValueCount> relaxedShapes =
to_array({A, B, B, C, C, B, X, B, X});
return relaxedShapes[static_cast<size_t>(shape)];
}
// Gets the shape from a non-empty set of shapes that most closely resembles a reference shape.
Shape getClosestShape(Shape reference, ShapeSet shapes) {
if (shapes.empty()) {
throw std::invalid_argument("Cannot select from empty set of shapes.");
@@ -59,6 +66,19 @@ Shape getClosestShape(Shape reference, ShapeSet shapes) {
throw std::invalid_argument("Unable to find closest shape.");
}
// Indicates how to time a tween between two mouth shapes
enum class TweenTiming {
// Tween should end at the original transition
Early,
// Tween should overlap both original mouth shapes equally
Centered,
// Tween should begin at the original transition
Late
};
// Returns the tween shape and timing to use to transition between the specified two mouth shapes.
optional<pair<Shape, TweenTiming>> getTween(Shape first, Shape second) {
// Note that most of the following rules work in one direction only.
// That's because in animation, the mouth should usually "pop" open without inbetweens,
@@ -78,6 +98,10 @@ optional<pair<Shape, TweenTiming>> getTween(Shape first, Shape second) {
return it != lookup.end() ? it->second : optional<pair<Shape, TweenTiming>>();
}
// Returns the shape set(s) to use for a given phone.
// The resulting timeline will always cover the entire duration of the phone (starting at 0 cs).
// It may extend into the negative time range if animation is required prior to the sound being
// heard.
Timeline<ShapeSet> getShapeSets(Phone phone, centiseconds duration, centiseconds previousDuration) {
// Returns a timeline with a single shape set
const auto single = [duration](ShapeSet value) {

View File

@@ -1,37 +0,0 @@
#pragma once
#include <set>
#include "core/phone.h"
#include "core/shape.h"
#include "time/timeline.h"
// Returns the basic shape (A-F) that most closely resembles the specified shape.
Shape getBasicShape(Shape shape);
// Returns the mouth shape that results from relaxing the specified shape.
Shape relax(Shape shape);
// Gets the shape from a non-empty set of shapes that most closely resembles a reference shape.
Shape getClosestShape(Shape reference, ShapeSet shapes);
// Indicates how to time a tween between two mouth shapes
enum class TweenTiming {
// Tween should end at the original transition
Early,
// Tween should overlap both original mouth shapes equally
Centered,
// Tween should begin at the original transition
Late
};
// Returns the tween shape and timing to use to transition between the specified two mouth shapes.
boost::optional<std::pair<Shape, TweenTiming>> getTween(Shape first, Shape second);
// Returns the shape set(s) to use for a given phone.
// The resulting timeline will always cover the entire duration of the phone (starting at 0 cs).
// It may extend into the negative time range if animation is required prior to the sound being
// heard.
Timeline<ShapeSet> getShapeSets(Phone phone, centiseconds duration, centiseconds previousDuration);

View File

@@ -0,0 +1,7 @@
export module rhubarb.animation;
import :target_shape_set;
import :mouth_animation;
export using ::convertToTargetShapeSet;
export using ::animate;

View File

@@ -1,15 +1,21 @@
#include "mouth-animation.h"
module;
#include "pause-animation.h"
#include "rough-animation.h"
#include "shape-rule.h"
#include "static-segments.h"
#include "target-shape-set.h"
#include "core/phone.h"
#include "core/shape.h"
#include "time/continuous-timeline.h"
#include "time/timed-logging.h"
#include "timing-optimization.h"
#include "tweening.h"
JoiningContinuousTimeline<Shape> animate(
export module rhubarb.animation:mouth_animation;
import :pause_animation;
import :rough_animation;
import :shape_rule;
import :static_segments;
import :target_shape_set;
import :timing_optimization;
import :tweening;
export JoiningContinuousTimeline<Shape> animate(
const BoundedTimeline<Phone>& phones, const ShapeSet& targetShapeSet
) {
// Create timeline of shape rules

View File

@@ -1,10 +0,0 @@
#pragma once
#include "core/phone.h"
#include "core/shape.h"
#include "target-shape-set.h"
#include "time/continuous-timeline.h"
JoiningContinuousTimeline<Shape> animate(
const BoundedTimeline<Phone>& phones, const ShapeSet& targetShapeSet
);

View File

@@ -1,6 +1,11 @@
#include "pause-animation.h"
module;
#include "animation-rules.h"
#include "core/shape.h"
#include "time/continuous-timeline.h"
export module rhubarb.animation:pause_animation;
import :animation_rules;
Shape getPauseShape(Shape previous, Shape next, centiseconds duration) {
// For very short pauses: Just hold the previous shape
@@ -29,7 +34,10 @@ Shape getPauseShape(Shape previous, Shape next, centiseconds duration) {
return Shape::X;
}
JoiningContinuousTimeline<Shape> animatePauses(const JoiningContinuousTimeline<Shape>& animation) {
// Takes an existing animation and modifies the pauses (X shapes) to look better.
export JoiningContinuousTimeline<Shape> animatePauses(
const JoiningContinuousTimeline<Shape>& animation
) {
JoiningContinuousTimeline<Shape> result(animation);
for_each_adjacent(

View File

@@ -1,7 +0,0 @@
#pragma once
#include "core/shape.h"
#include "time/continuous-timeline.h"
// Takes an existing animation and modifies the pauses (X shapes) to look better.
JoiningContinuousTimeline<Shape> animatePauses(const JoiningContinuousTimeline<Shape>& animation);

View File

@@ -1,8 +1,17 @@
#include "rough-animation.h"
module;
#include <boost/optional.hpp>
// Create timeline of shapes using a bidirectional algorithm.
#include "core/phone.h"
#include "core/shape.h"
#include "time/continuous-timeline.h"
export module rhubarb.animation:rough_animation;
import :shape_rule;
// Does a rough animation (no tweening, special pause animation, etc.) using a bidirectional
// algorithm.
// Here's a rough sketch:
//
// * Most consonants result in shape sets with multiple options; most vowels have only one shape

View File

@@ -1,7 +0,0 @@
#pragma once
#include "shape-rule.h"
// Does a rough animation (no tweening, special pause animation, etc.) using a bidirectional
// algorithm.
JoiningContinuousTimeline<Shape> animateRough(const ContinuousTimeline<ShapeRule>& shapeRules);

View File

@@ -1,9 +1,16 @@
#include "shape-rule.h"
module;
#include <boost/range/adaptor/transformed.hpp>
#include <utility>
#include "core/phone.h"
#include "core/shape.h"
#include "time/bounded-timeline.h"
#include "time/continuous-timeline.h"
#include "time/time-range.h"
export module rhubarb.animation:shape_rule;
import :animation_rules;
using boost::optional;
using boost::adaptors::transformed;
@@ -21,30 +28,37 @@ ContinuousTimeline<optional<T>, AutoJoin> boundedTimelinetoContinuousOptional(
};
}
ShapeRule::ShapeRule(ShapeSet shapeSet, optional<Phone> phone, TimeRange phoneTiming) :
shapeSet(std::move(shapeSet)),
phone(std::move(phone)),
phoneTiming(phoneTiming) {}
export struct ShapeRule {
ShapeSet shapeSet;
optional<Phone> phone;
TimeRange phoneTiming;
ShapeRule ShapeRule::getInvalid() {
return {{}, boost::none, {0_cs, 0_cs}};
}
ShapeRule(ShapeSet shapeSet, optional<Phone> phone, TimeRange phoneTiming) :
shapeSet(std::move(shapeSet)),
phone(std::move(phone)),
phoneTiming(phoneTiming) {}
bool ShapeRule::operator==(const ShapeRule& rhs) const {
return shapeSet == rhs.shapeSet && phone == rhs.phone && phoneTiming == rhs.phoneTiming;
}
static ShapeRule getInvalid() {
return {{}, boost::none, {0_cs, 0_cs}};
}
bool ShapeRule::operator!=(const ShapeRule& rhs) const {
return !operator==(rhs);
}
bool operator==(const ShapeRule& rhs) const {
return shapeSet == rhs.shapeSet && phone == rhs.phone && phoneTiming == rhs.phoneTiming;
}
bool ShapeRule::operator<(const ShapeRule& rhs) const {
return shapeSet < rhs.shapeSet
|| phone < rhs.phone
|| phoneTiming.getStart() < rhs.phoneTiming.getStart()
|| phoneTiming.getEnd() < rhs.phoneTiming.getEnd();
}
bool operator!=(const ShapeRule& rhs) const {
return !operator==(rhs);
}
bool operator<(const ShapeRule& rhs) const {
return shapeSet < rhs.shapeSet
|| phone < rhs.phone
|| phoneTiming.getStart() < rhs.phoneTiming.getStart()
|| phoneTiming.getEnd() < rhs.phoneTiming.getEnd();
}
};
// Returns shape rules for an entire timeline of phones.
ContinuousTimeline<ShapeRule> getShapeRules(const BoundedTimeline<Phone>& phones) {
// Convert to continuous timeline so that silences aren't skipped when iterating
auto continuousPhones = boundedTimelinetoContinuousOptional(phones);

View File

@@ -1,24 +0,0 @@
#pragma once
#include "animation-rules.h"
#include "core/phone.h"
#include "time/bounded-timeline.h"
#include "time/continuous-timeline.h"
#include "time/time-range.h"
struct ShapeRule {
ShapeSet shapeSet;
boost::optional<Phone> phone;
TimeRange phoneTiming;
ShapeRule(ShapeSet shapeSet, boost::optional<Phone> phone, TimeRange phoneTiming);
static ShapeRule getInvalid();
bool operator==(const ShapeRule&) const;
bool operator!=(const ShapeRule&) const;
bool operator<(const ShapeRule&) const;
};
// Returns shape rules for an entire timeline of phones.
ContinuousTimeline<ShapeRule> getShapeRules(const BoundedTimeline<Phone>& phones);

View File

@@ -0,0 +1,15 @@
module;
#include "core/shape.h"
export module rhubarb.animation:shape_shorthands;
export constexpr Shape B = Shape::B;
export constexpr Shape A = Shape::A;
export constexpr Shape C = Shape::C;
export constexpr Shape D = Shape::D;
export constexpr Shape E = Shape::E;
export constexpr Shape F = Shape::F;
export constexpr Shape G = Shape::G;
export constexpr Shape H = Shape::H;
export constexpr Shape X = Shape::X;

View File

@@ -1,12 +1,23 @@
#include "static-segments.h"
module;
#include <functional>
#include <numeric>
#include <vector>
#include "core/phone.h"
#include "core/shape.h"
#include "time/continuous-timeline.h"
#include "tools/next-combination.h"
export module rhubarb.animation:static_segments;
import :shape_rule;
using std::vector;
export using AnimationFunction =
std::function<JoiningContinuousTimeline<Shape>(const ContinuousTimeline<ShapeRule>&)>;
int getSyllableCount(const ContinuousTimeline<ShapeRule>& shapeRules, TimeRange timeRange) {
if (timeRange.empty()) return 0;
@@ -210,7 +221,13 @@ TimeRange extendToFixedRules(
return {first->getStart(), last->getEnd()};
}
JoiningContinuousTimeline<Shape> avoidStaticSegments(
// Calls the specified animation function with the specified shape rules.
// If the resulting animation contains long static segments, the shape rules are tweaked and
// animated again.
// Static segments happen rather often.
// See
// http://animateducated.blogspot.de/2016/10/lip-sync-animation-2.html?showComment=1478861729702#c2940729096183546458.
export JoiningContinuousTimeline<Shape> avoidStaticSegments(
const ContinuousTimeline<ShapeRule>& shapeRules, const AnimationFunction& animate
) {
const auto animation = animate(shapeRules);

View File

@@ -1,20 +0,0 @@
#pragma once
#include <functional>
#include "core/shape.h"
#include "shape-rule.h"
#include "time/continuous-timeline.h"
using AnimationFunction =
std::function<JoiningContinuousTimeline<Shape>(const ContinuousTimeline<ShapeRule>&)>;
// Calls the specified animation function with the specified shape rules.
// If the resulting animation contains long static segments, the shape rules are tweaked and
// animated again.
// Static segments happen rather often.
// See
// http://animateducated.blogspot.de/2016/10/lip-sync-animation-2.html?showComment=1478861729702#c2940729096183546458.
JoiningContinuousTimeline<Shape> avoidStaticSegments(
const ContinuousTimeline<ShapeRule>& shapeRules, const AnimationFunction& animate
);

View File

@@ -1,6 +1,14 @@
#include "target-shape-set.h"
module;
Shape convertToTargetShapeSet(Shape shape, const ShapeSet& targetShapeSet) {
#include "core/shape.h"
#include "time/continuous-timeline.h"
export module rhubarb.animation:target_shape_set;
import :shape_rule;
// Returns the closest shape to the specified one that occurs in the target shape set.
export Shape convertToTargetShapeSet(Shape shape, const ShapeSet& targetShapeSet) {
if (targetShapeSet.find(shape) != targetShapeSet.end()) {
return shape;
}
@@ -13,7 +21,9 @@ Shape convertToTargetShapeSet(Shape shape, const ShapeSet& targetShapeSet) {
return basicShape;
}
ShapeSet convertToTargetShapeSet(const ShapeSet& shapes, const ShapeSet& targetShapeSet) {
// Replaces each shape in the specified set with the closest shape that occurs in the target shape
// set.
export ShapeSet convertToTargetShapeSet(const ShapeSet& shapes, const ShapeSet& targetShapeSet) {
ShapeSet result;
for (Shape shape : shapes) {
result.insert(convertToTargetShapeSet(shape, targetShapeSet));
@@ -21,7 +31,8 @@ ShapeSet convertToTargetShapeSet(const ShapeSet& shapes, const ShapeSet& targetS
return result;
}
ContinuousTimeline<ShapeRule> convertToTargetShapeSet(
// Replaces each shape in each rule with the closest shape that occurs in the target shape set.
export ContinuousTimeline<ShapeRule> convertToTargetShapeSet(
const ContinuousTimeline<ShapeRule>& shapeRules, const ShapeSet& targetShapeSet
) {
ContinuousTimeline<ShapeRule> result(shapeRules);
@@ -33,7 +44,9 @@ ContinuousTimeline<ShapeRule> convertToTargetShapeSet(
return result;
}
JoiningContinuousTimeline<Shape> convertToTargetShapeSet(
// Replaces each shape in the specified animation with the closest shape that occurs in the target
// shape set.
export JoiningContinuousTimeline<Shape> convertToTargetShapeSet(
const JoiningContinuousTimeline<Shape>& animation, const ShapeSet& targetShapeSet
) {
JoiningContinuousTimeline<Shape> result(animation);

View File

@@ -1,22 +0,0 @@
#pragma once
#include "core/shape.h"
#include "shape-rule.h"
// Returns the closest shape to the specified one that occurs in the target shape set.
Shape convertToTargetShapeSet(Shape shape, const ShapeSet& targetShapeSet);
// Replaces each shape in the specified set with the closest shape that occurs in the target shape
// set.
ShapeSet convertToTargetShapeSet(const ShapeSet& shapes, const ShapeSet& targetShapeSet);
// Replaces each shape in each rule with the closest shape that occurs in the target shape set.
ContinuousTimeline<ShapeRule> convertToTargetShapeSet(
const ContinuousTimeline<ShapeRule>& shapeRules, const ShapeSet& targetShapeSet
);
// Replaces each shape in the specified animation with the closest shape that occurs in the target
// shape set.
JoiningContinuousTimeline<Shape> convertToTargetShapeSet(
const JoiningContinuousTimeline<Shape>& animation, const ShapeSet& targetShapeSet
);

View File

@@ -1,12 +1,17 @@
#include "timing-optimization.h"
module;
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <map>
#include "shape-rule.h"
#include "core/shape.h"
#include "time/continuous-timeline.h"
#include "time/timed-logging.h"
export module rhubarb.animation:timing_optimization;
import :shape_rule;
using std::map;
using std::string;
@@ -177,7 +182,12 @@ JoiningContinuousTimeline<Shape> retime(
enum class MouthState { Idle, Closed, Open };
JoiningContinuousTimeline<Shape> optimizeTiming(const JoiningContinuousTimeline<Shape>& animation) {
// Changes the timing of an existing animation to reduce jitter and to make sure all shapes register
// visually.
// In some cases, shapes may be omitted.
export JoiningContinuousTimeline<Shape> optimizeTiming(
const JoiningContinuousTimeline<Shape>& animation
) {
// Identify segments with idle, closed, and open mouth shapes
JoiningContinuousTimeline<MouthState> segments(animation.getRange(), MouthState::Idle);
for (const auto& timedShape : animation) {

View File

@@ -1,9 +0,0 @@
#pragma once
#include "core/shape.h"
#include "time/continuous-timeline.h"
// Changes the timing of an existing animation to reduce jitter and to make sure all shapes register
// visually.
// In some cases, shapes may be omitted.
JoiningContinuousTimeline<Shape> optimizeTiming(const JoiningContinuousTimeline<Shape>& animation);

View File

@@ -1,8 +1,16 @@
#include "tweening.h"
module;
#include "animation-rules.h"
#include "core/shape.h"
#include "time/continuous-timeline.h"
JoiningContinuousTimeline<Shape> insertTweens(const JoiningContinuousTimeline<Shape>& animation) {
export module rhubarb.animation:tweening;
import :animation_rules;
// Takes an existing animation and inserts inbetween shapes for smoother results.
export JoiningContinuousTimeline<Shape> insertTweens(
const JoiningContinuousTimeline<Shape>& animation
) {
const centiseconds minTweenDuration = 4_cs;
const centiseconds maxTweenDuration = 8_cs;

View File

@@ -1,7 +0,0 @@
#pragma once
#include "core/shape.h"
#include "time/continuous-timeline.h"
// Takes an existing animation and inserts inbetween shapes for smoother results.
JoiningContinuousTimeline<Shape> insertTweens(const JoiningContinuousTimeline<Shape>& animation);

View File

@@ -2,7 +2,7 @@
#include <boost/lexical_cast.hpp>
#include "animation/target-shape-set.h"
import rhubarb.animation;
using std::string;
using std::chrono::duration;

View File

@@ -1,6 +1,6 @@
#include "exporter-tools.h"
#include "animation/target-shape-set.h"
import rhubarb.animation;
// Makes sure there is at least one mouth shape
std::vector<Timed<Shape>> dummyShapeIfEmpty(

View File

@@ -1,6 +1,6 @@
#include "tsv-exporter.h"
#include "animation/target-shape-set.h"
import rhubarb.animation;
void TsvExporter::exportAnimation(const ExporterInput& input, std::ostream& outputStream) {
// Output shapes with start times

View File

@@ -1,9 +1,8 @@
#include "rhubarb-lib.h"
#include "animation/mouth-animation.h"
#include "audio/audio-file-reading.h"
#include "core/phone.h"
#include "tools/text-files.h"
import rhubarb.animation;
using boost::optional;
using std::string;

View File

@@ -2,7 +2,6 @@
#include <filesystem>
#include "animation/target-shape-set.h"
#include "audio/audio-clip.h"
#include "core/shape.h"
#include "recognition/recognizer.h"

View File

@@ -7,7 +7,6 @@
#include <fstream>
#include <iostream>
#include "animation/target-shape-set.h"
#include "core/app-info.h"
#include "export-format.h"
#include "exporters/dat-exporter.h"

View File

@@ -2,6 +2,15 @@
#include "timeline.h"
// FIXME: Remove this workaround once we're using modules exclusively
inline centiseconds _min(centiseconds a, centiseconds b) {
return a < b ? a : b;
}
inline centiseconds _max(centiseconds a, centiseconds b) {
return a > b ? a : b;
}
template <typename T, bool AutoJoin = false>
class BoundedTimeline : public Timeline<T, AutoJoin> {
using typename Timeline<T, AutoJoin>::time_type;
@@ -48,7 +57,7 @@ public:
// Clip the value's range to bounds
TimeRange& valueRange = timedValue.getTimeRange();
valueRange.resize(
max(range.getStart(), valueRange.getStart()), min(range.getEnd(), valueRange.getEnd())
_max(range.getStart(), valueRange.getStart()), _min(range.getEnd(), valueRange.getEnd())
);
return Timeline<T, AutoJoin>::set(timedValue);

View File

@@ -3,9 +3,15 @@
#include <set>
#include <type_traits>
#include "centiseconds.h"
#include "timed.h"
#include "tools/tools.h"
using std::chrono::operator<;
using std::chrono::operator>;
using std::chrono::operator>=;
using std::chrono::operator<=;
enum class FindMode { SampleLeft, SampleRight, SearchLeft, SearchRight };
namespace internal {