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 <cstdint>
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/* FIXME remove these globals */
38#define TRANSPORT_IS_ROLLING \
39 (TRANSPORT->play_state == PlayState::PLAYSTATE_ROLLING)
40#define TRANSPORT_IS_PAUSED \
41 (TRANSPORT->play_state == PlayState::PLAYSTATE_PAUSED)
42#define TRANSPORT_IS_LOOPING (TRANSPORT->loop)
43#define TRANSPORT_IS_RECORDING (TRANSPORT->recording)
44
45enum class PrerollCountBars
46{
47 PREROLL_COUNT_BARS_NONE,
48 PREROLL_COUNT_BARS_ONE,
49 PREROLL_COUNT_BARS_TWO,
50 PREROLL_COUNT_BARS_FOUR,
51};
52
53static const char * preroll_count_bars_str[] = {
54 N_ ("None"),
55 N_ ("1 bar"),
56 N_ ("2 bars"),
57 N_ ("4 bars"),
58};
59
60static inline const char *
61transport_preroll_count_to_str (PrerollCountBars bars)
62{
63 return preroll_count_bars_str[static_cast<int> (bars)];
64}
65
66static inline int
67transport_preroll_count_bars_enum_to_int (PrerollCountBars bars)
68{
69 switch (bars)
70 {
71 case PrerollCountBars::PREROLL_COUNT_BARS_NONE:
72 return 0;
73 case PrerollCountBars::PREROLL_COUNT_BARS_ONE:
74 return 1;
75 case PrerollCountBars::PREROLL_COUNT_BARS_TWO:
76 return 2;
77 case PrerollCountBars::PREROLL_COUNT_BARS_FOUR:
78 return 4;
79 }
80 return -1;
81}
82
83enum class PlayState
84{
85 PLAYSTATE_ROLL_REQUESTED,
86 PLAYSTATE_ROLLING,
87 PLAYSTATE_PAUSE_REQUESTED,
88 PLAYSTATE_PAUSED
89};
90
96{
97 TRANSPORT_DISPLAY_BBT,
98 TRANSPORT_DISPLAY_TIME,
99};
100
146
150typedef struct Transport
151{
154
157
160
163
166
169
172
184 Position range_2;
185
188
189 // TimeSignature time_sig;
190
191 /* ---------- CACHE -------------- */
192 int ticks_per_beat;
193 int ticks_per_bar;
194 int sixteenths_per_beat;
195 int sixteenths_per_bar;
196
197 /* ------------------------------- */
198
202
204 bool loop;
205
208
212
215
218
221
224
225 TransportRecordingMode recording_mode;
226
234 // int starting_recording;
235
237 ZixSem paused;
238
245
252
259
262
265
268
271
273 PlayState play_state;
274
278
281} Transport;
282
286NONNULL Transport *
288
296void
298 Transport * self,
299 AudioEngine * engine,
300 Track * tempo_track);
301
305Transport *
307
308bool
309transport_is_rolling (const Transport * self);
310
319void
321 Transport * self,
322 TimelineSelections * sel);
323
339bool
341 Transport * self,
342 TimelineSelections * sel,
343 bool with_fixed_ratio,
344 double time_ratio,
345 bool force,
346 GError ** error);
347
348void
349transport_set_punch_mode_enabled (Transport * self, bool enabled);
350
351void
352transport_set_start_playback_on_midi_input (Transport * self, bool enabled);
353
354void
355transport_set_recording_mode (Transport * self, TransportRecordingMode mode);
356
360void
361transport_set_metronome_enabled (Transport * self, const int enabled);
362
368void
370
379void
380transport_request_pause (Transport * self, bool with_wait);
381
390void
391transport_request_roll (Transport * self, bool with_wait);
392
396void
398
399void
400transport_set_playhead_to_bar (Transport * self, int bar);
401
405void
407
411void
412transport_move_backward (Transport * self, bool with_wait);
413
417void
418transport_move_forward (Transport * self, bool with_wait);
419
424bool
426
440void
442 Transport * self,
443 const Position * target,
444 bool panic,
445 bool set_cue_point,
446 bool fire_events);
447
451void
452transport_set_loop (Transport * self, bool enabled, bool with_wait);
453
457void
459
463void
465
469void
471
475void
477
485void
486transport_update_positions (Transport * self, bool update_from_ticks);
487
488#if 0
496long
497transport_frames_add_frames (
498 const Transport * self,
499 const long gframes,
500 const nframes_t frames);
501#endif
502
509void
511 const Transport * self,
512 Position * pos,
513 const signed_frame_t frames);
514
518double
520
524void
525transport_get_range_pos (Transport * self, bool first, Position * pos);
526
530void
532
536void
537transport_set_has_range (Transport * self, bool has_range);
538
545void
547 Transport * self,
548 bool range1,
549 const Position * start_pos,
550 const Position * pos,
551 bool snap);
552
558void
560 Transport * self,
561 bool start,
562 const Position * start_pos,
563 const Position * pos,
564 bool snap);
565
573HOT NONNULL WARN_UNUSED_RESULT static inline nframes_t
574transport_is_loop_point_met (
575 const Transport * self,
576 const signed_frame_t g_start_frames,
577 const nframes_t nframes)
578{
579 if (
580 self->loop
581 && G_UNLIKELY (
582 self->loop_end_pos.frames > g_start_frames
583 && self->loop_end_pos.frames <= g_start_frames + (long) nframes))
584 {
585 return (nframes_t) (self->loop_end_pos.frames - g_start_frames);
586 }
587 return 0;
588}
589
590bool
591transport_position_is_inside_punch_range (Transport * self, Position * pos);
592
601void
603
607void
608transport_update_total_bars (Transport * self, int total_bars, bool fire_events);
609
610void
611transport_update_caches (Transport * self, int beats_per_bar, int beat_unit);
612
616void
618 Transport * self,
619 bool record,
620 bool with_wait,
621 bool fire_events);
622
623void
624transport_free (Transport * self);
625
630#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: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_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 Region::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: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_MUTED
Same as RECORDING_MODE_TAKES, except previous takes (since recording started) are muted.
@ RECORDING_MODE_OVERWRITE_EVENTS
Overwrite events in first recorded region.
@ RECORDING_MODE_MERGE_EVENTS
Merge events in existing region.
@ RECORDING_MODE_TAKES
Events get put in new lanes each time recording starts/resumes (eg, when looping or entering/leaving ...
int_fast64_t signed_frame_t
Signed type for frame index.
Definition types.h:59
uint32_t nframes_t
Frame count.
Definition types.h:39
The audio engine.
Definition engine.h:353
Must ONLY be created via port_new()
Definition port.h:136
A Position is made up of bars.beats.sixteenths.ticks.
Definition position.h:124
signed_frame_t frames
Position in frames (samples).
Definition position.h:133
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:177
The transport.
Definition transport.h:151
Port * forward
Forward MIDI port.
Definition transport.h:264
bool punch_mode
Whether punch in/out mode is enabled.
Definition transport.h:207
Position range_1
Selected range.
Definition transport.h:183
int total_bars
Total bars in the song.
Definition transport.h:153
int has_range
Whether range should be displayed or not.
Definition transport.h:187
signed_frame_t countin_frames_remaining
Metronome countin frames remaining.
Definition transport.h:220
Position loop_end_pos
Loop end position.
Definition transport.h:165
Position playhead_before_pause
Position of the playhead before pausing.
Definition transport.h:244
Position punch_out_pos
Punch out position.
Definition transport.h:171
PlayState play_state
Play state.
Definition transport.h:273
Port * backward
Backward MIDI port.
Definition transport.h:261
Position playhead_pos
Playhead position.
Definition transport.h:156
nframes_t position
Transport position in frames.
Definition transport.h:201
bool loop
Looping or not.
Definition transport.h:204
signed_frame_t preroll_frames_remaining
Recording preroll frames remaining.
Definition transport.h:217
AudioEngine * audio_engine
Pointer to owner audio engine, if any.
Definition transport.h:280
Port * rec_toggle
Rec toggle MIDI port.
Definition transport.h:270
Position loop_start_pos
Loop start position.
Definition transport.h:162
ZixSem paused
This is set when record is toggled and is used to check if a new region should be created.
Definition transport.h:237
bool recording
Whether MIDI/audio recording is enabled (recording toggle in transport bar).
Definition transport.h:211
Port * roll
Roll/play MIDI port.
Definition transport.h:251
Port * stop
Stop MIDI port.
Definition transport.h:258
Position punch_in_pos
Punch in position.
Definition transport.h:168
bool metronome_enabled
Metronome enabled or not.
Definition transport.h:214
gint64 last_manual_playhead_change
Last timestamp the playhead position was changed manually.
Definition transport.h:277
bool start_playback_on_midi_input
Whether to start playback on MIDI input.
Definition transport.h:223
Port * loop_toggle
Loop toggle MIDI port.
Definition transport.h:267
Position cue_pos
Cue point position.
Definition transport.h:159
Custom types.