* libardour uses ARDOUR::nframes_t and ARDOUR::nframes64_t explicitly in headers
[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 (nframes64_t start)
85                 : _frame (start), _movable (true) {}
86
87         virtual ~MetricSection() {}
88
89         const BBT_Time& start() const { return _start; }
90         nframes64_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 (nframes64_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         nframes64_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 (nframes64_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 (nframes64_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 (nframes64_t f)     { _frame = f; }
153         void set_start (const BBT_Time& t) { _start = t; }
154         
155         const Meter&    meter() const { return *_meter; }
156         const Tempo&    tempo() const { return *_tempo; }
157         nframes64_t     frame() const { return _frame; }
158         const BBT_Time& start() const { return _start; }
159         
160   private:
161         const Meter*   _meter;
162         const Tempo*   _tempo;
163         nframes64_t    _frame;
164         BBT_Time       _start;
165 };
166
167 class TempoMap : public PBD::StatefulDestructible
168 {
169   public:
170         TempoMap (nframes64_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                 nframes64_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, nframes64_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 (nframes64_t start, nframes64_t end) const;
201
202         void      bbt_time (nframes64_t when, BBT_Time&) const;
203         nframes64_t frame_time (const BBT_Time&) const;
204         nframes64_t bbt_duration_at (nframes64_t, const BBT_Time&, int dir) const;
205
206         void bbt_time_add (nframes64_t origin, BBT_Time& start, const BBT_Time& shift);
207
208         static const Tempo& default_tempo() { return _default_tempo; }
209         static const Meter& default_meter() { return _default_meter; }
210
211         const Tempo& tempo_at (nframes64_t) const;
212         const Meter& meter_at (nframes64_t) const;
213
214         const TempoSection& tempo_section_at (nframes64_t);
215
216         void add_tempo(const Tempo&, BBT_Time where);
217         void add_meter(const Meter&, BBT_Time where);
218
219         void add_tempo(const Tempo&, nframes64_t where);
220         void add_meter(const Meter&, nframes64_t where);
221
222         void move_tempo (TempoSection&, const BBT_Time& to);
223         void move_meter (MeterSection&, const BBT_Time& to);
224
225         void remove_tempo(const TempoSection&);
226         void remove_meter(const MeterSection&);
227
228         void replace_tempo (TempoSection& existing, const Tempo& replacement);
229         void replace_meter (MeterSection& existing, const Meter& replacement);
230
231         nframes64_t round_to_bar  (nframes64_t frame, int dir);
232         nframes64_t round_to_beat (nframes64_t frame, int dir);
233         nframes64_t round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir);
234         nframes64_t round_to_tick (nframes64_t frame, int dir);
235
236         void set_length (nframes64_t frames);
237
238         XMLNode& get_state (void);
239         int set_state (const XMLNode&, int version);
240
241         void dump (std::ostream&) const;
242         void clear ();
243
244         TempoMetric metric_at (BBT_Time bbt) const;
245         TempoMetric metric_at (nframes64_t) const;
246         void bbt_time_with_metric (nframes64_t, BBT_Time&, const TempoMetric&) const;
247
248         BBT_Time bbt_add (const BBT_Time&, const BBT_Time&, const TempoMetric&) const;
249         BBT_Time bbt_add (const BBT_Time& a, const BBT_Time& b) const;
250         BBT_Time bbt_subtract (const BBT_Time&, const BBT_Time&) const;
251
252         void change_existing_tempo_at (nframes64_t, double bpm, double note_type);
253         void change_initial_tempo (double bpm, double note_type);
254
255         void insert_time (nframes64_t, nframes64_t);
256
257         int n_tempos () const;
258         int n_meters () const;
259
260         nframes_t frame_rate () const { return _frame_rate; }
261
262         sigc::signal<void,ARDOUR::Change> StateChanged;
263
264   private:
265         static Tempo    _default_tempo;
266         static Meter    _default_meter;
267
268         Metrics*             metrics;
269         nframes_t           _frame_rate;
270         nframes64_t          last_bbt_when;
271         bool                 last_bbt_valid;
272         BBT_Time             last_bbt;
273         mutable Glib::RWLock lock;
274
275         void timestamp_metrics (bool use_bbt);
276
277         nframes64_t round_to_type (nframes64_t fr, int dir, BBTPointType);
278
279         nframes64_t frame_time_unlocked (const BBT_Time&) const;
280
281         void bbt_time_unlocked (nframes64_t, BBT_Time&) const;
282
283         nframes64_t bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir) const;
284
285         const MeterSection& first_meter() const;
286         const TempoSection& first_tempo() const;
287
288         nframes64_t count_frames_between (const BBT_Time&, const BBT_Time&) const;
289         nframes64_t count_frames_between_metrics (const Meter&, const Tempo&,
290                         const BBT_Time&, const BBT_Time&) const;
291
292         int move_metric_section (MetricSection&, const BBT_Time& to);
293         void do_insert (MetricSection* section, bool with_bbt);
294 };
295
296 }; /* namespace ARDOUR */
297
298 #endif /* __ardour_tempo_h__ */