Skip to content

Commit

Permalink
Allow setting Oscilloscope line color
Browse files Browse the repository at this point in the history
  • Loading branch information
exa04 committed Mar 17, 2024
1 parent 798c868 commit 210a26f
Showing 1 changed file with 96 additions and 47 deletions.
143 changes: 96 additions & 47 deletions src/visualizers/oscilloscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,55 +68,104 @@ where
let w = bounds.w;
let h = bounds.h;

if self.scale_by_db {
canvas.fill_path(
&{
let mut path = vg::Path::new();
let binding = self.buffer.get(cx);
let ring_buf = &(binding.lock().unwrap());

path.move_to(x, y + h / 2.);

let mut i = 0.;
for v in ring_buf.into_iter() {
// Convert value to dB
let mut py = amplitude_to_db(v.0.abs());
// Clamp value to be in range
py = py.clamp(self.display_range.0, self.display_range.1);

// Normalize value
py -= self.display_range.0;
py /= self.display_range.1 - self.display_range.0;
py *= v.0.signum();

path.line_to(
x + (w / ring_buf.len() as f32) * i,
y + (h / 2.) * (1. - py) + 1.,
);
i += 1.;
}
for v in ring_buf.into_iter().rev() {
// Convert value to dB
let mut py = amplitude_to_db(v.1.abs());
// Clamp value to be in range
py = py.clamp(self.display_range.0, self.display_range.1);
let mut fill = vg::Path::new();
let mut path_a = vg::Path::new();
let mut path_b = vg::Path::new();
let binding = self.buffer.get(cx);
let ring_buf = &(binding.lock().unwrap());
let mut rb_iter = ring_buf.into_iter();

// Normalize value
py -= self.display_range.0;
py /= self.display_range.1 - self.display_range.0;
py *= v.1.signum();
let mut i = 1.;

path.line_to(
x + (w / ring_buf.len() as f32) * i,
y + (h / 2.) * (1. - py) + 1.,
);
i -= 1.;
}
path.close();
path
},
&vg::Paint::color(cx.font_color().into()),
);
if self.scale_by_db {
// Get first value to move_to it
let v = rb_iter.next().unwrap();

// Convert value to dB
let mut py = amplitude_to_db(v.0.abs());

// Clamp value to be in range
py = py.clamp(self.display_range.0, self.display_range.1);

// Normalize value
py -= self.display_range.0;
py /= self.display_range.1 - self.display_range.0;
py *= v.0.signum();

fill.move_to(x, y + (h / 2.) * (1. - py) + 1.);
path_a.move_to(x, y + (h / 2.) * (1. - py) + 1.);

for v in rb_iter {
// Convert value to dB
py = amplitude_to_db(v.0.abs());
// Clamp value to be in range
py = py.clamp(self.display_range.0, self.display_range.1);

// Normalize value
py -= self.display_range.0;
py /= self.display_range.1 - self.display_range.0;
py *= v.0.signum();

fill.line_to(
x + (w / ring_buf.len() as f32) * i,
y + (h / 2.) * (1. - py) + 1.,
);
path_a.line_to(
x + (w / ring_buf.len() as f32) * i,
y + (h / 2.) * (1. - py) + 1.,
);

i += 1.;
}

i -= 2.;

let mut rb_iter = ring_buf.into_iter().rev();

// Get last value to move_to it
let v = rb_iter.next().unwrap();

// Convert value to dB
let mut py = amplitude_to_db(v.0.abs());

// Clamp value to be in range
py = py.clamp(self.display_range.0, self.display_range.1);

// Normalize value
py -= self.display_range.0;
py /= self.display_range.1 - self.display_range.0;
py *= v.0.signum();

fill.line_to(x + w, y + (h / 2.) * (1. - py) + 1.);
path_b.move_to(x + w, y + (h / 2.) * (1. - py) + 1.);

for v in rb_iter {
// Convert value to dB
py = amplitude_to_db(v.1.abs());
// Clamp value to be in range
py = py.clamp(self.display_range.0, self.display_range.1);

// Normalize value
py -= self.display_range.0;
py /= self.display_range.1 - self.display_range.0;
py *= v.1.signum();

fill.line_to(
x + (w / ring_buf.len() as f32) * i,
y + (h / 2.) * (1. - py) + 1.,
);
path_b.line_to(
x + (w / ring_buf.len() as f32) * i,
y + (h / 2.) * (1. - py) + 1.,
);

i -= 1.;
}

fill.close();
canvas.fill_path(&fill, &vg::Paint::color(cx.background_color().into()));
canvas.stroke_path(&fill, &vg::Paint::color(cx.font_color().into()));
canvas.stroke_path(&fill, &vg::Paint::color(cx.font_color().into()));
} else {
canvas.fill_path(
&{
Expand Down

0 comments on commit 210a26f

Please sign in to comment.