use {Duplex, Sample};
#[inline]
pub fn map_in_place<S, F>(a: &mut [S], mut map: F)
where F: FnMut(S) -> S,
S: Sample,
{
for i in 0..a.len() {
a[i] = map(a[i]);
}
}
pub fn equilibrium<S>(a: &mut [S])
where S: Sample,
{
map_in_place(a, |_| S::equilibrium())
}
#[inline]
pub fn map_in_place_with<S, F>(a: &mut [S], b: &[S], map_with: F)
where F: FnMut(S, S) -> S,
S: Sample,
{
assert_eq!(a.len(), b.len());
unchecked_map_in_place_with(a, b, map_with);
}
#[inline]
pub fn write<S>(a: &mut [S], b: &[S])
where S: Sample,
{
assert_eq!(a.len(), b.len());
unchecked_map_in_place_with(a, b, |_, b| b);
}
#[inline]
pub fn add<S>(a: &mut [S], b: &[S])
where S: Sample,
{
assert_eq!(a.len(), b.len());
unchecked_map_in_place_with(a, b, |a, b| a + b);
}
#[inline]
pub fn add_with_amp_per_channel<S>(a: &mut [S], b: &[S], amp_per_channel: &[f32])
where S: Sample + Duplex<f32>,
{
let n_samples = a.len();
let n_channels = amp_per_channel.len();
assert_eq!(b.len(), n_samples);
assert!(n_samples % n_channels == 0, "n_samples must be a multiple of n_channels");
if n_channels > 0 {
let n_frames = n_samples / n_channels;
for i in 0..n_frames {
for j in 0..n_channels {
let idx = i * n_channels + j;
let to_add: S = (b[idx].to_sample::<f32>() * amp_per_channel[j]).to_sample();
a[idx] = a[idx] + to_add;
}
}
} else {
unchecked_map_in_place_with(a, b, |a, b| a + b);
}
}
#[inline]
fn unchecked_map_in_place_with<S, F>(a: &mut [S], b: &[S], mut map_with: F)
where F: FnMut(S, S) -> S,
S: Sample,
{
for i in 0..a.len() {
a[i] = map_with(a[i], b[i]);
}
}