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

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

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

Resumes the input stream that has previously been suspended/paused.

Panics

Will panic if:

  • The input device is not available
  • For a BackendSpecificError

Closes the microphone input stream, releasing the system resources being used.

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.

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.