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 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 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<S> FromSample<S> for S

source§

fn from_sample_(s: S) -> S

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.

§

impl<F, T> IntoSample<T> for Fwhere
    T: FromSample<F>,

§

fn into_sample(self) -> T

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

source§

fn to_sample_(self) -> U

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.
source§

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