phosphor_app/state/track.rs
1//! Track state — TrackState, TrackElement, Clip, MidiNote.
2
3use phosphor_core::project::{TrackConfig, TrackKind};
4
5// ── Track Element Navigation ──
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum TrackElement {
9 Label,
10 Fx,
11 Volume,
12 Mute,
13 Solo,
14 RecordArm,
15 Clip(usize),
16}
17
18impl TrackElement {
19 pub fn move_right(self, num_clips: usize) -> Self {
20 match self {
21 Self::Label => Self::Fx,
22 Self::Fx => Self::Volume,
23 Self::Volume => Self::Mute,
24 Self::Mute => Self::Solo,
25 Self::Solo => Self::RecordArm,
26 Self::RecordArm => {
27 if num_clips > 0 { Self::Clip(0) } else { Self::RecordArm }
28 }
29 Self::Clip(i) => {
30 if i + 1 < num_clips { Self::Clip(i + 1) } else { Self::Clip(i) }
31 }
32 }
33 }
34
35 pub fn move_left(self) -> Self {
36 match self {
37 Self::Label => Self::Label,
38 Self::Fx => Self::Label,
39 Self::Volume => Self::Fx,
40 Self::Mute => Self::Volume,
41 Self::Solo => Self::Mute,
42 Self::RecordArm => Self::Solo,
43 Self::Clip(0) => Self::RecordArm,
44 Self::Clip(i) => Self::Clip(i - 1),
45 }
46 }
47}
48
49// ── Data Models ──
50
51#[derive(Debug, Clone)]
52pub struct Clip {
53 pub number: usize,
54 pub width: u16,
55 pub has_content: bool,
56 /// Start position on the timeline (ticks).
57 pub start_tick: i64,
58 /// Length in ticks.
59 pub length_ticks: i64,
60 /// Notes for piano roll display (from ClipSnapshot).
61 pub notes: Vec<phosphor_core::clip::NoteSnapshot>,
62 /// Notes hidden by shrinking the clip. Stored with start_frac and
63 /// duration_frac as absolute tick ratios (tick / original_length_when_hidden)
64 /// converted to tick offsets for stable restore.
65 /// Format: (tick_offset_from_clip_start, duration_ticks, note, velocity)
66 pub hidden_notes: Vec<(i64, i64, u8, u8)>,
67}
68
69#[derive(Debug, Clone)]
70pub struct TrackState {
71 pub name: String,
72 pub muted: bool,
73 pub soloed: bool,
74 pub armed: bool,
75 pub color_index: usize,
76 pub kind: TrackKind,
77 pub clips: Vec<Clip>,
78 /// Track-level FX chain.
79 pub fx_chain: Vec<super::FxInstance>,
80 /// Fader position as a linear gain, mirroring the audio thread's
81 /// `TrackConfig::volume`. Travel is
82 /// [`TrackConfig::MIN_VOLUME`]..=[`TrackConfig::MAX_VOLUME`]; the audio
83 /// thread's copy is clamped to it by `TrackConfig::set_volume`, so this
84 /// mirror is the only place an out-of-range value could survive.
85 pub volume: f32,
86 /// Unique ID for this track (matches the mixer's track ID).
87 pub mixer_id: Option<usize>,
88 /// Handle to the audio engine's track state. When present, mute/solo/arm/volume
89 /// writes go directly to the audio thread via atomics.
90 pub handle: Option<std::sync::Arc<phosphor_core::project::TrackHandle>>,
91 /// What type of instrument this track has.
92 pub instrument_type: Option<super::InstrumentType>,
93 /// Parameter values (mirrors the audio thread's plugin params).
94 pub synth_params: Vec<f32>,
95}
96
97impl TrackState {
98 /// One press of the fader, in dB.
99 ///
100 /// The fader is stepped in dB rather than in linear gain so that every
101 /// press moves the readout by exactly one. A linear step small enough to
102 /// be useful near the top of the travel is a 6 dB jump near the bottom,
103 /// and three consecutive linear detents around unity all round to the
104 /// same displayed number — a control that does not appear to respond.
105 pub const VOLUME_STEP_DB: f32 = 1.0;
106
107 /// Bottom of the fader's travel, below which it goes to silence.
108 ///
109 /// −40 dB rather than the −60 a drawn fader usually bottoms out at: this
110 /// one is stepped a keypress at a time, and 20 extra presses to reach a
111 /// level that is inaudible anyway is travel nobody wants. Muting a track
112 /// outright is `m`.
113 pub const VOLUME_FLOOR_DB: f32 = -40.0;
114
115 pub fn new(name: &str, color_index: usize, armed: bool, kind: TrackKind, clips: Vec<Clip>) -> Self {
116 Self {
117 name: name.to_string(),
118 muted: false,
119 soloed: false,
120 armed,
121 color_index,
122 kind,
123 clips,
124 fx_chain: Vec::new(),
125 volume: TrackConfig::DEFAULT_VOLUME,
126 mixer_id: None,
127 handle: None,
128 instrument_type: None,
129 synth_params: Vec::new(),
130 }
131 }
132
133 /// Sync mute/solo/arm/volume to the audio thread handle (if wired up).
134 pub fn sync_to_audio(&self) {
135 if let Some(ref h) = self.handle {
136 h.config.muted.store(self.muted, std::sync::atomic::Ordering::Relaxed);
137 h.config.soloed.store(self.soloed, std::sync::atomic::Ordering::Relaxed);
138 h.config.armed.store(self.armed, std::sync::atomic::Ordering::Relaxed);
139 h.config.set_volume(self.volume);
140 }
141 }
142
143 /// The fader position in dB relative to unity. `None` at the bottom of
144 /// the travel, where the answer is negative infinity.
145 pub fn volume_db(&self) -> Option<f32> {
146 (self.volume > 0.0).then(|| 20.0 * self.volume.log10())
147 }
148
149 /// Move the fader by `steps` presses and push the result to the audio
150 /// thread. Returns the new linear gain.
151 ///
152 /// The current position is rounded onto the dB grid before stepping, so
153 /// the fader self-corrects: the default of 0.75 is −2.5 dB, off the grid,
154 /// and the first press lands it on −2 or −3 and every press after that
155 /// moves exactly one. A session saved with a hand-edited volume snaps the
156 /// same way.
157 pub fn adjust_volume(&mut self, steps: i32) -> f32 {
158 let top_db = 20.0 * TrackConfig::MAX_VOLUME.log10();
159 // One step below the floor is the silent position, so stepping down
160 // from the bottom of the travel reaches it and stepping up leaves it.
161 let silent_db = Self::VOLUME_FLOOR_DB - Self::VOLUME_STEP_DB;
162 let current_db = self.volume_db().map_or(silent_db, |db| db.round());
163
164 let target_db =
165 (current_db + steps as f32 * Self::VOLUME_STEP_DB).clamp(silent_db, top_db);
166 self.volume = if target_db < Self::VOLUME_FLOOR_DB {
167 TrackConfig::MIN_VOLUME
168 } else {
169 10.0f32
170 .powf(target_db / 20.0)
171 .clamp(TrackConfig::MIN_VOLUME, TrackConfig::MAX_VOLUME)
172 };
173
174 self.sync_to_audio();
175 self.volume
176 }
177
178 /// Read VU levels from the audio thread handle.
179 pub fn vu_levels(&self) -> (f32, f32) {
180 self.handle.as_ref().map(|h| h.vu.get()).unwrap_or((0.0, 0.0))
181 }
182
183 /// Whether this track is wired to the audio engine.
184 pub fn is_live(&self) -> bool {
185 self.handle.is_some()
186 }
187}