Zrythm
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
transport.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2018-2023 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
10#ifndef __AUDIO_TRANSPORT_H__
11#define __AUDIO_TRANSPORT_H__
12
13#include <stdint.h>
14
15#include "dsp/port.h"
16#include "dsp/region.h"
17#include "utils/types.h"
18
19#include "zix/sem.h"
20
22typedef struct AudioEngine AudioEngine;
23
30#define TIME_SIGNATURE_SCHEMA_VERSION 1
31#define TRANSPORT_SCHEMA_VERSION 1
32
33#define TRANSPORT (AUDIO_ENGINE->transport)
34#define TRANSPORT_DEFAULT_TOTAL_BARS 128
35
36#define PLAYHEAD (&TRANSPORT->playhead_pos)
37#define TRANSPORT_IS_ROLLING \
38 (TRANSPORT->play_state == PLAYSTATE_ROLLING)
39#define TRANSPORT_IS_PAUSED \
40 (TRANSPORT->play_state == PLAYSTATE_PAUSED)
41#define TRANSPORT_IS_LOOPING (TRANSPORT->loop)
42#define TRANSPORT_IS_RECORDING (TRANSPORT->recording)
43
44typedef enum PrerollCountBars
45{
46 PREROLL_COUNT_BARS_NONE,
47 PREROLL_COUNT_BARS_ONE,
48 PREROLL_COUNT_BARS_TWO,
49 PREROLL_COUNT_BARS_FOUR,
50} PrerollCountBars;
51
52static const char * preroll_count_bars_str[] = {
53 N_ ("None"),
54 N_ ("1 bar"),
55 N_ ("2 bars"),
56 N_ ("4 bars"),
57};
58
59static inline const char *
60transport_preroll_count_to_str (PrerollCountBars bars)
61{
62 return preroll_count_bars_str[bars];
63}
64
65static inline int
66transport_preroll_count_bars_enum_to_int (
67 PrerollCountBars bars)
68{
69 switch (bars)
70 {
71 case PREROLL_COUNT_BARS_NONE:
72 return 0;
73 case PREROLL_COUNT_BARS_ONE:
74 return 1;
75 case PREROLL_COUNT_BARS_TWO:
76 return 2;
77 case PREROLL_COUNT_BARS_FOUR:
78 return 4;
79 }
80 return -1;
81}
82
83typedef enum PlayState
84{
85 PLAYSTATE_ROLL_REQUESTED,
86 PLAYSTATE_ROLLING,
87 PLAYSTATE_PAUSE_REQUESTED,
88 PLAYSTATE_PAUSED
89} PlayState;
90
95typedef enum TransportDisplay
96{
97 TRANSPORT_DISPLAY_BBT,
98 TRANSPORT_DISPLAY_TIME,
100
146
147#if 0
148typedef struct TimeSignature
149{
150 int schema_version;
151
160 int beats_per_bar;
161
167 int beat_unit;
168} TimeSignature;
169
170static const cyaml_schema_field_t
171time_signature_fields_schema[] =
172{
173 YAML_FIELD_INT (
174 TimeSignature, schema_version),
175 YAML_FIELD_INT (
176 TimeSignature, beats_per_bar),
177 YAML_FIELD_INT (
178 TimeSignature, beat_unit),
179
180 CYAML_FIELD_END
181};
182
183static const cyaml_schema_value_t
184time_signature_schema =
185{
187 TimeSignature, time_signature_fields_schema),
188};
189#endif
190
194typedef struct Transport
195{
196 int schema_version;
197
200
203
206
209
212
215
218
230 Position range_2;
231
234
235 //TimeSignature time_sig;
236
237 /* ---------- CACHE -------------- */
238 int ticks_per_beat;
239 int ticks_per_bar;
240 int sixteenths_per_beat;
241 int sixteenths_per_bar;
242
243 /* ------------------------------- */
244
248
250 bool loop;
251
254
258
261
264
267
270
271 TransportRecordingMode recording_mode;
272
280 //int starting_recording;
281
283 ZixSem paused;
284
291
298
305
308
311
314
317
319 PlayState play_state;
320
324
327} Transport;
328
329static const cyaml_schema_field_t transport_fields_schema[] = {
330 YAML_FIELD_INT (Transport, schema_version),
331 YAML_FIELD_INT (Transport, total_bars),
333 Transport,
334 playhead_pos,
335 position_fields_schema),
337 Transport,
338 cue_pos,
339 position_fields_schema),
341 Transport,
342 loop_start_pos,
343 position_fields_schema),
345 Transport,
346 loop_end_pos,
347 position_fields_schema),
349 Transport,
350 punch_in_pos,
351 position_fields_schema),
353 Transport,
354 punch_out_pos,
355 position_fields_schema),
357 Transport,
358 range_1,
359 position_fields_schema),
361 Transport,
362 range_2,
363 position_fields_schema),
364 YAML_FIELD_INT (Transport, has_range),
365 YAML_FIELD_INT (Transport, position),
367 Transport,
368 roll,
369 port_fields_schema),
371 Transport,
372 stop,
373 port_fields_schema),
375 Transport,
376 backward,
377 port_fields_schema),
379 Transport,
380 forward,
381 port_fields_schema),
383 Transport,
384 loop_toggle,
385 port_fields_schema),
387 Transport,
388 rec_toggle,
389 port_fields_schema),
390
391 CYAML_FIELD_END
392};
393
394static const cyaml_schema_value_t transport_schema = {
395 YAML_VALUE_PTR (Transport, transport_fields_schema),
396};
397
401NONNULL Transport *
403
412void
414 Transport * self,
415 AudioEngine * engine,
416 Track * tempo_track);
417
421Transport *
423
432void
434 Transport * self,
435 TimelineSelections * sel);
436
452bool
454 Transport * self,
455 TimelineSelections * sel,
456 bool with_fixed_ratio,
457 double time_ratio,
458 bool force,
459 GError ** error);
460
461void
462transport_set_punch_mode_enabled (
463 Transport * self,
464 bool enabled);
465
466void
467transport_set_start_playback_on_midi_input (
468 Transport * self,
469 bool enabled);
470
471void
472transport_set_recording_mode (
473 Transport * self,
475
479void
481 Transport * self,
482 const int enabled);
483
489void
491 Transport * self,
492 const signed_frame_t nframes);
493
502void
503transport_request_pause (Transport * self, bool with_wait);
504
513void
514transport_request_roll (Transport * self, bool with_wait);
515
519void
521
522void
523transport_set_playhead_to_bar (Transport * self, int bar);
524
528void
530
534void
535transport_move_backward (Transport * self, bool with_wait);
536
540void
541transport_move_forward (Transport * self, bool with_wait);
542
547bool
549
563void
565 Transport * self,
566 const Position * target,
567 bool panic,
568 bool set_cue_point,
569 bool fire_events);
570
574void
576 Transport * self,
577 bool enabled,
578 bool with_wait);
579
583void
585
589void
591
595void
597
601void
603
611void
613 Transport * self,
614 bool update_from_ticks);
615
616#if 0
624long
625transport_frames_add_frames (
626 const Transport * self,
627 const long gframes,
628 const nframes_t frames);
629#endif
630
637void
639 const Transport * self,
640 Position * pos,
641 const signed_frame_t frames);
642
646double
648
652void
654 Transport * self,
655 bool first,
656 Position * pos);
657
661void
662transport_set_has_range (Transport * self, bool has_range);
663
670void
672 Transport * self,
673 bool range1,
674 const Position * start_pos,
675 const Position * pos,
676 bool snap);
677
685HOT NONNULL WARN_UNUSED_RESULT static inline nframes_t
686transport_is_loop_point_met (
687 const Transport * self,
688 const signed_frame_t g_start_frames,
689 const nframes_t nframes)
690{
691 if (
692 self->loop
693 && G_UNLIKELY (
694 self->loop_end_pos.frames > g_start_frames
695 && self->loop_end_pos.frames
696 <= g_start_frames + (long) nframes))
697 {
698 return (
699 nframes_t) (self->loop_end_pos.frames - g_start_frames);
700 }
701 return 0;
702}
703
704bool
705transport_position_is_inside_punch_range (
706 Transport * self,
707 Position * pos);
708
717void
719 Transport * self,
720 ArrangerSelections * sel);
721
725void
727 Transport * self,
728 int total_bars,
729 bool fire_events);
730
731void
732transport_update_caches (
733 Transport * self,
734 int beats_per_bar,
735 int beat_unit);
736
740void
742 Transport * self,
743 bool record,
744 bool with_wait,
745 bool fire_events);
746
747void
748transport_free (Transport * self);
749
754#endif
A region in the timeline.
TransportDisplay
Corrseponts to "transport-display" in the gsettings.
Definition transport.h:96
void transport_move_backward(Transport *self, bool with_wait)
Move to the previous snap point on the timeline.
bool transport_can_user_move_playhead(const Transport *self)
Returns whether the user can currently move the playhead (eg, via the UI or via scripts).
void transport_add_to_playhead(Transport *self, const signed_frame_t nframes)
Moves the playhead by the time corresponding to given samples, taking into account the loop end point...
void transport_set_loop(Transport *self, bool enabled, bool with_wait)
Enables or disables loop.
void transport_update_positions(Transport *self, bool update_from_ticks)
Updates the frames in all transport positions.
void transport_prepare_audio_regions_for_stretch(Transport *self, TimelineSelections *sel)
Prepares audio regions for stretching (sets the ZRegion::before_length).
void transport_set_has_range(Transport *self, bool has_range)
Sets if the project has range and updates UI.
void transport_set_metronome_enabled(Transport *self, const int enabled)
Sets whether metronome is enabled or not.
void transport_get_playhead_pos(Transport *self, Position *pos)
Getter for playhead Position.
TransportRecordingMode
Recording mode for MIDI and audio.
Definition transport.h:109
NONNULL Transport * transport_new(AudioEngine *engine)
Initialize transport.
void transport_init_loaded(Transport *self, AudioEngine *engine, Track *tempo_track)
Initialize loaded transport.
void transport_update_total_bars(Transport *self, int total_bars, bool fire_events)
Updates the total bars.
void transport_set_recording(Transport *self, bool record, bool with_wait, bool fire_events)
Sets recording on/off.
void transport_move_playhead(Transport *self, const Position *target, bool panic, bool set_cue_point, bool fire_events)
Moves playhead to given pos.
double transport_get_ppqn(Transport *self)
Returns the PPQN (Parts/Ticks Per Quarter Note).
void transport_move_forward(Transport *self, bool with_wait)
Move to the next snap point on the timeline.
void transport_recalculate_total_bars(Transport *self, ArrangerSelections *sel)
Recalculates the total bars based on the last object's position.
void transport_get_range_pos(Transport *self, bool first, Position *pos)
Stores the position of the range in pos.
void transport_goto_next_marker(Transport *self)
Moves the playhead to the next Marker.
void transport_goto_end_marker(Transport *self)
Moves the playhead to the end Marker.
Transport * transport_clone(const Transport *src)
Clones the transport values.
bool transport_stretch_regions(Transport *self, TimelineSelections *sel, bool with_fixed_ratio, double time_ratio, bool force, GError **error)
Stretches regions.
void transport_set_playhead_pos(Transport *self, Position *pos)
Setter for playhead Position.
void transport_request_pause(Transport *self, bool with_wait)
Request pause.
void transport_request_roll(Transport *self, bool with_wait)
Request playback.
void transport_goto_prev_marker(Transport *self)
Moves the playhead to the prev Marker.
void transport_position_add_frames(const Transport *self, Position *pos, const signed_frame_t frames)
Adds frames to the given position similar to position_add_frames, except that it adjusts the new Posi...
void transport_goto_start_marker(Transport *self)
Moves the playhead to the start Marker.
void transport_set_range(Transport *self, bool range1, const Position *start_pos, const Position *pos, bool snap)
Set the range1 or range2 position.
@ RECORDING_MODE_TAKES
Events get put in new lanes each time recording starts/resumes (eg, when looping or entering/leaving ...
Definition transport.h:137
@ RECORDING_MODE_MERGE_EVENTS
Merge events in existing region.
Definition transport.h:130
@ RECORDING_MODE_TAKES_MUTED
Same as RECORDING_MODE_TAKES, except previous takes (since recording started) are muted.
Definition transport.h:144
@ RECORDING_MODE_OVERWRITE_EVENTS
Overwrite events in first recorded region.
Definition transport.h:119
#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
uint32_t nframes_t
Frame count.
Definition types.h:34
#define YAML_VALUE_PTR(cc, fields_schema)
Schema to be used as a pointer.
Definition yaml.h:221
#define YAML_FIELD_MAPPING_PTR_OPTIONAL(owner, member, schema)
Mapping pointer to a struct.
Definition yaml.h:46
Ports that transfer audio/midi/other signals to one another.
The audio engine.
Definition engine.h:375
Must ONLY be created via port_new()
Definition port.h:150
A Position is made up of bars.beats.sixteenths.ticks.
Definition position.h:137
signed_frame_t frames
Position in frames (samples).
Definition position.h:148
Selections to be used for the timeline's current selections, copying, undoing, etc.
Track to be inserted into the Project's Tracklist.
Definition track.h:193
The transport.
Definition transport.h:195
Port * forward
Forward MIDI port.
Definition transport.h:310
bool punch_mode
Whether punch in/out mode is enabled.
Definition transport.h:253
Position range_1
Selected range.
Definition transport.h:229
int total_bars
Total bars in the song.
Definition transport.h:199
int has_range
Whether range should be displayed or not.
Definition transport.h:233
signed_frame_t countin_frames_remaining
Metronome countin frames remaining.
Definition transport.h:266
Position loop_end_pos
Loop end position.
Definition transport.h:211
Position playhead_before_pause
Position of the playhead before pausing.
Definition transport.h:290
Position punch_out_pos
Punch out position.
Definition transport.h:217
PlayState play_state
Play state.
Definition transport.h:319
Port * backward
Backward MIDI port.
Definition transport.h:307
Position playhead_pos
Playhead position.
Definition transport.h:202
nframes_t position
Transport position in frames.
Definition transport.h:247
bool loop
Looping or not.
Definition transport.h:250
signed_frame_t preroll_frames_remaining
Recording preroll frames remaining.
Definition transport.h:263
AudioEngine * audio_engine
Pointer to owner audio engine, if any.
Definition transport.h:326
Port * rec_toggle
Rec toggle MIDI port.
Definition transport.h:316
Position loop_start_pos
Loop start position.
Definition transport.h:208
ZixSem paused
This is set when record is toggled and is used to check if a new region should be created.
Definition transport.h:283
bool recording
Whether MIDI/audio recording is enabled (recording toggle in transport bar).
Definition transport.h:257
Port * roll
Roll/play MIDI port.
Definition transport.h:297
Port * stop
Stop MIDI port.
Definition transport.h:304
Position punch_in_pos
Punch in position.
Definition transport.h:214
bool metronome_enabled
Metronome enabled or not.
Definition transport.h:260
gint64 last_manual_playhead_change
Last timestamp the playhead position was changed manually.
Definition transport.h:323
bool start_playback_on_midi_input
Whether to start playback on MIDI input.
Definition transport.h:269
Port * loop_toggle
Loop toggle MIDI port.
Definition transport.h:313
Position cue_pos
Cue point position.
Definition transport.h:205
Custom types.