RAUL  0.5.1
TimeSlice.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007 Dave 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 #include <boost/utility.hpp>
24 #include <raul/TimeStamp.hpp>
25 #include <raul/lv2_event.h>
26 
27 namespace Raul {
28 
29 
30 /* FIXME: all the conversion here is wrong now */
31 
50 class TimeSlice : public boost::noncopyable {
51 public:
52  TimeSlice(uint32_t rate, double bpm)
53  : _tick_rate(rate)
54  , _beat_rate(60.0/bpm)
55  , _start_ticks(Raul::TimeUnit(Raul::TimeUnit::FRAMES, rate), 0, 0)
56  , _length_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
57  , _start_beats(TimeUnit(TimeUnit::BEATS, LV2_EVENT_PPQN), 0, 0)
58  , _length_beats(TimeUnit(TimeUnit::BEATS, LV2_EVENT_PPQN), 0, 0)
59  , _offset_ticks(TimeUnit(TimeUnit::FRAMES, rate), 0, 0)
60  {}
61 
62 
68  void set_window(TimeStamp start, TimeDuration length) {
69  _start_ticks = start;
70  _length_ticks = length;
71  update_beat_time();
72  }
73 
74  void set_start(TimeStamp time) { _start_ticks = time; update_beat_time(); }
75 
76  void set_length(TimeDuration length) { _length_ticks = length; update_beat_time(); }
77 
78  bool contains(TimeStamp time) {
79  return (time >= start_ticks() && time < start_ticks() + length_ticks());
80  }
81 
82  double tick_rate() { return _tick_rate; }
83  double beat_rate() { return _beat_rate; }
84  double bpm() { return 60/_beat_rate; }
85 
86  void set_tick_rate(double tick_rate) {
87  _tick_rate = tick_rate;
88  update_beat_time();
89  }
90 
91  void set_bpm(double bpm) {
92  _beat_rate = 60.0/bpm;
93  update_beat_time();
94  }
95 
96  // FIXME
97 
98  inline TimeStamp beats_to_seconds(TimeStamp beats) const {
99  //return (beats * _beat_rate);
100  throw;
101  }
102 
103  inline TimeStamp beats_to_ticks(TimeStamp beats) const {
104  //return static_cast<TimeStamp>(floor(beats_to_seconds(beats) / _tick_rate));
105  throw;
106  }
107 
108  inline TimeStamp ticks_to_seconds(TimeStamp ticks) const {
109  //return (ticks * _tick_rate);
110  throw;
111  }
112 
113  inline TimeStamp ticks_to_beats(TimeStamp ticks) const {
114  //return ticks_to_seconds(ticks) / _beat_rate;
115  throw;
116  }
117 
119  inline TimeStamp start_ticks() const { return _start_ticks; }
120 
122  inline TimeDuration length_ticks() const { return _length_ticks; }
123 
125  inline TimeStamp start_beats() const { return _start_beats; }
126 
128  inline TimeDuration length_beats() const { return _length_beats; }
129 
131  inline void set_offset(TimeDuration offset) { _offset_ticks = offset; }
132 
134  inline TimeDuration offset_ticks() const { return _offset_ticks; }
135 
136 private:
137 
138  inline void update_beat_time() {
139  _start_beats = ticks_to_beats(_start_ticks);
140  _length_beats = ticks_to_beats(_length_ticks);
141  }
142 
143  // Rate/Tempo
144  double _tick_rate;
145  double _beat_rate;
146 
147  // Current time
148  TimeStamp _start_ticks;
149  TimeDuration _length_ticks;
150  TimeStamp _start_beats;
151  TimeDuration _length_beats;
152 
153  TimeDuration _offset_ticks;
154 };
155 
156 
157 } // namespace Raul
158 
159 #endif // RAUL_TIME_SLICE_HPP
TimeStamp start_ticks() const
Start of current sub-cycle in ticks.
Definition: TimeSlice.hpp:119
TimeDuration offset_ticks() const
Offset relative to external (e.g Jack) time.
Definition: TimeSlice.hpp:134
TimeDuration length_beats() const
Length of current sub-cycle in beats.
Definition: TimeSlice.hpp:128
TimeStamp start_beats() const
Start of current sub-cycle in beats.
Definition: TimeSlice.hpp:125
TimeDuration length_ticks() const
Length of current sub-cycle in ticks.
Definition: TimeSlice.hpp:122
A type of time stamp.
Definition: TimeStamp.hpp:32
This header defines the code portion of the LV2 events extension with URI http://lv2plug.in/ns/ext/event ('lv2ev').
Definition: Array.hpp:26
A real-time time stamp (possible units: frame, absolute (s), or beat).
Definition: TimeStamp.hpp:78
void set_window(TimeStamp start, TimeDuration length)
Set the start and length of the slice.
Definition: TimeSlice.hpp:68
A duration of time, with conversion between tick time and beat time.
Definition: TimeSlice.hpp:50
void set_offset(TimeDuration offset)
Set the offset between real-time and timeslice-time.
Definition: TimeSlice.hpp:131