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"
34 #include "ardour/ardour.h"
42 Tempo (double bpm, double type=4.0) // defaulting to quarter note
43 : _beats_per_minute (bpm), _note_type(type) {}
45 double beats_per_minute () const { return _beats_per_minute;}
46 double note_type () const { return _note_type;}
47 double frames_per_beat (nframes_t sr, const Meter& meter) const;
50 double _beats_per_minute;
56 static const double ticks_per_beat;
58 Meter (double bpb, double bt)
59 : _beats_per_bar (bpb), _note_type (bt) {}
61 double beats_per_bar () const { return _beats_per_bar; }
62 double note_divisor() const { return _note_type; }
64 double frames_per_bar (const Tempo&, nframes_t sr) const;
67 /** The number of beats in a bar. This is a real value because
68 there are musical traditions on our planet that do not limit
69 themselves to integral numbers of beats per bar.
71 double _beats_per_bar;
73 /** The type of "note" that a beat represents. For example, 4.0 is
74 a quarter (crotchet) note, 8.0 is an eighth (quaver) note, etc.
81 MetricSection (const BBT_Time& start)
82 : _start (start), _frame (0), _movable (true) {}
83 MetricSection (nframes64_t start)
84 : _frame (start), _movable (true) {}
86 virtual ~MetricSection() {}
88 const BBT_Time& start() const { return _start; }
89 nframes64_t frame() const { return _frame; }
91 void set_movable (bool yn) { _movable = yn; }
92 bool movable() const { return _movable; }
94 virtual void set_frame (nframes64_t f) {
98 virtual void set_start (const BBT_Time& w) {
102 /* MeterSections are not stateful in the full sense,
103 but we do want them to control their own
104 XML state information.
106 virtual XMLNode& get_state() const = 0;
114 class MeterSection : public MetricSection, public Meter {
116 MeterSection (const BBT_Time& start, double bpb, double note_type)
117 : MetricSection (start), Meter (bpb, note_type) {}
118 MeterSection (nframes64_t start, double bpb, double note_type)
119 : MetricSection (start), Meter (bpb, note_type) {}
120 MeterSection (const XMLNode&);
122 static const std::string xml_state_node_name;
124 XMLNode& get_state() const;
127 class TempoSection : public MetricSection, public Tempo {
129 TempoSection (const BBT_Time& start, double qpm, double note_type)
130 : MetricSection (start), Tempo (qpm, note_type) {}
131 TempoSection (nframes64_t start, double qpm, double note_type)
132 : MetricSection (start), Tempo (qpm, note_type) {}
133 TempoSection (const XMLNode&);
135 static const std::string xml_state_node_name;
137 XMLNode& get_state() const;
140 typedef std::list<MetricSection*> Metrics;
142 /** Helper class that we use to be able to keep track of which
143 meter *AND* tempo are in effect at a given point in time.
147 TempoMetric (const Meter& m, const Tempo& t) : _meter (&m), _tempo (&t), _frame (0) {}
149 void set_tempo (const Tempo& t) { _tempo = &t; }
150 void set_meter (const Meter& m) { _meter = &m; }
151 void set_frame (nframes64_t f) { _frame = f; }
152 void set_start (const BBT_Time& t) { _start = t; }
154 const Meter& meter() const { return *_meter; }
155 const Tempo& tempo() const { return *_tempo; }
156 nframes64_t frame() const { return _frame; }
157 const BBT_Time& start() const { return _start; }
166 class TempoMap : public PBD::StatefulDestructible
169 TempoMap (nframes64_t frame_rate);
172 /* measure-based stuff */
187 BBTPoint (const Meter& m, const Tempo& t, nframes64_t f,
188 BBTPointType ty, uint32_t b, uint32_t e)
189 : type (ty), frame (f), meter (&m), tempo (&t), bar (b), beat (e) {}
192 typedef std::vector<BBTPoint> BBTPointList;
194 template<class T> void apply_with_metrics (T& obj, void (T::*method)(const Metrics&)) {
195 Glib::RWLock::ReaderLock lm (lock);
196 (obj.*method)(*metrics);
199 BBTPointList *get_points (nframes64_t start, nframes64_t end) const;
201 void bbt_time (nframes64_t when, BBT_Time&) const;
202 nframes64_t frame_time (const BBT_Time&) const;
203 nframes64_t bbt_duration_at (nframes64_t, const BBT_Time&, int dir) const;
205 void bbt_time_add (nframes64_t origin, BBT_Time& start, const BBT_Time& shift);
207 static const Tempo& default_tempo() { return _default_tempo; }
208 static const Meter& default_meter() { return _default_meter; }
210 const Tempo& tempo_at (nframes64_t) const;
211 const Meter& meter_at (nframes64_t) const;
213 const TempoSection& tempo_section_at (nframes64_t);
215 void add_tempo(const Tempo&, BBT_Time where);
216 void add_meter(const Meter&, BBT_Time where);
218 void add_tempo(const Tempo&, nframes64_t where);
219 void add_meter(const Meter&, nframes64_t where);
221 void move_tempo (TempoSection&, const BBT_Time& to);
222 void move_meter (MeterSection&, const BBT_Time& to);
224 void remove_tempo(const TempoSection&);
225 void remove_meter(const MeterSection&);
227 void replace_tempo (TempoSection& existing, const Tempo& replacement);
228 void replace_meter (MeterSection& existing, const Meter& replacement);
230 nframes64_t round_to_bar (nframes64_t frame, int dir);
231 nframes64_t round_to_beat (nframes64_t frame, int dir);
232 nframes64_t round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir);
233 nframes64_t round_to_tick (nframes64_t frame, int dir);
235 void set_length (nframes64_t frames);
237 XMLNode& get_state (void);
238 int set_state (const XMLNode&, int version);
240 void dump (std::ostream&) const;
243 TempoMetric metric_at (BBT_Time bbt) const;
244 TempoMetric metric_at (nframes64_t) const;
245 void bbt_time_with_metric (nframes64_t, BBT_Time&, const TempoMetric&) const;
247 BBT_Time bbt_add (const BBT_Time&, const BBT_Time&, const TempoMetric&) const;
248 BBT_Time bbt_add (const BBT_Time& a, const BBT_Time& b) const;
249 BBT_Time bbt_subtract (const BBT_Time&, const BBT_Time&) const;
251 void change_existing_tempo_at (nframes64_t, double bpm, double note_type);
252 void change_initial_tempo (double bpm, double note_type);
254 void insert_time (nframes64_t, nframes64_t);
256 int n_tempos () const;
257 int n_meters () const;
259 nframes_t frame_rate () const { return _frame_rate; }
261 PBD::Signal1<void,PBD::Change> StateChanged;
264 static Tempo _default_tempo;
265 static Meter _default_meter;
268 nframes_t _frame_rate;
269 nframes64_t last_bbt_when;
272 mutable Glib::RWLock lock;
274 void timestamp_metrics (bool use_bbt);
276 nframes64_t round_to_type (nframes64_t fr, int dir, BBTPointType);
278 nframes64_t frame_time_unlocked (const BBT_Time&) const;
280 void bbt_time_unlocked (nframes64_t, BBT_Time&) const;
282 nframes64_t bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir) const;
284 const MeterSection& first_meter() const;
285 const TempoSection& first_tempo() const;
287 nframes64_t count_frames_between (const BBT_Time&, const BBT_Time&) const;
288 nframes64_t count_frames_between_metrics (const Meter&, const Tempo&,
289 const BBT_Time&, const BBT_Time&) const;
291 int move_metric_section (MetricSection&, const BBT_Time& to);
292 void do_insert (MetricSection* section, bool with_bbt);
295 }; /* namespace ARDOUR */
297 #endif /* __ardour_tempo_h__ */