Zrythm
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
region.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2018-2022 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
9#ifndef __AUDIO_REGION_H__
10#define __AUDIO_REGION_H__
11
13#include "dsp/chord_object.h"
14#include "dsp/midi_note.h"
15#include "dsp/position.h"
18#include "utils/yaml.h"
19
20#include <glib/gi18n.h>
21
22typedef struct Channel Channel;
23typedef struct Track Track;
24typedef struct MidiNote MidiNote;
25typedef struct TrackLane TrackLane;
26typedef struct RegionLinkGroup RegionLinkGroup;
27typedef struct Stretcher Stretcher;
28typedef struct AudioClip AudioClip;
29
36#define REGION_SCHEMA_VERSION 1
37
38#define REGION_MAGIC 93075327
39#define IS_REGION(x) (((ZRegion *) x)->magic == REGION_MAGIC)
40#define IS_REGION_AND_NONNULL(x) (x && IS_REGION (x))
41
42#define REGION_PRINTF_FILENAME "%s_%s.mid"
43
44#define region_is_selected(r) \
45 arranger_object_is_selected ((ArrangerObject *) r)
46
61
62static const cyaml_strval_t region_musical_mode_strings[] = {
63 {N_ ("Inherit"), REGION_MUSICAL_MODE_INHERIT},
64 { N_ ("Off"), REGION_MUSICAL_MODE_OFF },
65 { N_ ("On"), REGION_MUSICAL_MODE_ON },
66};
67
77typedef struct ZRegion
78{
83
84 int schema_version;
85
88
90 char * name;
91
94
98 GdkRGBA color;
99
106
107 /* ==== MIDI REGION ==== */
108
113 int num_midi_notes;
114 size_t midi_notes_size;
115
131 int num_unended_notes;
132
133 /* ==== MIDI REGION END ==== */
134
135 /* ==== AUDIO REGION ==== */
136
140
149
154
157
163
166 float gain;
167
172
173#if 0
180 sample_t * frames;
181 size_t num_frames;
182
186 sample_t * ch_frames[16];
187#endif
188
191
194 int num_split_points;
195 size_t split_points_size;
196
197 /* ==== AUDIO REGION END ==== */
198
199 /* ==== AUTOMATION REGION ==== */
200
210 int num_aps;
211 size_t aps_size;
212
215
216 /* ==== AUTOMATION REGION END ==== */
217
218 /* ==== CHORD REGION ==== */
219
222 int num_chord_objects;
223 size_t chord_objects_size;
224
225 /* ==== CHORD REGION END ==== */
226
234
235 /* --- drawing caches --- */
236
237 /* New region drawing needs to be cached in the
238 * following situations:
239 *
240 * 1. when hidden part of the region is revealed
241 * (on x axis).
242 * TODO max 140% of the region should be
243 * cached (20% before and 20% after if before/
244 * after is not fully visible)
245 * 2. when full rect (x/width) changes
246 * 3. when a region marker is moved
247 * 4. when the clip actually changes (use
248 * last-change timestamp on the clip or region)
249 * 5. when fades change
250 * 6. when region height changes (track/lane)
251 */
252
254 PangoLayout * layout;
255
258 PangoLayout * chords_layout;
259
260 /* these are used for caching */
261 GdkRectangle last_main_full_rect;
262
265
266 /* these are used for caching */
267 GdkRectangle last_lane_full_rect;
268
271
275
278
282
283 /* --- drawing caches end --- */
284
285 int magic;
286} ZRegion;
287
288static const cyaml_schema_field_t region_fields_schema[] = {
289 YAML_FIELD_INT (ZRegion, schema_version),
291 ZRegion,
292 base,
293 arranger_object_fields_schema),
295 ZRegion,
296 id,
297 region_identifier_fields_schema),
298 YAML_FIELD_STRING_PTR (ZRegion, name),
299 YAML_FIELD_INT (ZRegion, pool_id),
300 YAML_FIELD_FLOAT (ZRegion, gain),
302 ZRegion,
303 color,
304 gdk_rgba_fields_schema),
305 YAML_FIELD_INT (ZRegion, use_color),
306 CYAML_FIELD_SEQUENCE_COUNT (
307 "midi_notes",
308 CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL,
309 ZRegion,
310 midi_notes,
311 num_midi_notes,
312 &midi_note_schema,
313 0,
314 CYAML_UNLIMITED),
315 CYAML_FIELD_SEQUENCE_COUNT (
316 "aps",
317 CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL,
318 ZRegion,
319 aps,
320 num_aps,
321 &automation_point_schema,
322 0,
323 CYAML_UNLIMITED),
324 CYAML_FIELD_SEQUENCE_COUNT (
325 "chord_objects",
326 CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL,
327 ZRegion,
328 chord_objects,
329 num_chord_objects,
330 &chord_object_schema,
331 0,
332 CYAML_UNLIMITED),
333 YAML_FIELD_ENUM (
334 ZRegion,
335 musical_mode,
336 region_musical_mode_strings),
337
338 CYAML_FIELD_END
339};
340
341static const cyaml_schema_value_t region_schema = {
342 YAML_VALUE_PTR_NULLABLE (ZRegion, region_fields_schema),
343};
344
348#define region_type_can_fade(rtype) \
349 (rtype == REGION_TYPE_AUDIO)
350
354void
356 ZRegion * region,
357 const Position * start_pos,
358 const Position * end_pos,
359 unsigned int track_name_hash,
360 int lane_pos,
361 int idx_inside_lane);
362
366HOT NONNULL ZRegion *
367region_find (const RegionIdentifier * const id);
368
369#if 0
370static inline void
371region_set_track_name_hash (
372 ZRegion * self,
373 unsigned int name_hash)
374{
375 self->id.track_name_hash = name_hash;
376}
377#endif
378
379NONNULL void
380region_print_to_str (
381 const ZRegion * self,
382 char * buf,
383 const size_t buf_size);
384
390NONNULL void
391region_print (const ZRegion * region);
392
398TrackLane *
399region_get_lane (const ZRegion * region);
400
408
417void
419 ZRegion * region,
420 int group_idx,
421 bool update_identifier);
422
423NONNULL bool
424region_has_link_group (ZRegion * region);
425
435MidiNote *
437
454NONNULL HOT signed_frame_t
456 const ZRegion * const self,
457 const signed_frame_t timeline_frames,
458 const bool normalize);
459
473NONNULL void
475 const ZRegion * const self,
476 const signed_frame_t timeline_frames,
477 signed_frame_t * ret_frames,
478 bool * is_loop);
479
485NONNULL void
486region_set_lane (ZRegion * self, const TrackLane * const lane);
487
495void
497 ZRegion * region,
498 const char * base_name,
499 AutomationTrack * at,
500 Track * track);
501
514NONNULL WARN_UNUSED_RESULT bool
515region_stretch (ZRegion * self, double ratio, GError ** error);
516
523NONNULL void
525
532NONNULL void
534
552void
554 ZRegion * region,
555 Track * track,
556 int lane_or_at_index,
557 int index);
558
563CONST
564static inline int
565region_type_has_lane (const RegionType type)
566{
567 return type == REGION_TYPE_MIDI || type == REGION_TYPE_AUDIO;
568}
569
575void
577 ZRegion * region,
578 AutomationTrack * at);
579
587
594void
596
609NONNULL PURE int
611 const ZRegion * region,
612 const signed_frame_t gframes,
613 const bool inclusive);
614
623int
625 const ZRegion * region,
626 const signed_frame_t gframes_start,
627 const signed_frame_t gframes_end,
628 const bool end_inclusive);
629
639NONNULL_ARGS (3)
641 const Track * track,
642 const AutomationTrack * at,
643 const Position * pos);
644
652char *
653region_generate_filename (ZRegion * region);
654
655void
656region_get_type_as_string (RegionType type, char * buf);
657
664bool
665region_is_recording (ZRegion * self);
666
675bool
676region_get_musical_mode (ZRegion * self);
677
683void
684region_add_arranger_object (
685 ZRegion * self,
686 ArrangerObject * obj,
687 bool fire_events);
688
689void
690region_create_link_group_if_none (ZRegion * region);
691
697void
698region_unlink (ZRegion * region);
699
705void
706region_remove_all_children (ZRegion * region);
707
712void
714
715NONNULL bool
716region_is_looped (const ZRegion * const self);
717
725region_get_arranger_selections (ZRegion * self);
726
733region_get_arranger_for_children (ZRegion * self);
734
744bool
745region_validate (
746 ZRegion * self,
747 bool is_project,
748 double frames_per_tick);
749
758void
759region_disconnect (ZRegion * self);
760
765#endif // __AUDIO_REGION_H__
Macros for arranger object backends.
Automation point API.
Chord object in the TimelineArranger.
API for MIDI notes in the PianoRoll.
RegionLinkGroup * region_get_link_group(ZRegion *self)
Returns the region's link group.
PURE ZRegion * region_at_position(const Track *track, const AutomationTrack *at, const Position *pos)
Returns the region at the given position in the given Track.
NONNULL void region_update_link_group(ZRegion *self)
Updates all other regions in the region link group, if any.
NONNULL HOT signed_frame_t region_timeline_frames_to_local(const ZRegion *const self, const signed_frame_t timeline_frames, const bool normalize)
Converts frames on the timeline (global) to local frames (in the clip).
void region_copy_children(ZRegion *dest, ZRegion *src)
Clones and copies all children from src to dest.
NONNULL void region_print(const ZRegion *region)
Print region info for debugging.
NONNULL PURE int region_is_hit(const ZRegion *region, const signed_frame_t gframes, const bool inclusive)
Returns if the position is inside the region or not.
NONNULL WARN_UNUSED_RESULT bool region_stretch(ZRegion *self, double ratio, GError **error)
Stretch the region's contents.
void region_init(ZRegion *region, const Position *start_pos, const Position *end_pos, unsigned int track_name_hash, int lane_pos, int idx_inside_lane)
Only to be used by implementing structs.
int region_is_hit_by_range(const ZRegion *region, const signed_frame_t gframes_start, const signed_frame_t gframes_end, const bool end_inclusive)
Returns if any part of the ZRegion is inside the given range, inclusive.
NONNULL void region_get_frames_till_next_loop_or_end(const ZRegion *const self, const signed_frame_t timeline_frames, signed_frame_t *ret_frames, bool *is_loop)
Returns the number of frames until the next loop end point or the end of the region.
MidiNote * region_find_midi_note(ZRegion *r, MidiNote *_mn)
Returns the MidiNote matching the properties of the given MidiNote.
AutomationTrack * region_get_automation_track(const ZRegion *const region)
Gets the AutomationTrack using the saved index.
NONNULL void region_set_lane(ZRegion *self, const TrackLane *const lane)
Sets the track lane.
RegionMusicalMode
Musical mode setting for audio regions.
Definition region.h:51
RegionType
Type of Region.
void region_move_to_track(ZRegion *region, Track *track, int lane_or_at_index, int index)
Moves the ZRegion to the given Track, maintaining the selection status of the ZRegion.
TrackLane * region_get_lane(const ZRegion *region)
Get lane.
NONNULL void region_update_identifier(ZRegion *self)
To be called every time the identifier changes to update the region's children.
void region_gen_name(ZRegion *region, const char *base_name, AutomationTrack *at, Track *track)
Generates a name for the ZRegion, either using the given AutomationTrack or Track,...
void region_copy(ZRegion *src, ZRegion *dest)
Copies the data from src to dest.
HOT NONNULL ZRegion * region_find(const RegionIdentifier *const id)
Looks for the ZRegion matching the identifier.
void region_set_automation_track(ZRegion *region, AutomationTrack *at)
Sets the automation track.
COLD NONNULL_ARGS(1) void automation_track_init_loaded(AutomationTrack *self
Inits a loaded AutomationTracklist.
void region_set_link_group(ZRegion *region, int group_idx, bool update_identifier)
Sets the link group to the region.
@ REGION_MUSICAL_MODE_ON
Musical mode on - auto-stretch when BPM changes.
Definition region.h:59
@ REGION_MUSICAL_MODE_OFF
Musical mode off - don't auto-stretch when BPM changes.
Definition region.h:56
@ REGION_MUSICAL_MODE_INHERIT
Inherit from global musical mode setting.
Definition region.h:53
#define YAML_FIELD_MAPPING_EMBEDDED(owner, member, schema)
Mapping embedded inside the struct.
Definition yaml.h:32
int_fast64_t signed_frame_t
Signed type for frame index.
Definition types.h:54
float sample_t
The sample type.
Definition types.h:46
#define YAML_VALUE_PTR_NULLABLE(cc, fields_schema)
Schema to be used as a pointer that can be NULL.
Definition yaml.h:228
Position struct and API.
Region identifier.
Base struct for arranger objects.
The arranger widget is a canvas that draws all the arranger objects it contains.
Definition arranger.h:113
Audio clips for the pool.
Definition clip.h:33
An automation point inside an AutomationTrack.
A Channel is part of a Track (excluding Tracks that don't have Channels) and contains information rel...
Definition channel.h:63
A ChordObject to be shown in the TimelineArrangerWidget.
A MIDI note inside a ZRegion shown in the piano roll.
Definition midi_note.h:50
A Position is made up of bars.beats.sixteenths.ticks.
Definition position.h:137
Index/identifier for a Region, so we can get Region objects quickly with it without searching by name...
A group of linked regions.
Stretcher interface.
Definition stretcher.h:44
A TrackLane belongs to a Track (can have many TrackLanes in a Track) and contains Regions.
Definition track_lane.h:43
Track to be inserted into the Project's Tracklist.
Definition track.h:193
A region (clip) is an object on the timeline that contains either MidiNote's or AudioClip's.
Definition region.h:78
AutomationPoint * last_recorded_ap
Last recorded automation point.
Definition region.h:214
RegionIdentifier id
Unique ID.
Definition region.h:87
char * escaped_name
Escaped name for drawing.
Definition region.h:93
GdkRectangle last_main_draw_rect
Last main draw rect.
Definition region.h:264
int bounce
Set to ON during bouncing if this region should be included.
Definition region.h:233
ChordObject ** chord_objects
ChordObject's in this Region.
Definition region.h:221
gint64 last_clip_change
Last timestamp the audio clip or its contents changed.
Definition region.h:274
bool stretching
Whether currently running the stretching algorithm.
Definition region.h:148
MidiNote * unended_notes[12000]
Unended notes started in recording with MIDI NOTE ON signal but haven't received a NOTE OFF yet.
Definition region.h:130
AutomationPoint ** aps
The automation points this region contains.
Definition region.h:209
GdkRGBA color
Region color independent of track.
Definition region.h:98
ArrangerObject last_positions_obj
Last known marker positions (only positions are used).
Definition region.h:281
char * name
Name to be shown on the widget.
Definition region.h:90
PangoLayout * layout
Cache layout for drawing the name.
Definition region.h:254
GdkRectangle last_lane_draw_rect
Last lane draw rect.
Definition region.h:270
AudioClip * clip
Clip to read frames from, if not from the pool.
Definition region.h:171
int pool_id
Audio pool ID of the associated audio file, mostly used during serialization.
Definition region.h:139
double before_length
The length before stretching, in ticks.
Definition region.h:153
RegionMusicalMode musical_mode
Musical mode setting.
Definition region.h:190
float gain
Gain to apply to the audio (amplitude 0.0-2.0).
Definition region.h:166
Position * split_points
Array of split points.
Definition region.h:193
bool read_from_pool
Whether to read the clip from the pool (used in most cases).
Definition region.h:162
ArrangerObject base
Base struct.
Definition region.h:82
double stretch_ratio
Used during arranger UI overlay actions.
Definition region.h:156
gint64 last_cache_time
Last timestamp the region was cached.
Definition region.h:277
MidiNote ** midi_notes
MIDI notes.
Definition region.h:112
PangoLayout * chords_layout
Cache layout for drawing the chord names inside the region.
Definition region.h:258
bool use_color
Whether to use the custom color.
Definition region.h:105
YAML utils.