Struct screech::Signal

source ·
pub struct Signal {
    pub samples: Vec<f32>,
}
Expand description

Struct containing audio samples

Fields§

§samples: Vec<f32>

buffer containing samples

Implementations§

source§

impl Signal

source

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]);
source

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]);
source

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]);
source

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]
);

Trait Implementations§

source§

impl Clone for Signal

source§

fn clone(&self) -> Signal

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl FromPoints<f32, Signal> for Signal

source§

fn from_points(points: Vec<f32>) -> Signal

Create new signal based on f32 points

source§

impl FromPoints<i16, Signal> for Signal

source§

fn from_points(points: Vec<i16>) -> Signal

Create new signal based on i16 points, converts i16 to point value (f32 between -1.0 and 1.0)

source§

impl FromPoints<i32, Signal> for Signal

source§

fn from_points(points: Vec<i32>) -> Signal

Create new signal based on i32 points, converts i32 to point value (f32 between -1.0 and 1.0)

source§

impl FromPoints<u8, Signal> for Signal

source§

fn from_points(points: Vec<u8>) -> Signal

Create new signal based on u8 points, converts u8 to point value (f32 between -1.0 and 1.0)

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.