summaryrefslogtreecommitdiffstats
path: root/src/audio
diff options
context:
space:
mode:
authorJan Holthuis <jholthuis@mixxx.org>2021-07-01 12:36:22 +0200
committerJan Holthuis <jholthuis@mixxx.org>2021-07-01 16:39:40 +0200
commit7a05af6d9c66a6a794406f1d07dc06c29b81af18 (patch)
treee58686a094a8d323ed72f6ac3d135041c48adcfe /src/audio
parent3d9df08d47230155d2ee70ae6b6df20316e83546 (diff)
FramePos: Move class alongside other audio helper classes
Diffstat (limited to 'src/audio')
-rw-r--r--src/audio/frame.h120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/audio/frame.h b/src/audio/frame.h
new file mode 100644
index 0000000000..32ff916650
--- /dev/null
+++ b/src/audio/frame.h
@@ -0,0 +1,120 @@
+#pragma once
+
+#include <QDebug>
+#include <limits>
+
+namespace mixxx {
+namespace audio {
+/// FrameDiff_t can be used to store the difference in position between
+/// two frames and to store the length of a segment of track in terms of frames.
+typedef double FrameDiff_t;
+
+/// FramePos defines the position of a frame in a track
+/// with respect to a fixed origin, i.e. start of the track.
+class FramePos final {
+ public:
+ typedef double value_t;
+ static constexpr value_t kStartValue = 0;
+ static constexpr value_t kInvalidValue = std::numeric_limits<FramePos::value_t>::quiet_NaN();
+
+ constexpr FramePos()
+ : m_dFramePos(kStartValue) {
+ }
+
+ constexpr explicit FramePos(value_t dFramePos)
+ : m_dFramePos(dFramePos) {
+ }
+
+ void setValue(value_t dFramePos) {
+ m_dFramePos = dFramePos;
+ }
+
+ value_t getValue() const {
+ return m_dFramePos;
+ }
+
+ FramePos& operator+=(FrameDiff_t increment) {
+ m_dFramePos += increment;
+ return *this;
+ }
+
+ FramePos& operator-=(FrameDiff_t decrement) {
+ m_dFramePos -= decrement;
+ return *this;
+ }
+
+ FramePos& operator*=(double multiple) {
+ m_dFramePos *= multiple;
+ return *this;
+ }
+
+ FramePos& operator/=(double divisor) {
+ m_dFramePos /= divisor;
+ return *this;
+ }
+
+ private:
+ value_t m_dFramePos;
+};
+
+// FramePos can be added to and subtracted from a FrameDiff_t
+inline FramePos operator+(FramePos framePos, FrameDiff_t frameDiff) {
+ return FramePos(framePos.getValue() + frameDiff);
+}
+
+inline FramePos operator-(FramePos framePos, FrameDiff_t frameDiff) {
+ return FramePos(framePos.getValue() - frameDiff);
+}
+
+inline FrameDiff_t operator-(FramePos framePos1, FramePos framePos2) {
+ return framePos1.getValue() - framePos2.getValue();
+}
+
+// Adding two FramePos is not allowed since every FramePos shares a common
+// reference or origin i.e. the start of the track.
+
+// FramePos can be multiplied or divided by a double
+inline FramePos operator*(FramePos framePos, double multiple) {
+ return FramePos(framePos.getValue() * multiple);
+}
+
+inline FramePos operator/(FramePos framePos, double divisor) {
+ return FramePos(framePos.getValue() / divisor);
+}
+
+inline bool operator<(FramePos frame1, FramePos frame2) {
+ return frame1.getValue() < frame2.getValue();
+}
+
+inline bool operator<=(FramePos frame1, FramePos frame2) {
+ return frame1.getValue() <= frame2.getValue();
+}
+
+inline bool operator>(FramePos frame1, FramePos frame2) {
+ return frame1.getValue() > frame2.getValue();
+}
+
+inline bool operator>=(FramePos frame1, FramePos frame2) {
+ return frame1.getValue() >= frame2.getValue();
+}
+
+inline bool operator==(FramePos frame1, FramePos frame2) {
+ return frame1.getValue() == frame2.getValue();
+}
+
+inline bool operator!=(FramePos frame1, FramePos frame2) {
+ return !(frame1.getValue() == frame2.getValue());
+}
+
+inline QDebug operator<<(QDebug dbg, FramePos arg) {
+ dbg << arg.getValue();
+ return dbg;
+}
+
+constexpr FramePos kInvalidFramePos = FramePos(FramePos::kInvalidValue);
+constexpr FramePos kStartFramePos = FramePos(FramePos::kStartValue);
+} // namespace audio
+} // namespace mixxx
+
+Q_DECLARE_TYPEINFO(mixxx::audio::FramePos, Q_MOVABLE_TYPE);
+Q_DECLARE_METATYPE(mixxx::audio::FramePos);