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