Struct web_audio_api::media::Microphone
source · pub struct Microphone { /* private fields */ }
Expand description
Microphone input stream
The Microphone can set up a MediaStream
value which can be used
inside a MediaStreamAudioSourceNode
.
It is okay for the Microphone struct to go out of scope, any corresponding stream will still be
kept alive and emit audio buffers. Call the close()
method if you want to stop the microphone
input and release all system resources.
Warning
This abstraction is not part of the Web Audio API and does not aim at implementing the full MediaDevices API. It is only provided for convenience reasons.
Example
use web_audio_api::context::{BaseAudioContext, AudioContext};
use web_audio_api::context::{AudioContextLatencyCategory, AudioContextOptions};
use web_audio_api::media::Microphone;
use web_audio_api::node::AudioNode;
let context = AudioContext::default();
// Request an input sample rate of 44.1 kHz and default latency (buffer size 128, if available)
let opts = AudioContextOptions {
sample_rate: Some(44100.),
..AudioContextOptions::default()
};
let mic = Microphone::new(opts);
// or you can create Microphone with default options
// let stream = Microphone::default();
// register as media element in the audio context
let background = context.create_media_stream_source(mic.stream());
// connect the node directly to the destination node (speakers)
background.connect(&context.destination());
// enjoy listening
std::thread::sleep(std::time::Duration::from_secs(4));
Implementations
sourceimpl Microphone
impl Microphone
sourcepub fn new(options: AudioContextOptions) -> Self
pub fn new(options: AudioContextOptions) -> Self
Setup the default microphone input stream
Note: the specified latency_hint
is currently ignored, follow our progress at
https://github.com/orottier/web-audio-api-rs/issues/51
sourcepub fn suspend(&self)
pub fn suspend(&self)
Suspends the input stream, temporarily halting audio hardware access and reducing CPU/battery usage in the process.
Panics
Will panic if:
- The input device is not available
- For a
BackendSpecificError
sourcepub fn resume(&self)
pub fn resume(&self)
Resumes the input stream that has previously been suspended/paused.
Panics
Will panic if:
- The input device is not available
- For a
BackendSpecificError
sourcepub fn close(self)
pub fn close(self)
Closes the microphone input stream, releasing the system resources being used.
sourcepub fn stream(&self) -> impl MediaStream
pub fn stream(&self) -> impl MediaStream
A MediaStream
iterator producing audio buffers from the microphone input
Note that while you can call this function multiple times and poll all iterators concurrently, this could lead to unexpected behavior as the buffers will only be offered once.