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