Skip to main content

AudioContext

Struct AudioContext 

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

The main entry point for the audio system.

AudioContext manages the audio graph, the audio backend (CPAL), and the real-time audio thread. It provides a high-level API for building and controlling audio processing graphs.

§Examples

§Basic Usage

use rust_audio_api::AudioContext;

let mut ctx = AudioContext::new().unwrap();
// ... build graph ...
// ctx.resume(destination_id).unwrap();

§Dynamic Parameter Updates

use rust_audio_api::{AudioContext, NodeParameter};
use rust_audio_api::nodes::{GainNode, NodeType};

let mut ctx = AudioContext::new().unwrap();
let mut gain_id = None;

let dest_id = ctx.build_graph(|builder| {
    let gain = builder.add_node(NodeType::Gain(GainNode::new(0.5)));
    gain_id = Some(gain);
    gain
});

ctx.resume(dest_id).unwrap();

// Later, send a message to change the gain
let sender = ctx.control_sender();
sender.send(rust_audio_api::graph::ControlMessage::SetParameter(
    gain_id.unwrap(),
    NodeParameter::Gain(0.8)
)).unwrap();

Implementations§

Source§

impl AudioContext

Source

pub fn new() -> Result<Self, Error>

Creates a new AudioContext with the default output device and sample rate.

Source

pub fn performance_monitor(&self) -> PerformanceMonitor

Returns a PerformanceMonitor to track the audio thread’s health.

Source

pub fn sample_rate(&self) -> u32

Returns the sample rate of the audio context.

Source

pub fn build_graph<F>(&mut self, builder_func: F) -> NodeId
where F: FnOnce(&mut GraphBuilder) -> NodeId,

Provides a GraphBuilder to construct the audio processing graph.

This method takes a closure where you can add nodes and define their connections. It returns the NodeId of the final destination node in the graph.

Source

pub fn resume(&mut self, destination_id: NodeId) -> Result<(), Error>

Starts the audio processing thread and begins playback.

This method finalizes the graph construction and hands it over to the audio backend. destination_id should be the ID of the final node that outputs audio.

Source

pub fn control_sender(&self) -> Sender<ControlMessage>

Returns a Sender for sending control messages (non-blocking)

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>,