1
0
Fork 0

Use AtomicF32 for the smoothing

This should at least not have a big performance impact since we can use
relaxed memory ordering here. AtomicCell always uses acquire/release
ordering.
This commit is contained in:
Robbert van der Helm 2022-02-06 03:38:59 +01:00
parent 5e2086e1ee
commit 2486368d08
3 changed files with 19 additions and 11 deletions

7
Cargo.lock generated
View file

@ -41,6 +41,12 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55ca83137a482d61d916ceb1eba52a684f98004f18e0cafea230fe5579c178a3"
[[package]]
name = "atomic_float"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62af46d040ba9df09edc6528dae9d8e49f5f3e82f55b7d2ec31a733c38dbc49d"
[[package]]
name = "atomic_refcell"
version = "0.1.8"
@ -490,6 +496,7 @@ name = "nih_plug"
version = "0.0.0"
dependencies = [
"assert_no_alloc",
"atomic_float",
"cfg-if 1.0.0",
"crossbeam",
"lazy_static",

View file

@ -22,6 +22,7 @@ members = [
[dependencies]
nih_plug_derive = { path = "nih_plug_derive" }
atomic_float = "0.1"
cfg-if = "1.0"
crossbeam = "0.8"
lazy_static = "1.4"

View file

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crossbeam::atomic::AtomicCell;
use atomic_float::AtomicF32;
use std::sync::atomic::{AtomicU32, Ordering};
/// Controls if and how parameters gets smoothed.
@ -47,7 +47,7 @@ pub struct Smoother<T> {
/// uniform.
step_size: f32,
/// The value for the current sample. Always stored as floating point for obvious reasons.
current: AtomicCell<f32>,
current: AtomicF32,
/// The value we're smoothing towards
target: T,
}
@ -58,7 +58,7 @@ impl<T: Default> Default for Smoother<T> {
style: SmoothingStyle::None,
steps_left: AtomicU32::new(0),
step_size: Default::default(),
current: AtomicCell::new(0.0),
current: AtomicF32::new(0.0),
target: Default::default(),
}
}
@ -91,7 +91,7 @@ impl Smoother<f32> {
/// Reset the smoother the specified value.
pub fn reset(&mut self, value: f32) {
self.target = value;
self.current.store(value);
self.current.store(value, Ordering::Relaxed);
self.steps_left.store(0, Ordering::Relaxed);
}
@ -107,7 +107,7 @@ impl Smoother<f32> {
};
self.steps_left.store(steps_left, Ordering::Relaxed);
let current = self.current.load();
let current = self.current.load(Ordering::Relaxed);
self.step_size = match self.style {
SmoothingStyle::None => 0.0,
SmoothingStyle::Linear(_) => (self.target - current) / steps_left as f32,
@ -124,7 +124,7 @@ impl Smoother<f32> {
#[allow(clippy::should_implement_trait)]
pub fn next(&self) -> f32 {
if self.steps_left.load(Ordering::Relaxed) > 1 {
let current = self.current.load();
let current = self.current.load(Ordering::Relaxed);
// The number of steps usually won't fit exactly, so make sure we don't do weird things
// with overshoots or undershoots
@ -138,7 +138,7 @@ impl Smoother<f32> {
SmoothingStyle::Logarithmic(_) => current * self.step_size,
}
};
self.current.store(new);
self.current.store(new, Ordering::Relaxed);
new
} else {
@ -151,7 +151,7 @@ impl Smoother<i32> {
/// Reset the smoother the specified value.
pub fn reset(&mut self, value: i32) {
self.target = value;
self.current.store(value as f32);
self.current.store(value as f32, Ordering::Relaxed);
self.steps_left.store(0, Ordering::Relaxed);
}
@ -166,7 +166,7 @@ impl Smoother<i32> {
};
self.steps_left.store(steps_left, Ordering::Relaxed);
let current = self.current.load();
let current = self.current.load(Ordering::Relaxed);
self.step_size = match self.style {
SmoothingStyle::None => 0.0,
SmoothingStyle::Linear(_) => (self.target as f32 - current) / steps_left as f32,
@ -180,7 +180,7 @@ impl Smoother<i32> {
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> i32 {
if self.steps_left.load(Ordering::Relaxed) > 1 {
let current = self.current.load();
let current = self.current.load(Ordering::Relaxed);
// The number of steps usually won't fit exactly, so make sure we don't do weird things
// with overshoots or undershoots
@ -194,7 +194,7 @@ impl Smoother<i32> {
SmoothingStyle::Logarithmic(_) => current * self.step_size,
}
};
self.current.store(new);
self.current.store(new, Ordering::Relaxed);
new.round() as i32
} else {