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-2024 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 (TRANSPORT->play_state == PLAYSTATE_ROLLING)
38#define TRANSPORT_IS_PAUSED (TRANSPORT->play_state == PLAYSTATE_PAUSED)
39#define TRANSPORT_IS_LOOPING (TRANSPORT->loop)
40#define TRANSPORT_IS_RECORDING (TRANSPORT->recording)
41
42typedef enum PrerollCountBars
43{
44 PREROLL_COUNT_BARS_NONE,
45 PREROLL_COUNT_BARS_ONE,
46 PREROLL_COUNT_BARS_TWO,
47 PREROLL_COUNT_BARS_FOUR,
48} PrerollCountBars;
49
50static const char * preroll_count_bars_str[] = {
51 N_ ("None"),
52 N_ ("1 bar"),
53 N_ ("2 bars"),
54 N_ ("4 bars"),
55};
56
57static inline const char *
58transport_preroll_count_to_str (PrerollCountBars bars)
59{
60 return preroll_count_bars_str[bars];
61}
62
63static inline int
64transport_preroll_count_bars_enum_to_int (PrerollCountBars bars)
65{
66 switch (bars)
67 {
68 case PREROLL_COUNT_BARS_NONE:
69 return 0;
70 case PREROLL_COUNT_BARS_ONE:
71 return 1;
72 case PREROLL_COUNT_BARS_TWO:
73 return 2;
74 case PREROLL_COUNT_BARS_FOUR:
75 return 4;
76 }
77 return -1;
78}
79
80typedef enum PlayState
81{
82 PLAYSTATE_ROLL_REQUESTED,
83 PLAYSTATE_ROLLING,
84 PLAYSTATE_PAUSE_REQUESTED,
85 PLAYSTATE_PAUSED
86} PlayState;
87
92typedef enum TransportDisplay
93{
94 TRANSPORT_DISPLAY_BBT,
95 TRANSPORT_DISPLAY_TIME,
97
143
147typedef struct Transport
148{
151
154
157
160
163
166
169
181 Position range_2;
182
185
186 // TimeSignature time_sig;
187
188 /* ---------- CACHE -------------- */
189 int ticks_per_beat;
190 int ticks_per_bar;
191 int sixteenths_per_beat;
192 int sixteenths_per_bar;
193
194 /* ------------------------------- */
195
199
201 bool loop;
202
205
209
212
215
218
221
222 TransportRecordingMode recording_mode;
223
231 // int starting_recording;
232
234 ZixSem paused;
235
242
249
256
259
262
265
268
270 PlayState play_state;
271
275
278} Transport;
279
283NONNULL Transport *
285
293void
295 Transport * self,
296 AudioEngine * engine,
297 Track * tempo_track);
298
302Transport *
304
313void
315 Transport * self,
316 TimelineSelections * sel);
317
333bool
335 Transport * self,
336 TimelineSelections * sel,
337 bool with_fixed_ratio,
338 double time_ratio,
339 bool force,
340 GError ** error);
341
342void
343transport_set_punch_mode_enabled (Transport * self, bool enabled);
344
345void
346transport_set_start_playback_on_midi_input (Transport * self, bool enabled);
347
348void
349transport_set_recording_mode (Transport * self, TransportRecordingMode mode);
350
354void
355transport_set_metronome_enabled (Transport * self, const int enabled);
356
362void
364
373void
374transport_request_pause (Transport * self, bool with_wait);
375
384void
385transport_request_roll (Transport * self, bool with_wait);
386
390void
392
393void
394transport_set_playhead_to_bar (Transport * self, int bar);
395
399void
401
405void
406transport_move_backward (Transport * self, bool with_wait);
407
411void
412transport_move_forward (Transport * self, bool with_wait);
413
418bool
420
434void
436 Transport * self,
437 const Position * target,
438 bool panic,
439 bool set_cue_point,
440 bool fire_events);
441
445void
446transport_set_loop (Transport * self, bool enabled, bool with_wait);
447
451void
453
457void
459
463void
465
469void
471
479void
480transport_update_positions (Transport * self, bool update_from_ticks);
481
482#if 0
490long
491transport_frames_add_frames (
492 const Transport * self,
493 const long gframes,
494 const nframes_t frames);
495#endif
496
503void
505 const Transport * self,
506 Position * pos,
507 const signed_frame_t frames);
508
512double
514
518void
519transport_get_range_pos (Transport * self, bool first, Position * pos);
520
524void
526
530void
531transport_set_has_range (Transport * self, bool has_range);
532
539void
541 Transport * self,
542 bool range1,
543 const Position * start_pos,
544 const Position * pos,
545 bool snap);
546
552void
554 Transport * self,
555 bool start,
556 const Position * start_pos,
557 const Position * pos,
558 bool snap);
559
567HOT NONNULL WARN_UNUSED_RESULT static inline nframes_t
568transport_is_loop_point_met (
569 const Transport * self,
570 const signed_frame_t g_start_frames,
571 const nframes_t nframes)
572{
573 if (
574 self->loop
575 && G_UNLIKELY (
576 self->loop_end_pos.frames > g_start_frames
577 && self->loop_end_pos.frames <= g_start_frames + (long) nframes))
578 {
579 return (nframes_t) (self->loop_end_pos.frames - g_start_frames);
580 }
581 return 0;
582}
583
584bool
585transport_position_is_inside_punch_range (Transport * self, Position * pos);
586
595void
597
601void
602transport_update_total_bars (Transport * self, int total_bars, bool fire_events);
603
604void
605transport_update_caches (Transport * self, int beats_per_bar, int beat_unit);
606
610void
612 Transport * self,
613 bool record,
614 bool with_wait,
615 bool fire_events);
616
617void
618transport_free (Transport * self);
619
624#endif
Ports that transfer audio/midi/other signals to one another.
A region in the timeline.
TransportDisplay
Corrseponts to "transport-display" in the gsettings.
Definition transport.h:93
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_get_loop_range_pos(Transport *self, bool first, Position *pos)
Stores the position of the range in pos.
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_loop_range(Transport *self, bool start, const Position *start_pos, const Position *pos, bool snap)
Set the loop range.
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:106
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:134
@ RECORDING_MODE_MERGE_EVENTS
Merge events in existing region.
Definition transport.h:127
@ RECORDING_MODE_TAKES_MUTED
Same as RECORDING_MODE_TAKES, except previous takes (since recording started) are muted.
Definition transport.h:141
@ RECORDING_MODE_OVERWRITE_EVENTS
Overwrite events in first recorded region.
Definition transport.h:116
int_fast64_t signed_frame_t
Signed type for frame index.
Definition types.h:55
uint32_t nframes_t
Frame count.
Definition types.h:35
The audio engine.
Definition engine.h:361
Must ONLY be created via port_new()
Definition port.h:140
A Position is made up of bars.beats.sixteenths.ticks.
Definition position.h:129
signed_frame_t frames
Position in frames (samples).
Definition position.h:140
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:186
The transport.
Definition transport.h:148
Port * forward
Forward MIDI port.
Definition transport.h:261
bool punch_mode
Whether punch in/out mode is enabled.
Definition transport.h:204
Position range_1
Selected range.
Definition transport.h:180
int total_bars
Total bars in the song.
Definition transport.h:150
int has_range
Whether range should be displayed or not.
Definition transport.h:184
signed_frame_t countin_frames_remaining
Metronome countin frames remaining.
Definition transport.h:217
Position loop_end_pos
Loop end position.
Definition transport.h:162
Position playhead_before_pause
Position of the playhead before pausing.
Definition transport.h:241
Position punch_out_pos
Punch out position.
Definition transport.h:168
PlayState play_state
Play state.
Definition transport.h:270
Port * backward
Backward MIDI port.
Definition transport.h:258
Position playhead_pos
Playhead position.
Definition transport.h:153
nframes_t position
Transport position in frames.
Definition transport.h:198
bool loop
Looping or not.
Definition transport.h:201
signed_frame_t preroll_frames_remaining
Recording preroll frames remaining.
Definition transport.h:214
AudioEngine * audio_engine
Pointer to owner audio engine, if any.
Definition transport.h:277
Port * rec_toggle
Rec toggle MIDI port.
Definition transport.h:267
Position loop_start_pos
Loop start position.
Definition transport.h:159
ZixSem paused
This is set when record is toggled and is used to check if a new region should be created.
Definition transport.h:234
bool recording
Whether MIDI/audio recording is enabled (recording toggle in transport bar).
Definition transport.h:208
Port * roll
Roll/play MIDI port.
Definition transport.h:248
Port * stop
Stop MIDI port.
Definition transport.h:255
Position punch_in_pos
Punch in position.
Definition transport.h:165
bool metronome_enabled
Metronome enabled or not.
Definition transport.h:211
gint64 last_manual_playhead_change
Last timestamp the playhead position was changed manually.
Definition transport.h:274
bool start_playback_on_midi_input
Whether to start playback on MIDI input.
Definition transport.h:220
Port * loop_toggle
Loop toggle MIDI port.
Definition transport.h:264
Position cue_pos
Cue point position.
Definition transport.h:156
Custom types.