TimeSlice.hpp
/* This file is part of Raul.
* Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
*
* Raul is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* Raul is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef RAUL_TIME_SLICE_HPP
#define RAUL_TIME_SLICE_HPP
#include <cassert>
#include <cmath>
#include <boost/utility.hpp>
#include "raul/TimeStamp.hpp"
namespace Raul {
/* FIXME: all the conversion here is wrong now */
class TimeSlice : public boost::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)
{}
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 TimeStamp(real_unit(), beats.to_double() * 1/(double)_beat_rate);
}
inline TimeStamp beats_to_ticks(TimeStamp beats) const {
return TimeStamp(ticks_unit(), beats.to_double() * (double)_beat_rate * _tick_rate);
}
inline TimeStamp ticks_to_seconds(TimeStamp ticks) const {
return TimeStamp(real_unit(), ticks.ticks() * 1/(double)_tick_rate);
}
inline TimeStamp ticks_to_beats(TimeStamp ticks) const {
return TimeStamp(beats_unit(), ticks.ticks() * 1/(double)_tick_rate * _beat_rate);
}
inline TimeStamp start_ticks() const { return _start_ticks; }
inline TimeDuration length_ticks() const { return _length_ticks; }
inline TimeStamp start_beats() const { return _start_beats; }
inline TimeDuration length_beats() const { return _length_beats; }
inline void set_offset(TimeDuration offset) { _offset_ticks = offset; }
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(); }
inline TimeUnit real_unit() const { return TimeUnit(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;
double _beat_rate;
// Current time
TimeStamp _start_ticks;
TimeDuration _length_ticks;
TimeStamp _start_beats;
TimeDuration _length_beats;
TimeDuration _offset_ticks;
};
} // namespace Raul
#endif // RAUL_TIME_SLICE_HPP
Raul::TimeSlice::start_ticks
TimeStamp start_ticks() const
Start of current sub-cycle in ticks.
Definition: TimeSlice.hpp:117
Raul::TimeSlice::offset_ticks
TimeDuration offset_ticks() const
Offset relative to external (e.g Jack) time.
Definition: TimeSlice.hpp:132
Raul::TimeSlice::length_beats
TimeDuration length_beats() const
Length of current sub-cycle in beats.
Definition: TimeSlice.hpp:126
Raul::TimeSlice::start_beats
TimeStamp start_beats() const
Start of current sub-cycle in beats.
Definition: TimeSlice.hpp:123
Raul::TimeSlice::length_ticks
TimeDuration length_ticks() const
Length of current sub-cycle in ticks.
Definition: TimeSlice.hpp:120
Raul::TimeUnit
A type of time stamp.
Definition: TimeStamp.hpp:33
Raul
Definition: Array.hpp:26
Raul::TimeSlice::set_slice
void set_slice(TimeStamp start, TimeDuration length)
Set the start and length of the slice.
Definition: TimeSlice.hpp:68
Raul::TimeStamp
A real-time time stamp (possible units: frame, absolute (s), or beat).
Definition: TimeStamp.hpp:80
Raul::TimeSlice
A duration of time, with conversion between tick time and beat time.
Definition: TimeSlice.hpp:51
Raul::TimeSlice::set_offset
void set_offset(TimeDuration offset)
Set the offset between real-time and timeslice-time.
Definition: TimeSlice.hpp:129