2022-03-06 05:39:52 +11:00
|
|
|
// Diopser: a phase rotation plugin
|
|
|
|
// Copyright (C) 2021-2022 Robbert van der Helm
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-11-18 10:38:54 +11:00
|
|
|
use atomic_float::AtomicF32;
|
2022-11-12 04:14:01 +11:00
|
|
|
use nih_plug::nih_debug_assert_failure;
|
2022-11-12 03:45:21 +11:00
|
|
|
use nih_plug::prelude::{Editor, Plugin};
|
2022-06-18 09:59:53 +10:00
|
|
|
use nih_plug_vizia::vizia::prelude::*;
|
2022-03-23 04:32:02 +11:00
|
|
|
use nih_plug_vizia::widgets::*;
|
2022-11-06 23:26:32 +11:00
|
|
|
use nih_plug_vizia::{assets, create_vizia_editor, ViziaState, ViziaTheming};
|
2022-11-18 09:56:06 +11:00
|
|
|
use std::sync::{Arc, Mutex};
|
2022-03-06 05:39:52 +11:00
|
|
|
|
2022-11-05 04:52:35 +11:00
|
|
|
use self::button::SafeModeButton;
|
2022-11-23 04:06:59 +11:00
|
|
|
use crate::params::DiopserParams;
|
2022-11-18 09:56:06 +11:00
|
|
|
use crate::spectrum::SpectrumOutput;
|
2022-11-23 04:06:59 +11:00
|
|
|
use crate::Diopser;
|
2022-03-06 05:39:52 +11:00
|
|
|
|
2022-11-18 11:06:25 +11:00
|
|
|
mod analyzer;
|
2022-11-05 04:52:35 +11:00
|
|
|
mod button;
|
2022-11-23 11:00:45 +11:00
|
|
|
mod safe_mode;
|
2022-11-12 05:45:03 +11:00
|
|
|
mod xy_pad;
|
2022-11-05 04:52:35 +11:00
|
|
|
|
2022-11-23 11:00:45 +11:00
|
|
|
pub use safe_mode::SafeModeClamper;
|
|
|
|
|
2022-11-09 06:51:27 +11:00
|
|
|
const EDITOR_WIDTH: u32 = 600;
|
|
|
|
const EDITOR_HEIGHT: u32 = 490;
|
|
|
|
|
2022-11-12 05:44:28 +11:00
|
|
|
const SPECTRUM_ANALYZER_HEIGHT: f32 = 260.0;
|
2022-11-12 03:09:53 +11:00
|
|
|
|
2022-11-12 03:45:21 +11:00
|
|
|
const DARK_GRAY: Color = Color::rgb(0xc4, 0xc4, 0xc4);
|
|
|
|
const DARKER_GRAY: Color = Color::rgb(0x69, 0x69, 0x69);
|
|
|
|
|
2022-11-18 10:38:54 +11:00
|
|
|
#[derive(Lens, Clone)]
|
|
|
|
pub(crate) struct Data {
|
|
|
|
pub(crate) params: Arc<DiopserParams>,
|
2022-11-05 04:32:35 +11:00
|
|
|
|
2022-11-18 10:38:54 +11:00
|
|
|
/// The plugin's current sample rate.
|
|
|
|
pub(crate) sample_rate: Arc<AtomicF32>,
|
|
|
|
pub(crate) spectrum: Arc<Mutex<SpectrumOutput>>,
|
2022-11-05 04:32:35 +11:00
|
|
|
/// Whether the safe mode button is enabled. The number of filter stages is capped at 40 while
|
|
|
|
/// this is active.
|
2022-11-23 11:00:45 +11:00
|
|
|
pub(crate) safe_mode_clamper: SafeModeClamper,
|
2022-03-16 11:20:02 +11:00
|
|
|
}
|
|
|
|
|
2022-03-23 04:32:02 +11:00
|
|
|
impl Model for Data {}
|
2022-03-16 11:20:02 +11:00
|
|
|
|
2022-03-23 04:32:02 +11:00
|
|
|
// Makes sense to also define this here, makes it a bit easier to keep track of
|
|
|
|
pub(crate) fn default_state() -> Arc<ViziaState> {
|
2022-11-09 06:51:27 +11:00
|
|
|
ViziaState::from_size(EDITOR_WIDTH, EDITOR_HEIGHT)
|
2022-03-16 11:20:02 +11:00
|
|
|
}
|
|
|
|
|
2022-11-18 10:38:54 +11:00
|
|
|
pub(crate) fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option<Box<dyn Editor>> {
|
2022-11-06 23:26:32 +11:00
|
|
|
create_vizia_editor(editor_state, ViziaTheming::Custom, move |cx, _| {
|
2022-11-06 23:48:12 +11:00
|
|
|
assets::register_noto_sans_light(cx);
|
|
|
|
assets::register_noto_sans_thin(cx);
|
|
|
|
|
2022-11-12 03:34:35 +11:00
|
|
|
cx.add_theme(include_str!("editor/theme.css"));
|
2022-11-04 08:27:47 +11:00
|
|
|
|
2022-11-18 10:38:54 +11:00
|
|
|
editor_data.clone().build(cx);
|
2022-03-23 04:32:02 +11:00
|
|
|
|
2022-03-29 09:49:31 +11:00
|
|
|
ResizeHandle::new(cx);
|
|
|
|
|
2022-03-23 04:32:02 +11:00
|
|
|
VStack::new(cx, |cx| {
|
2022-11-04 08:27:47 +11:00
|
|
|
top_bar(cx);
|
|
|
|
spectrum_analyzer(cx);
|
|
|
|
other_params(cx);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This contain's the plugin's name, a bypass button, and some other controls.
|
|
|
|
fn top_bar(cx: &mut Context) {
|
|
|
|
HStack::new(cx, |cx| {
|
|
|
|
Label::new(cx, "Diopser")
|
|
|
|
.font(assets::NOTO_SANS_THIN)
|
2022-11-12 03:09:53 +11:00
|
|
|
.font_size(37.0)
|
2022-11-04 08:27:47 +11:00
|
|
|
.top(Pixels(-2.0))
|
2022-11-12 04:14:01 +11:00
|
|
|
.left(Pixels(8.0))
|
|
|
|
.on_mouse_down(|_, _| {
|
|
|
|
// Try to open the Diopser plugin's page when clicking on the title. If this fails
|
|
|
|
// then that's not a problem
|
|
|
|
let result = open::that(Diopser::URL);
|
|
|
|
if cfg!(debug) && result.is_err() {
|
|
|
|
nih_debug_assert_failure!("Failed to open web browser: {:?}", result);
|
|
|
|
}
|
|
|
|
});
|
2022-11-12 03:45:21 +11:00
|
|
|
Label::new(cx, Diopser::VERSION)
|
|
|
|
.color(DARKER_GRAY)
|
|
|
|
.top(Stretch(1.0))
|
|
|
|
.bottom(Pixels(7.5))
|
|
|
|
.left(Pixels(2.0));
|
2022-11-04 08:27:47 +11:00
|
|
|
|
|
|
|
HStack::new(cx, |cx| {
|
2022-11-08 01:09:03 +11:00
|
|
|
ParamSlider::new(cx, Data::params, |params| ¶ms.automation_precision)
|
2022-11-08 01:17:16 +11:00
|
|
|
.with_label("Automation Precision")
|
|
|
|
.id("automation-precision");
|
2022-11-04 08:27:47 +11:00
|
|
|
|
2022-11-23 11:00:45 +11:00
|
|
|
SafeModeButton::new(cx, Data::safe_mode_clamper, "Safe mode").left(Pixels(10.0));
|
2022-11-04 08:27:47 +11:00
|
|
|
|
2022-11-05 02:12:30 +11:00
|
|
|
ParamButton::new(cx, Data::params, |params| ¶ms.bypass)
|
|
|
|
.for_bypass()
|
|
|
|
.left(Pixels(10.0));
|
2022-03-23 04:32:02 +11:00
|
|
|
})
|
2022-11-04 08:27:47 +11:00
|
|
|
.child_space(Pixels(10.0))
|
|
|
|
.left(Stretch(1.0));
|
|
|
|
})
|
|
|
|
.id("top-bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This shows a spectrum analyzer for the plugin's output, and also acts as an X-Y pad for the
|
|
|
|
/// frequency and resonance parameters.
|
|
|
|
fn spectrum_analyzer(cx: &mut Context) {
|
2022-11-12 05:44:28 +11:00
|
|
|
const LABEL_HEIGHT: f32 = 20.0;
|
2022-11-12 03:09:53 +11:00
|
|
|
|
|
|
|
HStack::new(cx, |cx| {
|
|
|
|
Label::new(cx, "Resonance")
|
|
|
|
.font_size(18.0)
|
|
|
|
// HACK: Rotating doesn't really work in vizia, but with text wrap disabled this at
|
|
|
|
// least visually does the right thing
|
|
|
|
.text_wrap(false)
|
|
|
|
.rotate(270.0f32)
|
2022-11-12 05:44:28 +11:00
|
|
|
.width(Pixels(LABEL_HEIGHT))
|
|
|
|
.height(Pixels(SPECTRUM_ANALYZER_HEIGHT))
|
2022-11-12 03:09:53 +11:00
|
|
|
// HACK: The `.space()` on the HStack doesn't seem to work correctly here
|
|
|
|
.left(Pixels(10.0))
|
|
|
|
.right(Pixels(-5.0))
|
|
|
|
.child_space(Stretch(1.0));
|
|
|
|
|
|
|
|
VStack::new(cx, |cx| {
|
2022-11-12 03:58:41 +11:00
|
|
|
ZStack::new(cx, |cx| {
|
2022-11-18 11:06:25 +11:00
|
|
|
analyzer::SpectrumAnalyzer::new(cx, Data::spectrum, Data::sample_rate)
|
|
|
|
.width(Percentage(100.0))
|
|
|
|
.height(Percentage(100.0));
|
2022-11-12 03:58:41 +11:00
|
|
|
|
2022-11-12 05:45:03 +11:00
|
|
|
xy_pad::XyPad::new(
|
2022-11-12 03:58:41 +11:00
|
|
|
cx,
|
2022-11-12 05:45:03 +11:00
|
|
|
Data::params,
|
|
|
|
|params| ¶ms.filter_frequency,
|
|
|
|
|params| ¶ms.filter_resonance,
|
2022-11-12 03:58:41 +11:00
|
|
|
)
|
2022-11-12 05:45:03 +11:00
|
|
|
.width(Percentage(100.0))
|
|
|
|
.height(Percentage(100.0));
|
2022-11-12 03:58:41 +11:00
|
|
|
})
|
|
|
|
.width(Percentage(100.0))
|
|
|
|
.background_color(DARK_GRAY)
|
2022-11-12 05:44:28 +11:00
|
|
|
.height(Pixels(SPECTRUM_ANALYZER_HEIGHT));
|
2022-11-12 03:09:53 +11:00
|
|
|
|
|
|
|
Label::new(cx, "Frequency")
|
|
|
|
.font_size(18.0)
|
|
|
|
.width(Stretch(1.0))
|
|
|
|
.height(Pixels(20.0))
|
|
|
|
.child_space(Stretch(1.0));
|
|
|
|
})
|
|
|
|
.space(Pixels(10.0))
|
|
|
|
.width(Stretch(1.0));
|
|
|
|
});
|
2022-11-04 08:27:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The area below the spectrum analyzer that contains all of the other parameters.
|
|
|
|
fn other_params(cx: &mut Context) {
|
2022-11-12 03:09:53 +11:00
|
|
|
VStack::new(cx, |cx| {
|
|
|
|
HStack::new(cx, move |cx| {
|
|
|
|
Label::new(cx, "Filter Stages").class("param-label");
|
|
|
|
ParamSlider::new(cx, Data::params, |params| ¶ms.filter_stages);
|
|
|
|
})
|
|
|
|
.bottom(Pixels(10.0));
|
|
|
|
|
|
|
|
HStack::new(cx, move |cx| {
|
|
|
|
Label::new(cx, "Frequency Spread").class("param-label");
|
|
|
|
ParamSlider::new(cx, Data::params, |params| ¶ms.filter_spread_octaves);
|
|
|
|
})
|
|
|
|
.bottom(Pixels(10.0));
|
|
|
|
|
|
|
|
HStack::new(cx, move |cx| {
|
|
|
|
Label::new(cx, "Spread Style").class("param-label");
|
|
|
|
ParamSlider::new(cx, Data::params, |params| ¶ms.filter_spread_style)
|
|
|
|
.set_style(ParamSliderStyle::CurrentStepLabeled { even: true });
|
|
|
|
});
|
2022-03-23 04:32:02 +11:00
|
|
|
})
|
2022-11-12 03:09:53 +11:00
|
|
|
.id("param-sliders")
|
2022-11-04 08:27:47 +11:00
|
|
|
.width(Percentage(100.0))
|
2022-11-12 03:09:53 +11:00
|
|
|
.top(Pixels(7.0))
|
|
|
|
// This should take up all remaining space
|
|
|
|
.bottom(Stretch(1.0))
|
|
|
|
.child_space(Stretch(1.0))
|
|
|
|
.child_left(Stretch(1.0));
|
2022-03-06 05:39:52 +11:00
|
|
|
}
|