diff --git a/CMakeLists.txt b/CMakeLists.txt index c340f79..9904fbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,6 +118,8 @@ set(SOURCE_FILES src/Timed.h src/TimeRange.cpp src/TimeRange.h src/Timeline.h + src/BoundedTimeline.h + src/ContinuousTimeline.h src/pairs.h src/Exporter.cpp src/Exporter.h ) @@ -130,6 +132,8 @@ target_compile_options(rhubarb PUBLIC ${enableWarningsFlags}) set(TEST_FILES tests/stringToolsTests.cpp tests/TimelineTests.cpp + tests/BoundedTimelineTests.cpp + tests/ContinuousTimelineTests.cpp tests/pairsTests.cpp src/stringTools.cpp src/stringTools.h src/Timeline.h diff --git a/src/BoundedTimeline.h b/src/BoundedTimeline.h new file mode 100644 index 0000000..891dfe3 --- /dev/null +++ b/src/BoundedTimeline.h @@ -0,0 +1,66 @@ +#pragma once + +#include "Timeline.h" + +template +class BoundedTimeline : public Timeline { + using Timeline::time_type; + using Timeline::equals; + +public: + using Timeline::iterator; + using Timeline::end; + + explicit BoundedTimeline(TimeRange range) : + range(range) + {} + + template + BoundedTimeline(TimeRange range, InputIterator first, InputIterator last) : + range(range) + { + for (auto it = first; it != last; ++it) { + // Virtual function call in constructor. Derived constructors shouldn't call this one! + BoundedTimeline::set(*it); + } + } + + BoundedTimeline(TimeRange range, std::initializer_list> initializerList) : + BoundedTimeline(range, initializerList.begin(), initializerList.end()) + {} + + TimeRange getRange() const override { + return range; + } + + using Timeline::set; + + iterator set(Timed timedValue) override { + // Exit if the value's range is completely out of bounds + if (timedValue.getEnd() <= range.getStart() || timedValue.getStart() >= range.getEnd()) { + return end(); + } + + // Clip the value's range to bounds + TimeRange& valueRange = timedValue.getTimeRange(); + valueRange.resize(max(range.getStart(), valueRange.getStart()), min(range.getEnd(), valueRange.getEnd())); + + return Timeline::set(timedValue); + } + + void shift(time_type offset) override { + Timeline::shift(offset); + range.shift(offset); + } + + bool operator==(const BoundedTimeline& rhs) const { + return Timeline::equals(rhs) && range == rhs.range; + } + + bool operator!=(const BoundedTimeline& rhs) const { + return !operator==(rhs); + } + +private: + TimeRange range; +}; \ No newline at end of file diff --git a/src/ContinuousTimeline.h b/src/ContinuousTimeline.h new file mode 100644 index 0000000..59bc477 --- /dev/null +++ b/src/ContinuousTimeline.h @@ -0,0 +1,39 @@ +#pragma once + +#include "BoundedTimeline.h" + +template +class ContinuousTimeline : public BoundedTimeline { + +public: + ContinuousTimeline(TimeRange range, T defaultValue) : + BoundedTimeline(range), + defaultValue(defaultValue) + { + // Virtual function call in constructor. Derived constructors shouldn't call this one! + ContinuousTimeline::clear(range); + } + + template + ContinuousTimeline(TimeRange range, T defaultValue, InputIterator first, InputIterator last) : + ContinuousTimeline(range, defaultValue) + { + // Virtual function calls in constructor. Derived constructors shouldn't call this one! + for (auto it = first; it != last; ++it) { + ContinuousTimeline::set(*it); + } + } + + ContinuousTimeline(TimeRange range, T defaultValue, std::initializer_list> initializerList) : + ContinuousTimeline(range, defaultValue, initializerList.begin(), initializerList.end()) + {} + + using BoundedTimeline::clear; + + void clear(const TimeRange& range) override { + set(Timed(range, defaultValue)); + } + +private: + T defaultValue; +}; diff --git a/src/Exporter.cpp b/src/Exporter.cpp index 80fc85f..ae5d003 100644 --- a/src/Exporter.cpp +++ b/src/Exporter.cpp @@ -1,5 +1,4 @@ #include "Exporter.h" -#include #include #include #include @@ -43,7 +42,7 @@ std::vector> dummyShapeIfEmpty(const Timeline& shapes) { return result; } -void TSVExporter::exportShapes(const boost::filesystem::path& inputFilePath, const Timeline& shapes, std::ostream& outputStream) { +void TSVExporter::exportShapes(const boost::filesystem::path& inputFilePath, const ContinuousTimeline& shapes, std::ostream& outputStream) { UNUSED(inputFilePath); // Output shapes with start times @@ -55,7 +54,7 @@ void TSVExporter::exportShapes(const boost::filesystem::path& inputFilePath, con outputStream << formatDuration(shapes.getRange().getEnd()) << "\t" << Shape::A << "\n"; } -void XMLExporter::exportShapes(const boost::filesystem::path& inputFilePath, const Timeline& shapes, std::ostream& outputStream) { +void XMLExporter::exportShapes(const boost::filesystem::path& inputFilePath, const ContinuousTimeline& shapes, std::ostream& outputStream) { ptree tree; // Add metadata @@ -94,7 +93,7 @@ string escapeJSONString(const string& s) { return result; } -void JSONExporter::exportShapes(const boost::filesystem::path& inputFilePath, const Timeline& shapes, std::ostream& outputStream) { +void JSONExporter::exportShapes(const boost::filesystem::path& inputFilePath, const ContinuousTimeline& shapes, std::ostream& outputStream) { // Export as JSON. // I'm not using a library because the code is short enough without one and it lets me control the formatting. outputStream << "{\n"; diff --git a/src/Exporter.h b/src/Exporter.h index cd66502..3389b5b 100644 --- a/src/Exporter.h +++ b/src/Exporter.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include "ContinuousTimeline.h" #include #include @@ -26,20 +26,20 @@ std::istream& operator>>(std::istream& stream, ExportFormat& value); class Exporter { public: virtual ~Exporter() {} - virtual void exportShapes(const boost::filesystem::path& inputFilePath, const Timeline& shapes, std::ostream& outputStream) = 0; + virtual void exportShapes(const boost::filesystem::path& inputFilePath, const ContinuousTimeline& shapes, std::ostream& outputStream) = 0; }; class TSVExporter : public Exporter { public: - void exportShapes(const boost::filesystem::path& inputFilePath, const Timeline& shapes, std::ostream& outputStream) override; + void exportShapes(const boost::filesystem::path& inputFilePath, const ContinuousTimeline& shapes, std::ostream& outputStream) override; }; class XMLExporter : public Exporter { public: - void exportShapes(const boost::filesystem::path& inputFilePath, const Timeline& shapes, std::ostream& outputStream) override; + void exportShapes(const boost::filesystem::path& inputFilePath, const ContinuousTimeline& shapes, std::ostream& outputStream) override; }; class JSONExporter : public Exporter { public: - void exportShapes(const boost::filesystem::path& inputFilePath, const Timeline& shapes, std::ostream& outputStream) override; + void exportShapes(const boost::filesystem::path& inputFilePath, const ContinuousTimeline& shapes, std::ostream& outputStream) override; }; diff --git a/src/Phone.cpp b/src/Phone.cpp index 752c9e0..a2cf98e 100644 --- a/src/Phone.cpp +++ b/src/Phone.cpp @@ -2,6 +2,7 @@ #include "Phone.h" using std::string; +using boost::optional; PhoneConverter& PhoneConverter::get() { static PhoneConverter converter; @@ -14,7 +15,6 @@ string PhoneConverter::getTypeName() { EnumConverter::member_data PhoneConverter::getMemberData() { return member_data{ - { Phone::None, "None" }, { Phone::Unknown, "Unknown" }, { Phone::AO, "AO" }, { Phone::AA, "AA" }, @@ -58,8 +58,7 @@ EnumConverter::member_data PhoneConverter::getMemberData() { }; } -boost::optional PhoneConverter::tryParse(const string& s) { - if (s == "SIL") return Phone::None; +optional PhoneConverter::tryParse(const string& s) { auto result = EnumConverter::tryParse(s); return result ? result : Phone::Unknown; } diff --git a/src/Phone.h b/src/Phone.h index 76894b9..ef9183c 100644 --- a/src/Phone.h +++ b/src/Phone.h @@ -4,7 +4,6 @@ // Defines a subset of the Arpabet enum class Phone { - None, Unknown, ///////// diff --git a/src/Shape.h b/src/Shape.h index 3b49904..4acab6b 100644 --- a/src/Shape.h +++ b/src/Shape.h @@ -7,7 +7,6 @@ // For reference, see http://sunewatts.dk/lipsync/lipsync/article_02.php // For visual examples, see https://flic.kr/s/aHsj86KR4J. Their shapes "BMP".."L" map to A..H. enum class Shape { - Invalid = -1, A, // Closed mouth (silence, M, B, P) B, // Clenched teeth (most vowels, m[e]n) C, // Mouth slightly open (b[ir]d, s[ay], w[i]n...) diff --git a/src/TimeRange.cpp b/src/TimeRange.cpp index 0def66e..72e3d8d 100644 --- a/src/TimeRange.cpp +++ b/src/TimeRange.cpp @@ -4,6 +4,11 @@ using time_type = TimeRange::time_type; +TimeRange TimeRange::zero() { + static TimeRange zero(time_type::zero(), time_type::zero()); + return zero; +} + TimeRange::TimeRange(time_type start, time_type end) : start(start), end(end) @@ -23,6 +28,10 @@ time_type TimeRange::getLength() const { return end - start; } +bool TimeRange::empty() const { + return start == end; +} + void TimeRange::resize(const TimeRange& newRange) { start = newRange.start; end = newRange.end; diff --git a/src/TimeRange.h b/src/TimeRange.h index eef8a43..3f6ba63 100644 --- a/src/TimeRange.h +++ b/src/TimeRange.h @@ -5,6 +5,8 @@ class TimeRange { public: using time_type = centiseconds; + static TimeRange zero(); + TimeRange(time_type start, time_type end); TimeRange(const TimeRange&) = default; TimeRange(TimeRange&&) = default; @@ -15,6 +17,7 @@ public: time_type getStart() const; time_type getEnd() const; time_type getLength() const; + bool empty() const; void resize(const TimeRange& newRange); void resize(time_type start, time_type end); diff --git a/src/Timed.h b/src/Timed.h index 5ed0ddc..356879f 100644 --- a/src/Timed.h +++ b/src/Timed.h @@ -4,15 +4,14 @@ #include template -class Timed : public TimeRange { +class Timed { public: - Timed(time_type start, time_type end, const TValue& value) : - TimeRange(start, end), - value(value) + Timed(TimeRange::time_type start, TimeRange::time_type end, const TValue& value) : + Timed(TimeRange(start, end), value) {} Timed(const TimeRange& timeRange, const TValue& value) : - TimeRange(timeRange), + timeRange(timeRange), value(value) {} @@ -22,6 +21,26 @@ public: Timed& operator=(const Timed&) = default; Timed& operator=(Timed&&) = default; + TimeRange& getTimeRange() { + return timeRange; + } + + const TimeRange& getTimeRange() const { + return timeRange; + } + + TimeRange::time_type getStart() const { + return timeRange.getStart(); + } + + TimeRange::time_type getEnd() const { + return timeRange.getEnd(); + } + + void setTimeRange(const TimeRange& timeRange) { + this->timeRange = timeRange; + } + TValue& getValue() { return value; } @@ -30,12 +49,12 @@ public: return value; } - void setValue(TValue value) { + void setValue(const TValue& value) { this->value = value; } bool operator==(const Timed& rhs) const { - return TimeRange::operator==(rhs) && value == rhs.value; + return timeRange == rhs.timeRange && value == rhs.value; } bool operator!=(const Timed& rhs) const { @@ -43,6 +62,7 @@ public: } private: + TimeRange timeRange; TValue value; }; @@ -50,3 +70,57 @@ template std::ostream& operator<<(std::ostream& stream, const Timed& timedValue) { return stream << "Timed(" << timedValue.getStart() << ", " << timedValue.getEnd() << ", " << timedValue.getValue() << ")"; } + +template<> +class Timed { +public: + Timed(TimeRange::time_type start, TimeRange::time_type end) : + Timed(TimeRange(start, end)) + {} + + Timed(const TimeRange& timeRange) : + timeRange(timeRange) + {} + + Timed(const Timed&) = default; + Timed(Timed&&) = default; + + Timed& operator=(const Timed&) = default; + Timed& operator=(Timed&&) = default; + + TimeRange& getTimeRange() { + return timeRange; + } + + const TimeRange& getTimeRange() const { + return timeRange; + } + + TimeRange::time_type getStart() const { + return timeRange.getStart(); + } + + TimeRange::time_type getEnd() const { + return timeRange.getEnd(); + } + + void setTimeRange(const TimeRange& timeRange) { + this->timeRange = timeRange; + } + + bool operator==(const Timed& rhs) const { + return timeRange == rhs.timeRange; + } + + bool operator!=(const Timed& rhs) const { + return !operator==(rhs); + } + +private: + TimeRange timeRange; +}; + +template +std::ostream& operator<<(std::ostream& stream, const Timed& timedValue) { + return stream << "Timed(" << timedValue.getTimeRange().getStart() << ", " << timedValue.getTimeRange().getEnd() << ")"; +} diff --git a/src/Timeline.h b/src/Timeline.h index 8da4f49..8d84d91 100644 --- a/src/Timeline.h +++ b/src/Timeline.h @@ -1,7 +1,30 @@ #pragma once #include "Timed.h" #include -#include +#include +#include +#include "tools.h" + +enum class FindMode { + SampleLeft, + SampleRight, + SearchLeft, + SearchRight +}; + +namespace internal { + template + bool valueEquals(const Timed& lhs, const Timed& rhs) { + return lhs.getValue() == rhs.getValue(); + } + + template<> + inline bool valueEquals(const Timed& lhs, const Timed& rhs) { + UNUSED(lhs); + UNUSED(rhs); + return true; + } +} template class Timeline { @@ -16,6 +39,9 @@ private: bool operator()(const time_type& lhs, const Timed& rhs) const { return lhs < rhs.getStart(); } + bool operator()(const Timed& lhs, const time_type& rhs) const { + return lhs.getStart() < rhs; + } using is_transparent = int; }; @@ -29,14 +55,28 @@ public: class reference { public: - using const_reference = const T&; - - operator const_reference() const { - return timeline.get(time).getValue(); + operator boost::optional() const { + auto optional = timeline.get(time); + return optional ? optional->getValue() : boost::optional(); } - reference& operator=(const T& value) { - timeline.set(time, time + time_type(1), value); + operator boost::optional() const { + auto optional = timeline.get(time); + return optional ? optional->getValue() : boost::optional(); + } + + operator const T&() const { + auto optional = timeline.get(time); + assert(optional); + return optional->getValue(); + } + + reference& operator=(boost::optional value) { + if (value) { + timeline.set(time, time + time_type(1), *value); + } else { + timeline.clear(time, time + time_type(1)); + } return *this; } @@ -52,36 +92,22 @@ public: time_type time; }; - explicit Timeline(const Timed timedValue) : - elements(), - range(timedValue) - { - if (timedValue.getLength() != time_type::zero()) { - elements.insert(timedValue); - } - }; - - explicit Timeline(const TimeRange& timeRange, const T& value = T()) : - Timeline(Timed(timeRange, value)) - { } - - Timeline(time_type start, time_type end, const T& value = T()) : - Timeline(Timed(start, end, value)) - {} + Timeline() {} template - Timeline(InputIterator first, InputIterator last, const T& value = T()) : - Timeline(getRange(first, last), value) - { + Timeline(InputIterator first, InputIterator last) { for (auto it = first; it != last; ++it) { - set(*it); + // Virtual function call in constructor. Derived constructors don't call this one. + Timeline::set(*it); } } - explicit Timeline(std::initializer_list> initializerList, const T& value = T()) : - Timeline(initializerList.begin(), initializerList.end(), value) + explicit Timeline(std::initializer_list> initializerList) : + Timeline(initializerList.begin(), initializerList.end()) {} + virtual ~Timeline() {} + bool empty() const { return elements.empty(); } @@ -90,8 +116,10 @@ public: return elements.size(); } - const TimeRange& getRange() const { - return range; + virtual TimeRange getRange() const { + return empty() + ? TimeRange(time_type::zero(), time_type::zero()) + : TimeRange(begin()->getStart(), rbegin()->getEnd()); } iterator begin() const { @@ -110,79 +138,101 @@ public: return elements.rend(); } - iterator find(time_type time) const { - if (time < range.getStart() || time >= range.getEnd()) { - return elements.end(); + iterator find(time_type time, FindMode findMode = FindMode::SampleRight) const { + switch (findMode) { + case FindMode::SampleLeft: { + iterator left = find(time, FindMode::SearchLeft); + return left != end() && left->getEnd() >= time ? left : end(); } + case FindMode::SampleRight: { + iterator right = find(time, FindMode::SearchRight); + return right != end() && right->getStart() <= time ? right : end(); + } + case FindMode::SearchLeft: { + // Get first element starting >= time + iterator it = elements.lower_bound(time); - iterator it = elements.upper_bound(time); - --it; - return it; + // Go one element back + return it != begin() ? --it : end(); + } + case FindMode::SearchRight: { + // Get first element starting > time + iterator it = elements.upper_bound(time); + + // Go one element back + if (it != begin()) { + iterator left = it; + --left; + if (left->getEnd() > time) return left; + } + return it; + } + default: + throw std::invalid_argument("Unexpected find mode."); + } } - const Timed& get(time_type time) const { + boost::optional&> get(time_type time) const { iterator it = find(time); - if (it == elements.end()) { - throw std::invalid_argument("Argument out of range."); - } - return *it; + return (it != end()) ? *it : boost::optional&>(); } - iterator set(Timed timedValue) { - // Make sure the timed value overlaps with our range - if (timedValue.getEnd() <= range.getStart() || timedValue.getStart() >= range.getEnd()) { - return elements.end(); - } - - // Make sure the timed value is not empty - if (timedValue.getLength() == time_type::zero()) { - return elements.end(); - } - - // Trim the timed value to our range - timedValue.resize( - std::max(timedValue.getStart(), range.getStart()), - std::min(timedValue.getEnd(), range.getEnd())); - - // Extend the timed value if it touches elements with equal value - bool isFlushLeft = timedValue.getStart() == range.getStart(); - if (!isFlushLeft) { - iterator elementBefore = find(timedValue.getStart() - time_type(1)); - if (elementBefore->getValue() == timedValue.getValue()) { - timedValue.resize(elementBefore->getStart(), timedValue.getEnd()); - } - } - bool isFlushRight = timedValue.getEnd() == range.getEnd(); - if (!isFlushRight) { - iterator elementAfter = find(timedValue.getEnd()); - if (elementAfter->getValue() == timedValue.getValue()) { - timedValue.resize(timedValue.getStart(), elementAfter->getEnd()); - } - } + virtual void clear(const TimeRange& range) { + // Make sure the time range is not empty + if (range.empty()) return; // Split overlapping elements - splitAt(timedValue.getStart()); - splitAt(timedValue.getEnd()); + splitAt(range.getStart()); + splitAt(range.getEnd()); // Erase overlapping elements - elements.erase(find(timedValue.getStart()), find(timedValue.getEnd())); + elements.erase(find(range.getStart(), FindMode::SearchRight), find(range.getEnd(), FindMode::SearchRight)); + } + + void clear(time_type start, time_type end) { + clear(TimeRange(start, end)); + } + + virtual iterator set(Timed timedValue) { + // Make sure the timed value is not empty + if (timedValue.getTimeRange().empty()) { + return end(); + } + + // Extend the timed value if it touches elements with equal value + iterator elementBefore = find(timedValue.getStart(), FindMode::SampleLeft); + if (elementBefore != end() && ::internal::valueEquals(*elementBefore, timedValue)) { + timedValue.getTimeRange().resize(elementBefore->getStart(), timedValue.getEnd()); + } + iterator elementAfter = find(timedValue.getEnd(), FindMode::SampleRight); + if (elementAfter != end() && ::internal::valueEquals(*elementAfter, timedValue)) { + timedValue.getTimeRange().resize(timedValue.getStart(), elementAfter->getEnd()); + } + + // Erase overlapping elements + Timeline::clear(timedValue.getTimeRange()); // Add timed value return elements.insert(timedValue).first; } - iterator set(const TimeRange& timeRange, const T& value) { + template + iterator set(const TimeRange& timeRange, const std::enable_if_t::value, T>& value) { return set(Timed(timeRange, value)); } - iterator set(time_type start, time_type end, const T& value) { + template + iterator set(time_type start, time_type end, const std::enable_if_t::value, T>& value) { return set(Timed(start, end, value)); } + template + std::enable_if_t::value, iterator> + set(time_type start, time_type end) { + return set(Timed(start, end)); + } + reference operator[](time_type time) { - if (time < range.getStart() || time >= range.getEnd()) { - throw std::invalid_argument("Argument out of range."); - } return reference(*this, time); } @@ -191,13 +241,12 @@ public: return reference(*this, time); } - void shift(time_type offset) { + virtual void shift(time_type offset) { if (offset == time_type::zero()) return; - range.shift(offset); set_type newElements; for (Timed element : elements) { - element.shift(offset); + element.getTimeRange().shift(offset); newElements.insert(element); } elements = std::move(newElements); @@ -209,44 +258,34 @@ public: Timeline& operator=(Timeline&&) = default; bool operator==(const Timeline& rhs) const { - return range == rhs.range && elements == rhs.elements; + return equals(rhs); } bool operator!=(const Timeline& rhs) const { - return !operator==(rhs); + return !equals(rhs); + } + +protected: + bool equals(const Timeline& rhs) const { + return elements == rhs.elements; } private: - template - static TimeRange getRange(InputIterator first, InputIterator last) { - if (first == last) { - return TimeRange(time_type::zero(), time_type::zero()); - } - - time_type start = time_type::max(); - time_type end = time_type::min(); - for (auto it = first; it != last; ++it) { - start = std::min(start, it->getStart()); - end = std::max(end, it->getEnd()); - } - return TimeRange(start, end); - } - void splitAt(time_type splitTime) { - if (splitTime == range.getStart() || splitTime == range.getEnd()) return; - iterator elementBefore = find(splitTime - time_type(1)); iterator elementAfter = find(splitTime); - if (elementBefore != elementAfter) return; + if (elementBefore != elementAfter || elementBefore == end()) return; - Timed tmp = *elementBefore; + Timed first = *elementBefore; + Timed second = *elementBefore; elements.erase(elementBefore); - elements.insert(Timed(tmp.getStart(), splitTime, tmp.getValue())); - elements.insert(Timed(splitTime, tmp.getEnd(), tmp.getValue())); + first.getTimeRange().resize(first.getStart(), splitTime); + elements.insert(first); + second.getTimeRange().resize(splitTime, second.getEnd()); + elements.insert(second); } set_type elements; - TimeRange range; }; template diff --git a/src/audio/WaveFileReader.cpp b/src/audio/WaveFileReader.cpp index f3b574a..37c19c9 100644 --- a/src/audio/WaveFileReader.cpp +++ b/src/audio/WaveFileReader.cpp @@ -2,7 +2,6 @@ #include #include "WaveFileReader.h" #include "ioTools.h" -#include using std::runtime_error; using fmt::format; diff --git a/src/audio/voiceActivityDetection.cpp b/src/audio/voiceActivityDetection.cpp index 661b9c7..19f7ecd 100644 --- a/src/audio/voiceActivityDetection.cpp +++ b/src/audio/voiceActivityDetection.cpp @@ -3,6 +3,7 @@ #include