Struct web_audio_api::buffer::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.
- MDN documentation: https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer
- specification: https://webaudio.github.io/web-audio-api/#AudioBuffer
- see also:
BaseAudioContext::create_buffer
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_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
sourceimpl AudioBuffer
impl AudioBuffer
sourcepub fn new(options: AudioBufferOptions) -> Self
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.
sourcepub fn from(samples: Vec<Vec<f32>>, sample_rate: SampleRate) -> Self
pub fn from(samples: Vec<Vec<f32>>, sample_rate: SampleRate) -> 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
sourcepub fn number_of_channels(&self) -> usize
pub fn number_of_channels(&self) -> usize
Number of channels in this AudioBuffer
sourcepub fn sample_rate(&self) -> f32
pub fn sample_rate(&self) -> f32
Sample rate of this AudioBuffer in Hertz
sourcepub fn sample_rate_raw(&self) -> SampleRate
pub fn sample_rate_raw(&self) -> SampleRate
The raw sample rate of the AudioBuffer (which has more precision than the float
sample_rate() value).
sourcepub fn copy_from_channel(&self, destination: &mut [f32], channel_number: usize)
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()
sourcepub fn copy_from_channel_with_offset(
&self,
destination: &mut [f32],
channel_number: usize,
offset: usize
)
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.
sourcepub fn copy_to_channel(&mut self, source: &[f32], channel_number: usize)
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.
Trait Implementations
sourceimpl Clone for AudioBuffer
impl Clone for AudioBuffer
sourcefn clone(&self) -> AudioBuffer
fn clone(&self) -> AudioBuffer
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source. Read more
Auto Trait Implementations
impl RefUnwindSafe for AudioBuffer
impl Send for AudioBuffer
impl Sync for AudioBuffer
impl Unpin for AudioBuffer
impl UnwindSafe for AudioBuffer
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<F, T> IntoSample<T> for F where
T: FromSample<F>,
impl<F, T> IntoSample<T> for F where
T: FromSample<F>,
fn into_sample(self) -> T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more