Struct web_audio_api::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();
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
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: f32) -> Self
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
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 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.
sourcepub fn copy_to_channel_with_offset(
&mut self,
source: &[f32],
channel_number: usize,
offset: usize,
)
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.
sourcepub fn get_channel_data(&self, channel_number: usize) -> &[f32]
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.
sourcepub fn get_channel_data_mut(&mut self, channel_number: usize) -> &mut [f32]
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
impl Clone for AudioBuffer
source§fn clone(&self) -> AudioBuffer
fn clone(&self) -> AudioBuffer
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for AudioBuffer
impl RefUnwindSafe for AudioBuffer
impl Send for AudioBuffer
impl Sync for AudioBuffer
impl Unpin for AudioBuffer
impl UnwindSafe for AudioBuffer
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)