Merge with 2.0-ongoing R2988
[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 <sigc++/signal.h>
34
35 #include <ardour/ardour.h>
36
37 class XMLNode;
38
39 using std::list;
40 using std::vector;
41
42 namespace ARDOUR {
43 class Meter;
44 class Tempo {
45   public:
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;
51         }
52         void operator= (const Tempo& other) {
53                 if (&other != this) {
54                         _beats_per_minute = other._beats_per_minute;
55                         _note_type = other._note_type;
56                 }
57         }
58
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;
62
63   protected:
64         double _beats_per_minute;
65         double _note_type;
66 };
67
68 class Meter {
69   public:
70         static const double ticks_per_beat;
71
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;
77         }
78         void operator= (const Meter& other) {
79                 if (&other != this) {
80                         _beats_per_bar = other._beats_per_bar;
81                         _note_type = other._note_type;
82                 }
83         }
84
85         double beats_per_bar () const { return _beats_per_bar; }
86         double note_divisor() const { return _note_type; }
87         
88         double frames_per_bar (const Tempo&, nframes_t sr) const;
89
90   protected:
91
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
95            per bar.
96         */
97
98         double _beats_per_bar;
99
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 
102            (quaver) note, etc.
103         */
104
105         double _note_type;
106 };
107
108 class MetricSection {
109   public:
110         MetricSection (const BBT_Time& start)
111                 : _start (start), _frame (0), _movable (true) {}
112         MetricSection (nframes_t start)
113                 : _frame (start), _movable (true) {}
114
115         virtual ~MetricSection() {}
116
117         const BBT_Time& start() const { return _start; }
118         const nframes_t frame() const { return _frame; }
119
120         void set_movable (bool yn) { _movable = yn; }
121         bool movable() const { return _movable; }
122
123         virtual void set_frame (nframes_t f) {
124                 _frame = f;
125         };
126
127         virtual void set_start (const BBT_Time& w) {
128                 _start = w;
129         }
130
131         /* MeterSections are not stateful in the full sense,
132            but we do want them to control their own
133            XML state information.
134         */
135
136         virtual XMLNode& get_state() const = 0;
137
138   private:
139         BBT_Time       _start;
140         nframes_t _frame;
141         bool           _movable;
142 };
143
144 class MeterSection : public MetricSection, public Meter {
145   public:
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&);
151
152         static const string xml_state_node_name;
153
154         XMLNode& get_state() const;
155 };
156
157 class TempoSection : public MetricSection, public Tempo {
158   public:
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&);
164
165         static const string xml_state_node_name;
166
167         XMLNode& get_state() const;
168 };
169
170 typedef list<MetricSection*> Metrics;
171
172 class TempoMap : public PBD::StatefulDestructible
173 {
174   public:
175         TempoMap (nframes_t frame_rate);
176         ~TempoMap();
177
178         /* measure-based stuff */
179
180         enum BBTPointType {
181                 Bar,
182                 Beat,
183         };
184
185         struct BBTPoint {
186             BBTPointType type;
187             nframes_t frame;
188             const Meter* meter;
189             const Tempo* tempo;
190             uint32_t bar;
191             uint32_t beat;
192             
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) {}
195         };
196
197         typedef vector<BBTPoint> BBTPointList;
198         
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);
202         }
203
204         BBTPointList *get_points (nframes_t start, nframes_t end) const;
205
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;
209
210         static const Tempo& default_tempo() { return _default_tempo; }
211         static const Meter& default_meter() { return _default_meter; }
212
213         const Tempo& tempo_at (nframes_t);
214         const Meter& meter_at (nframes_t);
215
216         const TempoSection& tempo_section_at (nframes_t);
217
218         void add_tempo(const Tempo&, BBT_Time where);
219         void add_meter(const Meter&, BBT_Time where);
220
221         void add_tempo(const Tempo&, nframes_t where);
222         void add_meter(const Meter&, nframes_t where);
223
224         void move_tempo (TempoSection&, const BBT_Time& to);
225         void move_meter (MeterSection&, const BBT_Time& to);
226         
227         void remove_tempo(const TempoSection&);
228         void remove_meter(const MeterSection&);
229
230         void replace_tempo (TempoSection& existing, const Tempo& replacement);
231         void replace_meter (MeterSection& existing, const Meter& replacement);
232
233
234         nframes_t round_to_bar  (nframes_t frame, int dir);
235
236         nframes_t round_to_beat (nframes_t frame, int dir);
237
238         nframes_t round_to_beat_subdivision (nframes_t fr, int sub_num);
239
240         nframes_t round_to_tick (nframes_t frame, int dir);
241
242         void set_length (nframes_t frames);
243
244         XMLNode& get_state (void);
245         int set_state (const XMLNode&);
246
247         void dump (std::ostream&) const;
248         void clear ();
249
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.
253         */
254
255         class Metric {
256           public:
257                 Metric (const Meter& m, const Tempo& t) : _meter (&m), _tempo (&t), _frame (0) {}
258                 
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; }
263                 
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; }
268                 
269           private:
270                 const Meter*   _meter;
271                 const Tempo*   _tempo;
272                 nframes_t _frame;
273                 BBT_Time       _start;
274                 
275         };
276
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;
280
281         void change_existing_tempo_at (nframes_t, double bpm, double note_type);
282         void change_initial_tempo (double bpm, double note_type);
283
284         int n_tempos () const;
285         int n_meters () const;
286
287         sigc::signal<void,ARDOUR::Change> StateChanged;
288
289   private:
290         static Tempo    _default_tempo;
291         static Meter    _default_meter;
292
293         Metrics*             metrics;
294         nframes_t           _frame_rate;
295         nframes_t            last_bbt_when;
296         bool                 last_bbt_valid;
297         BBT_Time             last_bbt;
298         mutable Glib::RWLock lock;
299         
300         void timestamp_metrics (bool use_bbt);
301
302         nframes_t round_to_type (nframes_t fr, int dir, BBTPointType);
303
304         nframes_t frame_time_unlocked (const BBT_Time&) const;
305
306         void bbt_time_unlocked (nframes_t, BBT_Time&) const;
307
308         nframes_t bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir) const;
309
310         const MeterSection& first_meter() const;
311         const TempoSection& first_tempo() const;
312
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;
315
316         int move_metric_section (MetricSection&, const BBT_Time& to);
317         void do_insert (MetricSection* section, bool with_bbt);
318 };
319
320 }; /* namespace ARDOUR */
321
322 #endif /* __ardour_tempo_h__ */