Zrythm
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
position.h
Go to the documentation of this file.
1// clang-format off
2// SPDX-FileCopyrightText: © 2018-2021, 2023-2024 Alexandros Theodotou <alex@zrythm.org>
3// SPDX-License-Identifier: LicenseRef-ZrythmLicense
4// clang-format on
5
12#ifndef __AUDIO_POSITION_H__
13#define __AUDIO_POSITION_H__
14
15#include "utils/types.h"
16#include "utils/yaml.h"
17
24#define TICKS_PER_QUARTER_NOTE 960
25#define TICKS_PER_SIXTEENTH_NOTE 240
26#define TICKS_PER_QUARTER_NOTE_DBL 960.0
27#define TICKS_PER_SIXTEENTH_NOTE_DBL 240.0
28#define TICKS_PER_NINETYSIXTH_NOTE_DBL 40.0
29#define position_add_sixteenths(_pos, _s) \
30 position_add_ticks ((_pos), (_s) * TICKS_PER_SIXTEENTH_NOTE)
31#define position_add_beats(_pos, _b) \
32 g_warn_if_fail (TRANSPORT->ticks_per_beat > 0); \
33 position_add_ticks ((_pos), (_b) * TRANSPORT->ticks_per_beat)
34#define position_add_bars(_pos, _b) \
35 g_warn_if_fail (TRANSPORT->ticks_per_bar > 0); \
36 position_add_ticks ((_pos), (_b) * TRANSPORT->ticks_per_bar)
37#define position_snap_simple(pos, sg) position_snap (NULL, pos, NULL, NULL, sg)
38
39#define POSITION_MAX_BAR 160000
40
45#define position_between_frames_excl2(pos, f1, f2) \
46 ((pos)->frames >= f1 && (pos)->frames < f2)
47
55#define position_compare_frames(p1, p2) ((p1)->frames - (p2)->frames)
56
58#define position_is_before(_pos, _cmp) \
59 (position_compare_frames (_pos, _cmp) < 0)
60
62#define position_is_before_or_equal(_pos, _cmp) \
63 (position_compare_frames (_pos, _cmp) <= 0)
64
66#define position_is_equal(_pos, _cmp) \
67 (position_compare_frames (_pos, _cmp) == 0)
68
70#define position_is_after(_pos, _cmp) (position_compare_frames (_pos, _cmp) > 0)
71
73#define position_is_after_or_equal(_pos, _cmp) \
74 (position_compare_frames (_pos, _cmp) >= 0)
75
76#define position_is_positive(pos) ((pos)->frames >= 0 && (pos)->ticks >= 0)
77
85#define position_compare_ticks(p1, p2) ((p1)->ticks - (p2)->ticks)
86
87#define position_is_equal_ticks(p1, p2) \
88 (fabs (position_compare_ticks (p1, p2)) <= DBL_EPSILON)
89
92#define position_is_between(_pos, _start, _end) \
93 (position_is_after_or_equal (_pos, _start) && position_is_before (_pos, _end))
94
97#define position_is_between_excl_start(_pos, _start, _end) \
98 (position_is_after (_pos, _start) && position_is_before (_pos, _end))
99
101#define position_min(p1, p2) (position_compare_frames (p1, p2) < 0 ? p1 : p2)
102
104#define position_max(p1, p2) (position_compare_frames (p1, p2) > 0 ? p1 : p2)
105
107#define POSITION_INIT_ON_STACK(name) Position name = POSITION_START;
108
114#define position_init(__pos) *(__pos) = POSITION_START
115
116typedef struct SnapGrid SnapGrid;
117typedef struct Track Track;
118typedef struct Region Region;
119
123typedef struct Position
124{
126 double ticks;
127
134
144 // double precise_frames;
145} Position;
146
148static const Position POSITION_START = { .ticks = 0.0, .frames = 0 };
149
150static inline int
151position_cmp_func (const void * _a, const void * _b)
152{
153 const Position * a = (Position const *) _a;
154 const Position * b = (Position const *) _b;
155
156 /* prevent conversion overflows */
158 if (diff < 0)
159 return -1;
160 else if (diff > 0)
161 return 1;
162 else
163 return 0;
164}
165
169void
171
175void
176position_sort_array (Position * array, const size_t size);
177
183#define position_set_to_pos(_pos, _target) *(_pos) = *(_target)
184
190HOT void
192
194#define position_to_frames(x) ((x)->frames)
195#define position_to_ticks(x) ((x)->ticks)
196
201void
202position_from_seconds (Position * position, double secs);
203
204HOT NONNULL void
205position_from_frames (Position * pos, const signed_frame_t frames);
206
210HOT NONNULL void
211position_from_ticks (Position * pos, double ticks);
212
213NONNULL void
214position_from_ms (Position * pos, const signed_ms_t ms);
215
216NONNULL void
217position_from_bars (Position * pos, int bars);
218
219HOT NONNULL void
220position_add_ticks (Position * self, double ticks);
221
227
229position_ms_to_frames (const double ms);
230
231double
232position_ms_to_ticks (const double ms);
233
234void
235position_add_ms (Position * pos, const double ms);
236
237void
238position_add_minutes (Position * pos, int mins);
239
240void
241position_add_seconds (Position * pos, const signed_sec_t seconds);
242
261NONNULL_ARGS (2)
263 const Position * start_pos,
264 Position * pos,
265 Track * track,
266 Region * region,
267 const SnapGrid * sg);
268
279void
281 const Position * start_pos,
282 Position * end_pos,
283 SnapGrid * snap);
284
291HOT NONNULL void
292position_update_ticks_from_frames (Position * position, double ticks_per_frame);
293
301position_get_frames_from_ticks (double ticks, double frames_per_tick);
302
309HOT NONNULL void
310position_update_frames_from_ticks (Position * self, double frames_per_tick);
311
321static inline void
322position_update (Position * self, bool from_ticks, double ratio)
323{
324 if (from_ticks)
326 else
328}
329
336void
337position_get_midway_pos (Position * start_pos, Position * end_pos, Position * pos);
338
349double
351 const Position * end_pos,
352 const Position * start_pos,
353 const SnapGrid * sg);
354
361NONNULL char *
363
364NONNULL void
365position_to_string_full (const Position * pos, char * buf, int decimal_places);
366
371NONNULL void
372position_to_string (const Position * pos, char * buf);
373
379NONNULL WARN_UNUSED_RESULT bool
380position_parse (Position * pos, const char * str);
381
385NONNULL void
387
388NONNULL void
389position_print_range (const Position * pos, const Position * pos2);
390
397NONNULL int
398position_get_total_bars (const Position * pos, bool include_current);
399
406NONNULL int
407position_get_total_beats (const Position * pos, bool include_current);
408
413NONNULL int
414position_get_total_sixteenths (const Position * pos, bool include_current);
415
421NONNULL void
423
433NONNULL int
434position_get_bars (const Position * pos, bool start_at_one);
435
445NONNULL int
446position_get_beats (const Position * pos, bool start_at_one);
447
457NONNULL int
458position_get_sixteenths (const Position * pos, bool start_at_one);
459
466NONNULL double
468
469NONNULL bool
470position_validate (const Position * pos);
471
476#endif
NONNULL_ARGS(1) int undo_manager_undo(UndoManager *self
Undo last action.
NONNULL int position_get_beats(const Position *pos, bool start_at_one)
Gets the beats of the position.
NONNULL int position_get_total_sixteenths(const Position *pos, bool include_current)
Returns the total number of sixteenths not including the current one.
NONNULL void position_print(const Position *pos)
Prints the Position in the "0.0.0.0" form.
HOT NONNULL void position_from_ticks(Position *pos, double ticks)
Sets position to the given total tick count.
HOT NONNULL void position_update_frames_from_ticks(Position *self, double frames_per_tick)
Updates frames.
signed_ms_t position_to_ms(const Position *pos)
Returns the Position in milliseconds.
NONNULL int position_get_sixteenths(const Position *pos, bool start_at_one)
Gets the sixteenths of the position.
signed_frame_t position_get_frames_from_ticks(double ticks, double frames_per_tick)
Converts ticks to frames.
NONNULL int position_get_bars(const Position *pos, bool start_at_one)
Gets the bars of the position.
void position_sort_array(Position *array, const size_t size)
Sorts an array of Position's.
void position_snap(const Position *start_pos, Position *pos, Track *track, Region *region, const SnapGrid *sg)
Snaps position using given options.
HOT void position_add_frames(Position *pos, const signed_frame_t frames)
Adds the frames to the position and updates the rest of the fields, and makes sure the frames are sti...
void position_set_min_size(const Position *start_pos, Position *end_pos, SnapGrid *snap)
Sets the end position to be 1 snap point away from the start pos.
void position_get_midway_pos(Position *start_pos, Position *end_pos, Position *pos)
Calculates the midway point between the two Positions and sets it on pos.
NONNULL void position_change_sign(Position *pos)
Changes the sign of the position.
NONNULL char * position_to_string_alloc(const Position *pos)
Creates a string in the form of "0.0.0.0" from the given position.
NONNULL double position_get_ticks(const Position *pos)
Gets the ticks of the position.
NONNULL WARN_UNUSED_RESULT bool position_parse(Position *pos, const char *str)
Parses a position from the given string.
void position_set_to_bar(Position *self, int bar)
Sets position to given bar.
#define position_compare_frames(p1, p2)
Compares 2 positions based on their frames.
Definition position.h:55
double position_get_ticks_diff(const Position *end_pos, const Position *start_pos, const SnapGrid *sg)
Returns the difference in ticks between the two Position's, snapped based on the given SnapGrid (if a...
NONNULL int position_get_total_beats(const Position *pos, bool include_current)
Returns the total number of beats.
NONNULL int position_get_total_bars(const Position *pos, bool include_current)
Returns the total number of beats.
HOT NONNULL void position_update_ticks_from_frames(Position *position, double ticks_per_frame)
Updates ticks.
NONNULL void position_to_string(const Position *pos, char *buf)
Creates a string in the form of "0.0.0.0" from the given position.
void position_from_seconds(Position *position, double secs)
Converts seconds to position and puts the result in the given Position.
signed_frame_t signed_sec_t
Signed second index.
Definition types.h:72
int_fast64_t signed_frame_t
Signed type for frame index.
Definition types.h:59
signed_frame_t signed_ms_t
Signed millisecond index.
Definition types.h:69
A Position is made up of bars.beats.sixteenths.ticks.
Definition position.h:124
double ticks
Precise total number of ticks.
Definition position.h:126
signed_frame_t frames
Position in frames (samples).
Definition position.h:133
A region (clip) is an object on the timeline that contains either MidiNote's or AudioClip's.
Definition region.h:72
Track to be inserted into the Project's Tracklist.
Definition track.h:177
Custom types.
YAML utils.