Skip to content

Commit

Permalink
Clean up app::run().
Browse files Browse the repository at this point in the history
This commit removes case-by-case `Message` generation from `app::run()`
and moves it into separate functions for readability.
  • Loading branch information
joculatrix committed Sep 20, 2024
1 parent db62be7 commit 9f739bb
Showing 1 changed file with 122 additions and 111 deletions.
233 changes: 122 additions & 111 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,127 +20,21 @@ pub fn run(tx: Sender<Message>) -> Result<(), Box<dyn Error>> {
main_window.on_osc_prop_changed(move |index, prop, value| {
// Index values are hardcoded in app.slint -- if this cast fails, something is very wrong.
let index = index as usize;

let msg = match prop {
OscProps::Bypass => {
let value = match value {
0.0 => false,
1.0 => true,
_ => panic!(),
};
Message::Bypass {
oscillator: index,
bypass: value
}
}
OscProps::Coarse => unsafe {
Message::Coarse {
oscillator: index,
coarse: value.to_int_unchecked()
}
}
OscProps::Fine => {
Message::Fine {
oscillator: index,
fine: value.into()
}
}
OscProps::FmRange => unsafe {
Message::FmRange {
oscillator: index,
range: value.to_int_unchecked()
}
}
OscProps::Freq => {
Message::Freq {
oscillator: index,
freq: value.into()
}
}
OscProps::Gain => {
Message::Gain {
oscillator: index,
gain: value.into()
}
}
OscProps::Mode => unsafe {
let value = match value.to_int_unchecked() {
0 => oscillator::PitchMode::MIDI,
1 => oscillator::PitchMode::Constant,
_ => panic!(),
};
Message::PitchMode {
oscillator: index,
mode: value
}
}
OscProps::Output => unsafe {
let value = match value.to_int_unchecked() {
j if
(1..=4).contains(&j)
&& j as usize - 1 != index => oscillator::OutputMode::Osc(j as usize - 1),
_ => oscillator::OutputMode::Master,
};

Message::Output {
oscillator: index,
mode: value
}
}
OscProps::Waveform => unsafe {
let waveform = match value.to_int_unchecked() {
0 => osc::wave::Waveform::Noise,
1 => osc::wave::Waveform::Saw,
3 => osc::wave::Waveform::Square,
4 => osc::wave::Waveform::Triangle,
_ => osc::wave::Waveform::Sine, // just set to Sine if something goes wrong?
};
Message::Waveform {
oscillator: index,
waveform
}
}
};

tx2.send(msg);
let msg = generate_osc_msg(index, prop, value);
let _ = tx2.send(msg);
});

let tx3 = tx.clone();

main_window.on_amp_prop_changed(move |prop, value| {
let msg = match prop {
AmpProps::Attack => {
Message::Attack(value.into())
}
AmpProps::Decay => {
Message::Decay(value.into())
}
AmpProps::Sustain => {
Message::Sustain(value.into())
}
AmpProps::Release => {
Message::Release(value.into())
}
AmpProps::Gain => {
Message::Master(value.into())
}
AmpProps::Mode => unsafe {
let value = match value.to_int_unchecked() {
0 => SynthMode::MIDI,
1 => SynthMode::Constant,
_ => panic!()
};
Message::MixerMode(value)
}
};

tx3.send(msg);
let msg = generate_amp_msg(prop, value);
let _ = tx3.send(msg);
});

let tx4 = tx.clone();

main_window.on_midi_reset(move || {
tx4.send(Message::ResetMIDI());
let _ = tx4.send(Message::ResetMIDI());
});

main_window.on_set_precision(|value, precision| {
Expand All @@ -152,4 +46,121 @@ pub fn run(tx: Sender<Message>) -> Result<(), Box<dyn Error>> {
tx.send(Message::Quit())?;

Ok(())
}

/// Generates a [`Message`] to send to other tasks based on changes made to [`Amplifier`] properties.
///
/// [`Amplifier`]: crate::synth::amp::Amplifier
fn generate_amp_msg(prop: AmpProps, value: f32) -> Message {
match prop {
AmpProps::Attack => {
Message::Attack(value.into())
}
AmpProps::Decay => {
Message::Decay(value.into())
}
AmpProps::Sustain => {
Message::Sustain(value.into())
}
AmpProps::Release => {
Message::Release(value.into())
}
AmpProps::Gain => {
Message::Master(value.into())
}
AmpProps::Mode => unsafe {
let value = match value.to_int_unchecked() {
0 => SynthMode::MIDI,
1 => SynthMode::Constant,
_ => panic!()
};
Message::MixerMode(value)
}
}
}

/// Generates a [`Message`] to send to other tasks based on changes made to [`Oscillator`] properties.
///
/// [`Oscillator`]: oscillator::Oscillator
fn generate_osc_msg(index: usize, prop: OscProps, value: f32) -> Message {
match prop {
OscProps::Bypass => {
let value = match value {
0.0 => false,
1.0 => true,
_ => panic!(),
};
Message::Bypass {
oscillator: index,
bypass: value
}
}
OscProps::Coarse => unsafe {
Message::Coarse {
oscillator: index,
coarse: value.to_int_unchecked()
}
}
OscProps::Fine => {
Message::Fine {
oscillator: index,
fine: value.into()
}
}
OscProps::FmRange => unsafe {
Message::FmRange {
oscillator: index,
range: value.to_int_unchecked()
}
}
OscProps::Freq => {
Message::Freq {
oscillator: index,
freq: value.into()
}
}
OscProps::Gain => {
Message::Gain {
oscillator: index,
gain: value.into()
}
}
OscProps::Mode => unsafe {
let value = match value.to_int_unchecked() {
0 => oscillator::PitchMode::MIDI,
1 => oscillator::PitchMode::Constant,
_ => panic!(),
};
Message::PitchMode {
oscillator: index,
mode: value
}
}
OscProps::Output => unsafe {
let value = match value.to_int_unchecked() {
j if
(1..=4).contains(&j)
&& j as usize - 1 != index => oscillator::OutputMode::Osc(j as usize - 1),
_ => oscillator::OutputMode::Master,
};

Message::Output {
oscillator: index,
mode: value
}
}
OscProps::Waveform => unsafe {
let waveform = match value.to_int_unchecked() {
0 => osc::wave::Waveform::Noise,
1 => osc::wave::Waveform::Saw,
3 => osc::wave::Waveform::Square,
4 => osc::wave::Waveform::Triangle,
_ => osc::wave::Waveform::Sine, // just set to Sine if something goes wrong?
};
Message::Waveform {
oscillator: index,
waveform
}
}
}
}

0 comments on commit 9f739bb

Please sign in to comment.