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 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 <stdbool.h>
16
17#include "utils/types.h"
18#include "utils/yaml.h"
19
26#define POSITION_SCHEMA_VERSION 1
27
28#define TICKS_PER_QUARTER_NOTE 960
29#define TICKS_PER_SIXTEENTH_NOTE 240
30#define TICKS_PER_QUARTER_NOTE_DBL 960.0
31#define TICKS_PER_SIXTEENTH_NOTE_DBL 240.0
32#define TICKS_PER_NINETYSIXTH_NOTE_DBL 40.0
33#define position_add_sixteenths(_pos, _s) \
34 position_add_ticks ((_pos), (_s) * TICKS_PER_SIXTEENTH_NOTE)
35#define position_add_beats(_pos, _b) \
36 g_warn_if_fail (TRANSPORT->ticks_per_beat > 0); \
37 position_add_ticks ((_pos), (_b) * TRANSPORT->ticks_per_beat)
38#define position_add_bars(_pos, _b) \
39 g_warn_if_fail (TRANSPORT->ticks_per_bar > 0); \
40 position_add_ticks ((_pos), (_b) * TRANSPORT->ticks_per_bar)
41#define position_snap_simple(pos, sg) position_snap (NULL, pos, NULL, NULL, sg)
42
43#define POSITION_MAX_BAR 160000
44
49#define position_between_frames_excl2(pos, f1, f2) \
50 ((pos)->frames >= f1 && (pos)->frames < f2)
51
59#define position_compare_frames(p1, p2) ((p1)->frames - (p2)->frames)
60
62#define position_is_before(_pos, _cmp) \
63 (position_compare_frames (_pos, _cmp) < 0)
64
66#define position_is_before_or_equal(_pos, _cmp) \
67 (position_compare_frames (_pos, _cmp) <= 0)
68
70#define position_is_equal(_pos, _cmp) \
71 (position_compare_frames (_pos, _cmp) == 0)
72
74#define position_is_after(_pos, _cmp) (position_compare_frames (_pos, _cmp) > 0)
75
77#define position_is_after_or_equal(_pos, _cmp) \
78 (position_compare_frames (_pos, _cmp) >= 0)
79
80#define position_is_positive(pos) ((pos)->frames >= 0 && (pos)->ticks >= 0)
81
89#define position_compare_ticks(p1, p2) ((p1)->ticks - (p2)->ticks)
90
91#define position_is_equal_ticks(p1, p2) \
92 (fabs (position_compare_ticks (p1, p2)) <= DBL_EPSILON)
93
96#define position_is_between(_pos, _start, _end) \
97 (position_is_after_or_equal (_pos, _start) && position_is_before (_pos, _end))
98
101#define position_is_between_excl_start(_pos, _start, _end) \
102 (position_is_after (_pos, _start) && position_is_before (_pos, _end))
103
105#define position_min(p1, p2) (position_compare_frames (p1, p2) < 0 ? p1 : p2)
106
108#define position_max(p1, p2) (position_compare_frames (p1, p2) > 0 ? p1 : p2)
109
111#define POSITION_INIT_ON_STACK(name) Position name = POSITION_START;
112
118#define position_init(__pos) *(__pos) = POSITION_START
119
120typedef struct SnapGrid SnapGrid;
121typedef struct Track Track;
122typedef struct ZRegion ZRegion;
123
128typedef struct Position
129{
130 int schema_version;
131
133 double ticks;
134
141
151 // double precise_frames;
152} Position;
153
154static const cyaml_schema_field_t position_fields_schema[] = {
155 YAML_FIELD_INT (Position, schema_version), YAML_FIELD_FLOAT (Position, ticks),
156 YAML_FIELD_INT (Position, frames),
157
158 CYAML_FIELD_END
159};
160
161static const cyaml_schema_value_t position_schema = {
162 YAML_VALUE_PTR (Position, position_fields_schema),
163};
164
166static const Position POSITION_START = {
167 .schema_version = POSITION_SCHEMA_VERSION,
168 .ticks = 0.0,
169 .frames = 0
170};
171
172static inline int
173position_cmp_func (const void * _a, const void * _b)
174{
175 const Position * a = (Position const *) _a;
176 const Position * b = (Position const *) _b;
177
178 /* prevent conversion overflows */
180 if (diff < 0)
181 return -1;
182 else if (diff > 0)
183 return 1;
184 else
185 return 0;
186}
187
191void
193
197void
198position_sort_array (Position * array, const size_t size);
199
205#define position_set_to_pos(_pos, _target) *(_pos) = *(_target)
206
212HOT void
214
216#define position_to_frames(x) ((x)->frames)
217#define position_to_ticks(x) ((x)->ticks)
218
223void
224position_from_seconds (Position * position, double secs);
225
226HOT NONNULL void
227position_from_frames (Position * pos, const signed_frame_t frames);
228
232HOT NONNULL void
233position_from_ticks (Position * pos, double ticks);
234
235NONNULL void
236position_from_ms (Position * pos, const signed_ms_t ms);
237
238NONNULL void
239position_from_bars (Position * pos, int bars);
240
241HOT NONNULL void
242position_add_ticks (Position * self, double ticks);
243
249
251position_ms_to_frames (const double ms);
252
253double
254position_ms_to_ticks (const double ms);
255
256void
257position_add_ms (Position * pos, const double ms);
258
259void
260position_add_minutes (Position * pos, int mins);
261
262void
263position_add_seconds (Position * pos, const signed_sec_t seconds);
264
283NONNULL_ARGS (2)
285 const Position * start_pos,
286 Position * pos,
287 Track * track,
288 ZRegion * region,
289 const SnapGrid * sg);
290
301void
303 const Position * start_pos,
304 Position * end_pos,
305 SnapGrid * snap);
306
313HOT NONNULL void
314position_update_ticks_from_frames (Position * position, double ticks_per_frame);
315
323position_get_frames_from_ticks (double ticks, double frames_per_tick);
324
331HOT NONNULL void
332position_update_frames_from_ticks (Position * self, double frames_per_tick);
333
343static inline void
344position_update (Position * self, bool from_ticks, double ratio)
345{
346 if (from_ticks)
348 else
350}
351
358void
359position_get_midway_pos (Position * start_pos, Position * end_pos, Position * pos);
360
371double
373 const Position * end_pos,
374 const Position * start_pos,
375 const SnapGrid * sg);
376
383NONNULL char *
385
386NONNULL void
387position_to_string_full (const Position * pos, char * buf, int decimal_places);
388
393NONNULL void
394position_to_string (const Position * pos, char * buf);
395
401NONNULL WARN_UNUSED_RESULT bool
402position_parse (Position * pos, const char * str);
403
407NONNULL void
409
410NONNULL void
411position_print_range (const Position * pos, const Position * pos2);
412
419NONNULL int
420position_get_total_bars (const Position * pos, bool include_current);
421
428NONNULL int
429position_get_total_beats (const Position * pos, bool include_current);
430
435NONNULL int
436position_get_total_sixteenths (const Position * pos, bool include_current);
437
443NONNULL void
445
455NONNULL int
456position_get_bars (const Position * pos, bool start_at_one);
457
467NONNULL int
468position_get_beats (const Position * pos, bool start_at_one);
469
479NONNULL int
480position_get_sixteenths (const Position * pos, bool start_at_one);
481
488NONNULL double
490
491NONNULL bool
492position_validate (const Position * pos);
493
498#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.
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:59
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.
void position_snap(const Position *start_pos, Position *pos, Track *track, ZRegion *region, const SnapGrid *sg)
Snaps position using given options.
signed_frame_t signed_sec_t
Signed second index.
Definition types.h:68
int_fast64_t signed_frame_t
Signed type for frame index.
Definition types.h:55
signed_frame_t signed_ms_t
Signed millisecond index.
Definition types.h:65
#define YAML_VALUE_PTR(cc, fields_schema)
Schema to be used as a pointer.
Definition yaml.h:202
A Position is made up of bars.beats.sixteenths.ticks.
Definition position.h:129
double ticks
Precise total number of ticks.
Definition position.h:133
signed_frame_t frames
Position in frames (samples).
Definition position.h:140
Track to be inserted into the Project's Tracklist.
Definition track.h:186
A region (clip) is an object on the timeline that contains either MidiNote's or AudioClip's.
Definition region.h:77
Custom types.
YAML utils.