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