Struct AudioContext

Source
pub struct AudioContext { /* private fields */ }
Expand description

This interface represents an audio graph whose AudioDestinationNode is routed to a real-time output device that produces a signal directed at the user.

Implementations§

Source§

impl AudioContext

Source

pub fn new(options: AudioContextOptions) -> Self

Creates and returns a new AudioContext object.

This will play live audio on the default output device.

use web_audio_api::context::{AudioContext, AudioContextOptions};

// Request a sample rate of 44.1 kHz and default latency (buffer size 128, if available)
let opts = AudioContextOptions {
    sample_rate: Some(44100.),
    ..AudioContextOptions::default()
};

// Setup the audio context that will emit to your speakers
let context = AudioContext::new(opts);

// Alternatively, use the default constructor to get the best settings for your hardware
// let context = AudioContext::default();
§Panics

The AudioContext constructor will panic when an invalid sinkId is provided in the AudioContextOptions. In a future version, a try_new constructor will be introduced that never panics.

Source

pub fn base_latency(&self) -> f64

This represents the number of seconds of processing latency incurred by the AudioContext passing the audio from the AudioDestinationNode to the audio subsystem.

Source

pub fn output_latency(&self) -> f64

The estimation in seconds of audio output latency, i.e., the interval between the time the UA requests the host system to play a buffer and the time at which the first sample in the buffer is actually processed by the audio output device.

Source

pub fn sink_id(&self) -> String

Identifier or the information of the current audio output device.

The initial value is "", which means the default audio output device.

Source

pub fn render_capacity(&self) -> AudioRenderCapacity

Returns an AudioRenderCapacity instance associated with an AudioContext.

Source

pub fn set_sink_id_sync(&self, sink_id: String) -> Result<(), Box<dyn Error>>

Update the current audio output device.

The provided sink_id string must match a device name enumerate_devices_sync.

Supplying "none" for the sink_id will process the audio graph without playing through an audio output device.

This function operates synchronously and might block the current thread. An async version is currently not implemented.

Source

pub fn set_onsinkchange<F: FnMut(Event) + Send + 'static>(&self, callback: F)

Register callback to run when the audio sink has changed

Only a single event handler is active at any time. Calling this method multiple times will override the previous event handler.

Source

pub fn clear_onsinkchange(&self)

Unset the callback to run when the audio sink has changed

Source

pub async fn suspend(&self)

Suspends the progression of time in the audio context.

This will temporarily halt audio hardware access and reducing CPU/battery usage in the process.

§Panics

Will panic if:

  • The audio device is not available
  • For a BackendSpecificError
Source

pub async fn resume(&self)

Resumes the progression of time in an audio context that has previously been suspended/paused.

§Panics

Will panic if:

  • The audio device is not available
  • For a BackendSpecificError
Source

pub async fn close(&self)

Closes the AudioContext, releasing the system resources being used.

This will not automatically release all AudioContext-created objects, but will suspend the progression of the currentTime, and stop processing audio data.

§Panics

Will panic when this function is called multiple times

Source

pub fn suspend_sync(&self)

Suspends the progression of time in the audio context.

This will temporarily halt audio hardware access and reducing CPU/battery usage in the process.

This function operates synchronously and blocks the current thread until the audio thread has stopped processing.

§Panics

Will panic if:

  • The audio device is not available
  • For a BackendSpecificError
Source

pub fn resume_sync(&self)

Resumes the progression of time in an audio context that has previously been suspended/paused.

This function operates synchronously and blocks the current thread until the audio thread has started processing again.

§Panics

Will panic if:

  • The audio device is not available
  • For a BackendSpecificError
Source

pub fn close_sync(&self)

Closes the AudioContext, releasing the system resources being used.

This will not automatically release all AudioContext-created objects, but will suspend the progression of the currentTime, and stop processing audio data.

This function operates synchronously and blocks the current thread until the audio thread has stopped processing.

§Panics

Will panic when this function is called multiple times

Source

pub fn create_media_stream_source( &self, media: &MediaStream, ) -> MediaStreamAudioSourceNode

Source

pub fn create_media_stream_destination(&self) -> MediaStreamAudioDestinationNode

Source

pub fn create_media_stream_track_source( &self, media: &MediaStreamTrack, ) -> MediaStreamTrackAudioSourceNode

Source

pub fn create_media_element_source( &self, media_element: &mut MediaElement, ) -> MediaElementAudioSourceNode

Trait Implementations§

Source§

impl BaseAudioContext for AudioContext

Source§

fn decode_audio_data_sync<R: Read + Send + Sync + 'static>( &self, input: R, ) -> Result<AudioBuffer, Box<dyn Error + Send + Sync>>

Decode an AudioBuffer from a given input stream. Read more
Source§

fn decode_audio_data<R: Read + Send + Sync + 'static>( &self, input: R, ) -> impl Future<Output = Result<AudioBuffer, Box<dyn Error + Send + Sync>>> + Send + 'static

Decode an AudioBuffer from a given input stream. Read more
Source§

fn create_buffer( &self, number_of_channels: usize, length: usize, sample_rate: f32, ) -> AudioBuffer

Create an new “in-memory” AudioBuffer with the given number of channels, length (i.e. number of samples per channel) and sample rate. Read more
Source§

fn create_analyser(&self) -> AnalyserNode

Creates a AnalyserNode
Source§

fn create_biquad_filter(&self) -> BiquadFilterNode

Creates an BiquadFilterNode which implements a second order filter
Source§

fn create_buffer_source(&self) -> AudioBufferSourceNode

Creates an AudioBufferSourceNode
Source§

fn create_constant_source(&self) -> ConstantSourceNode

Creates an ConstantSourceNode, a source representing a constant value
Source§

fn create_convolver(&self) -> ConvolverNode

Creates an ConvolverNode, a processing node which applies linear convolution
Source§

fn create_channel_merger(&self, number_of_inputs: usize) -> ChannelMergerNode

Creates a ChannelMergerNode
Source§

fn create_channel_splitter( &self, number_of_outputs: usize, ) -> ChannelSplitterNode

Creates a ChannelSplitterNode
Source§

fn create_delay(&self, max_delay_time: f64) -> DelayNode

Creates a DelayNode, delaying the audio signal
Source§

fn create_dynamics_compressor(&self) -> DynamicsCompressorNode

Creates a DynamicsCompressorNode, compressing the audio signal
Source§

fn create_gain(&self) -> GainNode

Creates an GainNode, to control audio volume
Source§

fn create_iir_filter( &self, feedforward: Vec<f64>, feedback: Vec<f64>, ) -> IIRFilterNode

Creates an IirFilterNode Read more
Source§

fn create_oscillator(&self) -> OscillatorNode

Creates an OscillatorNode, a source representing a periodic waveform.
Source§

fn create_panner(&self) -> PannerNode

Creates a PannerNode
Source§

fn create_periodic_wave(&self, options: PeriodicWaveOptions) -> PeriodicWave

Creates a periodic wave Read more
Source§

fn create_script_processor( &self, buffer_size: usize, number_of_input_channels: usize, number_of_output_channels: usize, ) -> ScriptProcessorNode

Creates an ScriptProcessorNode for custom audio processing (deprecated); Read more
Source§

fn create_stereo_panner(&self) -> StereoPannerNode

Creates an StereoPannerNode to pan a stereo output
Source§

fn create_wave_shaper(&self) -> WaveShaperNode

Creates a WaveShaperNode
Source§

fn destination(&self) -> AudioDestinationNode

Returns an AudioDestinationNode representing the final destination of all audio in the context. It can be thought of as the audio-rendering device.
Source§

fn listener(&self) -> AudioListener

Returns the AudioListener which is used for 3D spatialization
Source§

fn sample_rate(&self) -> f32

The sample rate (in sample-frames per second) at which the AudioContext handles audio.
Source§

fn state(&self) -> AudioContextState

Returns state of current context
Source§

fn current_time(&self) -> f64

This is the time in seconds of the sample frame immediately following the last sample-frame in the block of audio most recently processed by the context’s rendering graph.
Source§

fn create_audio_param( &self, opts: AudioParamDescriptor, dest: &AudioContextRegistration, ) -> (AudioParam, AudioParamId)

Create an AudioParam. Read more
Source§

fn set_onstatechange<F: FnMut(Event) + Send + 'static>(&self, callback: F)

Register callback to run when the state of the AudioContext has changed Read more
Source§

fn clear_onstatechange(&self)

Unset the callback to run when the state of the AudioContext has changed
Source§

impl Debug for AudioContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AudioContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for AudioContext

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

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 T
where U: From<T>,

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.

Source§

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

Source§

fn into_sample(self) -> T

Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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