2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __ardour_tempo_h__
21 #define __ardour_tempo_h__
27 #include <glibmm/thread.h>
30 #include <pbd/stateful.h>
31 #include <pbd/statefuldestructible.h>
33 #include <sigc++/signal.h>
35 #include <ardour/ardour.h>
46 Tempo (double bpm, double type=4.0) // defaulting to quarter note
47 : _beats_per_minute (bpm), _note_type(type) {}
48 Tempo (const Tempo& other) {
49 _beats_per_minute = other._beats_per_minute;
50 _note_type = other._note_type;
52 void operator= (const Tempo& other) {
54 _beats_per_minute = other._beats_per_minute;
55 _note_type = other._note_type;
59 double beats_per_minute () const { return _beats_per_minute;}
60 double note_type () const { return _note_type;}
61 double frames_per_beat (nframes_t sr, const Meter& meter) const;
64 double _beats_per_minute;
70 static const double ticks_per_beat;
72 Meter (double bpb, double bt)
73 : _beats_per_bar (bpb), _note_type (bt) {}
74 Meter (const Meter& other) {
75 _beats_per_bar = other._beats_per_bar;
76 _note_type = other._note_type;
78 void operator= (const Meter& other) {
80 _beats_per_bar = other._beats_per_bar;
81 _note_type = other._note_type;
85 double beats_per_bar () const { return _beats_per_bar; }
86 double note_divisor() const { return _note_type; }
88 double frames_per_bar (const Tempo&, nframes_t sr) const;
92 /* this is the number of beats in a bar. it is a real value
93 because there are musical traditions on our planet
94 that do not limit themselves to integral numbers of beats
98 double _beats_per_bar;
100 /* this is the type of "note" that a beat represents. for example,
101 4.0 would be a quarter (crotchet) note, 8.0 would be an eighth
108 class MetricSection {
110 MetricSection (const BBT_Time& start)
111 : _start (start), _frame (0), _movable (true) {}
112 MetricSection (nframes_t start)
113 : _frame (start), _movable (true) {}
115 virtual ~MetricSection() {}
117 const BBT_Time& start() const { return _start; }
118 const nframes_t frame() const { return _frame; }
120 void set_movable (bool yn) { _movable = yn; }
121 bool movable() const { return _movable; }
123 virtual void set_frame (nframes_t f) {
127 virtual void set_start (const BBT_Time& w) {
131 /* MeterSections are not stateful in the full sense,
132 but we do want them to control their own
133 XML state information.
136 virtual XMLNode& get_state() const = 0;
144 class MeterSection : public MetricSection, public Meter {
146 MeterSection (const BBT_Time& start, double bpb, double note_type)
147 : MetricSection (start), Meter (bpb, note_type) {}
148 MeterSection (nframes_t start, double bpb, double note_type)
149 : MetricSection (start), Meter (bpb, note_type) {}
150 MeterSection (const XMLNode&);
152 static const string xml_state_node_name;
154 XMLNode& get_state() const;
157 class TempoSection : public MetricSection, public Tempo {
159 TempoSection (const BBT_Time& start, double qpm, double note_type)
160 : MetricSection (start), Tempo (qpm, note_type) {}
161 TempoSection (nframes_t start, double qpm, double note_type)
162 : MetricSection (start), Tempo (qpm, note_type) {}
163 TempoSection (const XMLNode&);
165 static const string xml_state_node_name;
167 XMLNode& get_state() const;
170 typedef list<MetricSection*> Metrics;
172 class TempoMap : public PBD::StatefulDestructible
175 TempoMap (nframes_t frame_rate);
178 /* measure-based stuff */
193 BBTPoint (const Meter& m, const Tempo& t, nframes_t f, BBTPointType ty, uint32_t b, uint32_t e)
194 : type (ty), frame (f), meter (&m), tempo (&t), bar (b), beat (e) {}
197 typedef vector<BBTPoint> BBTPointList;
199 template<class T> void apply_with_metrics (T& obj, void (T::*method)(const Metrics&)) {
200 Glib::RWLock::ReaderLock lm (lock);
201 (obj.*method)(*metrics);
204 BBTPointList *get_points (nframes_t start, nframes_t end) const;
206 void bbt_time (nframes_t when, BBT_Time&) const;
207 nframes_t frame_time (const BBT_Time&) const;
208 nframes_t bbt_duration_at (nframes_t, const BBT_Time&, int dir) const;
210 static const Tempo& default_tempo() { return _default_tempo; }
211 static const Meter& default_meter() { return _default_meter; }
213 const Tempo& tempo_at (nframes_t);
214 const Meter& meter_at (nframes_t);
216 const TempoSection& tempo_section_at (nframes_t);
218 void add_tempo(const Tempo&, BBT_Time where);
219 void add_meter(const Meter&, BBT_Time where);
221 void add_tempo(const Tempo&, nframes_t where);
222 void add_meter(const Meter&, nframes_t where);
224 void move_tempo (TempoSection&, const BBT_Time& to);
225 void move_meter (MeterSection&, const BBT_Time& to);
227 void remove_tempo(const TempoSection&);
228 void remove_meter(const MeterSection&);
230 void replace_tempo (TempoSection& existing, const Tempo& replacement);
231 void replace_meter (MeterSection& existing, const Meter& replacement);
234 nframes_t round_to_bar (nframes_t frame, int dir);
236 nframes_t round_to_beat (nframes_t frame, int dir);
238 nframes_t round_to_beat_subdivision (nframes_t fr, int sub_num);
240 nframes_t round_to_tick (nframes_t frame, int dir);
242 void set_length (nframes_t frames);
244 XMLNode& get_state (void);
245 int set_state (const XMLNode&);
247 void dump (std::ostream&) const;
250 /* this is a helper class that we use to be able to keep
251 track of which meter *AND* tempo are in effect at
252 a given point in time.
257 Metric (const Meter& m, const Tempo& t) : _meter (&m), _tempo (&t), _frame (0) {}
259 void set_tempo (const Tempo& t) { _tempo = &t; }
260 void set_meter (const Meter& m) { _meter = &m; }
261 void set_frame (nframes_t f) { _frame = f; }
262 void set_start (const BBT_Time& t) { _start = t; }
264 const Meter& meter() const { return *_meter; }
265 const Tempo& tempo() const { return *_tempo; }
266 nframes_t frame() const { return _frame; }
267 const BBT_Time& start() const { return _start; }
277 Metric metric_at (BBT_Time bbt) const;
278 Metric metric_at (nframes_t) const;
279 void bbt_time_with_metric (nframes_t, BBT_Time&, const Metric&) const;
281 void change_existing_tempo_at (nframes_t, double bpm, double note_type);
282 void change_initial_tempo (double bpm, double note_type);
284 int n_tempos () const;
285 int n_meters () const;
287 sigc::signal<void,ARDOUR::Change> StateChanged;
290 static Tempo _default_tempo;
291 static Meter _default_meter;
294 nframes_t _frame_rate;
295 nframes_t last_bbt_when;
298 mutable Glib::RWLock lock;
300 void timestamp_metrics (bool use_bbt);
302 nframes_t round_to_type (nframes_t fr, int dir, BBTPointType);
304 nframes_t frame_time_unlocked (const BBT_Time&) const;
306 void bbt_time_unlocked (nframes_t, BBT_Time&) const;
308 nframes_t bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir) const;
310 const MeterSection& first_meter() const;
311 const TempoSection& first_tempo() const;
313 nframes_t count_frames_between (const BBT_Time&, const BBT_Time&) const;
314 nframes_t count_frames_between_metrics (const Meter&, const Tempo&, const BBT_Time&, const BBT_Time&) const;
316 int move_metric_section (MetricSection&, const BBT_Time& to);
317 void do_insert (MetricSection* section, bool with_bbt);
320 }; /* namespace ARDOUR */
322 #endif /* __ardour_tempo_h__ */