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 mut analyser = context.create_analyser();
analyser.connect(&context.destination());
let mut 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
cd showcase/mic_playback && cargo run --release
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(&mut self, fft_size: usize)
pub fn set_fft_size(&mut 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(&mut self, value: f64)
pub fn set_smoothing_time_constant(&mut 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(&mut self, value: f64)
pub fn set_min_decibels(&mut 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(&mut self, value: f64)
pub fn set_max_decibels(&mut 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(&mut self, buffer: &mut [f32])
pub fn get_float_time_domain_data(&mut 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(&mut self, buffer: &mut [u8])
pub fn get_byte_time_domain_data(&mut 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(&mut self, buffer: &mut [f32])
pub fn get_float_frequency_data(&mut 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(&mut self, buffer: &mut [u8])
pub fn get_byte_frequency_data(&mut 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
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