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