/builddir/build/BUILD/raul-0.4.0/raul/TimeSlice.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
00003  * 
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  * 
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  * 
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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/types.hpp>
00025 
00026 namespace Raul {
00027 
00028 
00047 class TimeSlice : public boost::noncopyable {
00048 public:
00049     TimeSlice(double tick_rate, double bpm)
00050         : _tick_rate(tick_rate)
00051         , _beat_rate(60.0/bpm)
00052         , _start_ticks(0)
00053         , _length_ticks(0)
00054         , _start_beats(0)
00055         , _length_beats(0)
00056         , _offset_ticks(0)
00057     {}
00058 
00059 
00065     void set_window(TickTime start, TickCount length) {
00066         _start_ticks = start;
00067         _length_ticks = length;
00068         update_beat_time();
00069     }
00070 
00071     void set_start(TickTime time) { _start_ticks = time; update_beat_time(); }
00072 
00073     void set_length(TickCount length) { _length_ticks = length; update_beat_time(); }
00074 
00075     bool contains(TickTime time) {
00076         return (time >= start_ticks() && time < start_ticks() + length_ticks());
00077     }
00078 
00079     double tick_rate() { return _tick_rate; }
00080     double beat_rate() { return _beat_rate; }
00081     double bpm()       { return 60/_beat_rate; }
00082 
00083     void set_tick_rate(double tick_rate) {
00084         _tick_rate = tick_rate;
00085         update_beat_time();
00086     }
00087 
00088     void set_bpm(double bpm) {
00089         _beat_rate = 60.0/bpm;
00090         update_beat_time();
00091     }
00092     
00093     inline Seconds beats_to_seconds(BeatTime beats) const {
00094         return (beats * _beat_rate);
00095     }
00096 
00097     inline TickTime beats_to_ticks(BeatTime beats) const {
00098         return static_cast<TickTime>(floor(beats_to_seconds(beats) / _tick_rate)); 
00099     }
00100         
00101     inline Seconds ticks_to_seconds(TickTime ticks) const {
00102         return (ticks * _tick_rate);
00103     }
00104 
00105     inline BeatTime ticks_to_beats(TickTime ticks) const {
00106         return ticks_to_seconds(ticks) / _beat_rate;
00107     }
00108     
00110     inline TickTime start_ticks() const { return _start_ticks; }
00111 
00113     inline TickCount length_ticks() const { return _length_ticks; }
00114 
00116     inline BeatTime start_beats() const { return _start_beats; }
00117 
00119     inline BeatCount length_beats() const { return _length_beats; }
00120 
00122     inline void set_offset(TickCount offset) { _offset_ticks = offset; }
00123 
00125     inline TickCount offset_ticks() const { return _offset_ticks; }
00126 
00127 private:
00128 
00129     inline void update_beat_time() {
00130         _start_beats  = ticks_to_beats(_start_ticks);
00131         _length_beats = ticks_to_beats(_length_ticks);
00132     }
00133 
00134     // Rate/Tempo
00135     double _tick_rate; 
00136     double _beat_rate; 
00137 
00138     // Current time
00139     TickTime  _start_ticks;  
00140     TickCount _length_ticks; 
00141     BeatTime  _start_beats;  
00142     BeatCount _length_beats; 
00143     
00144     TickCount _offset_ticks; 
00145 };
00146 
00147 
00148 } // namespace Raul
00149 
00150 #endif // RAUL_TIME_SLICE_HPP

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1