Skip to main content

phosphor_app/state/
params.rs

1//! NavState methods: params.
2
3use super::*;
4
5impl NavState {
6
7    /// Adjust the currently selected synth parameter by delta.
8    /// Returns the (mixer_id, param_index, new_value) if changed, for sending to audio.
9    pub fn adjust_synth_param(&mut self, delta: f32) -> Option<(usize, usize, f32)> {
10        let idx = self.clip_view.synth_param_cursor;
11        if let Some(track) = self.tracks.get_mut(self.track_cursor) {
12            if idx < track.synth_params.len() {
13                // Index 0 is always a discrete selector (waveform for synth, kit for drums)
14                // Synth: 4 options → step 0.25. Drums: 5 kits → step 0.20
15                let is_jupiter = track.instrument_type == Some(InstrumentType::Jupiter8);
16                let is_odyssey = track.instrument_type == Some(InstrumentType::Odyssey);
17                let is_juno = track.instrument_type == Some(InstrumentType::Juno60);
18                let is_dx7 = track.instrument_type == Some(InstrumentType::DX7);
19                let is_discrete = if is_jupiter {
20                    phosphor_dsp::jupiter::is_discrete(idx)
21                } else if is_odyssey {
22                    phosphor_dsp::odyssey::is_discrete(idx)
23                } else if is_juno {
24                    phosphor_dsp::juno::is_discrete(idx)
25                } else if is_dx7 {
26                    // Two selectors, not one: the bank knob is as discrete as
27                    // the patch knob, and stepping it by a continuous delta
28                    // would walk through cartridges a fraction at a time.
29                    phosphor_dsp::dx7::is_discrete(idx)
30                } else {
31                    idx == 0
32                };
33                let actual_delta = if is_discrete {
34                    let step = if is_jupiter {
35                        match idx {
36                            0 => 1.0 / (phosphor_dsp::jupiter::PATCH_COUNT as f32 - 0.01),
37                            _ => 0.25,
38                        }
39                    } else if is_odyssey {
40                        match idx {
41                            0 => 1.0 / (phosphor_dsp::odyssey::PATCH_COUNT as f32 - 0.01),
42                            6 => 0.34, // 3 filter types
43                            _ => 0.5,
44                        }
45                    } else {
46                        match track.instrument_type {
47                            Some(InstrumentType::DrumRack) => 0.1, // 10 kits
48                            _ => 0.25,
49                        }
50                    };
51                    if delta > 0.0 { step } else { -step }
52                } else {
53                    delta
54                };
55                // The DX7 and the Juno step their selectors by index rather
56                // than by adding a fraction of the knob's travel: 256 voices,
57                // or 18 patches and a three-position range switch, are coarse
58                // enough that an accumulated rounding error lands on the wrong
59                // side of a step boundary, which reads as a keypress that did
60                // nothing.
61                let new_val = if is_discrete && is_dx7 {
62                    phosphor_dsp::dx7::step_discrete(idx, track.synth_params[idx], delta > 0.0)
63                } else if is_discrete && is_juno {
64                    phosphor_dsp::juno::step_discrete(idx, track.synth_params[idx], delta > 0.0)
65                } else {
66                    (track.synth_params[idx] + actual_delta).clamp(0.0, 1.0)
67                };
68                track.synth_params[idx] = new_val;
69
70                // When patch selector changes, sync all params from preset.
71                // The banks no longer agree on how many parameters an
72                // instrument has, so this collects rather than matching on a
73                // fixed-size array, and writes through a zip so a track
74                // carrying a shorter block than its instrument now has cannot
75                // index off the end of itself.
76                if idx == 0 {
77                    let new_params: Option<Vec<f32>> = match track.instrument_type {
78                        Some(InstrumentType::Jupiter8) => {
79                            Some(phosphor_dsp::jupiter::Jupiter8Synth::params_for_patch(new_val).to_vec())
80                        }
81                        Some(InstrumentType::Odyssey) => {
82                            Some(phosphor_dsp::odyssey::OdysseySynth::params_for_patch(new_val).to_vec())
83                        }
84                        Some(InstrumentType::Juno60) => {
85                            Some(phosphor_dsp::juno::Juno60Synth::params_for_patch(new_val).to_vec())
86                        }
87                        _ => None,
88                    };
89                    if let Some(preset_params) = new_params {
90                        for (slot, v) in track.synth_params.iter_mut().zip(preset_params) {
91                            *slot = v;
92                        }
93                    }
94                }
95
96                if let Some(mixer_id) = track.mixer_id {
97                    return Some((mixer_id, idx, new_val));
98                }
99            }
100        }
101        None
102    }
103
104
105    /// Show controls for the currently selected track and route MIDI to it.
106    /// For instrument tracks: opens clip view with Synth tab, activates MIDI input.
107    /// For bus tracks: no clip view, deactivates MIDI.
108    pub fn show_current_track_controls(&mut self) {
109        // Deactivate MIDI on ALL tracks first
110        for track in &self.tracks {
111            if let Some(ref h) = track.handle {
112                h.config.midi_active.store(false, std::sync::atomic::Ordering::Relaxed);
113            }
114        }
115
116        if let Some(track) = self.tracks.get(self.track_cursor) {
117            if track.is_live() {
118                if let Some(ref h) = track.handle {
119                    h.config.midi_active.store(true, std::sync::atomic::Ordering::Relaxed);
120                }
121                self.clip_view_visible = true;
122
123                // Use the currently selected clip element, or default to clip 0
124                let clip_idx = match self.track_element {
125                    super::TrackElement::Clip(i) if i < track.clips.len() => i,
126                    _ => 0,
127                };
128                self.clip_view_target = Some((self.track_cursor, clip_idx));
129
130                // If track has recorded clips, show piano roll. Otherwise show synth.
131                if !track.clips.is_empty() {
132                    self.clip_view.clip_tab = ClipTab::PianoRoll;
133                    self.clip_view.focus = ClipViewFocus::PianoRoll;
134                    self.clip_view.piano_roll.focus = PianoRollFocus::Navigation;
135                    self.clip_view.piano_roll.column = 0;
136                } else {
137                    self.clip_view.fx_panel_tab = FxPanelTab::Synth;
138                    self.clip_view.focus = ClipViewFocus::FxPanel;
139                    self.clip_view.synth_param_cursor = 0;
140                }
141            } else {
142                // Bus track — hide clip view
143                self.clip_view_visible = false;
144                self.clip_view_target = None;
145            }
146        }
147    }
148
149}
150
151#[cfg(test)]
152mod tests {
153    use super::*;
154    use phosphor_dsp::dx7;
155
156    /// A nav state whose selected track is a DX7 at its default parameters.
157    fn dx7_track() -> NavState {
158        let mut nav = NavState::new(super::super::initial_tracks());
159        let mut track = TrackState::new("dx7", 0, true, TrackKind::Instrument, vec![]);
160        track.instrument_type = Some(InstrumentType::DX7);
161        track.synth_params = dx7::PARAM_DEFAULTS.to_vec();
162        nav.tracks.insert(0, track);
163        nav.track_cursor = 0;
164        nav
165    }
166
167    fn selected(nav: &NavState) -> (usize, usize) {
168        let p = &nav.tracks[0].synth_params;
169        (dx7::bank_index(p[dx7::P_BANK]), dx7::patch_index(p[dx7::P_PATCH]))
170    }
171
172    #[test]
173    fn dx7_selectors_move_one_step_per_keypress() {
174        // Both DX7 selectors are discrete: a keypress is one voice or one
175        // cartridge, not a fraction of the knob's travel. The patch knob alone
176        // would otherwise take 256 presses to cross the factory set.
177        let mut nav = dx7_track();
178        nav.clip_view.synth_param_cursor = dx7::P_PATCH;
179        let (bank, patch) = selected(&nav);
180        for step in 1..=5 {
181            nav.adjust_synth_param(0.05);
182            assert_eq!(selected(&nav), (bank, patch + step), "patch knob step {step}");
183        }
184        for step in (0..5).rev() {
185            nav.adjust_synth_param(-0.05);
186            assert_eq!(selected(&nav), (bank, patch + step), "patch knob back to {step}");
187        }
188
189        nav.clip_view.synth_param_cursor = dx7::P_BANK;
190        for step in 1..dx7::BANK_COUNT {
191            nav.adjust_synth_param(0.05);
192            assert_eq!(selected(&nav), (step, patch), "bank knob step {step}");
193        }
194        // ...and neither runs off its end.
195        for _ in 0..4 {
196            nav.adjust_synth_param(0.05);
197        }
198        assert_eq!(selected(&nav), (dx7::BANK_COUNT - 1, patch));
199    }
200
201    #[test]
202    fn the_dx7_bank_knob_is_the_last_parameter() {
203        // Sessions store `synth_params` positionally, so the bank selector was
204        // appended rather than filed next to the patch selector: inserting it
205        // would load every saved value of every existing session one slot out.
206        let nav = dx7_track();
207        assert_eq!(nav.tracks[0].synth_params.len(), dx7::PARAM_COUNT);
208        assert_eq!(dx7::P_BANK, dx7::PARAM_COUNT - 1);
209        assert_eq!(dx7::PARAM_NAMES[dx7::P_GAIN], "gain", "index 0-7 must not move");
210    }
211}