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