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