summaryrefslogtreecommitdiff
path: root/libs/evoral/evoral/types.hpp
blob: 719007abe5adde5c06bc51ca5479aef454b79896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* This file is part of Evoral.
 * Copyright (C) 2008 David Robillard <http://drobilla.net>
 * Copyright (C) 2000-2008 Paul Davis
 *
 * Evoral is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef EVORAL_TYPES_HPP
#define EVORAL_TYPES_HPP

#include <float.h>
#include <math.h>
#include <stdint.h>

#include <iostream>
#include <limits>
#include <list>

#include "evoral/visibility.h"

#include "pbd/debug.h"

namespace Evoral {

/** ID of an event (note or other). This must be operable on by glib
    atomic ops
*/
typedef int32_t event_id_t;

/** Musical time: beats relative to some defined origin */
class LIBEVORAL_API MusicalTime {
public:
	static const double PPQN;

	MusicalTime() : _time(0.0) {}

	/** Create from a real number of beats. */
	explicit MusicalTime(double time) : _time(time) {}

	/** Create from an integer number of beats. */
	static MusicalTime beats(int32_t beats) {
		return MusicalTime((double)beats);
	}

	/** Create from ticks at the standard PPQN. */
	static MusicalTime ticks(uint32_t ticks) {
		return MusicalTime(ticks / PPQN);
	}

	/** Create from ticks at a given rate.
	 *
	 * Note this can also be used to create from frames by setting ppqn to the
	 * number of samples per beat.
	 */
	static MusicalTime ticks_at_rate(uint64_t ticks, uint32_t ppqn) {
		return MusicalTime((double)ticks / (double)ppqn);
	}

	MusicalTime& operator=(const MusicalTime& other) {
		_time = other._time;
		return *this;
	}

	MusicalTime round_up_to_beat() const {
		return Evoral::MusicalTime(ceil(_time));
	}

	MusicalTime round_down_to_beat() const {
		return Evoral::MusicalTime(floor(_time));
	}

	MusicalTime snap_to(const Evoral::MusicalTime& snap) const {
		return MusicalTime(ceil(_time / snap._time) * snap._time);
	}

	inline bool operator==(const MusicalTime& b) const {
		/* Acceptable tolerance is 1 tick. */
		return fabs(_time - b._time) <= (1.0 / PPQN);
	}

	inline bool operator==(double t) const {
		/* Acceptable tolerance is 1 tick. */
		return fabs(_time - t) <= (1.0 / PPQN);
	}

	inline bool operator==(int beats) const {
		/* Acceptable tolerance is 1 tick. */
		return fabs(_time - beats) <= (1.0 / PPQN);
	}

	inline bool operator!=(const MusicalTime& b) const {
		return !operator==(b);
	}

	inline bool operator<(const MusicalTime& b) const {
		/* Acceptable tolerance is 1 tick. */
		if (fabs(_time - b._time) <= (1.0 / PPQN)) {
			return false;  /* Effectively identical. */
		} else {
			return _time < b._time;
		}
	}

	inline bool operator<=(const MusicalTime& b) const {
		return operator==(b) || operator<(b);
	}

	inline bool operator>(const MusicalTime& b) const {
		/* Acceptable tolerance is 1 tick. */
		if (fabs(_time - b._time) <= (1.0 / PPQN)) {
			return false;  /* Effectively identical. */
		} else {
			return _time > b._time;
		}
	}

	inline bool operator>=(const MusicalTime& b) const {
		return operator==(b) || operator>(b);
	}

	inline bool operator<(double b) const {
		/* Acceptable tolerance is 1 tick. */
		if (fabs(_time - b) <= (1.0 / PPQN)) {
			return false;  /* Effectively identical. */
		} else {
			return _time < b;
		}
	}

	inline bool operator<=(double b) const {
		return operator==(b) || operator<(b);
	}

	inline bool operator>(double b) const {
		/* Acceptable tolerance is 1 tick. */
		if (fabs(_time - b) <= (1.0 / PPQN)) {
			return false;  /* Effectively identical. */
		} else {
			return _time > b;
		}
	}

	inline bool operator>=(double b) const {
		return operator==(b) || operator>(b);
	}

	MusicalTime operator+(const MusicalTime& b) const {
		return MusicalTime(_time + b._time);
	}

	MusicalTime operator-(const MusicalTime& b) const {
		return MusicalTime(_time - b._time);
	}

	MusicalTime operator+(double d) const {
		return MusicalTime(_time + d);
	}

	MusicalTime operator-(double d) const {
		return MusicalTime(_time - d);
	}

	MusicalTime operator-() const {
		return MusicalTime(-_time);
	}

	template<typename Number>
	MusicalTime operator*(Number factor) const {
		return MusicalTime(_time * factor);
	}

	MusicalTime& operator+=(const MusicalTime& b) {
		_time += b._time;
		return *this;
	}

	MusicalTime& operator-=(const MusicalTime& b) {
		_time -= b._time;
		return *this;
	}

	double   to_double()              const { return _time; }
	uint64_t to_ticks()               const { return lrint(_time * PPQN); }
	uint64_t to_ticks(uint32_t ppqn)  const { return lrint(_time * ppqn); }

	uint32_t get_beats() const { return floor(_time); }
	uint32_t get_ticks() const { return (uint32_t)lrint(fmod(_time, 1.0) * PPQN); }

	bool operator!() const { return _time == 0; }

	static MusicalTime min()  { return MusicalTime(DBL_MIN); }
	static MusicalTime max()  { return MusicalTime(DBL_MAX); }
	static MusicalTime tick() { return MusicalTime(1.0 / PPQN); }

private:
	double _time;
};

extern const MusicalTime MaxMusicalTime;
extern const MusicalTime MinMusicalTime;

/** Type of an event (opaque, mapped by application) */
typedef uint32_t EventType;

/*
  TIL, several horrible hours later, that sometimes the compiler looks in the
  namespace of a type (Evoral::MusicalTime in this case) for an operator, and
  does *NOT* look in the global namespace.

  C++ is proof that hell exists and we are living in it.  In any case, move
  these to the global namespace and PBD::Property's loopy
  virtual-method-in-a-template will bite you.
*/

inline std::ostream&
operator<<(std::ostream& os, const MusicalTime& t)
{
	os << t.to_double();
	return os;
}

inline std::istream&
operator>>(std::istream& is, MusicalTime& t)
{
	double beats;
	is >> beats;
	t = MusicalTime(beats);
	return is;
}

} // namespace Evoral

namespace PBD {
	namespace DEBUG {
		LIBEVORAL_API extern uint64_t Sequence;
		LIBEVORAL_API extern uint64_t Note;
		LIBEVORAL_API extern uint64_t ControlList;
		LIBEVORAL_API extern uint64_t MusicalTime;
	}
}

namespace std {
	template<>
	struct numeric_limits<Evoral::MusicalTime> {
		static Evoral::MusicalTime min() { return Evoral::MusicalTime::min(); }
		static Evoral::MusicalTime max() { return Evoral::MusicalTime::max(); }
	};
}

#endif // EVORAL_TYPES_HPP