diff --git a/Cargo.lock b/Cargo.lock index 958d1f3..5623661 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2602,9 +2602,9 @@ dependencies = [ [[package]] name = "maolan-widgets" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d40abf7b434a82ec7b99d264acef501ef3461e9aa766b00966db3defda4a8117" +checksum = "3191c14ec75e04d40fc622d6f15f81f8c637fe41df51e5c8bfbf9cbe2fa447af" dependencies = [ "iced", "iced_aw", diff --git a/Cargo.toml b/Cargo.toml index 6a317ee..3e7656e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ pitch-detection = "0.3.0" symphonia = { version = "0.6.1", features = ["aac", "alac", "flac", "isomp4", "mp3", "ogg", "vorbis", "wav"] } maolan-baseview = { version = "0.1.1", features = ["canvas"] } maolan-editor = { version = "0.1.3", default-features = false } -maolan-widgets = "0.1.2" +maolan-widgets = "0.1.3" parking_lot = "0.13.0" portable-atomic = { version = "1.15.0", features = ["float"] } quick-xml = { version = "0.42.0", features = ["serialize"] } diff --git a/src/common/wavetable_factory.rs b/src/common/wavetable_factory.rs index c0cb6ec..999596b 100644 --- a/src/common/wavetable_factory.rs +++ b/src/common/wavetable_factory.rs @@ -13,13 +13,12 @@ pub const FACTORY_SIZE: usize = 2048; /// Number of bundled factory wavetables. Param values `0..FACTORY_COUNT` /// select factory tables, `FACTORY_COUNT` selects the custom file loaded /// through the GUI. -pub const FACTORY_COUNT: usize = 12; +pub const FACTORY_COUNT: usize = 11; /// Entry label selecting the per-patch custom file instead of a factory table. pub const CUSTOM_WAVETABLE_LABEL: &str = "Custom file..."; pub const FACTORY_NAMES: [&str; FACTORY_COUNT] = [ - "Sine", "Triangle", "Square", "Sawtooth", @@ -60,19 +59,18 @@ fn additive(size: usize, max_harmonic: usize, mut amp: impl FnMut(usize) -> f32) fn build_factory_table(index: usize) -> Wavetable { let size = FACTORY_SIZE; let frames = match index { - 0 => additive(size, 1, |_| 1.0), - 1 => additive(size, 64, |h| { + 0 => additive(size, 64, |h| { if h % 2 == 1 { 1.0 / (h * h) as f32 } else { 0.0 } }), - 2 => additive(size, 64, |h| if h % 2 == 1 { 1.0 / h as f32 } else { 0.0 }), - 3 => additive(size, 96, |h| 1.0 / h as f32), - 4 => additive(size, 32, |h| 1.0 / (h * h) as f32), - 5 => additive(size, 192, |h| 1.0 / h as f32), - 6 => additive(size, 64, |h| { + 1 => additive(size, 64, |h| if h % 2 == 1 { 1.0 / h as f32 } else { 0.0 }), + 2 => additive(size, 96, |h| 1.0 / h as f32), + 3 => additive(size, 32, |h| 1.0 / (h * h) as f32), + 4 => additive(size, 192, |h| 1.0 / h as f32), + 5 => additive(size, 64, |h| { // 25% pulse via odd-harmonic Lanczos-ish sigma approximation. let sigma = 0.25f32; if h % 2 == 1 { @@ -82,8 +80,8 @@ fn build_factory_table(index: usize) -> Wavetable { 0.0 } }), - 7 => additive(size, 8, |h| 1.0 / h as f32), - 8 => additive(size, 12, |h| match h { + 6 => additive(size, 8, |h| 1.0 / h as f32), + 7 => additive(size, 12, |h| match h { 1 => 1.0, 2 => 0.6, 3 => 0.35, @@ -92,8 +90,8 @@ fn build_factory_table(index: usize) -> Wavetable { 6 => 0.08, _ => 0.05, }), - 9 => additive(size, 48, |h| 1.0 / (h as f32).powi(3).sqrt()), - 10 => { + 8 => additive(size, 48, |h| 1.0 / (h as f32).powi(3).sqrt()), + 9 => { // Two slightly detuned saws plus a touch of noise for a "dirty" table. let mut table = additive(size, 48, |h| 1.0 / h as f32); for (i, slot) in table.iter_mut().enumerate() { diff --git a/src/synth/gui.rs b/src/synth/gui.rs index e9639f9..ac10cfa 100644 --- a/src/synth/gui.rs +++ b/src/synth/gui.rs @@ -38,7 +38,7 @@ use crate::{ lfo_assignment::{LfoAssignmentConfig, LfoAssignmentState, ModRouteParamIds}, preset::{self, PresetProvider, PresetUi}, wavetable::Wavetable, - wavetable_factory::{CUSTOM_WAVETABLE_LABEL, FACTORY_COUNT, FACTORY_NAMES}, + wavetable_factory::FACTORY_COUNT, }, synth::{ dsp::{FilterRouting, LfoShape, ModTarget, OscType}, @@ -655,49 +655,6 @@ fn waveform_options(osc_type: OscType) -> Vec<&'static str> { } } -/// Wavetable selector for the Wavetable and Window oscillator types. Renders -/// as a zero-width empty container for other oscillator types so the osc -/// panel layout stays stable. -fn wavetable_dropdown<'a>(state: &'a State, osc_index: usize) -> Element<'a, Message> { - let (type_id, wt_id) = match osc_index { - 0 => (ParamId::Osc1Type, ParamId::Osc1Wavetable), - 1 => (ParamId::Osc2Type, ParamId::Osc2Wavetable), - _ => (ParamId::Osc3Type, ParamId::Osc3Wavetable), - }; - let osc_type = OscType::from_u8(state.shared.params.get(type_id) as u8); - if osc_type != OscType::Wavetable && osc_type != OscType::Window { - return container(text("")).width(Length::Fixed(0.0)).into(); - } - - let value = state.shared.params.get(wt_id) as usize; - let selected: &'static str = if value >= FACTORY_COUNT { - CUSTOM_WAVETABLE_LABEL - } else { - FACTORY_NAMES[value] - }; - let options: Vec<&'static str> = FACTORY_NAMES - .iter() - .copied() - .chain(std::iter::once(CUSTOM_WAVETABLE_LABEL)) - .collect(); - let dropdown = maolan_baseview::iced::widget::pick_list(options, Some(selected), move |name| { - match FACTORY_NAMES.iter().position(|&n| n == name) { - Some(index) => Message::SetParam(wt_id, index as f32), - None => Message::PickWavetableFile(osc_index), - } - }) - .placeholder("Wavetable") - .width(Length::Fixed(104.0)); - - container( - column![text("Wavetable").size(11), dropdown] - .spacing(2) - .align_x(Alignment::Center), - ) - .width(Length::Fixed(110.0)) - .into() -} - const FILTER_TYPES: &[FilterType] = &[ FilterType::Off, FilterType::Lowpass, @@ -792,7 +749,6 @@ fn lfo_shape_dropdown<'a>(id: ParamId, state: &'a State) -> Element<'a, Message> fn named_enum_dropdown<'a>( id: ParamId, options: &'static [&'static str], - width: f32, state: &'a State, ) -> Element<'a, Message> { let index = (state.shared.params.get(id) as usize).min(options.len() - 1); @@ -808,8 +764,8 @@ fn named_enum_dropdown<'a>( Message::SetParam(id, value as f32) }, ) - .width(Length::Fixed(width)); - container(dropdown).width(Length::Fixed(width + 6.0)).into() + .width(Length::Shrink); + container(dropdown).width(Length::Shrink).into() } const OSC_FM_MODE_NAMES: &[&str] = &[ @@ -1032,10 +988,13 @@ fn lfo_tab_button(label: &'static str, index: usize, state: &State) -> Element<' } fn view(state: &State) -> Element<'_, Message> { - let fm_panel = panel_no_title(knob_row(vec![ - named_enum_dropdown(ParamId::OscFmMode, OSC_FM_MODE_NAMES, 112.0, state), - param_control(ParamId::OscFmDepth, "Depth", state), - ])); + let fm_panel = panel( + "FM Mode", + knob_row(vec![ + named_enum_dropdown(ParamId::OscFmMode, OSC_FM_MODE_NAMES, state), + param_control(ParamId::OscFmDepth, "Depth", state), + ]), + ); let filter_routing = column![ filter_routing_dropdown(state), @@ -1076,7 +1035,6 @@ fn view(state: &State) -> Element<'_, Message> { let osc1 = column![ knob_row(vec![ - wavetable_dropdown(state, 0), param_control(ParamId::Osc1Octave, "Oct", state), param_control(ParamId::Osc1Semitone, "Semi", state), param_control(ParamId::Osc1Fine, "Fine", state), @@ -1095,7 +1053,6 @@ fn view(state: &State) -> Element<'_, Message> { let osc2 = column![ knob_row(vec![ - wavetable_dropdown(state, 1), param_control(ParamId::Osc2Octave, "Oct", state), param_control(ParamId::Osc2Semitone, "Semi", state), param_control(ParamId::Osc2Fine, "Fine", state), @@ -1114,7 +1071,6 @@ fn view(state: &State) -> Element<'_, Message> { let osc3 = column![ knob_row(vec![ - wavetable_dropdown(state, 2), param_control(ParamId::Osc3Octave, "Oct", state), param_control(ParamId::Osc3Semitone, "Semi", state), param_control(ParamId::Osc3Fine, "Fine", state), diff --git a/src/synth/gui/oscillator_display.rs b/src/synth/gui/oscillator_display.rs index c119f80..6d4fe98 100644 --- a/src/synth/gui/oscillator_display.rs +++ b/src/synth/gui/oscillator_display.rs @@ -1,13 +1,10 @@ use std::sync::Arc; use maolan_baseview::iced::{ - Color, Element, Length, Point, Rectangle, Renderer, Theme, mouse, - widget::{ - Space, Stack, canvas, - canvas::{Frame, Geometry, Path, Program, Stroke}, - container, mouse_area, row, text, - }, + Element, Length, mouse, + widget::{Space, Stack, button, container, mouse_area, row, text}, }; +use maolan_widgets::shape::shape; use crate::{ common::{ @@ -39,83 +36,206 @@ impl OscillatorDisplay { pub fn view(self) -> Element<'static, super::Message> { let (osc_type, waveform) = display_labels(&self.shared, self.osc_index); + let wavetable_id = match self.osc_index { + 0 => Some(ParamId::Osc1Wavetable), + 1 => Some(ParamId::Osc2Wavetable), + 2 => Some(ParamId::Osc3Wavetable), + _ => None, + }; + let osc_type_id = match self.osc_index { + 0 => Some(ParamId::Osc1Type), + 1 => Some(ParamId::Osc2Type), + 2 => Some(ParamId::Osc3Type), + _ => None, + }; + let selected_osc_type = + osc_type_id.map(|id| OscType::from_u8(self.shared.params.get(id) as u8)); + let custom_selected = wavetable_id.is_some_and(|id| { + matches!( + selected_osc_type, + Some(OscType::Wavetable | OscType::Window) + ) && self.shared.params.get(id) as usize >= FACTORY_COUNT + }); let type_shared = self.shared.clone(); - let type_label: Element<'static, super::Message> = if self.osc_index == 3 { - text(osc_type).size(11).into() + let type_index = self.osc_index; + let type_label = row![ + button(text("‹").size(10)) + .padding(1) + .style(|theme, status| { + let mut style = maolan_baseview::iced::widget::button::text(theme, status); + style.background = None; + style + }) + .on_press(scroll_osc_type( + &type_shared, + type_index, + mouse::ScrollDelta::Lines { x: 0.0, y: 1.0 }, + )), + mouse_area( + text(osc_type) + .size(11) + .width(Length::Fill) + .align_x(maolan_baseview::iced::alignment::Horizontal::Center), + ) + .on_scroll(move |delta| scroll_osc_type(&type_shared, type_index, delta)), + button(text("›").size(10)) + .padding(1) + .style(|theme, status| { + let mut style = maolan_baseview::iced::widget::button::text(theme, status); + style.background = None; + style + }) + .on_press(scroll_osc_type( + &self.shared, + type_index, + mouse::ScrollDelta::Lines { x: 0.0, y: -1.0 }, + )), + ] + .align_y(maolan_baseview::iced::Alignment::Center); + let waveform_shared = self.shared.clone(); + let waveform_index = self.osc_index; + let waveform_text = text(waveform) + .size(11) + .width(Length::Fill) + .align_x(maolan_baseview::iced::alignment::Horizontal::Center); + let waveform_text = if custom_selected { + mouse_area(waveform_text) + .on_scroll(move |delta| scroll_waveform(&waveform_shared, waveform_index, delta)) + .on_press(super::Message::PickWavetableFile(waveform_index)) } else { - mouse_area(text(osc_type).size(11)) - .on_scroll(move |delta| scroll_osc_type(&type_shared, self.osc_index, delta)) - .into() + mouse_area(waveform_text) + .on_scroll(move |delta| scroll_waveform(&waveform_shared, waveform_index, delta)) }; let waveform_shared = self.shared.clone(); - let waveform_label = mouse_area(text(waveform).size(11)) - .on_scroll(move |delta| scroll_waveform(&waveform_shared, self.osc_index, delta)); - let waveform_canvas = canvas(self) + let waveform_label = row![ + button(text("‹").size(10)) + .padding(1) + .style(|theme, status| { + let mut style = maolan_baseview::iced::widget::button::text(theme, status); + style.background = None; + style + }) + .on_press(scroll_waveform( + &waveform_shared, + waveform_index, + mouse::ScrollDelta::Lines { x: 0.0, y: 1.0 }, + )), + waveform_text, + button(text("›").size(10)) + .padding(1) + .style(|theme, status| { + let mut style = maolan_baseview::iced::widget::button::text(theme, status); + style.background = None; + style + }) + .on_press(scroll_waveform( + &self.shared, + waveform_index, + mouse::ScrollDelta::Lines { x: 0.0, y: -1.0 }, + )), + ] + .align_y(maolan_baseview::iced::Alignment::Center); + let selector_shared = self.shared.clone(); + let waveform_canvas = container(shape(render_samples(&self.shared, self.osc_index))) .width(Length::Fixed(220.0)) .height(Length::Fill) .into(); - let labels = container(row![ - type_label, - Space::new().width(Length::Fill), - waveform_label, - ]) + let waveform_canvas: Element<'static, super::Message> = if custom_selected { + mouse_area(waveform_canvas) + .on_press(super::Message::PickWavetableFile(waveform_index)) + .into() + } else { + waveform_canvas + }; + let labels = container( + row![ + container(type_label) + .width(Length::Fill) + .align_x(maolan_baseview::iced::alignment::Horizontal::Center), + container(waveform_label) + .width(Length::Fill) + .align_x(maolan_baseview::iced::alignment::Horizontal::Center), + ] + .spacing(6), + ) .width(Length::Fill) .padding(6); - Stack::with_children(vec![waveform_canvas, labels.into()]).into() - } -} - -impl Program for OscillatorDisplay { - type State = (); - - fn draw( - &self, - _state: &Self::State, - renderer: &Renderer, - _theme: &Theme, - bounds: Rectangle, - _cursor: maolan_baseview::iced::mouse::Cursor, - ) -> Vec { - let samples = render_samples(&self.shared, self.osc_index); - let mut frame = Frame::new(renderer, bounds.size()); - frame.fill_rectangle( - Point::ORIGIN, - bounds.size(), - Color::from_rgb(0.075, 0.09, 0.115), - ); - - let center_y = bounds.height * 0.5; - let center = Path::line( - Point::new(0.0, center_y), - Point::new(bounds.width, center_y), - ); - frame.stroke( - ¢er, - Stroke::default() - .with_color(Color::from_rgba(0.55, 0.60, 0.66, 0.22)) - .with_width(1.0), - ); - - let path = Path::new(|builder| { - for (index, sample) in samples.iter().copied().enumerate() { - let x = index as f32 / (POINT_COUNT - 1) as f32 * bounds.width; - let y = center_y - sample.clamp(-1.0, 1.0) * center_y * 0.9; - if index == 0 { - builder.move_to(Point::new(x, y)); - } else { - builder.line_to(Point::new(x, y)); - } - } - }); - frame.stroke( - &path, - Stroke::default() - .with_color(Color::from_rgb(0.72, 0.58, 0.28)) - .with_width(1.6), - ); + let window_wavetable_selector: Element<'static, super::Message> = if selected_osc_type + == Some(OscType::Window) + { + let wavetable_id = wavetable_id.expect("window oscillator has a wavetable param"); + let table_index = selector_shared.params.get(wavetable_id) as usize; + let selected = if table_index >= FACTORY_COUNT { + CUSTOM_WAVETABLE_LABEL + } else { + FACTORY_NAMES[table_index] + }; + let label_text = text(selected) + .size(11) + .width(Length::Fill) + .align_x(maolan_baseview::iced::alignment::Horizontal::Center); + let label_shared = selector_shared.clone(); + let label = if table_index >= FACTORY_COUNT { + mouse_area(label_text) + .on_scroll(move |delta| scroll_wavetable(&label_shared, waveform_index, delta)) + .on_press(super::Message::PickWavetableFile(waveform_index)) + } else { + mouse_area(label_text) + .on_scroll(move |delta| scroll_wavetable(&label_shared, waveform_index, delta)) + }; + let left_shared = selector_shared.clone(); + let right_shared = selector_shared.clone(); + let selector = row![ + button(text("‹").size(10)) + .padding(1) + .style(|theme, status| { + let mut style = maolan_baseview::iced::widget::button::text(theme, status); + style.background = None; + style + }) + .on_press(scroll_wavetable( + &left_shared, + waveform_index, + mouse::ScrollDelta::Lines { x: 0.0, y: 1.0 }, + )), + label, + button(text("›").size(10)) + .padding(1) + .style(|theme, status| { + let mut style = maolan_baseview::iced::widget::button::text(theme, status); + style.background = None; + style + }) + .on_press(scroll_wavetable( + &right_shared, + waveform_index, + mouse::ScrollDelta::Lines { x: 0.0, y: -1.0 }, + )), + ] + .align_y(maolan_baseview::iced::Alignment::Center); + container( + row![ + Space::new().width(Length::Fill), + selector.width(Length::Fill) + ] + .spacing(6), + ) + .width(Length::Fill) + .height(Length::Fill) + .padding(6) + .align_y(maolan_baseview::iced::alignment::Vertical::Bottom) + .into() + } else { + container(Space::new()).into() + }; - vec![frame.into_geometry()] + Stack::with_children(vec![ + waveform_canvas, + labels.into(), + window_wavetable_selector, + ]) + .into() } } @@ -154,31 +274,14 @@ fn scroll_waveform( return super::Message::SetParam(ParamId::NoiseType, next as f32); } - let (type_id, waveform_id, shape_id, wavetable_id) = match osc_index { - 0 => ( - ParamId::Osc1Type, - ParamId::Osc1Waveform, - ParamId::Osc1Shape, - ParamId::Osc1Wavetable, - ), - 1 => ( - ParamId::Osc2Type, - ParamId::Osc2Waveform, - ParamId::Osc2Shape, - ParamId::Osc2Wavetable, - ), - _ => ( - ParamId::Osc3Type, - ParamId::Osc3Waveform, - ParamId::Osc3Shape, - ParamId::Osc3Wavetable, - ), + let (type_id, waveform_id, shape_id) = match osc_index { + 0 => (ParamId::Osc1Type, ParamId::Osc1Waveform, ParamId::Osc1Shape), + 1 => (ParamId::Osc2Type, ParamId::Osc2Waveform, ParamId::Osc2Shape), + _ => (ParamId::Osc3Type, ParamId::Osc3Waveform, ParamId::Osc3Shape), }; let osc_type = OscType::from_u8(shared.params.get(type_id) as u8); if osc_type == OscType::Wavetable { - let current = shared.params.get(wavetable_id) as usize; - let next = offset_index(current, FACTORY_COUNT, direction); - return super::Message::SetParam(wavetable_id, next as f32); + return scroll_wavetable(shared, osc_index, delta); } let options = super::waveform_options(osc_type); @@ -198,6 +301,21 @@ fn scroll_waveform( } } +fn scroll_wavetable( + shared: &SharedState, + osc_index: usize, + delta: mouse::ScrollDelta, +) -> super::Message { + let wavetable_id = match osc_index { + 0 => ParamId::Osc1Wavetable, + 1 => ParamId::Osc2Wavetable, + _ => ParamId::Osc3Wavetable, + }; + let current = shared.params.get(wavetable_id) as usize; + let next = offset_index(current, FACTORY_COUNT + 1, scroll_direction(delta)); + super::Message::SetParam(wavetable_id, next as f32) +} + fn scroll_direction(delta: mouse::ScrollDelta) -> isize { let y = match delta { mouse::ScrollDelta::Lines { y, .. } | mouse::ScrollDelta::Pixels { y, .. } => y,