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::new(None);

let length = context.sample_rate() as usize;
let sample_rate = context.sample_rate_raw();
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

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.

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

Number of channels in this AudioBuffer

Number of samples per channel in this AudioBuffer

Sample rate of this AudioBuffer in Hertz

The raw sample rate of the AudioBuffer (which has more precision than the float sample_rate() value).

Duration in seconds of the AudioBuffer

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()

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.

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.

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.

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.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.