00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef RAUL_TIME_SLICE_HPP
00019 #define RAUL_TIME_SLICE_HPP
00020
00021 #include <cassert>
00022 #include <cmath>
00023 #include <boost/utility.hpp>
00024 #include "raul/TimeStamp.hpp"
00025
00026 namespace Raul {
00027
00028
00029
00030
00049 class TimeSlice : public boost::noncopyable {
00050 public:
00051 TimeSlice(uint32_t rate, uint32_t ppqn, double bpm)
00052 : _tick_rate(rate)
00053 , _beat_rate(60.0/bpm)
00054 , _start_ticks(Raul::TimeUnit(Raul::TimeUnit::FRAMES, rate), 0, 0)
00055 , _length_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
00056 , _start_beats(TimeUnit(TimeUnit::BEATS, ppqn), 0, 0)
00057 , _length_beats(TimeUnit(TimeUnit::BEATS, ppqn), 0, 0)
00058 , _offset_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
00059 {}
00060
00066 void set_slice(TimeStamp start, TimeDuration length) {
00067 assert(start.unit() == ticks_unit());
00068 assert(length.unit() == ticks_unit());
00069 _start_ticks = start;
00070 _length_ticks = length;
00071 update_beat_time();
00072 }
00073
00074 void set_length(TimeDuration length) {
00075 assert(length.unit() == ticks_unit());
00076 _length_ticks = length;
00077 _length_beats = ticks_to_beats(_length_ticks);
00078 }
00079
00080 bool contains(TimeStamp time) {
00081 return (time >= start_ticks() && time < start_ticks() + length_ticks());
00082 }
00083
00084 double tick_rate() { return _tick_rate; }
00085 double beat_rate() { return _beat_rate; }
00086 double bpm() { return 60/_beat_rate; }
00087
00088 void set_tick_rate(double tick_rate) {
00089 _tick_rate = tick_rate;
00090 update_beat_time();
00091 }
00092
00093 void set_bpm(double bpm) {
00094 _beat_rate = 60.0/bpm;
00095 update_beat_time();
00096 }
00097
00098 inline TimeStamp beats_to_seconds(TimeStamp beats) const {
00099 return TimeStamp(real_unit(), beats.to_double() * 1/(double)_beat_rate);
00100 }
00101
00102 inline TimeStamp beats_to_ticks(TimeStamp beats) const {
00103 return TimeStamp(ticks_unit(), beats.to_double() / (double)_beat_rate * _tick_rate);
00104 }
00105
00106 inline TimeStamp ticks_to_seconds(TimeStamp ticks) const {
00107 return TimeStamp(real_unit(), ticks.ticks() * 1/(double)_tick_rate);
00108 }
00109
00110 inline TimeStamp ticks_to_beats(TimeStamp ticks) const {
00111 return TimeStamp(beats_unit(), ticks.ticks() * 1/(double)_tick_rate * _beat_rate);
00112 }
00113
00115 inline TimeStamp start_ticks() const { return _start_ticks; }
00116
00118 inline TimeDuration length_ticks() const { return _length_ticks; }
00119
00121 inline TimeStamp start_beats() const { return _start_beats; }
00122
00124 inline TimeDuration length_beats() const { return _length_beats; }
00125
00127 inline void set_offset(TimeDuration offset) { _offset_ticks = offset; }
00128
00130 inline TimeDuration offset_ticks() const { return _offset_ticks; }
00131
00132 inline TimeUnit beats_unit() const { return _start_beats.unit(); }
00133 inline TimeUnit ticks_unit() const { return _start_ticks.unit(); }
00134 inline TimeUnit real_unit() const { return TimeUnit(TimeUnit::SECONDS, 0); }
00135
00136 private:
00137 inline void update_beat_time() {
00138 _start_beats = ticks_to_beats(_start_ticks);
00139 _length_beats = ticks_to_beats(_length_ticks);
00140 }
00141
00142
00143 double _tick_rate;
00144 double _beat_rate;
00145
00146
00147 TimeStamp _start_ticks;
00148 TimeDuration _length_ticks;
00149 TimeStamp _start_beats;
00150 TimeDuration _length_beats;
00151
00152 TimeDuration _offset_ticks;
00153 };
00154
00155
00156 }
00157
00158 #endif // RAUL_TIME_SLICE_HPP