Struct AudioBuffer

Source
pub struct AudioBuffer { /* private fields */ }
Expand description

Memory-resident audio asset, basically a matrix of channels * samples

An AudioBuffer has copy-on-write semantics, so it is cheap to clone.

§Usage

use std::f32::consts::PI;
use web_audio_api::context::{AudioContext, BaseAudioContext};
use web_audio_api::node::{AudioNode, AudioScheduledSourceNode};

let context = AudioContext::default();

let length = context.sample_rate() as usize;
let sample_rate = context.sample_rate();
let mut buffer = context.create_buffer(1, length, sample_rate);

// fill buffer with a sine wave
let mut sine = vec![];

for i in 0..length {
    let phase = i as f32 / length as f32 * 2. * PI * 200.;
    sine.push(phase.sin());
}

buffer.copy_to_channel(&sine, 0);

// play the buffer in a loop
let mut src = context.create_buffer_source();
src.set_buffer(buffer.clone());
src.set_loop(true);
src.connect(&context.destination());
src.start();

§Example

  • cargo run --release --example audio_buffer

Implementations§

Source§

impl AudioBuffer

Source

pub fn new(options: AudioBufferOptions) -> Self

Allocate a silent audiobuffer with AudioBufferOptions

§Panics

This function will panic if:

  • the given sample rate is zero
  • the given number of channels is outside the [1, 32] range, 32 being defined by the MAX_CHANNELS constant.
Source

pub fn from(samples: Vec<Vec<f32>>, sample_rate: f32) -> Self

Convert raw samples to an AudioBuffer

The outer Vec determine the channels. The inner Vecs should have the same length.

§Panics

This function will panic if:

  • the given sample rate is zero
  • the given number of channels defined by samples.len()is outside the [1, 32] range, 32 being defined by the MAX_CHANNELS constant.
  • any of its items have different lengths
Source

pub fn number_of_channels(&self) -> usize

Number of channels in this AudioBuffer

Source

pub fn length(&self) -> usize

Number of samples per channel in this AudioBuffer

Source

pub fn sample_rate(&self) -> f32

Sample rate of this AudioBuffer in Hertz

Source

pub fn duration(&self) -> f64

Duration in seconds of the AudioBuffer

Source

pub fn copy_from_channel(&self, destination: &mut [f32], channel_number: usize)

Copy data from a given channel to the given Vec

§Panics

This function will panic if channel_number is greater or equal than AudioBuffer::number_of_channels()

Source

pub fn copy_from_channel_with_offset( &self, destination: &mut [f32], channel_number: usize, offset: usize, )

Copy data from a given channel to the given Vec starting at offset

§Panics

This function will panic if:

  • the given channel number is greater than or equal to the given number of channels.
Source

pub fn copy_to_channel(&mut self, source: &[f32], channel_number: usize)

Copy data from a given source to the given channel.

§Panics

This function will panic if:

  • the given channel number is greater than or equal to the given number of channels.
Source

pub fn copy_to_channel_with_offset( &mut self, source: &[f32], channel_number: usize, offset: usize, )

Copy data from a given source to the given channel starting at offset.

§Panics

This function will panic if:

  • the given channel number is greater than or equal to the given number of channels.
Source

pub fn get_channel_data(&self, channel_number: usize) -> &[f32]

Return a read-only copy of the underlying data of the channel

§Panics

This function will panic if:

  • the given channel number is greater than or equal to the given number of channels.
Source

pub fn get_channel_data_mut(&mut self, channel_number: usize) -> &mut [f32]

Return a mutable slice of the underlying data of the channel

§Panics

This function will panic if:

  • the given channel number is greater than or equal to the given number of channels.

Trait Implementations§

Source§

impl Clone for AudioBuffer

Source§

fn clone(&self) -> AudioBuffer

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 Debug for AudioBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

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

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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,