Use double MIDI timestamps (towards tempo based time, and more-than-sample-accurate...
[ardour.git] / libs / ardour / ardour / types.h
1 /*
2     Copyright (C) 2002 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_types_h__
21 #define __ardour_types_h__
22
23 #ifndef __STDC_FORMAT_MACROS
24 #define __STDC_FORMAT_MACROS /* PRI<foo>; C++ requires explicit requesting of these */
25 #endif
26
27 #include <istream>
28 #include <vector>
29 #include <boost/shared_ptr.hpp>
30
31 #include <inttypes.h>
32 #include <jack/types.h>
33 #include <jack/midiport.h>
34 #include <control_protocol/smpte.h>
35 #include <pbd/id.h>
36
37 #include <map>
38
39 #if __GNUC__ < 3
40
41 typedef int intptr_t;
42 #endif
43
44 /* eventually, we'd like everything (including JACK) to 
45    move to this. for now, its a dedicated type.
46 */
47
48 typedef int64_t                    nframes64_t;
49
50 namespace ARDOUR {
51
52         class Source;
53         class AudioSource;
54
55         typedef jack_default_audio_sample_t Sample;
56         typedef float                       pan_t;
57         typedef float                       gain_t;
58         typedef uint32_t                    layer_t;
59         typedef uint64_t                    microseconds_t;
60         typedef uint32_t                    nframes_t;
61
62         typedef unsigned char Byte;
63
64         /** Identical to jack_midi_event_t, but with double timestamp
65          *
66          * time is either a frame time (from/to Jack) or a beat time (internal
67          * tempo time, used in MidiModel) depending on context.
68          */
69         struct MidiEvent {
70                 MidiEvent(double t=0, size_t s=0, Byte* b=NULL)
71                 : time(t), size(s), buffer(b)
72                 {}
73                 
74                 double time;   /**< Sample index (or beat time) at which event is valid */
75                 size_t size;   /**< Number of bytes of data in \a buffer */
76                 Byte*  buffer; /**< Raw MIDI data */
77         };
78
79         enum IOChange {
80                 NoChange = 0,
81                 ConfigurationChanged = 0x1,
82                 ConnectionsChanged = 0x2
83         };
84
85         enum OverlapType {
86                 OverlapNone,      // no overlap
87                 OverlapInternal,  // the overlap is 100% with the object
88                 OverlapStart,     // overlap covers start, but ends within
89                 OverlapEnd,       // overlap begins within and covers end 
90                 OverlapExternal   // overlap extends to (at least) begin+end
91         };
92
93         OverlapType coverage (nframes_t start_a, nframes_t end_a,
94                               nframes_t start_b, nframes_t end_b);
95
96         enum AutomationType {
97                 GainAutomation = 0x1,
98                 PanAutomation = 0x2,
99                 PluginAutomation = 0x4,
100                 SoloAutomation = 0x8,
101                 MuteAutomation = 0x10
102         };
103
104         enum AutoState {
105                 Off = 0x0,
106                 Write = 0x1,
107                 Touch = 0x2,
108                 Play = 0x4
109         };
110
111         std::string auto_state_to_string (AutoState);
112         AutoState string_to_auto_state (std::string);
113
114         enum AutoStyle {
115                 Absolute = 0x1,
116                 Trim = 0x2
117         };
118
119         std::string auto_style_to_string (AutoStyle);
120         AutoStyle string_to_auto_style (std::string);
121
122         enum AlignStyle {
123                 CaptureTime,
124                 ExistingMaterial
125         };
126
127         enum MeterPoint {
128                 MeterInput,
129                 MeterPreFader,
130                 MeterPostFader
131         };
132
133         enum TrackMode {
134                 Normal,
135                 Destructive
136         };
137         
138         struct BBT_Time {
139             uint32_t bars;
140             uint32_t beats;
141             uint32_t ticks;
142
143             BBT_Time() {
144                     bars = 1;
145                     beats = 1;
146                     ticks = 0;
147             }
148
149             /* we can't define arithmetic operators for BBT_Time, because
150                the results depend on a TempoMap, but we can define 
151                a useful check on the less-than condition.
152             */
153
154             bool operator< (const BBT_Time& other) const {
155                     return bars < other.bars || 
156                             (bars == other.bars && beats < other.beats) ||
157                             (bars == other.bars && beats == other.beats && ticks < other.ticks);
158             }
159
160             bool operator== (const BBT_Time& other) const {
161                     return bars == other.bars && beats == other.beats && ticks == other.ticks;
162             }
163             
164         };
165         enum SmpteFormat {
166                 smpte_23976,
167                 smpte_24,
168                 smpte_24976,
169                 smpte_25,
170                 smpte_2997,
171                 smpte_2997drop,
172                 smpte_30,
173                 smpte_30drop,
174                 smpte_5994,
175                 smpte_60
176         };
177
178         struct AnyTime {
179             enum Type {
180                     SMPTE,
181                     BBT,
182                     Frames,
183                     Seconds
184             };
185
186             Type type;
187
188             SMPTE::Time    smpte;
189             BBT_Time       bbt;
190
191             union { 
192                 nframes_t frames;
193                 double         seconds;
194             };
195
196             AnyTime() { type = Frames; frames = 0; }
197         };
198
199         struct AudioRange {
200             nframes_t start;
201             nframes_t end;
202             uint32_t id;
203             
204             AudioRange (nframes_t s, nframes_t e, uint32_t i) : start (s), end (e) , id (i) {}
205             
206             nframes_t length() { return end - start + 1; } 
207
208             bool operator== (const AudioRange& other) const {
209                     return start == other.start && end == other.end && id == other.id;
210             }
211
212             bool equal (const AudioRange& other) const {
213                     return start == other.start && end == other.end;
214             }
215
216             OverlapType coverage (nframes_t s, nframes_t e) const {
217                     return ARDOUR::coverage (start, end, s, e);
218             }
219         };
220         
221         struct MusicRange {
222             BBT_Time start;
223             BBT_Time end;
224             uint32_t id;
225             
226             MusicRange (BBT_Time& s, BBT_Time& e, uint32_t i)
227                     : start (s), end (e), id (i) {}
228
229             bool operator== (const MusicRange& other) const {
230                     return start == other.start && end == other.end && id == other.id;
231             }
232
233             bool equal (const MusicRange& other) const {
234                     return start == other.start && end == other.end;
235             }
236         };
237
238         /*
239             Slowest = 6.6dB/sec falloff at update rate of 40ms
240             Slow    = 6.8dB/sec falloff at update rate of 40ms
241         */
242
243         enum MeterFalloff {
244                 MeterFalloffOff = 0,
245                 MeterFalloffSlowest = 1,
246                 MeterFalloffSlow = 2,
247                 MeterFalloffMedium = 3,
248                 MeterFalloffFast = 4,
249                 MeterFalloffFaster = 5,
250                 MeterFalloffFastest = 6
251         };
252
253         enum MeterHold {
254                 MeterHoldOff = 0,
255                 MeterHoldShort = 40,
256                 MeterHoldMedium = 100,
257                 MeterHoldLong = 200
258         };
259
260         enum EditMode {
261                 Slide,
262                 Splice
263         };
264
265         enum RegionPoint { 
266             Start,
267             End,
268             SyncPoint
269         };
270
271         enum Change {
272                 range_guarantee = ~0
273         };
274
275
276         enum Placement {
277                 PreFader,
278                 PostFader
279         };
280
281         enum MonitorModel {
282                 HardwareMonitoring,
283                 SoftwareMonitoring,
284                 ExternalMonitoring,
285         };
286
287         enum DenormalModel {
288                 DenormalNone,
289                 DenormalFTZ,
290                 DenormalDAZ,
291                 DenormalFTZDAZ
292         };
293
294         enum RemoteModel {
295                 UserOrdered,
296                 MixerOrdered,
297                 EditorOrdered,
298         };
299
300         enum CrossfadeModel {
301                 FullCrossfade,
302                 ShortCrossfade
303         };
304         
305         enum LayerModel {
306                 LaterHigher,
307                 MoveAddHigher,
308                 AddHigher
309         };
310
311         enum SoloModel {
312                 InverseMute,
313                 SoloBus
314         };
315
316         enum AutoConnectOption {
317                 AutoConnectPhysical = 0x1,
318                 AutoConnectMaster = 0x2
319         };
320
321         struct InterThreadInfo {
322             volatile bool  done;
323             volatile bool  cancel;
324             volatile float progress;
325             pthread_t      thread;
326         };
327
328         enum SampleFormat {
329                 FormatFloat = 0,
330                 FormatInt24
331         };
332
333
334         enum HeaderFormat {
335                 BWF,
336                 WAVE,
337                 WAVE64,
338                 CAF,
339                 AIFF,
340                 iXML,
341                 RF64
342         };
343
344         struct PeakData {
345             typedef Sample PeakDatum;
346             
347             PeakDatum min;
348             PeakDatum max;
349         };
350         
351         enum PluginType {
352                 AudioUnit,
353                 LADSPA,
354                 VST
355         };
356
357         enum SlaveSource {
358                 None = 0,
359                 MTC,
360                 JACK
361         };
362
363         enum ShuttleBehaviour {
364                 Sprung,
365                 Wheel
366         };
367
368         enum ShuttleUnits {
369                 Percentage,
370                 Semitones
371         };
372
373         typedef std::vector<boost::shared_ptr<Source> > SourceList;
374 } // namespace ARDOUR
375
376 std::istream& operator>>(std::istream& o, ARDOUR::SampleFormat& sf);
377 std::istream& operator>>(std::istream& o, ARDOUR::HeaderFormat& sf);
378 std::istream& operator>>(std::istream& o, ARDOUR::AutoConnectOption& sf);
379 std::istream& operator>>(std::istream& o, ARDOUR::EditMode& sf);
380 std::istream& operator>>(std::istream& o, ARDOUR::MonitorModel& sf);
381 std::istream& operator>>(std::istream& o, ARDOUR::RemoteModel& sf);
382 std::istream& operator>>(std::istream& o, ARDOUR::SoloModel& sf);
383 std::istream& operator>>(std::istream& o, ARDOUR::LayerModel& sf);
384 std::istream& operator>>(std::istream& o, ARDOUR::CrossfadeModel& sf);
385 std::istream& operator>>(std::istream& o, ARDOUR::SlaveSource& sf);
386 std::istream& operator>>(std::istream& o, ARDOUR::ShuttleBehaviour& sf);
387 std::istream& operator>>(std::istream& o, ARDOUR::ShuttleUnits& sf);
388 std::istream& operator>>(std::istream& o, ARDOUR::SmpteFormat& sf);
389 std::istream& operator>>(std::istream& o, ARDOUR::DenormalModel& sf);
390
391 using ARDOUR::nframes_t;
392
393 static inline nframes_t
394 session_frame_to_track_frame (nframes_t session_frame, double speed)
395 {
396         return (nframes_t)( (double)session_frame * speed );
397 }
398
399 static inline nframes_t
400 track_frame_to_session_frame (nframes_t track_frame, double speed)
401 {
402         return (nframes_t)( (double)track_frame / speed );
403 }
404
405
406 #endif /* __ardour_types_h__ */
407