Skip to content

Commit

Permalink
make left and right switchable on graph
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Apr 24, 2024
1 parent 202898f commit 74389c0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 80 deletions.
131 changes: 77 additions & 54 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct LambData {
level_buffer_r: Arc<Mutex<PeakBuffer>>,
gr_buffer_l: Arc<Mutex<MinimaBuffer>>,
gr_buffer_r: Arc<Mutex<MinimaBuffer>>,
show_left: bool,
show_right: bool,
}

impl LambData {
Expand All @@ -33,18 +35,32 @@ impl LambData {
level_buffer_r: Arc<Mutex<PeakBuffer>>,
gr_buffer_l: Arc<Mutex<MinimaBuffer>>,
gr_buffer_r: Arc<Mutex<MinimaBuffer>>,
show_left: bool,
show_right: bool,
) -> Self {
Self {
params,
level_buffer_l,
level_buffer_r,
gr_buffer_l,
gr_buffer_r,
show_left,
show_right,
}
}
}

impl Model for LambData {}
pub enum AppEvent {
ShowLeft,
ShowRight,
}
impl Model for LambData {
fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|app_event, meta| match app_event {
AppEvent::ShowLeft => self.show_left = self.params.show_left.value(),
AppEvent::ShowRight => self.show_right = self.params.show_right.value(),
});
}
}

// Makes sense to also define this here, makes it a bit easier to keep track of
pub(crate) fn default_state() -> Arc<ViziaState> {
Expand All @@ -59,6 +75,8 @@ pub(crate) fn create(
level_buffer_r: Arc<Mutex<PeakBuffer>>,
gr_buffer_l: Arc<Mutex<MinimaBuffer>>,
gr_buffer_r: Arc<Mutex<MinimaBuffer>>,
show_left: bool,
show_right: bool,
editor_state: Arc<ViziaState>,
) -> Option<Box<dyn Editor>> {
create_vizia_editor(editor_state, ViziaTheming::Custom, move |cx, _| {
Expand All @@ -75,6 +93,8 @@ pub(crate) fn create(
level_buffer_r: level_buffer_r.clone(),
gr_buffer_l: gr_buffer_l.clone(),
gr_buffer_r: gr_buffer_r.clone(),
show_left: true,
show_right: true,
}
.build(cx);

Expand Down Expand Up @@ -203,17 +223,21 @@ pub(crate) fn create(
.bottom(Pixels(6.0));
HStack::new(cx, |cx| {
ParamButton::new(cx, LambData::params, |params| &params.show_left)
.on_press(|ex| ex.emit(AppEvent::ShowLeft))
.disable_scroll_wheel()
// .on_scroll(|ex| ex.emit(AppEvent::ShowLeft))
.with_label("left")
.width(Stretch(1.0))
.bottom(Pixels(6.0));
ParamButton::new(cx, LambData::params, |params| &params.show_right)
.on_press(|ex| ex.emit(AppEvent::ShowRight))
.disable_scroll_wheel()
// .on_scroll(|ex| ex.emit(AppEvent::ShowRight))
.with_label("right")
.width(Stretch(1.0))
.bottom(Pixels(6.0));
})
.width(Stretch(1.0))
// .width(Percentage(100.0))
;
.width(Stretch(1.0));
})
.width(Percentage(100.0))
.col_between(Percentage(5.0));
Expand All @@ -225,13 +249,13 @@ pub(crate) fn create(
.col_between(Percentage(2.5))
.height(Auto)
.width(Percentage(100.0));
peak_graph(cx, params.show_left.value(), params.show_right.value());
peak_graph(cx);
}) // everything
.width(Percentage(95.0))
.height(Auto)
.left(Percentage(2.5))
.right(Percentage(2.5))
.class("center");
.width(Percentage(95.0))
.height(Auto)
.left(Percentage(2.5))
.right(Percentage(2.5))
.class("center");
ResizeHandle::new(cx);
})
}
Expand Down Expand Up @@ -398,7 +422,7 @@ impl<AttackReleaseDataL: Lens<Target = Arc<LambParams>>> View
}

/// Draws a peak graph with a grid backdrop, unit ruler, and a peak meter to side.
fn peak_graph(cx: &mut Context, show_left: bool, show_right: bool) {
fn peak_graph(cx: &mut Context) {
HStack::new(cx, |cx| {
ZStack::new(cx, |cx| {
Grid::new(
Expand All @@ -410,51 +434,50 @@ fn peak_graph(cx: &mut Context, show_left: bool, show_right: bool) {
)
.color(Color::rgba(160, 160, 160, 60));

if show_left {
println!("left");
// level
Graph::new(
cx,
LambData::level_buffer_l,
(METER_MIN, METER_MAX),
ValueScaling::Decibels,
)
.color(Color::rgba(0, 0, 255, 30))
.background_color(Color::rgba(0, 0, 0, 40));
// level
Graph::new(
cx,
LambData::level_buffer_l,
(METER_MIN, METER_MAX),
ValueScaling::Decibels,
)
.visibility(LambData::show_left)
.color(Color::rgba(0, 0, 255, 30))
.background_color(Color::rgba(0, 0, 0, 40));

// gain reduction
Graph::new(
cx,
LambData::gr_buffer_l,
(METER_MIN, METER_MAX),
ValueScaling::Decibels,
)
.color(Color::rgba(0, 0, 255, 255))
.background_color(Color::rgba(250, 250, 250, 40))
.fill_from(0.0);
};
if show_right {
println!("right");
// level
Graph::new(
cx,
LambData::level_buffer_r,
(METER_MIN, METER_MAX),
ValueScaling::Decibels,
)
// level
Graph::new(
cx,
LambData::level_buffer_r,
(METER_MIN, METER_MAX),
ValueScaling::Decibels,
)
.visibility(LambData::show_right)
.color(Color::rgba(255, 0, 0, 30))
.background_color(Color::rgba(0, 0, 0, 40));
// gain reduction
Graph::new(
cx,
LambData::gr_buffer_r,
(METER_MIN, METER_MAX),
ValueScaling::Decibels,
)
.color(Color::rgba(255, 0, 0, 255))
.background_color(Color::rgba(250, 250, 250, 40))
.fill_from(0.0);
};
// gain reduction
Graph::new(
cx,
LambData::gr_buffer_l,
(METER_MIN, METER_MAX),
ValueScaling::Decibels,
)
.visibility(LambData::show_left)
.color(Color::rgba(0, 0, 255, 255))
.background_color(Color::rgba(250, 250, 250, 40))
.fill_from(0.0);
// gain reduction
Graph::new(
cx,
LambData::gr_buffer_r,
(METER_MIN, METER_MAX),
ValueScaling::Decibels,
)
.visibility(LambData::show_right)
.color(Color::rgba(255, 0, 0, 255))
.background_color(Color::rgba(250, 250, 250, 40))
.fill_from(0.0);
// };
});

ZStack::new(cx, |cx| {
Expand Down
20 changes: 7 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,17 @@ pub struct Lamb {
level_buffer_r: Arc<Mutex<PeakBuffer>>,
gr_buffer_l: Arc<Mutex<MinimaBuffer>>,
gr_buffer_r: Arc<Mutex<MinimaBuffer>>,
show_left: bool,
show_right: bool,

/// If this is set at the start of the processing cycle, then the graph duration should be updated.
should_update_time_scale: Arc<AtomicBool>,
should_update_show_left: Arc<AtomicBool>,
should_update_show_right: Arc<AtomicBool>,
}
impl Default for Lamb {
fn default() -> Self {
let should_update_time_scale = Arc::new(AtomicBool::new(false));
let should_update_show_left = Arc::new(AtomicBool::new(false));
let should_update_show_right = Arc::new(AtomicBool::new(false));
Self {
params: Arc::new(LambParams::new(
should_update_time_scale.clone(),
should_update_show_left.clone(),
should_update_show_right.clone(),
)),
params: Arc::new(LambParams::new(should_update_time_scale.clone())),
// params: Arc::new(LambParams::default()),
dsp: dsp::LambRs::default_boxed(),

Expand All @@ -63,9 +57,9 @@ impl Default for Lamb {
level_buffer_r: Arc::new(Mutex::new(PeakBuffer::new(1114, 7.0, 0.0))),
gr_buffer_l: Arc::new(Mutex::new(MinimaBuffer::new(1114, 7.0, 0.0))),
gr_buffer_r: Arc::new(Mutex::new(MinimaBuffer::new(1114, 7.0, 0.0))),
show_left: true,
show_right: true,
should_update_time_scale,
should_update_show_left,
should_update_show_right,
}
}
}
Expand Down Expand Up @@ -161,8 +155,6 @@ impl Plugin for Lamb {
// Reset buffers and envelopes here. This can be called from the audio thread and may not
// allocate. You can remove this function if you do not need it.
self.should_update_time_scale.store(true, Ordering::Release);
self.should_update_show_left.store(true, Ordering::Release);
self.should_update_show_right.store(true, Ordering::Release);
}

fn editor(&mut self, _async_executor: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> {
Expand All @@ -172,6 +164,8 @@ impl Plugin for Lamb {
self.level_buffer_r.clone(),
self.gr_buffer_l.clone(),
self.gr_buffer_r.clone(),
self.show_left.clone(),
self.show_right.clone(),
self.params.editor_state.clone(),
)
}
Expand Down
14 changes: 1 addition & 13 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,7 @@ pub fn s2v_bool_in_out() -> Arc<dyn Fn(&str) -> Option<bool> + Send + Sync> {
}

impl LambParams {
pub fn new(
should_update_time_scale: Arc<AtomicBool>,
should_update_show_left: Arc<AtomicBool>,
should_update_show_right: Arc<AtomicBool>,
) -> Self {
pub fn new(should_update_time_scale: Arc<AtomicBool>) -> Self {
Self {
editor_state: editor::default_state(),

Expand Down Expand Up @@ -357,17 +353,9 @@ impl LambParams {
.hide()
.hide_in_generic_ui(),
show_left: BoolParam::new("show_left", true)
.with_callback({
let should_update_show_left = should_update_show_left.clone();
Arc::new(move |_| should_update_show_left.store(true, Ordering::Release))
})
.hide()
.hide_in_generic_ui(),
show_right: BoolParam::new("show_right", true)
.with_callback({
let should_update_show_right = should_update_show_right.clone();
Arc::new(move |_| should_update_show_right.store(true, Ordering::Release))
})
.hide()
.hide_in_generic_ui(),
}
Expand Down

0 comments on commit 74389c0

Please sign in to comment.