pub struct Signal {
pub samples: Vec<f32>,
}
Expand description
Struct containing audio samples
Fields§
§samples: Vec<f32>
buffer containing samples
Implementations§
source§impl Signal
impl Signal
sourcepub fn empty(size: usize) -> Self
pub fn empty(size: usize) -> Self
Create zero initialized (silent) signal
use screech::Signal;
let signal = Signal::empty(4);
assert_eq!(&signal.samples, &[0.0, 0.0, 0.0, 0.0]);
sourcepub fn mix(signals: &[&Signal]) -> Self
pub fn mix(signals: &[&Signal]) -> Self
Mix multiple signals into a new signal
note the size of the resulting signal is equal to the longest signal in the slice
use screech::traits::FromPoints;
use screech::Signal;
let signals = [
&Signal::from_points(vec![0.1, 0.0, -0.1, -0.2, -0.3]),
&Signal::from_points(vec![0.2, 0.1, 0.0]),
&Signal::from_points(vec![0.3]),
];
let result = Signal::mix(&signals);
assert_eq!(&result.samples, &[0.6, 0.1, -0.1, -0.2, -0.3]);
sourcepub fn mix_into(&mut self, signals: &[&Signal]) -> &mut Self
pub fn mix_into(&mut self, signals: &[&Signal]) -> &mut Self
Same as Signal::mix
, but mixes sources into the current signal
note the size of the signal will be unchanged
use screech::traits::FromPoints;
use screech::Signal;
let signals = [
&Signal::from_points(vec![0.2, 0.1, 0.0]),
&Signal::from_points(vec![0.3]),
];
let mut signal = Signal::from_points(vec![0.1, 0.0, -0.1, -0.2, -0.3]);
signal.mix_into(&signals);
assert_eq!(&signal.samples, &[0.6, 0.1, -0.1, -0.2, -0.3]);
sourcepub fn resample_linear(&mut self, factor: f32) -> &mut Self
pub fn resample_linear(&mut self, factor: f32) -> &mut Self
Resample using linear interpolation
use screech::traits::FromPoints;
use screech::Signal;
let signal = Signal::from_points(vec![0.0, 0.5, 1.0, 0.5, 0.0]);
// should preserve the samples at 1.0
assert_eq!(
&signal.clone().resample_linear(1.0).samples,
&signal.clone().samples,
);
assert_eq!(
&signal.clone().resample_linear(2.0).samples,
&[0.0, 0.25, 0.5, 0.75, 1.0, 0.75, 0.5, 0.25, 0.0]
);
assert_eq!(
signal.clone().resample_linear(0.5).samples,
&[0.0, 1.0, 0.0]
);
assert_eq!(
signal.clone().resample_linear(0.8).samples,
&[0.0, 0.625, 0.75, 0.125]
);
let signal2 = Signal::from_points(
vec![0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
);
assert_eq!(
signal2.clone().resample_linear(0.5).samples,
&[0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
);
assert_eq!(
signal2.clone().resample_linear(0.2).samples,
&[0.0, 0.5, 1.0]
);