Rename libmusictime libtimecode (consistent with already used namespace "Timecode").
[ardour.git] / libs / ardour / ardour / tempo.h
1 /*
2     Copyright (C) 2000 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef __ardour_tempo_h__
21 #define __ardour_tempo_h__
22
23 #include <list>
24 #include <string>
25 #include <vector>
26 #include <cmath>
27 #include <glibmm/thread.h>
28
29 #include "pbd/undo.h"
30 #include "pbd/stateful.h"
31 #include "pbd/statefuldestructible.h"
32
33
34 #include "ardour/ardour.h"
35
36 class XMLNode;
37
38 namespace ARDOUR {
39 class Meter;
40 class Tempo {
41   public:
42         Tempo (double bpm, double type=4.0) // defaulting to quarter note
43                 : _beats_per_minute (bpm), _note_type(type) {}
44
45         double beats_per_minute () const { return _beats_per_minute;}
46         double note_type () const { return _note_type;}
47         double frames_per_beat (framecnt_t sr, const Meter& meter) const;
48
49   protected:
50         double _beats_per_minute;
51         double _note_type;
52 };
53
54 class Meter {
55   public:
56         static const double ticks_per_beat;
57
58         Meter (double bpb, double bt)
59                 : _beats_per_bar (bpb), _note_type (bt) {}
60
61         double beats_per_bar () const { return _beats_per_bar; }
62         double note_divisor() const { return _note_type; }
63
64         double frames_per_bar (const Tempo&, framecnt_t sr) const;
65
66   protected:
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.
70         */
71         double _beats_per_bar;
72
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.
75         */
76         double _note_type;
77 };
78
79 class MetricSection {
80   public:
81         MetricSection (const Timecode::BBT_Time& start)
82                 : _start (start), _frame (0), _movable (true) {}
83         MetricSection (framepos_t start)
84                 : _frame (start), _movable (true) {}
85
86         virtual ~MetricSection() {}
87
88         const Timecode::BBT_Time& start() const { return _start; }
89         framepos_t                frame() const { return _frame; }
90
91         void set_movable (bool yn) { _movable = yn; }
92         bool movable() const { return _movable; }
93
94         virtual void set_frame (framepos_t f) {
95                 _frame = f;
96         }
97
98         virtual void set_start (const Timecode::BBT_Time& w) {
99                 _start = w;
100         }
101
102         /* MeterSections are not stateful in the full sense,
103            but we do want them to control their own
104            XML state information.
105         */
106         virtual XMLNode& get_state() const = 0;
107
108         int compare (MetricSection *, bool) const;
109
110   private:
111         Timecode::BBT_Time _start;
112         framepos_t         _frame;
113         bool               _movable;
114 };
115
116 class MeterSection : public MetricSection, public Meter {
117   public:
118         MeterSection (const Timecode::BBT_Time& start, double bpb, double note_type)
119                 : MetricSection (start), Meter (bpb, note_type) {}
120         MeterSection (framepos_t start, double bpb, double note_type)
121                 : MetricSection (start), Meter (bpb, note_type) {}
122         MeterSection (const XMLNode&);
123
124         static const std::string xml_state_node_name;
125
126         XMLNode& get_state() const;
127 };
128
129 class TempoSection : public MetricSection, public Tempo {
130   public:
131         TempoSection (const Timecode::BBT_Time& start, double qpm, double note_type)
132                 : MetricSection (start), Tempo (qpm, note_type) {}
133         TempoSection (framepos_t start, double qpm, double note_type)
134                 : MetricSection (start), Tempo (qpm, note_type) {}
135         TempoSection (const XMLNode&);
136
137         static const std::string xml_state_node_name;
138
139         XMLNode& get_state() const;
140 };
141
142 typedef std::list<MetricSection*> Metrics;
143
144 /** Helper class that we use to be able to keep track of which
145     meter *AND* tempo are in effect at a given point in time.
146 */
147 class TempoMetric {
148   public:
149         TempoMetric (const Meter& m, const Tempo& t) : _meter (&m), _tempo (&t), _frame (0) {}
150         
151         void set_tempo (const Tempo& t)    { _tempo = &t; }
152         void set_meter (const Meter& m)    { _meter = &m; }
153         void set_frame (framepos_t f)      { _frame = f; }
154         void set_start (const Timecode::BBT_Time& t) { _start = t; }
155         
156         const Meter&    meter() const { return *_meter; }
157         const Tempo&    tempo() const { return *_tempo; }
158         framepos_t      frame() const { return _frame; }
159         const Timecode::BBT_Time& start() const { return _start; }
160         
161   private:
162         const Meter*       _meter;
163         const Tempo*       _tempo;
164         framepos_t         _frame;
165         Timecode::BBT_Time _start;
166 };
167
168 class TempoMap : public PBD::StatefulDestructible
169 {
170   public:
171         TempoMap (framecnt_t frame_rate);
172         ~TempoMap();
173
174         /* measure-based stuff */
175
176         enum BBTPointType {
177                 Bar,
178                 Beat,
179         };
180
181         struct BBTPoint {
182                 BBTPointType type;
183                 framepos_t  frame;
184                 const Meter* meter;
185                 const Tempo* tempo;
186                 uint32_t bar;
187                 uint32_t beat;
188
189                 BBTPoint (const Meter& m, const Tempo& t, framepos_t f,
190                                 BBTPointType ty, uint32_t b, uint32_t e)
191                         : type (ty), frame (f), meter (&m), tempo (&t), bar (b), beat (e) {}
192         };
193
194         typedef std::vector<BBTPoint> BBTPointList;
195
196         template<class T> void apply_with_metrics (T& obj, void (T::*method)(const Metrics&)) {
197                 Glib::RWLock::ReaderLock lm (lock);
198                 (obj.*method)(*metrics);
199         }
200
201         BBTPointList *get_points (framepos_t start, framepos_t end) const;
202
203         void      bbt_time (framepos_t when, Timecode::BBT_Time&) const;
204         framecnt_t frame_time (const Timecode::BBT_Time&) const;
205         framecnt_t bbt_duration_at (framepos_t, const Timecode::BBT_Time&, int dir) const;
206
207         void bbt_time_add (framepos_t origin, Timecode::BBT_Time& start, const Timecode::BBT_Time& shift);
208
209         static const Tempo& default_tempo() { return _default_tempo; }
210         static const Meter& default_meter() { return _default_meter; }
211
212         const Tempo& tempo_at (framepos_t) const;
213         const Meter& meter_at (framepos_t) const;
214
215         const TempoSection& tempo_section_at (framepos_t);
216
217         void add_tempo(const Tempo&, Timecode::BBT_Time where);
218         void add_meter(const Meter&, Timecode::BBT_Time where);
219
220         void add_tempo(const Tempo&, framepos_t where);
221         void add_meter(const Meter&, framepos_t where);
222
223         void move_tempo (TempoSection&, const Timecode::BBT_Time& to);
224         void move_meter (MeterSection&, const Timecode::BBT_Time& to);
225
226         void remove_tempo(const TempoSection&);
227         void remove_meter(const MeterSection&);
228
229         void replace_tempo (TempoSection& existing, const Tempo& replacement);
230         void replace_meter (MeterSection& existing, const Meter& replacement);
231
232         framepos_t round_to_bar  (framepos_t frame, int dir);
233         framepos_t round_to_beat (framepos_t frame, int dir);
234         framepos_t round_to_beat_subdivision (framepos_t fr, int sub_num, int dir);
235         framepos_t round_to_tick (framepos_t frame, int dir);
236
237         void set_length (framepos_t frames);
238
239         XMLNode& get_state (void);
240         int set_state (const XMLNode&, int version);
241
242         void dump (std::ostream&) const;
243         void clear ();
244
245         TempoMetric metric_at (Timecode::BBT_Time bbt) const;
246         TempoMetric metric_at (framepos_t) const;
247         void bbt_time_with_metric (framepos_t, Timecode::BBT_Time&, const TempoMetric&) const;
248
249         Timecode::BBT_Time bbt_add (const Timecode::BBT_Time&, const Timecode::BBT_Time&, const TempoMetric&) const;
250         Timecode::BBT_Time bbt_add (const Timecode::BBT_Time& a, const Timecode::BBT_Time& b) const;
251         Timecode::BBT_Time bbt_subtract (const Timecode::BBT_Time&, const Timecode::BBT_Time&) const;
252
253         void change_existing_tempo_at (framepos_t, double bpm, double note_type);
254         void change_initial_tempo (double bpm, double note_type);
255
256         void insert_time (framepos_t, framecnt_t);
257
258         int n_tempos () const;
259         int n_meters () const;
260
261         framecnt_t frame_rate () const { return _frame_rate; }
262
263   private:
264         static Tempo    _default_tempo;
265         static Meter    _default_meter;
266
267         Metrics*             metrics;
268         framecnt_t           _frame_rate;
269         framepos_t           last_bbt_when;
270         bool                 last_bbt_valid;
271         Timecode::BBT_Time   last_bbt;
272         mutable Glib::RWLock lock;
273
274         void timestamp_metrics (bool use_bbt);
275
276         framepos_t round_to_type (framepos_t fr, int dir, BBTPointType);
277
278         framepos_t frame_time_unlocked (const Timecode::BBT_Time&) const;
279
280         void bbt_time_unlocked (framepos_t, Timecode::BBT_Time&) const;
281
282         framecnt_t bbt_duration_at_unlocked (const Timecode::BBT_Time& when, const Timecode::BBT_Time& bbt, int dir) const;
283
284         const MeterSection& first_meter() const;
285         const TempoSection& first_tempo() const;
286
287         framecnt_t count_frames_between (const Timecode::BBT_Time&, const Timecode::BBT_Time&) const;
288         framecnt_t count_frames_between_metrics (const Meter&, const Tempo&,
289                                                  const Timecode::BBT_Time&, const Timecode::BBT_Time&) const;
290
291         int move_metric_section (MetricSection&, const Timecode::BBT_Time& to);
292         void do_insert (MetricSection* section, bool with_bbt);
293 };
294
295 }; /* namespace ARDOUR */
296
297 #endif /* __ardour_tempo_h__ */