summaryrefslogtreecommitdiffstats
path: root/include/raul/TimeSlice.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/raul/TimeSlice.hpp')
-rw-r--r--include/raul/TimeSlice.hpp215
1 files changed, 113 insertions, 102 deletions
diff --git a/include/raul/TimeSlice.hpp b/include/raul/TimeSlice.hpp
index 90eb7d4..3efa180 100644
--- a/include/raul/TimeSlice.hpp
+++ b/include/raul/TimeSlice.hpp
@@ -44,111 +44,122 @@ namespace Raul {
*
* \ingroup raul
*/
-class TimeSlice : public Noncopyable {
+class TimeSlice : public Noncopyable
+{
public:
- TimeSlice(uint32_t rate, uint32_t ppqn, double bpm)
- : _tick_rate(rate)
- , _beat_rate(60.0/bpm)
- , _start_ticks(Raul::TimeUnit(Raul::TimeUnit::FRAMES, rate), 0, 0)
- , _length_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
- , _start_beats(TimeUnit(TimeUnit::BEATS, ppqn), 0, 0)
- , _length_beats(TimeUnit(TimeUnit::BEATS, ppqn), 0, 0)
- , _offset_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
- {}
-
- /** Set the start and length of the slice.
- *
- * Note that external offset is not affected by this, don't forget to reset
- * the offset each cycle!
- */
- void set_slice(TimeStamp start, TimeDuration length) {
- assert(start.unit() == ticks_unit());
- assert(length.unit() == ticks_unit());
- _start_ticks = start;
- _length_ticks = length;
- update_beat_time();
- }
-
- void set_length(TimeDuration length) {
- assert(length.unit() == ticks_unit());
- _length_ticks = length;
- _length_beats = ticks_to_beats(_length_ticks);
- }
-
- bool contains(TimeStamp time) const {
- return (time >= start_ticks() && time < start_ticks() + length_ticks());
- }
-
- double tick_rate() const { return _tick_rate; }
- double beat_rate() const { return _beat_rate; }
- double bpm() const { return 60/_beat_rate; }
-
- void set_tick_rate(double tick_rate) {
- _tick_rate = tick_rate;
- update_beat_time();
- }
-
- void set_bpm(double bpm) {
- _beat_rate = 60.0/bpm;
- update_beat_time();
- }
-
- inline TimeStamp beats_to_seconds(TimeStamp beats) const {
- return {real_unit(), beats.to_double() * 1/_beat_rate};
- }
-
- inline TimeStamp beats_to_ticks(TimeStamp beats) const {
- return {ticks_unit(), beats.to_double() * _beat_rate * _tick_rate};
- }
-
- inline TimeStamp ticks_to_seconds(TimeStamp ticks) const {
- return {real_unit(), ticks.ticks() * 1/_tick_rate};
- }
-
- inline TimeStamp ticks_to_beats(TimeStamp ticks) const {
- return {beats_unit(), ticks.ticks() * 1/_tick_rate * _beat_rate};
- }
-
- /** Start of current sub-cycle in ticks */
- inline TimeStamp start_ticks() const { return _start_ticks; }
-
- /** Length of current sub-cycle in ticks */
- inline TimeDuration length_ticks() const { return _length_ticks; }
-
- /** Start of current sub-cycle in beats */
- inline TimeStamp start_beats() const { return _start_beats; }
-
- /** Length of current sub-cycle in beats */
- inline TimeDuration length_beats() const { return _length_beats; }
-
- /** Set the offset between real-time and timeslice-time. */
- inline void set_offset(TimeDuration offset) { _offset_ticks = offset; }
-
- /** Offset relative to external (e.g Jack) time */
- inline TimeDuration offset_ticks() const { return _offset_ticks; }
-
- inline TimeUnit beats_unit() const { return _start_beats.unit(); }
- inline TimeUnit ticks_unit() const { return _start_ticks.unit(); }
-
- static inline TimeUnit real_unit() { return {TimeUnit::SECONDS, 0}; }
+ TimeSlice(uint32_t rate, uint32_t ppqn, double bpm)
+ : _tick_rate(rate)
+ , _beat_rate(60.0 / bpm)
+ , _start_ticks(Raul::TimeUnit(Raul::TimeUnit::FRAMES, rate), 0, 0)
+ , _length_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
+ , _start_beats(TimeUnit(TimeUnit::BEATS, ppqn), 0, 0)
+ , _length_beats(TimeUnit(TimeUnit::BEATS, ppqn), 0, 0)
+ , _offset_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
+ {}
+
+ /** Set the start and length of the slice.
+ *
+ * Note that external offset is not affected by this, don't forget to reset
+ * the offset each cycle!
+ */
+ void set_slice(TimeStamp start, TimeDuration length)
+ {
+ assert(start.unit() == ticks_unit());
+ assert(length.unit() == ticks_unit());
+ _start_ticks = start;
+ _length_ticks = length;
+ update_beat_time();
+ }
+
+ void set_length(TimeDuration length)
+ {
+ assert(length.unit() == ticks_unit());
+ _length_ticks = length;
+ _length_beats = ticks_to_beats(_length_ticks);
+ }
+
+ bool contains(TimeStamp time) const
+ {
+ return (time >= start_ticks() && time < start_ticks() + length_ticks());
+ }
+
+ double tick_rate() const { return _tick_rate; }
+ double beat_rate() const { return _beat_rate; }
+ double bpm() const { return 60 / _beat_rate; }
+
+ void set_tick_rate(double tick_rate)
+ {
+ _tick_rate = tick_rate;
+ update_beat_time();
+ }
+
+ void set_bpm(double bpm)
+ {
+ _beat_rate = 60.0 / bpm;
+ update_beat_time();
+ }
+
+ inline TimeStamp beats_to_seconds(TimeStamp beats) const
+ {
+ return {real_unit(), beats.to_double() * 1 / _beat_rate};
+ }
+
+ inline TimeStamp beats_to_ticks(TimeStamp beats) const
+ {
+ return {ticks_unit(), beats.to_double() * _beat_rate * _tick_rate};
+ }
+
+ inline TimeStamp ticks_to_seconds(TimeStamp ticks) const
+ {
+ return {real_unit(), ticks.ticks() * 1 / _tick_rate};
+ }
+
+ inline TimeStamp ticks_to_beats(TimeStamp ticks) const
+ {
+ return {beats_unit(), ticks.ticks() * 1 / _tick_rate * _beat_rate};
+ }
+
+ /** Start of current sub-cycle in ticks */
+ inline TimeStamp start_ticks() const { return _start_ticks; }
+
+ /** Length of current sub-cycle in ticks */
+ inline TimeDuration length_ticks() const { return _length_ticks; }
+
+ /** Start of current sub-cycle in beats */
+ inline TimeStamp start_beats() const { return _start_beats; }
+
+ /** Length of current sub-cycle in beats */
+ inline TimeDuration length_beats() const { return _length_beats; }
+
+ /** Set the offset between real-time and timeslice-time. */
+ inline void set_offset(TimeDuration offset) { _offset_ticks = offset; }
+
+ /** Offset relative to external (e.g Jack) time */
+ inline TimeDuration offset_ticks() const { return _offset_ticks; }
+
+ inline TimeUnit beats_unit() const { return _start_beats.unit(); }
+ inline TimeUnit ticks_unit() const { return _start_ticks.unit(); }
+
+ static inline TimeUnit real_unit() { return {TimeUnit::SECONDS, 0}; }
private:
- inline void update_beat_time() {
- _start_beats = ticks_to_beats(_start_ticks);
- _length_beats = ticks_to_beats(_length_ticks);
- }
-
- // Rate/Tempo
- double _tick_rate; ///< Tick rate in Hz (e.g. sample rate)
- double _beat_rate; ///< Beat rate in Hz
-
- // Current time
- TimeStamp _start_ticks; ///< Current window start in ticks
- TimeDuration _length_ticks; ///< Current window length in ticks
- TimeStamp _start_beats; ///< Current window start in beats
- TimeDuration _length_beats; ///< Current window length in beats
-
- TimeDuration _offset_ticks; ///< Offset to global time
+ inline void update_beat_time()
+ {
+ _start_beats = ticks_to_beats(_start_ticks);
+ _length_beats = ticks_to_beats(_length_ticks);
+ }
+
+ // Rate/Tempo
+ double _tick_rate; ///< Tick rate in Hz (e.g. sample rate)
+ double _beat_rate; ///< Beat rate in Hz
+
+ // Current time
+ TimeStamp _start_ticks; ///< Current window start in ticks
+ TimeDuration _length_ticks; ///< Current window length in ticks
+ TimeStamp _start_beats; ///< Current window start in beats
+ TimeDuration _length_beats; ///< Current window length in beats
+
+ TimeDuration _offset_ticks; ///< Offset to global time
};
} // namespace Raul