Ardour  9.0-rc2-96-ge381c29281
temporal/temporal/types.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #pragma once
20 
21 #include <cassert>
22 #include <cstdlib>
23 #include <ostream>
24 #include <stdint.h>
25 
26 #include "pbd/integer_division.h"
27 
28 #include "temporal/visibility.h"
29 
30 namespace Temporal {
31 
32 #ifdef COMPILER_MSVC
33  LIBTEMPORAL_API void init ();
34  LIBTEMPORAL_API void reset ();
35 #else
36  extern void init ();
37  extern void reset ();
38 #endif
39 
40 /* Any position measured in audio samples.
41  Assumed to be non-negative but not enforced.
42 */
43 typedef int64_t samplepos_t;
44 
45 /* Any distance from a given samplepos_t.
46  Maybe positive or negative.
47 */
48 typedef int64_t sampleoffset_t;
49 
50 /* Any count of audio samples.
51  Assumed to be positive but not enforced.
52 */
53 typedef int64_t samplecnt_t;
54 
55 static const samplepos_t max_samplepos = INT64_MAX;
56 static const samplecnt_t max_samplecnt = INT64_MAX;
57 
58 /* This defines the smallest division of a "beat".
59 
60  The number is intended to have as many integer factors as possible so that
61  1/Nth divisions are integer numbers of ticks.
62 
63  1920 has many factors, though going up to 3840 gets a couple more.
64 */
65 
66 static const int32_t ticks_per_beat = 1920;
67 
68 template<class T>
69 class _ratio_t {
70  public:
71  /* do not allow negative values, this is just a ratio */
72 
73  _ratio_t (T n, T d) : _numerator (std::abs (n)), _denominator (std::abs(d)) { assert (_denominator != 0); }
74  _ratio_t (T n) : _numerator (std::abs (n)), _denominator (1) {}
75 
76  T numerator() const { return _numerator; }
77  T denominator() const { return _denominator; }
78 
79  bool is_unity() const { return _numerator == _denominator; }
80  bool is_zero() const { return _numerator == 0; }
81 
82  double to_double() const { return (double) _numerator / _denominator; };
83 
84  bool operator== (_ratio_t<T> const & other) const {
85  return _numerator == other._numerator && _denominator == other._denominator;
86  }
87 
88  bool operator!= (_ratio_t<T> const & other) const {
89  return _numerator != other._numerator || _denominator != other._denominator;
90  }
91 
92  private:
95 };
96 
98 
100  OverlapNone, // no overlap
101  OverlapInternal, // the overlap is 100% within the object
102  OverlapStart, // overlap covers start, but ends within
103  OverlapEnd, // overlap begins within and covers end
104  OverlapExternal // overlap extends to (at least) begin+end
105 };
106 
108  /* simple ordinals, since these are mutually exclusive */
110  BeatTime = 1,
111 };
112 
113 enum Dirty {
114  /* combinable */
115  SampleDirty = 0x1,
116  BeatsDirty = 0x2,
117  BBTDirty = 0x4
118 };
119 
120 enum RoundMode {
125  RoundUpMaybe = 2
126 };
127 
128 extern void setup_enum_writer ();
129 
130 }
131 
132 std::ostream& operator<< (std::ostream& o, Temporal::ratio_t const & r);
133 
bool operator==(_ratio_t< T > const &other) const
bool operator!=(_ratio_t< T > const &other) const
static const int32_t ticks_per_beat
static const samplecnt_t max_samplecnt
_ratio_t< int64_t > ratio_t
void reset()
static const samplepos_t max_samplepos
@ RoundDownAlways
Always round down, even if on a division.
@ RoundNearest
Round to nearest.
@ RoundUpAlways
Always round up, even if on a division.
@ RoundUpMaybe
Round up only if necessary.
@ RoundDownMaybe
Round down only if necessary.
void setup_enum_writer()
void init()
std::ostream & operator<<(std::ostream &o, Temporal::ratio_t const &r)
#define LIBTEMPORAL_API