2f04f603e78be13a8a2836209490259c238e82aa
[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     $Id$
19 */
20
21 #ifndef __ardour_tempo_h__
22 #define __ardour_tempo_h__
23
24 #include <list>
25 #include <string>
26 #include <vector>
27 #include <cmath>
28 #include <pthread.h>
29 #include <pbd/lockmonitor.h>
30 #include <pbd/undo.h>
31 #include <sigc++/signal.h>
32
33 #include <ardour/ardour.h>
34 #include <ardour/stateful.h>
35 #include <ardour/state_manager.h>
36
37 class XMLNode;
38
39 using std::list;
40 using std::vector;
41
42 namespace ARDOUR {
43
44 class Tempo {
45   public:
46         Tempo (double bpm)
47                 : _beats_per_minute (bpm) {}
48         Tempo (const Tempo& other) {
49                 _beats_per_minute = other._beats_per_minute;
50         }
51         void operator= (const Tempo& other) {
52                 if (&other != this) {
53                         _beats_per_minute = other._beats_per_minute;
54                 }
55         }
56
57         double beats_per_minute () const { return _beats_per_minute; }
58         double frames_per_beat (jack_nframes_t sr) const {
59                 return  ((60.0 * sr) / _beats_per_minute);
60         }
61
62   protected:
63         double _beats_per_minute;
64 };
65
66 class Meter {
67   public:
68         static const double ticks_per_beat;
69
70         Meter (double bpb, double bt) 
71                 : _beats_per_bar (bpb), _note_type (bt) {}
72         Meter (const Meter& other) {
73                 _beats_per_bar = other._beats_per_bar;
74                 _note_type = other._note_type;
75         }
76         void operator= (const Meter& other) {
77                 if (&other != this) {
78                         _beats_per_bar = other._beats_per_bar;
79                         _note_type = other._note_type;
80                 }
81         }
82
83         double beats_per_bar () const { return _beats_per_bar; }
84         double note_divisor() const { return _note_type; }
85         
86         double frames_per_bar (const Tempo&, jack_nframes_t sr) const;
87
88   protected:
89
90         /* this is the number of beats in a bar. it is a real value
91            because there are musical traditions on our planet
92            that do not limit themselves to integral numbers of beats
93            per bar.
94         */
95
96         double _beats_per_bar;
97
98         /* this is the type of "note" that a beat represents. for example,
99            4.0 would be a quarter (crotchet) note, 8.0 would be an eighth 
100            (quaver) note, etc.
101         */
102
103         double _note_type;
104 };
105
106 class MetricSection {
107   public:
108         MetricSection (const BBT_Time& start)
109                 : _start (start), _frame (0), _movable (true) {}
110         virtual ~MetricSection() {}
111
112         const BBT_Time& start() const { return _start; }
113         const jack_nframes_t frame() const { return _frame; }
114
115         void set_movable (bool yn) { _movable = yn; }
116         bool movable() const { return _movable; }
117
118         virtual void set_frame (jack_nframes_t f) {
119                 _frame = f;
120         };
121
122         virtual void set_start (const BBT_Time& w) {
123                 _start = w;
124         }
125
126         /* MeterSections are not stateful in the full sense,
127            but we do want them to control their own
128            XML state information.
129         */
130
131         virtual XMLNode& get_state() const = 0;
132
133   private:
134         BBT_Time       _start;
135         jack_nframes_t _frame;
136         bool           _movable;
137 };
138
139 class MeterSection : public MetricSection, public Meter {
140   public:
141         MeterSection (const BBT_Time& start, double bpb, double note_type)
142                 : MetricSection (start), Meter (bpb, note_type) {}
143         MeterSection (const XMLNode&);
144
145         static const string xml_state_node_name;
146
147         XMLNode& get_state() const;
148 };
149
150 class TempoSection : public MetricSection, public Tempo {
151   public:
152         TempoSection (const BBT_Time& start, double qpm)
153                 : MetricSection (start), Tempo (qpm) {}
154         TempoSection (const XMLNode&);
155
156         static const string xml_state_node_name;
157
158         XMLNode& get_state() const;
159 };
160
161 typedef list<MetricSection*> Metrics;
162
163 class TempoMapState : public StateManager::State {
164   public:
165         TempoMapState (std::string why) 
166                 : StateManager::State (why) {
167                 metrics = new Metrics;
168         }
169
170         Metrics *metrics;
171 };
172
173 class TempoMap : public Stateful, public StateManager {
174   public:
175
176         TempoMap (jack_nframes_t frame_rate);
177         ~TempoMap();
178
179         /* measure-based stuff */
180
181         enum BBTPointType {
182                 Bar,
183                 Beat,
184         };
185
186         struct BBTPoint {
187             BBTPointType type;
188             jack_nframes_t frame;
189             const Meter* meter;
190             const Tempo* tempo;
191             uint32_t bar;
192             uint32_t beat;
193             
194             BBTPoint (const Meter& m, const Tempo& t, jack_nframes_t f, BBTPointType ty, uint32_t b, uint32_t e) 
195                     : type (ty), frame (f), meter (&m), tempo (&t), bar (b), beat (e) {}
196         };
197
198         typedef vector<BBTPoint> BBTPointList;
199         
200         template<class T> void apply_with_metrics (T& obj, void (T::*method)(const Metrics&)) {
201                 LockMonitor lm (lock, __LINE__, __FILE__);
202                 (obj.*method)(*metrics);
203         }
204
205         BBTPointList *get_points (jack_nframes_t start, jack_nframes_t end) const;
206
207         void           bbt_time (jack_nframes_t when, BBT_Time&) const;
208         jack_nframes_t frame_time (const BBT_Time&) const;
209         jack_nframes_t bbt_duration_at (jack_nframes_t, const BBT_Time&, int dir) const;
210
211         static const Tempo& default_tempo() { return _default_tempo; }
212         static const Meter& default_meter() { return _default_meter; }
213
214         const Tempo& tempo_at (jack_nframes_t);
215         const Meter& meter_at (jack_nframes_t);
216
217         void add_tempo(const Tempo&, BBT_Time where);
218         void add_meter(const Meter&, BBT_Time where);
219
220         void move_tempo (TempoSection&, const BBT_Time& to);
221         void move_meter (MeterSection&, const 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
230         jack_nframes_t round_to_bar  (jack_nframes_t frame, int dir);
231
232         jack_nframes_t round_to_beat (jack_nframes_t frame, int dir);
233
234         jack_nframes_t round_to_beat_subdivision (jack_nframes_t fr, int sub_num);
235
236         jack_nframes_t round_to_tick (jack_nframes_t frame, int dir);
237
238         void set_length (jack_nframes_t frames);
239
240         XMLNode& get_state (void);
241         int set_state (const XMLNode&);
242
243         void dump (std::ostream&) const;
244         void clear ();
245
246         UndoAction get_memento() const;
247
248         /* this is a helper class that we use to be able to keep
249            track of which meter *AND* tempo are in effect at
250            a given point in time.
251         */
252
253         class Metric {
254           public:
255                 Metric (const Meter& m, const Tempo& t) : _meter (&m), _tempo (&t), _frame (0) {}
256                 
257                 void set_tempo (const Tempo& t)    { _tempo = &t; }
258                 void set_meter (const Meter& m)    { _meter = &m; }
259                 void set_frame (jack_nframes_t f)  { _frame = f; }
260                 void set_start (const BBT_Time& t) { _start = t; }
261                 
262                 const Meter&    meter() const { return *_meter; }
263                 const Tempo&    tempo() const { return *_tempo; }
264                 jack_nframes_t  frame() const { return _frame; }
265                 const BBT_Time& start() const { return _start; }
266                 
267           private:
268                 const Meter*   _meter;
269                 const Tempo*   _tempo;
270                 jack_nframes_t _frame;
271                 BBT_Time       _start;
272                 
273         };
274
275         Metric metric_at (BBT_Time bbt) const;
276         Metric metric_at (jack_nframes_t) const;
277         void bbt_time_with_metric (jack_nframes_t, BBT_Time&, const Metric&) const;
278
279   private:
280         static Tempo    _default_tempo;
281         static Meter    _default_meter;
282
283         Metrics            *metrics;
284         jack_nframes_t     _frame_rate;
285         jack_nframes_t      last_bbt_when;
286         bool                last_bbt_valid;
287         BBT_Time            last_bbt;
288         mutable PBD::Lock   lock;
289         
290         void timestamp_metrics ();
291
292
293         jack_nframes_t round_to_type (jack_nframes_t fr, int dir, BBTPointType);
294
295         jack_nframes_t frame_time_unlocked (const BBT_Time&) const;
296
297         void bbt_time_unlocked (jack_nframes_t, BBT_Time&) const;
298
299         jack_nframes_t bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir) const;
300
301         const MeterSection& first_meter() const;
302         const TempoSection& first_tempo() const;
303
304         jack_nframes_t count_frames_between (const BBT_Time&, const BBT_Time&) const;
305         jack_nframes_t count_frames_between_metrics (const Meter&, const Tempo&, const BBT_Time&, const BBT_Time&) const;
306
307         int move_metric_section (MetricSection&, const BBT_Time& to);
308         void do_insert (MetricSection* section);
309
310         Change  restore_state (StateManager::State&);
311         StateManager::State* state_factory (std::string why) const;
312
313         bool        in_set_state;
314
315         /* override state_manager::save_state so we can check in_set_state */
316
317         void save_state (std::string why);
318
319 };
320
321 }; /* namespace ARDOUR */
322
323 #endif /* __ardour_tempo_h__ */