Skip to content

Commit

Permalink
Allow Histogram range updates
Browse files Browse the repository at this point in the history
  • Loading branch information
exa04 committed May 31, 2024
1 parent bce8ce5 commit bdbc2ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/utils/buffers/histogram_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ pub struct HistogramBuffer {
// together these make older values decay; the smaller decay_weight, the faster the decay
decay_weight: f32,
edges: Vec<f32>,
range: (f32, f32),
}
const MIN_EDGE: f32 = -96.0;
const MAX_EDGE: f32 = 24.0;

impl HistogramBuffer {
/// Constructs a new HistogramBuffer with the given size.
Expand All @@ -41,9 +40,15 @@ impl HistogramBuffer {
decay,
decay_weight,
edges: vec![f32::default(); size - 1],
range: (-96., 24.),
}
}

pub(crate) fn set_range(&mut self, range: (f32, f32)) {
self.range = range;
self.update();
}

/// Sets the decay time of the `HistogramBuffer`.
///
/// * `decay` - The time it takes for a sample inside the buffer to decrease by -12dB, in milliseconds
Expand Down Expand Up @@ -91,12 +96,11 @@ impl HistogramBuffer {
self.edges = (0..nr_edges)
.map(|x| {
Self::db_to_linear(
MIN_EDGE + x as f32 * ((MAX_EDGE - MIN_EDGE) / (nr_edges as f32 - 1.0)),
self.range.0
+ x as f32 * ((self.range.1 - self.range.0) / (nr_edges as f32 - 1.0)),
)
})
.collect::<Vec<_>>()
.try_into()
.unwrap();
.collect::<Vec<_>>();

self.decay_weight = Self::decay_weight(self.decay, self.sample_rate);
}
Expand Down
8 changes: 5 additions & 3 deletions src/visualizers/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ where
fn element(&self) -> Option<&'static str> {
Some("histogram")
}
fn event(&mut self, _cx: &mut EventContext, event: &mut Event) {
fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|e, _| match e {
HistogramEvents::UpdateRange(v) => self.range = *v,
// HistogramEvents::UpdateDecay(s) => self.decay = *s,
HistogramEvents::UpdateRange(v) => {
self.range = *v;
self.buffer.get(cx).lock().unwrap().set_range(*v)
} // HistogramEvents::UpdateDecay(s) => self.decay = *s,
});
}
fn draw(&self, cx: &mut DrawContext, canvas: &mut Canvas) {
Expand Down

0 comments on commit bdbc2ae

Please sign in to comment.