From 2ff7f3837a2fb65754cb8d0475de37c7fb27b4bd Mon Sep 17 00:00:00 2001 From: Hailey Somerville Date: Wed, 8 Apr 2020 19:50:20 +1000 Subject: [PATCH] move plotter and refactor channel handling --- frontend/src/module/mod.rs | 1 + .../components => module}/plotter.rs | 41 ++++++++----------- frontend/src/workspace.rs | 5 +-- frontend/src/workspace/components/mod.rs | 1 - protocol/src/lib.rs | 2 +- src/module/plotter.rs | 17 +++++--- 6 files changed, 32 insertions(+), 35 deletions(-) rename frontend/src/{workspace/components => module}/plotter.rs (60%) delete mode 100644 frontend/src/workspace/components/mod.rs diff --git a/frontend/src/module/mod.rs b/frontend/src/module/mod.rs index 294e3ce..31a06e4 100644 --- a/frontend/src/module/mod.rs +++ b/frontend/src/module/mod.rs @@ -4,5 +4,6 @@ pub mod fm_sine; pub mod icecast_input; pub mod mixer_4ch; pub mod output_device; +pub mod plotter; pub mod sine_generator; pub mod trigger; diff --git a/frontend/src/workspace/components/plotter.rs b/frontend/src/module/plotter.rs similarity index 60% rename from frontend/src/workspace/components/plotter.rs rename to frontend/src/module/plotter.rs index a48320e..f48540f 100644 --- a/frontend/src/workspace/components/plotter.rs +++ b/frontend/src/module/plotter.rs @@ -1,4 +1,3 @@ -use itertools::{Itertools, Either}; use plotters::prelude::*; use web_sys::HtmlCanvasElement; use yew::{html, Component, ComponentLink, Html, ShouldRender, Properties, NodeRef}; @@ -13,10 +12,13 @@ pub struct PlotterProps { pub width: usize, } +struct Plot { + canvas: NodeRef, +} + pub struct Plotter { props: PlotterProps, - canvas_1: NodeRef, - canvas_2: NodeRef, + plots: Vec, } impl Component for Plotter { @@ -26,8 +28,10 @@ impl Component for Plotter { fn create(props: Self::Properties, _: ComponentLink) -> Self { Plotter { props, - canvas_1: NodeRef::default(), - canvas_2: NodeRef::default(), + plots: vec![ + Plot { canvas: NodeRef::default() }, + Plot { canvas: NodeRef::default() }, + ], } } @@ -42,21 +46,11 @@ impl Component for Plotter { fn change(&mut self, props: Self::Properties) -> ShouldRender { self.props = props; - if let Some(input) = &self.props.indication.inputs[0] { - let (channel_1, channel_2): (Vec, Vec) = input.into_iter().enumerate().partition_map(|(i, sample)| { - if i % 2 == 0 { - Either::Left(sample) - } else { - Either::Right(sample) + for (index, input) in self.props.indication.inputs.iter().enumerate() { + if let Some(plot) = self.plots.get(index) { + if let Some(canvas) = plot.canvas.cast::() { + render_plot(canvas, input); } - }); - - if let Some(canvas) = self.canvas_1.cast::() { - plot(canvas, &channel_1); - - } - if let Some(canvas) = self.canvas_2.cast::() { - plot(canvas, &channel_2); } } @@ -65,15 +59,14 @@ impl Component for Plotter { fn view(&self) -> Html { html! { - <> - - - + { for self.plots.iter().map(|plot| { + html! { } + })} } } } -fn plot(canvas: HtmlCanvasElement, channel: &[f32]) { +fn render_plot(canvas: HtmlCanvasElement, channel: &[f32]) { let backend = CanvasBackend::with_canvas_object(canvas).unwrap(); let root = backend.into_drawing_area(); root.fill(&WHITE).unwrap(); diff --git a/frontend/src/workspace.rs b/frontend/src/workspace.rs index 6f9f5d9..87ddd85 100644 --- a/frontend/src/workspace.rs +++ b/frontend/src/workspace.rs @@ -1,7 +1,3 @@ -mod components; - -use components::plotter::Plotter; - use std::cell::RefCell; use std::collections::{BTreeMap, HashSet}; use std::mem; @@ -19,6 +15,7 @@ use crate::module::fm_sine::FmSine; use crate::module::icecast_input::IcecastInput; use crate::module::mixer_4ch::Mixer4ch; use crate::module::output_device::OutputDevice; +use crate::module::plotter::Plotter; use crate::module::sine_generator::SineGenerator; use crate::module::trigger::Trigger; use crate::util::{callback_ex, stop_propagation, prevent_default}; diff --git a/frontend/src/workspace/components/mod.rs b/frontend/src/workspace/components/mod.rs deleted file mode 100644 index e0bb6a8..0000000 --- a/frontend/src/workspace/components/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod plotter; diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 825bebf..183cdf0 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -161,7 +161,7 @@ pub enum OutputDeviceWarning { #[derive(Serialize, Deserialize, Clone, Debug)] pub struct PlotterIndication { - pub inputs: Vec>>, + pub inputs: Vec>, } #[derive(Serialize, Deserialize, Clone, Debug)] diff --git a/src/module/plotter.rs b/src/module/plotter.rs index c5aba57..2bbcf1c 100644 --- a/src/module/plotter.rs +++ b/src/module/plotter.rs @@ -11,7 +11,7 @@ impl ModuleT for Plotter { type Indication = PlotterIndication; fn create(_: Self::Params) -> (Self, Self::Indication) { - (Plotter, PlotterIndication { inputs: vec![None] }) + (Plotter, PlotterIndication { inputs: vec![vec![], vec![]] }) } fn params(&self) -> Self::Params { @@ -24,11 +24,18 @@ impl ModuleT for Plotter { fn run_tick(&mut self, t: u64, inputs: &[Option<&[Sample]>], _outputs: &mut [&mut [Sample]]) -> Option { if t % 10 == 1 { - let inputs: Vec<_> = inputs.iter().map(|input| { - input.map(|x|x.to_vec()) - }).collect(); + inputs[0].map(|input| { + let samples = input.len() / 2; + let mut left = Vec::with_capacity(samples); + let mut right = Vec::with_capacity(samples); - Some(PlotterIndication { inputs }) + for i in 0..samples { + left.push(input[i * 2]); + right.push(input[i * 2 + 1]); + } + + PlotterIndication { inputs: vec![left, right] } + }) } else { None }