phosphor_app/state/
params.rs1use super::*;
4
5impl NavState {
6
7 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 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 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, _ => 0.5,
44 }
45 } else {
46 match track.instrument_type {
47 Some(InstrumentType::DrumRack) => 0.1, _ => 0.25,
49 }
50 };
51 if delta > 0.0 { step } else { -step }
52 } else {
53 delta
54 };
55 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 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 pub fn show_current_track_controls(&mut self) {
109 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 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.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 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, juno};
155
156 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 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 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 fn juno_track() -> NavState {
203 let mut nav = NavState::new(super::super::initial_tracks());
204 let mut track = TrackState::new("juno", 0, true, TrackKind::Instrument, vec![]);
205 track.instrument_type = Some(InstrumentType::Juno60);
206 track.synth_params = juno::PARAM_DEFAULTS.to_vec();
207 nav.tracks.insert(0, track);
208 nav.track_cursor = 0;
209 nav
210 }
211
212 fn juno_patch(nav: &NavState) -> usize {
213 juno::patch_index(nav.tracks[0].synth_params[juno::P_PATCH])
214 }
215
216 #[test]
217 fn juno_selectors_move_one_step_per_keypress() {
218 let mut nav = juno_track();
224 nav.clip_view.synth_param_cursor = juno::P_PATCH;
225 for step in 1..juno::PATCH_COUNT {
226 nav.adjust_synth_param(0.05);
227 assert_eq!(juno_patch(&nav), step, "patch knob step {step}");
228 }
229 nav.adjust_synth_param(0.05);
230 assert_eq!(juno_patch(&nav), juno::PATCH_COUNT - 1, "patch knob ran off the top");
231 for step in (0..juno::PATCH_COUNT - 1).rev() {
232 nav.adjust_synth_param(-0.05);
233 assert_eq!(juno_patch(&nav), step, "patch knob back to {step}");
234 }
235
236 for _ in 0..juno::PATCH_COUNT {
239 nav.adjust_synth_param(0.05);
240 }
241 let panel = &nav.tracks[0].synth_params;
242 assert_eq!(juno_patch(&nav), juno::PATCH_COUNT - 1);
243 assert!((panel[juno::P_RESO] - 1.0).abs() < 1e-6, "res {}", panel[juno::P_RESO]);
244
245 let mut nav = juno_track();
248 nav.clip_view.synth_param_cursor = juno::P_PWM_MODE;
249 let label = |nav: &NavState| {
250 juno::discrete_label(juno::P_PWM_MODE, nav.tracks[0].synth_params[juno::P_PWM_MODE])
251 };
252 assert_eq!(label(&nav), Some("LFO"));
253 let mut seen = Vec::new();
254 for _ in 0..3 {
255 nav.adjust_synth_param(0.05);
256 seen.push(label(&nav));
257 }
258 assert_eq!(seen, [Some("MAN"), Some("ENV"), Some("ENV")]);
259 }
260
261 #[test]
262 fn the_dx7_bank_knob_is_the_last_parameter() {
263 let nav = dx7_track();
267 assert_eq!(nav.tracks[0].synth_params.len(), dx7::PARAM_COUNT);
268 assert_eq!(dx7::P_BANK, dx7::PARAM_COUNT - 1);
269 assert_eq!(dx7::PARAM_NAMES[dx7::P_GAIN], "gain", "index 0-7 must not move");
270 }
271}