rework Stateful::set_state() patch to avoid default version argument
[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 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 (nframes_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         static const double ticks_per_beat;
58
59         Meter (double bpb, double bt)
60                 : _beats_per_bar (bpb), _note_type (bt) {}
61
62         double beats_per_bar () const { return _beats_per_bar; }
63         double note_divisor() const { return _note_type; }
64
65         double frames_per_bar (const Tempo&, nframes_t sr) const;
66
67   protected:
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.
71         */
72         double _beats_per_bar;
73
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.
76         */
77         double _note_type;
78 };
79
80 class MetricSection {
81   public:
82         MetricSection (const BBT_Time& start)
83                 : _start (start), _frame (0), _movable (true) {}
84         MetricSection (nframes_t start)
85                 : _frame (start), _movable (true) {}
86
87         virtual ~MetricSection() {}
88
89         const BBT_Time& start() const { return _start; }
90         nframes_t       frame() const { return _frame; }
91
92         void set_movable (bool yn) { _movable = yn; }
93         bool movable() const { return _movable; }
94
95         virtual void set_frame (nframes_t f) {
96                 _frame = f;
97         }
98
99         virtual void set_start (const BBT_Time& w) {
100                 _start = w;
101         }
102
103         /* MeterSections are not stateful in the full sense,
104            but we do want them to control their own
105            XML state information.
106         */
107         virtual XMLNode& get_state() const = 0;
108
109   private:
110         BBT_Time       _start;
111         nframes_t      _frame;
112         bool           _movable;
113 };
114
115 class MeterSection : public MetricSection, public Meter {
116   public:
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&);
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 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&);
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 class TempoMap : public PBD::StatefulDestructible
144 {
145   public:
146         TempoMap (nframes_t frame_rate);
147         ~TempoMap();
148
149         /* measure-based stuff */
150
151         enum BBTPointType {
152                 Bar,
153                 Beat,
154         };
155
156         struct BBTPoint {
157                 BBTPointType type;
158                 nframes_t frame;
159                 const Meter* meter;
160                 const Tempo* tempo;
161                 uint32_t bar;
162                 uint32_t beat;
163
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) {}
167         };
168
169         typedef std::vector<BBTPoint> BBTPointList;
170
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);
174         }
175
176         BBTPointList *get_points (nframes_t start, nframes_t end) const;
177
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;
181
182         void bbt_time_add (nframes64_t origin, BBT_Time& start, const BBT_Time& shift);
183
184         static const Tempo& default_tempo() { return _default_tempo; }
185         static const Meter& default_meter() { return _default_meter; }
186
187         const Tempo& tempo_at (nframes_t);
188         const Meter& meter_at (nframes_t);
189
190         const TempoSection& tempo_section_at (nframes_t);
191
192         void add_tempo(const Tempo&, BBT_Time where);
193         void add_meter(const Meter&, BBT_Time where);
194
195         void add_tempo(const Tempo&, nframes_t where);
196         void add_meter(const Meter&, nframes_t where);
197
198         void move_tempo (TempoSection&, const BBT_Time& to);
199         void move_meter (MeterSection&, const BBT_Time& to);
200
201         void remove_tempo(const TempoSection&);
202         void remove_meter(const MeterSection&);
203
204         void replace_tempo (TempoSection& existing, const Tempo& replacement);
205         void replace_meter (MeterSection& existing, const Meter& replacement);
206
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);
211
212         void set_length (nframes_t frames);
213
214         XMLNode& get_state (void);
215         int set_state (const XMLNode&, int version);
216
217         void dump (std::ostream&) const;
218         void clear ();
219
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.
222         */
223         class Metric {
224           public:
225                 Metric (const Meter& m, const Tempo& t) : _meter (&m), _tempo (&t), _frame (0) {}
226
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; }
231
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; }
236
237           private:
238                 const Meter*   _meter;
239                 const Tempo*   _tempo;
240                 nframes_t      _frame;
241                 BBT_Time       _start;
242         };
243
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;
247
248         void change_existing_tempo_at (nframes_t, double bpm, double note_type);
249         void change_initial_tempo (double bpm, double note_type);
250
251         void insert_time (nframes_t, nframes_t);
252
253         int n_tempos () const;
254         int n_meters () const;
255
256         sigc::signal<void,ARDOUR::Change> StateChanged;
257
258   private:
259         static Tempo    _default_tempo;
260         static Meter    _default_meter;
261
262         Metrics*             metrics;
263         nframes_t           _frame_rate;
264         nframes_t            last_bbt_when;
265         bool                 last_bbt_valid;
266         BBT_Time             last_bbt;
267         mutable Glib::RWLock lock;
268
269         void timestamp_metrics (bool use_bbt);
270
271         nframes_t round_to_type (nframes_t fr, int dir, BBTPointType);
272
273         nframes_t frame_time_unlocked (const BBT_Time&) const;
274
275         void bbt_time_unlocked (nframes_t, BBT_Time&) const;
276
277         nframes_t bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir) const;
278
279         const MeterSection& first_meter() const;
280         const TempoSection& first_tempo() const;
281
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;
285
286         int move_metric_section (MetricSection&, const BBT_Time& to);
287         void do_insert (MetricSection* section, bool with_bbt);
288 };
289
290 }; /* namespace ARDOUR */
291
292 #endif /* __ardour_tempo_h__ */