Struct web_audio_api::node::AnalyserNode
source · pub struct AnalyserNode { /* private fields */ }
Expand description
AnalyserNode
represents a node able to provide real-time frequency and
time-domain analysis information.
It is an AudioNode that passes the audio stream unchanged from the input to the output, but allows you to take the generated data, process it, and create audio visualizations..
- MDN documentation: https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode
- specification: https://webaudio.github.io/web-audio-api/#AnalyserNode
- see also:
BaseAudioContext::create_analyser
Usage
use web_audio_api::context::{BaseAudioContext, AudioContext};
use web_audio_api::node::{AudioNode, AudioScheduledSourceNode};
let context = AudioContext::default();
let analyser = context.create_analyser();
analyser.connect(&context.destination());
let osc = context.create_oscillator();
osc.frequency().set_value(200.);
osc.connect(&analyser);
osc.start();
let mut bins = vec![0.; analyser.frequency_bin_count()];
loop {
analyser.get_float_frequency_data(&mut bins);
println!("{:?}", &bins[0..20]); // print 20 first bins
std::thread::sleep(std::time::Duration::from_millis(1000));
}
Examples
cargo run --release --example analyser
cargo run --release --example mic_playback
Implementations§
source§impl AnalyserNode
impl AnalyserNode
pub fn new<C: BaseAudioContext>(context: &C, options: AnalyserOptions) -> Self
sourcepub fn fft_size(&self) -> usize
pub fn fft_size(&self) -> usize
The size of the FFT used for frequency-domain analysis (in sample-frames)
Panics
This method may panic if the lock to the inner analyser is poisoned
sourcepub fn set_fft_size(&self, fft_size: usize)
pub fn set_fft_size(&self, fft_size: usize)
Set FFT size
Panics
This function panics if fft_size is not a power of two or not in the range [32, 32768]
sourcepub fn smoothing_time_constant(&self) -> f64
pub fn smoothing_time_constant(&self) -> f64
Time averaging parameter with the last analysis frame. A value from 0 -> 1 where 0 represents no time averaging with the last analysis frame. The default value is 0.8.
Panics
This method may panic if the lock to the inner analyser is poisoned
sourcepub fn set_smoothing_time_constant(&self, value: f64)
pub fn set_smoothing_time_constant(&self, value: f64)
Set smoothing time constant
Panics
This function panics if the value is set to a value less than 0 or more than 1.
sourcepub fn min_decibels(&self) -> f64
pub fn min_decibels(&self) -> f64
Minimum power value in the scaling range for the FFT analysis data for conversion to unsigned byte values. The default value is -100.
Panics
This method may panic if the lock to the inner analyser is poisoned
sourcepub fn set_min_decibels(&self, value: f64)
pub fn set_min_decibels(&self, value: f64)
Set min decibels
Panics
This function panics if the value is set to a value more than or equal to max decibels.
sourcepub fn max_decibels(&self) -> f64
pub fn max_decibels(&self) -> f64
Maximum power value in the scaling range for the FFT analysis data for conversion to unsigned byte values. The default value is -30.
Panics
This method may panic if the lock to the inner analyser is poisoned
sourcepub fn set_max_decibels(&self, value: f64)
pub fn set_max_decibels(&self, value: f64)
Set max decibels
Panics
This function panics if the value is set to a value less than or equal to min decibels.
sourcepub fn frequency_bin_count(&self) -> usize
pub fn frequency_bin_count(&self) -> usize
Number of bins in the FFT results, is half the FFT size
Panics
This method may panic if the lock to the inner analyser is poisoned
sourcepub fn get_float_time_domain_data(&self, buffer: &mut [f32])
pub fn get_float_time_domain_data(&self, buffer: &mut [f32])
Copy the current time domain data as f32 values into the provided buffer
Panics
This method may panic if the lock to the inner analyser is poisoned
sourcepub fn get_byte_time_domain_data(&self, buffer: &mut [u8])
pub fn get_byte_time_domain_data(&self, buffer: &mut [u8])
Copy the current time domain data as u8 values into the provided buffer
Panics
This method may panic if the lock to the inner analyser is poisoned
sourcepub fn get_float_frequency_data(&self, buffer: &mut [f32])
pub fn get_float_frequency_data(&self, buffer: &mut [f32])
Copy the current frequency data into the provided buffer
Panics
This method may panic if the lock to the inner analyser is poisoned
sourcepub fn get_byte_frequency_data(&self, buffer: &mut [u8])
pub fn get_byte_frequency_data(&self, buffer: &mut [u8])
Copy the current frequency data scaled between min_decibels and max_decibels into the provided buffer
Panics
This method may panic if the lock to the inner analyser is poisoned
Trait Implementations§
source§impl AudioNode for AnalyserNode
impl AudioNode for AnalyserNode
fn registration(&self) -> &AudioContextRegistration
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_at<'a>(
&self,
dest: &'a dyn AudioNode,
output: usize,
input: usize
) -> &'a dyn AudioNode
fn connect_at<'a>(
&self,
dest: &'a dyn AudioNode,
output: usize,
input: usize
) -> &'a dyn AudioNode
source§fn disconnect_from<'a>(&self, dest: &'a dyn AudioNode) -> &'a dyn AudioNode
fn disconnect_from<'a>(&self, dest: &'a dyn AudioNode) -> &'a dyn AudioNode
source§fn disconnect(&self)
fn disconnect(&self)
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