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"
43 Tempo (double bpm, double type=4.0) // defaulting to quarter note
44 : _beats_per_minute (bpm), _note_type(type) {}
46 double beats_per_minute () const { return _beats_per_minute;}
47 double note_type () const { return _note_type;}
48 double frames_per_beat (nframes_t sr, const Meter& meter) const;
51 double _beats_per_minute;
57 static const double ticks_per_beat;
59 Meter (double bpb, double bt)
60 : _beats_per_bar (bpb), _note_type (bt) {}
62 double beats_per_bar () const { return _beats_per_bar; }
63 double note_divisor() const { return _note_type; }
65 double frames_per_bar (const Tempo&, nframes_t sr) const;
68 /** The number of beats in a bar. This is a real value because
69 there are musical traditions on our planet that do not limit
70 themselves to integral numbers of beats per bar.
72 double _beats_per_bar;
74 /** The type of "note" that a beat represents. For example, 4.0 is
75 a quarter (crotchet) note, 8.0 is an eighth (quaver) note, etc.
82 MetricSection (const BBT_Time& start)
83 : _start (start), _frame (0), _movable (true) {}
84 MetricSection (nframes_t start)
85 : _frame (start), _movable (true) {}
87 virtual ~MetricSection() {}
89 const BBT_Time& start() const { return _start; }
90 nframes_t frame() const { return _frame; }
92 void set_movable (bool yn) { _movable = yn; }
93 bool movable() const { return _movable; }
95 virtual void set_frame (nframes_t f) {
99 virtual void set_start (const BBT_Time& w) {
103 /* MeterSections are not stateful in the full sense,
104 but we do want them to control their own
105 XML state information.
107 virtual XMLNode& get_state() const = 0;
115 class MeterSection : public MetricSection, public Meter {
117 MeterSection (const BBT_Time& start, double bpb, double note_type)
118 : MetricSection (start), Meter (bpb, note_type) {}
119 MeterSection (nframes_t start, double bpb, double note_type)
120 : MetricSection (start), Meter (bpb, note_type) {}
121 MeterSection (const XMLNode&);
123 static const std::string xml_state_node_name;
125 XMLNode& get_state() const;
128 class TempoSection : public MetricSection, public Tempo {
130 TempoSection (const BBT_Time& start, double qpm, double note_type)
131 : MetricSection (start), Tempo (qpm, note_type) {}
132 TempoSection (nframes_t start, double qpm, double note_type)
133 : MetricSection (start), Tempo (qpm, note_type) {}
134 TempoSection (const XMLNode&);
136 static const std::string xml_state_node_name;
138 XMLNode& get_state() const;
141 typedef std::list<MetricSection*> Metrics;
143 class TempoMap : public PBD::StatefulDestructible
146 TempoMap (nframes_t frame_rate);
149 /* measure-based stuff */
164 BBTPoint (const Meter& m, const Tempo& t, nframes_t f,
165 BBTPointType ty, uint32_t b, uint32_t e)
166 : type (ty), frame (f), meter (&m), tempo (&t), bar (b), beat (e) {}
169 typedef std::vector<BBTPoint> BBTPointList;
171 template<class T> void apply_with_metrics (T& obj, void (T::*method)(const Metrics&)) {
172 Glib::RWLock::ReaderLock lm (lock);
173 (obj.*method)(*metrics);
176 BBTPointList *get_points (nframes_t start, nframes_t end) const;
178 void bbt_time (nframes_t when, BBT_Time&) const;
179 nframes_t frame_time (const BBT_Time&) const;
180 nframes_t bbt_duration_at (nframes_t, const BBT_Time&, int dir) const;
182 void bbt_time_add (nframes64_t origin, BBT_Time& start, const BBT_Time& shift);
184 static const Tempo& default_tempo() { return _default_tempo; }
185 static const Meter& default_meter() { return _default_meter; }
187 const Tempo& tempo_at (nframes_t);
188 const Meter& meter_at (nframes_t);
190 const TempoSection& tempo_section_at (nframes_t);
192 void add_tempo(const Tempo&, BBT_Time where);
193 void add_meter(const Meter&, BBT_Time where);
195 void add_tempo(const Tempo&, nframes_t where);
196 void add_meter(const Meter&, nframes_t where);
198 void move_tempo (TempoSection&, const BBT_Time& to);
199 void move_meter (MeterSection&, const BBT_Time& to);
201 void remove_tempo(const TempoSection&);
202 void remove_meter(const MeterSection&);
204 void replace_tempo (TempoSection& existing, const Tempo& replacement);
205 void replace_meter (MeterSection& existing, const Meter& replacement);
207 nframes_t round_to_bar (nframes_t frame, int dir);
208 nframes_t round_to_beat (nframes_t frame, int dir);
209 nframes_t round_to_beat_subdivision (nframes_t fr, int sub_num, int dir);
210 nframes_t round_to_tick (nframes_t frame, int dir);
212 void set_length (nframes_t frames);
214 XMLNode& get_state (void);
215 int set_state (const XMLNode&);
217 void dump (std::ostream&) const;
220 /** Helper class that we use to be able to keep track of which
221 meter *AND* tempo are in effect at a given point in time.
225 Metric (const Meter& m, const Tempo& t) : _meter (&m), _tempo (&t), _frame (0) {}
227 void set_tempo (const Tempo& t) { _tempo = &t; }
228 void set_meter (const Meter& m) { _meter = &m; }
229 void set_frame (nframes_t f) { _frame = f; }
230 void set_start (const BBT_Time& t) { _start = t; }
232 const Meter& meter() const { return *_meter; }
233 const Tempo& tempo() const { return *_tempo; }
234 nframes_t frame() const { return _frame; }
235 const BBT_Time& start() const { return _start; }
244 Metric metric_at (BBT_Time bbt) const;
245 Metric metric_at (nframes_t) const;
246 void bbt_time_with_metric (nframes_t, BBT_Time&, const Metric&) const;
248 void change_existing_tempo_at (nframes_t, double bpm, double note_type);
249 void change_initial_tempo (double bpm, double note_type);
251 void insert_time (nframes_t, nframes_t);
253 int n_tempos () const;
254 int n_meters () const;
256 sigc::signal<void,ARDOUR::Change> StateChanged;
259 static Tempo _default_tempo;
260 static Meter _default_meter;
263 nframes_t _frame_rate;
264 nframes_t last_bbt_when;
267 mutable Glib::RWLock lock;
269 void timestamp_metrics (bool use_bbt);
271 nframes_t round_to_type (nframes_t fr, int dir, BBTPointType);
273 nframes_t frame_time_unlocked (const BBT_Time&) const;
275 void bbt_time_unlocked (nframes_t, BBT_Time&) const;
277 nframes_t bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir) const;
279 const MeterSection& first_meter() const;
280 const TempoSection& first_tempo() const;
282 nframes_t count_frames_between (const BBT_Time&, const BBT_Time&) const;
283 nframes_t count_frames_between_metrics (const Meter&, const Tempo&,
284 const BBT_Time&, const BBT_Time&) const;
286 int move_metric_section (MetricSection&, const BBT_Time& to);
287 void do_insert (MetricSection* section, bool with_bbt);
290 }; /* namespace ARDOUR */
292 #endif /* __ardour_tempo_h__ */