pub struct AudioBufferSourceNode { /* private fields */ }
Expand description
AudioBufferSourceNode
represents an audio source that consists of an
in-memory audio source (i.e. an audio file completely loaded in memory),
stored in an AudioBuffer
.
- MDN documentation: https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode
- specification: https://webaudio.github.io/web-audio-api/#AudioBufferSourceNode
- see also:
BaseAudioContext::create_buffer_source
§Usage
use std::fs::File;
use web_audio_api::context::{BaseAudioContext, AudioContext};
use web_audio_api::node::{AudioNode, AudioScheduledSourceNode};
// create an `AudioContext`
let context = AudioContext::default();
// load and decode a soundfile
let file = File::open("samples/sample.wav").unwrap();
let audio_buffer = context.decode_audio_data_sync(file).unwrap();
// play the sound file
let mut src = context.create_buffer_source();
src.set_buffer(audio_buffer);
src.connect(&context.destination());
src.start();
§Examples
cargo run --release --example trigger_soundfile
cargo run --release --example granular
Implementations§
Source§impl AudioBufferSourceNode
impl AudioBufferSourceNode
Sourcepub fn new<C: BaseAudioContext>(
context: &C,
options: AudioBufferSourceOptions,
) -> Self
pub fn new<C: BaseAudioContext>( context: &C, options: AudioBufferSourceOptions, ) -> Self
Create a new AudioBufferSourceNode
instance
Sourcepub fn start_at_with_offset(&mut self, start: f64, offset: f64)
pub fn start_at_with_offset(&mut self, start: f64, offset: f64)
Start the playback at the given time and with a given offset
§Panics
Panics if the source was already started
Sourcepub fn start_at_with_offset_and_duration(
&mut self,
start: f64,
offset: f64,
duration: f64,
)
pub fn start_at_with_offset_and_duration( &mut self, start: f64, offset: f64, duration: f64, )
Start the playback at the given time, with a given offset, for a given duration
§Panics
Panics if the source was already started
Sourcepub fn buffer(&self) -> Option<&AudioBuffer>
pub fn buffer(&self) -> Option<&AudioBuffer>
Current buffer value (nullable)
Sourcepub fn set_buffer(&mut self, audio_buffer: AudioBuffer)
pub fn set_buffer(&mut self, audio_buffer: AudioBuffer)
Provide an AudioBuffer
as the source of data to be played bask
§Panics
Panics if a buffer has already been given to the source (though new
or through
set_buffer
)
Sourcepub fn playback_rate(&self) -> &AudioParam
pub fn playback_rate(&self) -> &AudioParam
K-rate AudioParam
that defines the speed at which the AudioBuffer
will be played, e.g.:
0.5
will play the file at half speed-1
will play the file in reverse
Note that playback rate will also alter the pitch of the AudioBuffer
Sourcepub fn position(&self) -> f64
pub fn position(&self) -> f64
Current playhead position in seconds within the AudioBuffer
.
This value is updated at the end of each render quantum.
Unofficial v2 API extension, not part of the spec yet. See also: https://github.com/WebAudio/web-audio-api/issues/2397#issuecomment-709478405
Sourcepub fn detune(&self) -> &AudioParam
pub fn detune(&self) -> &AudioParam
K-rate AudioParam
that defines a pitch transposition of the file,
expressed in cents
Sourcepub fn loop_(&self) -> bool
pub fn loop_(&self) -> bool
Defines if the playback the AudioBuffer
should be looped
pub fn set_loop(&mut self, value: bool)
Sourcepub fn loop_start(&self) -> f64
pub fn loop_start(&self) -> f64
Defines the loop start point, in the time reference of the AudioBuffer
pub fn set_loop_start(&mut self, value: f64)
Sourcepub fn loop_end(&self) -> f64
pub fn loop_end(&self) -> f64
Defines the loop end point, in the time reference of the AudioBuffer
pub fn set_loop_end(&mut self, value: f64)
Trait Implementations§
Source§impl AudioNode for AudioBufferSourceNode
impl AudioNode for AudioBufferSourceNode
Source§fn registration(&self) -> &AudioContextRegistration
fn registration(&self) -> &AudioContextRegistration
BaseAudioContext
. Read moreSource§fn channel_config(&self) -> &ChannelConfig
fn channel_config(&self) -> &ChannelConfig
Source§fn number_of_inputs(&self) -> usize
fn number_of_inputs(&self) -> usize
Source§fn number_of_outputs(&self) -> usize
fn number_of_outputs(&self) -> usize
Source§fn context(&self) -> &ConcreteBaseAudioContext
fn context(&self) -> &ConcreteBaseAudioContext
BaseAudioContext
concrete type which owns this
AudioNode.Source§fn connect<'a>(&self, dest: &'a dyn AudioNode) -> &'a dyn AudioNode
fn connect<'a>(&self, dest: &'a dyn AudioNode) -> &'a dyn AudioNode
Source§fn connect_from_output_to_input<'a>(
&self,
dest: &'a dyn AudioNode,
output: usize,
input: usize,
) -> &'a dyn AudioNode
fn connect_from_output_to_input<'a>( &self, dest: &'a dyn AudioNode, output: usize, input: usize, ) -> &'a dyn AudioNode
Source§fn disconnect(&self)
fn disconnect(&self)
Source§fn disconnect_dest(&self, dest: &dyn AudioNode)
fn disconnect_dest(&self, dest: &dyn AudioNode)
Source§fn disconnect_output(&self, output: usize)
fn disconnect_output(&self, output: usize)
Source§fn disconnect_dest_from_output(&self, dest: &dyn AudioNode, output: usize)
fn disconnect_dest_from_output(&self, dest: &dyn AudioNode, output: usize)
Source§fn disconnect_dest_from_output_to_input(
&self,
dest: &dyn AudioNode,
output: usize,
input: usize,
)
fn disconnect_dest_from_output_to_input( &self, dest: &dyn AudioNode, output: usize, input: usize, )
Source§fn channel_count_mode(&self) -> ChannelCountMode
fn channel_count_mode(&self) -> ChannelCountMode
Source§fn set_channel_count_mode(&self, v: ChannelCountMode)
fn set_channel_count_mode(&self, v: ChannelCountMode)
channel_count_mode
attributeSource§fn channel_interpretation(&self) -> ChannelInterpretation
fn channel_interpretation(&self) -> ChannelInterpretation
Source§fn set_channel_interpretation(&self, v: ChannelInterpretation)
fn set_channel_interpretation(&self, v: ChannelInterpretation)
channel_interpretation
attributeSource§fn channel_count(&self) -> usize
fn channel_count(&self) -> usize
Source§fn set_channel_count(&self, v: usize)
fn set_channel_count(&self, v: usize)
channel_count
attribute