RAUL  0.8.0
TimeSlice.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_TIME_SLICE_HPP
19 #define RAUL_TIME_SLICE_HPP
20 
21 #include <cassert>
22 #include <cmath>
23 
24 #include <boost/utility.hpp>
25 
26 #include "raul/TimeStamp.hpp"
27 
28 namespace Raul {
29 
30 
31 /* FIXME: all the conversion here is wrong now */
32 
51 class TimeSlice : public boost::noncopyable {
52 public:
53  TimeSlice(uint32_t rate, uint32_t ppqn, double bpm)
54  : _tick_rate(rate)
55  , _beat_rate(60.0/bpm)
56  , _start_ticks(Raul::TimeUnit(Raul::TimeUnit::FRAMES, rate), 0, 0)
57  , _length_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
58  , _start_beats(TimeUnit(TimeUnit::BEATS, ppqn), 0, 0)
59  , _length_beats(TimeUnit(TimeUnit::BEATS, ppqn), 0, 0)
60  , _offset_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
61  {}
62 
68  void set_slice(TimeStamp start, TimeDuration length) {
69  assert(start.unit() == ticks_unit());
70  assert(length.unit() == ticks_unit());
71  _start_ticks = start;
72  _length_ticks = length;
73  update_beat_time();
74  }
75 
76  void set_length(TimeDuration length) {
77  assert(length.unit() == ticks_unit());
78  _length_ticks = length;
79  _length_beats = ticks_to_beats(_length_ticks);
80  }
81 
82  bool contains(TimeStamp time) const {
83  return (time >= start_ticks() && time < start_ticks() + length_ticks());
84  }
85 
86  double tick_rate() const { return _tick_rate; }
87  double beat_rate() const { return _beat_rate; }
88  double bpm() const { return 60/_beat_rate; }
89 
90  void set_tick_rate(double tick_rate) {
91  _tick_rate = tick_rate;
92  update_beat_time();
93  }
94 
95  void set_bpm(double bpm) {
96  _beat_rate = 60.0/bpm;
97  update_beat_time();
98  }
99 
100  inline TimeStamp beats_to_seconds(TimeStamp beats) const {
101  return TimeStamp(real_unit(), beats.to_double() * 1/(double)_beat_rate);
102  }
103 
104  inline TimeStamp beats_to_ticks(TimeStamp beats) const {
105  return TimeStamp(ticks_unit(), beats.to_double() * (double)_beat_rate * _tick_rate);
106  }
107 
108  inline TimeStamp ticks_to_seconds(TimeStamp ticks) const {
109  return TimeStamp(real_unit(), ticks.ticks() * 1/(double)_tick_rate);
110  }
111 
112  inline TimeStamp ticks_to_beats(TimeStamp ticks) const {
113  return TimeStamp(beats_unit(), ticks.ticks() * 1/(double)_tick_rate * _beat_rate);
114  }
115 
117  inline TimeStamp start_ticks() const { return _start_ticks; }
118 
120  inline TimeDuration length_ticks() const { return _length_ticks; }
121 
123  inline TimeStamp start_beats() const { return _start_beats; }
124 
126  inline TimeDuration length_beats() const { return _length_beats; }
127 
129  inline void set_offset(TimeDuration offset) { _offset_ticks = offset; }
130 
132  inline TimeDuration offset_ticks() const { return _offset_ticks; }
133 
134  inline TimeUnit beats_unit() const { return _start_beats.unit(); }
135  inline TimeUnit ticks_unit() const { return _start_ticks.unit(); }
136  inline TimeUnit real_unit() const { return TimeUnit(TimeUnit::SECONDS, 0); }
137 
138 private:
139  inline void update_beat_time() {
140  _start_beats = ticks_to_beats(_start_ticks);
141  _length_beats = ticks_to_beats(_length_ticks);
142  }
143 
144  // Rate/Tempo
145  double _tick_rate;
146  double _beat_rate;
147 
148  // Current time
149  TimeStamp _start_ticks;
150  TimeDuration _length_ticks;
151  TimeStamp _start_beats;
152  TimeDuration _length_beats;
153 
154  TimeDuration _offset_ticks;
155 };
156 
157 
158 } // namespace Raul
159 
160 #endif // RAUL_TIME_SLICE_HPP
TimeStamp start_beats() const
Start of current sub-cycle in beats.
Definition: TimeSlice.hpp:123
A type of time stamp.
Definition: TimeStamp.hpp:33
Definition: Array.hpp:26
TimeStamp start_ticks() const
Start of current sub-cycle in ticks.
Definition: TimeSlice.hpp:117
void set_slice(TimeStamp start, TimeDuration length)
Set the start and length of the slice.
Definition: TimeSlice.hpp:68
TimeDuration length_beats() const
Length of current sub-cycle in beats.
Definition: TimeSlice.hpp:126
A real-time time stamp (possible units: frame, absolute (s), or beat).
Definition: TimeStamp.hpp:80
A duration of time, with conversion between tick time and beat time.
Definition: TimeSlice.hpp:51
void set_offset(TimeDuration offset)
Set the offset between real-time and timeslice-time.
Definition: TimeSlice.hpp:129
TimeDuration offset_ticks() const
Offset relative to external (e.g Jack) time.
Definition: TimeSlice.hpp:132
TimeDuration length_ticks() const
Length of current sub-cycle in ticks.
Definition: TimeSlice.hpp:120