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
impl AudioContext
Sourcepub fn new() -> Result<Self, Error>
pub fn new() -> Result<Self, Error>
Creates a new AudioContext with the default output device and sample rate.
Sourcepub fn performance_monitor(&self) -> PerformanceMonitor
pub fn performance_monitor(&self) -> PerformanceMonitor
Returns a PerformanceMonitor to track the audio thread’s health.
Sourcepub fn sample_rate(&self) -> u32
pub fn sample_rate(&self) -> u32
Returns the sample rate of the audio context.
Sourcepub fn build_graph<F>(&mut self, builder_func: F) -> NodeId
pub fn build_graph<F>(&mut self, builder_func: F) -> 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.
Sourcepub fn resume(&mut self, destination_id: NodeId) -> Result<(), Error>
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.
Sourcepub fn control_sender(&self) -> Sender<ControlMessage>
pub fn control_sender(&self) -> Sender<ControlMessage>
Returns a Sender for sending control messages (non-blocking)