Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
types.h
1// SPDX-FileCopyrightText: © 2019-2022, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "zrythm-config.h"
7
8#include <chrono>
9#include <cinttypes>
10#include <cstdint>
11#include <filesystem>
12
13#include <QtTypes>
14
15#include <gsl-lite/gsl-lite.hpp>
16
17#if defined(__GNUC__) || defined(__clang__)
18# pragma GCC diagnostic push
19# pragma GCC diagnostic ignored "-Wshadow"
20#elifdef _MSC_VER
21# pragma warning(push)
22# pragma warning(disable : 4458) // declaration hides class member
23#endif
24
25#include <magic_enum_all.hpp>
26
27#if defined(__GNUC__) || defined(__clang__)
28# pragma GCC diagnostic pop
29#elifdef _MSC_VER
30# pragma warning(pop)
31#endif
32
33using namespace magic_enum::bitwise_operators;
34using namespace std::literals;
35namespace gsl = ::gsl_lite;
36
37// For compilers without __cpp_size_t_suffix
38constexpr size_t
39operator""_zu (unsigned long long int x)
40{
41 return static_cast<size_t> (x);
42}
43
49
50// qint64
51using RtTimePoint = int64_t;
52using RtDuration = int64_t;
53
55using midi_byte_t = uint8_t;
56
58using nframes_t = uint32_t;
59
61using midi_time_t = uint32_t;
62
64using channels_t = uint_fast8_t;
65
67using audio_sample_type_t = float;
68
70using bpm_t = float;
71
72using curviness_t = double;
73
75using signed_frame_t = int_fast64_t;
76
78using unsigned_frame_t = uint_fast64_t;
79
82
85
87using ProcessId = qint64;
88
92using GenericFloatGetter = std::function<float ()>;
93
97using GenericFloatSetter = std::function<void (float)>;
98
102using GenericStringGetter = std::function<std::string ()>;
103
107using GenericStringSetter = std::function<void (const std::string &)>;
108
112using GenericCallback = std::function<void ()>;
113
114using GenericBoolGetter = std::function<bool ()>;
115
117{
120
123
126};
127
133{
134public:
135 void print () const;
136
137public:
140
144
148
153};
154
155// TODO: check if pausing/resuming can be done with RAII
157{
164};
165
166enum class TimeFormat : std::uint8_t
167{
175};
176
180enum class BeatUnit
181{
182 Two,
183 Four,
184 Eight,
185 Sixteen
186};
187
188#define ENUM_INT_TO_VALUE_CONST(_enum, _int) \
189 (magic_enum::enum_value<_enum, _int> ())
190#define ENUM_INT_TO_VALUE(_enum, _int) (magic_enum::enum_value<_enum> (_int))
191#define ENUM_VALUE_TO_INT(_val) (magic_enum::enum_integer (_val))
192
193#define ENUM_ENABLE_BITSET(_enum) \
194 template <> struct magic_enum::customize::enum_range<_enum> \
195 { \
196 static constexpr bool is_flags = true; \
197 }
198#define ENUM_BITSET(_enum, _val) (magic_enum::containers::bitset<_enum> (_val))
199#define ENUM_BITSET_TEST(_val, _other_val) \
200 /* (ENUM_BITSET (_enum, _val).test (_other_val)) */ \
201 (static_cast<std::underlying_type_t<decltype (_val)>> (_val) \
202 & static_cast<std::underlying_type_t<decltype (_val)>> (_other_val))
203
206#define ENUM_BITSET_TO_STRING(_enum, _val) \
207 (ENUM_BITSET (_enum, _val).to_string ().data ())
208
209#define ENUM_COUNT(_enum) (magic_enum::enum_count<_enum> ())
210#define ENUM_NAME(_val) (magic_enum::enum_name (_val).data ())
211#define ENUM_NAME_FROM_INT(_enum, _int) \
212 ENUM_NAME (ENUM_INT_TO_VALUE (_enum, _int))
213
214enum class CacheType
215{
216 // TrackNameHashes = 1 << 0,
217 // PluginPorts = 1 << 1,
218 PlaybackSnapshots = 1 << 2,
219 AutomationLaneRecordModes = 1 << 3,
220 AutomationLanePorts = 1 << 4,
221};
222
223ENUM_ENABLE_BITSET (CacheType);
224
225constexpr CacheType ALL_CACHE_TYPES =
226 CacheType::PlaybackSnapshots | CacheType::AutomationLaneRecordModes
227 | CacheType::AutomationLanePorts;
228
229/* types for simple timestamps/durations */
230using SteadyClock = std::chrono::steady_clock;
231using SteadyTimePoint = SteadyClock::time_point;
232using SteadyDuration = SteadyClock::duration;
233
234namespace fs = std::filesystem;
235
236#define ZRYTHM_IS_QT_THREAD (QThread::currentThread () == qApp->thread ())
237
238template <typename T>
239std::string
240typename_to_string ()
241{
242 return typeid (T).name ();
243}
244
245#define Z_DISABLE_COPY_MOVE(Class) Q_DISABLE_COPY_MOVE (Class)
246#define Z_DISABLE_COPY(Class) Q_DISABLE_COPY (Class)
247#define Z_DISABLE_MOVE(Class) \
248 Class (Class &&) = delete; \
249 Class &operator= (Class &&) = delete;
250
263template <typename T> struct OptionalRef
264{
265 OptionalRef () = default;
266 OptionalRef (std::nullopt_t) { }
267 OptionalRef (T &ref) : ref_ (ref) { }
268
269 std::optional<std::reference_wrapper<T>> ref_;
270
277 {
278 assert (has_value ());
279 return ref_->get ();
280 }
281 const T &operator* () const
282 {
283 assert (has_value ());
284 return ref_->get ();
285 }
286
287 const T * operator->() const
288 {
289 assert (has_value ());
290 return std::addressof (ref_->get ());
291 }
292 T * operator->()
293 {
294 assert (has_value ());
295 return std::addressof (ref_->get ());
296 }
297
298 explicit operator bool () const { return has_value (); }
299
305 bool has_value () const { return ref_.has_value (); }
306
312 T &value ()
313 {
314 assert (has_value ());
315 return ref_->get ();
316 }
317};
318
319template <typename Tuple, typename Callable>
320void
321iterate_tuple (Callable c, Tuple &&t)
322{
323 std::apply ([&] (auto &&... args) { (c (args), ...); }, t);
324}
325
326// We're using the C type here because MOC complains it can't find the type
327// otherwise (not even std::uint8_t)
328using basic_enum_base_type_t = uint8_t;
329
BeatUnit
Beat unit.
Definition types.h:181
std::function< float()> GenericFloatGetter
Getter prototype for float values.
Definition types.h:92
uint_fast64_t unsigned_frame_t
Unsigned type for frame index.
Definition types.h:78
std::function< void(float)> GenericFloatSetter
Setter prototype for float values.
Definition types.h:97
std::function< std::string()> GenericStringGetter
Getter prototype for strings.
Definition types.h:102
qint64 ProcessId
GPid equivalent.
Definition types.h:87
uint32_t nframes_t
Frame count.
Definition types.h:58
float audio_sample_type_t
The sample type.
Definition types.h:67
float bpm_t
The BPM type.
Definition types.h:70
uint32_t midi_time_t
MIDI time in global frames.
Definition types.h:61
signed_frame_t signed_ms_t
Signed millisecond index.
Definition types.h:81
TimeFormat
Definition types.h:167
signed_frame_t signed_sec_t
Signed second index.
Definition types.h:84
AudioValueFormat
Definition types.h:117
uint_fast8_t channels_t
Number of channels.
Definition types.h:64
std::function< void(const std::string &)> GenericStringSetter
Setter prototype for float values.
Definition types.h:107
std::function< void()> GenericCallback
Generic callback.
Definition types.h:112
int_fast64_t signed_frame_t
Signed type for frame index.
Definition types.h:75
uint8_t midi_byte_t
MIDI byte.
Definition types.h:55
@ Musical
Musical time (ticks).
Definition types.h:169
@ Absolute
Absolute time (seconds).
Definition types.h:174
@ DBFS
dbFS.
Definition types.h:122
@ Fader
0 to 1, suitable for drawing.
Definition types.h:125
@ Amplitude
0 to 2, amplitude.
Definition types.h:119
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition types.h:133
nframes_t local_offset_
Offset in the current processing cycle, between 0 and the number of frames in AudioEngine....
Definition types.h:147
unsigned_frame_t g_start_frame_
Global position at the start of the processing cycle (no offset added).
Definition types.h:139
unsigned_frame_t g_start_frame_w_offset_
Global position with EngineProcessTimeInfo.local_offset added, for convenience.
Definition types.h:143
nframes_t nframes_
Number of frames to process in this call, starting from the offset.
Definition types.h:152
bool looping_
Transport loop.
Definition types.h:163
bool running_
Engine running.
Definition types.h:159
bool playing_
Playback.
Definition types.h:161
bool has_value() const
Check if a value is present.
Definition types.h:305
T & operator*()
Dereference the underlying value.
Definition types.h:276
T & value()
Get a reference to the underlying value.
Definition types.h:312