//! Anvil editor, shared by the CLAP wrapper and the headless renderer. use anvil_core::{ AnalyzerFrame, Settings, Source, AUDITION, AUDITION_HITS, GHOST_COUNT, KICK_OFFSET_MAX, PHONE_LOW, SCOPE, }; use common_ui::*; use egui::{self, Color32, FontId, Pos2, RichText, Sense, Stroke, Vec2}; pub use common_ui::style; pub const WIDTH: u32 = 980; pub const HEIGHT: u32 = 872; pub struct State { pub solo: bool, /// Phone-like listen. A monitor, never saved. pub phone: bool, /// Kick offset audition. A monitor, never saved. pub audition: bool, pub analyzer: AnalyzerFrame, /// True while a pointer button is held, so knob drags become one host gesture. pub gesture_active: bool, pub help: String, } impl Default for State { fn default() -> Self { Self { solo: false, phone: false, audition: false, analyzer: AnalyzerFrame::default(), gesture_active: false, help: DEFAULT_HELP.into(), } } } const DEFAULT_HELP: &str = "Put Anvil on the bass, set Split just above the sub, then set Sub level once for every patch."; impl State { pub fn accept_frame(&mut self, frame: AnalyzerFrame) { let old = self.analyzer; self.analyzer = frame; // Meters fall smoothly; the readout and scope stay exact. self.analyzer.low_db = frame.low_db.max(old.low_db - 1.5); self.analyzer.sub_db = frame.sub_db.max(old.sub_db - 1.5); } } fn hint(r: &egui::Response, state: &mut State, text: &str) { if r.hovered() || r.has_focus() { state.help = text.into(); } } #[allow(clippy::too_many_arguments)] fn knob( ui: &mut egui::Ui, state: &mut State, name: &str, value: &mut f32, range: std::ops::RangeInclusive, default: f32, unit: &str, size: f32, help: &str, ) { ui.vertical_centered(|ui| { let r = rotary(ui, name, value, range, default, unit, size, ACCENT); hint(&r, state, help); }); } fn toggle(ui: &mut egui::Ui, state: &mut State, on: &mut bool, label: &str, help: &str) { let r = ui.selectable_label(*on, RichText::new(label).size(13.)); if r.clicked() { *on = !*on; } hint(&r, state, help); } /// Harmonics 2–10 of the current note: what the patch carries above the split (steel) /// and what Ghost adds (accent), from −48 dB to 0 dB against the sub. fn harmonics(p: &egui::Painter, rect: egui::Rect, frame: &AnalyzerFrame) { p.rect_filled(rect, 2., Color32::from_rgba_unmultiplied(12, 13, 16, 230)); p.rect_stroke( rect, 2., Stroke::new(1.0_f32, EDGE), egui::StrokeKind::Inside, ); p.text( rect.left_top() + Vec2::new(8., 6.), egui::Align2::LEFT_TOP, "GHOST", FontId::proportional(10.), ACCENT, ); p.text( rect.right_top() + Vec2::new(-8., 6.), egui::Align2::RIGHT_TOP, format!("from {:.0} Hz", frame.ghost_edge), FontId::proportional(10.), MUTED, ); let area = egui::Rect::from_min_max( rect.left_top() + Vec2::new(8., 22.), rect.right_bottom() - Vec2::new(8., 16.), ); let slot = area.width() / GHOST_COUNT as f32; let height = |db: f32| ((db + 48.) / 48.).clamp(0., 1.) * area.height(); for i in 0..GHOST_COUNT { let x = area.left() + slot * i as f32; let bar = |left: f32, db: f32, color: Color32| { let h = height(db); if h > 0.5 { p.rect_filled( egui::Rect::from_min_max( Pos2::new(left, area.bottom() - h), Pos2::new(left + slot * 0.36, area.bottom()), ), 1., color, ); } }; bar(x + slot * 0.12, frame.patch_harmonics[i], STEEL); bar(x + slot * 0.52, frame.ghost_harmonics[i], ACCENT); p.text( Pos2::new(x + slot * 0.5, area.bottom() + 2.), egui::Align2::CENTER_TOP, format!("{}", i + 2), FontId::proportional(9.), DIM, ); } } fn scope(ui: &mut egui::Ui, frame: &AnalyzerFrame, height: f32, ghost: bool) { let (rect, _) = ui.allocate_exact_size(Vec2::new(ui.available_width(), height), Sense::hover()); let p = ui.painter_at(rect); p.rect_filled(rect, 2., SCREEN); let mid = rect.center().y; for k in 1..6 { let x = rect.left() + rect.width() * k as f32 / 6.; p.line_segment( [Pos2::new(x, rect.top()), Pos2::new(x, rect.bottom())], Stroke::new(1.0_f32, Color32::from_rgb(24, 26, 30)), ); } p.line_segment( [Pos2::new(rect.left(), mid), Pos2::new(rect.right(), mid)], Stroke::new(1.0_f32, EDGE), ); let peak = frame .scope_low .iter() .chain(frame.scope_sub.iter()) .fold(0.05f32, |m, v| m.max(v.abs())); let scale = rect.height() * 0.42 / peak; for (data, color, width) in [ (&frame.scope_low, STEEL, 1.5_f32), (&frame.scope_sub, ACCENT, 2.0_f32), ] { let points: Vec = data .iter() .enumerate() .map(|(i, v)| { Pos2::new( rect.left() + rect.width() * i as f32 / (SCOPE - 1) as f32, mid - v * scale, ) }) .collect(); p.add(egui::Shape::line(points, Stroke::new(width, color))); } p.text( rect.left_top() + Vec2::new(10., 8.), egui::Align2::LEFT_TOP, "ORIGINAL LOW BAND", FontId::proportional(11.), STEEL, ); p.text( rect.left_top() + Vec2::new(130., 8.), egui::Align2::LEFT_TOP, "NEW SUB", FontId::proportional(11.), ACCENT, ); p.text( rect.right_top() + Vec2::new(-10., 8.), egui::Align2::RIGHT_TOP, format!("{:.0} ms", frame.scope_ms), FontId::proportional(11.), MUTED, ); if ghost { let size = Vec2::new(200., 92.); let corner = rect.right_bottom() - Vec2::new(8., 8.); harmonics(&p, egui::Rect::from_min_max(corner - size, corner), frame); } p.rect_stroke( rect, 2., Stroke::new(1.0_f32, EDGE), egui::StrokeKind::Inside, ); } pub fn show(ui: &mut egui::Ui, settings: &mut Settings, state: &mut State) { backdrop(ui); state.help = DEFAULT_HELP.into(); state.gesture_active = ui.input(|i| i.pointer.any_down()); ui.add_space(2.); header(ui, "ANVIL", "SUB ANCHOR", |ui| { let r = ui .selectable_label(settings.bypass, RichText::new("BYPASS").size(13.)) .on_hover_text("Crossfade to the delayed dry signal."); if r.clicked() { settings.bypass = !settings.bypass; } let r = ui .selectable_label(state.solo, RichText::new("SOLO SUB").size(13.)) .on_hover_text("Hear only the generated sub and Ghost harmonics."); if r.clicked() { state.solo = !state.solo; } let r = ui .selectable_label(state.phone, RichText::new("PHONE").size(13.)) .on_hover_text(format!( "Listen as on a phone: mono, nothing below {PHONE_LOW:.0} Hz. Not saved." )); if r.clicked() { state.phone = !state.phone; } ui.separator(); for (source, label) in [(Source::Midi, "MIDI"), (Source::Audio, "AUDIO")] { let r = ui.selectable_label(settings.source == source, RichText::new(label).size(13.)); if r.clicked() { settings.source = source; } } caption(ui, "PITCH FROM"); }); ui.add_space(6.); let frame = state.analyzer; ui.horizontal_top(|ui| { panel(ui, |ui| { ui.set_width(260.); ui.set_min_height(TOP); ui.vertical(|ui| note_panel(ui, settings, &frame)); }); ui.add_space(4.); panel(ui, |ui| { ui.set_width(ui.available_width()); scope(ui, &frame, TOP, settings.ghost > 0.); }); }); ui.add_space(6.); panel(ui, |ui| { ui.set_width(ui.available_width()); ui.vertical(|ui| controls(ui, settings, state)); }); ui.add_space(6.); panel(ui, |ui| { ui.set_width(ui.available_width()); kick_panel(ui, settings, state); }); ui.separator(); ui.label(RichText::new(&state.help).color(MUTED)); } const TOP: f32 = 196.; fn note_panel(ui: &mut egui::Ui, settings: &Settings, frame: &AnalyzerFrame) { caption(ui, "NOTE"); let (name, hz) = match frame.note { Some(n) if frame.hz > 0. => (common_dsp::note_name(n), format!("{:.1} Hz", frame.hz)), _ => ("—".into(), "no note".into()), }; ui.label( RichText::new(name) .font(FontId::new(54., egui::FontFamily::Name("medium".into()))) .color(if frame.note.is_some() { TEXT } else { DIM }), ); ui.label(RichText::new(hz).monospace().size(14.).color(MUTED)); ui.add_space(10.); meter(ui, "ORIGINAL", frame.low_db, STEEL, 100.); meter(ui, "NEW SUB", frame.sub_db, ACCENT, 100.); ui.add_space(10.); ui.label( RichText::new(format!( "{:.0} ms lookahead · {}", frame.latency as f32 / frame.sample_rate * 1000., match settings.source { Source::Audio => "detecting pitch", Source::Midi => "following MIDI notes", } )) .size(12.) .color(MUTED), ); } fn controls(ui: &mut egui::Ui, settings: &mut Settings, state: &mut State) { let defaults = Settings::default(); ui.columns(6, |c| { knob( &mut c[0], state, "Sub level", &mut settings.level, -30. ..=0., defaults.level, " dBFS", 88., "Peak level of the new sub. Every note gets exactly this level.", ); knob( &mut c[1], state, "Split", &mut settings.split, 40. ..=200., defaults.split, " Hz", 88., "Below this frequency the original sub is replaced.", ); knob( &mut c[2], state, "Dynamics", &mut settings.dynamics, 0. ..=100., defaults.dynamics, " %", 88., "0 %: constant level per note. Higher: follow the original's wobble shape.", ); knob( &mut c[3], state, "Drive", &mut settings.drive, 0. ..=100., defaults.drive, " %", 88., "Saturates the sine: odd harmonics, thicker sub. Changes the sub itself.", ); knob( &mut c[4], state, "Ghost", &mut settings.ghost, 0. ..=100., defaults.ghost, " %", 88., "Adds the sub's harmonics 2–10 above Ghost from, so phones hear the note. \ Only harmonics the patch lacks are added. The sub itself stays the same.", ); knob( &mut c[5], state, "Ghost from", &mut settings.ghost_from, 150. ..=600., defaults.ghost_from, " Hz", 88., "Lowest frequency Ghost adds. Phones cut off around 300–500 Hz. Never below Split.", ); }); ui.add_space(6.); ui.separator(); ui.columns(6, |c| { let mut octave = settings.octave as f32; knob( &mut c[0], state, "Octave", &mut octave, -2. ..=1., 0., "", 54., "Shift the sub against the detected note. −1 for basses played high.", ); settings.octave = octave.round() as i32; knob( &mut c[1], state, "Glide", &mut settings.glide, 0. ..=200., defaults.glide, " ms", 54., "Slide time when notes overlap. 0 jumps.", ); knob( &mut c[2], state, "Phase", &mut settings.phase, 0. ..=360., defaults.phase, "°", 54., "Start phase of every note. Line it up with your kick.", ); knob( &mut c[3], state, "Gate", &mut settings.gate, -60. ..=-20., defaults.gate, " dB", 54., "Input level that counts as a note. Raise it if tails retrigger.", ); knob( &mut c[4], state, "Keep", &mut settings.keep, 0. ..=100., defaults.keep, " %", 54., "Blend some original low band back under the new sub.", ); knob( &mut c[5], state, "Output", &mut settings.output, -12. ..=12., defaults.output, " dB", 54., "Output trim.", ); }); ui.add_space(4.); ui.horizontal(|ui| { toggle( ui, state, &mut settings.retrigger, "RETRIGGER", "Restart the sine at Phase on every note, so each note and kick hit lines up.", ); toggle( ui, state, &mut settings.snap, "SNAP TO SEMITONE", "Round the detected pitch. Turn off for slides and pitch bends.", ); }); } fn kick_panel(ui: &mut egui::Ui, settings: &mut Settings, state: &mut State) { let defaults = Settings::default(); let frame = state.analyzer; ui.horizontal_top(|ui| { ui.vertical(|ui| { ui.set_width(150.); caption(ui, "KICK LOCK"); toggle( ui, state, &mut settings.kick_lock, "KICK LOCK", "Route the kick into Anvil's sidechain. On each kick the sub dips and \ restarts at Phase, so kick and sub hit together.", ); ui.add_space(6.); let status = if !settings.kick_lock { "off".to_string() } else if frame.kick_hits == 0 { "no kick in the sidechain".to_string() } else if frame.kick_sum_db.is_finite() { format!("{} kicks · sum {:+.1} dB", frame.kick_hits, frame.kick_sum_db) } else { format!("{} kicks", frame.kick_hits) }; ui.label(RichText::new(status).size(12.).color(MUTED)); }); ui.columns(4, |c| { knob( &mut c[0], state, "Duck", &mut settings.kick_duck, 0. ..=24., defaults.kick_duck, " dB", 54., "How far the sub dips on each kick.", ); knob( &mut c[1], state, "Release", &mut settings.kick_release, 10. ..=300., defaults.kick_release, " ms", 54., "Time for the sub to come back after a kick.", ); knob( &mut c[2], state, "Offset", &mut settings.kick_offset, -KICK_OFFSET_MAX..=KICK_OFFSET_MAX, defaults.kick_offset, " ms", 54., "When the sub restarts against the kick. Negative: before it. Use Audition to find it by ear.", ); c[3].vertical(|ui| audition(ui, settings, state)); }); }); } /// The audition: each offset plays for a few kicks; bars show how kick and sub summed. fn audition(ui: &mut egui::Ui, settings: &mut Settings, state: &mut State) { let frame = state.analyzer; ui.horizontal(|ui| { let r = ui.selectable_label(state.audition, RichText::new("AUDITION").size(12.)); if r.clicked() { state.audition = !state.audition; } hint( &r, state, &format!( "Steps the offset from −30 to +30 ms, {AUDITION_HITS} kicks each. Press KEEP \ when kick and sub sound like one hit. Not saved." ), ); let r = ui.add_enabled( state.audition && frame.kick_hits > 0, egui::Button::new(RichText::new("KEEP").size(12.)), ); if r.clicked() { settings.kick_offset = frame.kick_offset; state.audition = false; } hint( &r, state, "Set Offset to the one playing now and stop the audition.", ); }); let (rect, _) = ui.allocate_exact_size(Vec2::new(ui.available_width(), 58.), Sense::hover()); let p = ui.painter_at(rect); p.rect_filled(rect, 2., SCREEN); let slot = rect.width() / AUDITION.len() as f32; // −9 dB to +3 dB: +3 is a full sum of equal levels. let height = |db: f32| ((db + 9.) / 12.).clamp(0.02, 1.) * (rect.height() - 14.); for (i, &offset) in AUDITION.iter().enumerate() { let x = rect.left() + slot * i as f32; let db = frame.audition_sums[i]; let playing = state.audition && (offset - frame.kick_offset).abs() < 0.01; if db.is_finite() { let h = height(db); p.rect_filled( egui::Rect::from_min_max( Pos2::new(x + slot * 0.2, rect.bottom() - 12. - h), Pos2::new(x + slot * 0.8, rect.bottom() - 12.), ), 1., if playing { ACCENT } else { STEEL }, ); } if (offset - settings.kick_offset).abs() < 0.01 { // The kept offset. let (cx, base) = (x + slot * 0.5, rect.bottom() - 2.); p.add(egui::Shape::convex_polygon( vec![ Pos2::new(cx, base - 7.), Pos2::new(cx + 4., base), Pos2::new(cx - 4., base), ], ACCENT, Stroke::NONE, )); } else if i % 2 == 0 { p.text( Pos2::new(x + slot * 0.5, rect.bottom() - 1.), egui::Align2::CENTER_BOTTOM, format!("{offset:+.0}"), FontId::proportional(8.), DIM, ); } } let line = if state.audition { format!("playing {:+.0} ms", frame.kick_offset) } else { "sum per offset, low band".into() }; ui.label(RichText::new(line).size(11.).color(MUTED)); }