NatDevice
Search…
⌃K

AudioDevice

class NatML.Devices.AudioDevice : IMediaDevice<AudioBuffer>
This class provides an abstraction for hardware microphones.

Identifying the Device

Audio devices can be identified either with a unique ID or with a device name.

Unique Identifier

/// <summary>
/// Device unique ID.
/// </summary>
string uniqueID { get; }
Refer to the Identifying the Device section of the IMediaDevice interface for more information.

Device Name

/// <summary>
/// Display friendly device name.
/// </summary>
string name { get; }
Refer to the Identifying the Device section of the IMediaDevice interface for more information.

Device Location

/// <summary>
/// Device location.
/// </summary>
DeviceLocation location { get; }
Refer to the Identifying the Device section of the IMediaDevice interface for more information.

Default Device

/// <summary>
/// Device is the default device for its media type.
/// </summary>
bool defaultForMediaType { get; }
Refer to the Identifying the Device section of the IMediaDevice interface for more information.

Specifying Echo Cancellation

/// <summary>
/// Enable or disable Adaptive Echo Cancellation (AEC)
/// </summary>
bool echoCancellation { get; set; }
Some audio devices support adaptive echo cancellation, which removes sound that originates from the device itself. This is useful for voice call applications for removing any audio feedback from the speakers.
Echo cancellation usually attenuates the volume, so the audio might be quieter than when AEC is disabled.

Echo Cancellation Support

Audio devices also report whether they support echo cancellation:
/// <summary>
/// Is echo cancellation supported?
/// </summary>
bool echoCancellationSupported { get; }

Specifying the Audio Format

Audio devices allow for inspecting the audio format that will be used to stream audio data. This format is defined by the sample rate and channel count.

Sample Rate

/// <summary>
/// Audio sample rate.
/// </summary>
int sampleRate { get; set; }
The device sample rate is specified in Hertz (Hz).
Audio devices do not support arbitrary sample rates. It is recommended to use one of 8000Hz, 16000Hz, 22050Hz, 24000Hz, 44100Hz, or 48000Hz.

Channel Count

/// <summary>
/// Audio channel count.
/// </summary>
int channelCount { get; set; }
The audio channel count. This will always be a non-negative number, typically 1 (mono) or 2 (stereo).
Audio devices are always guaranteed to support mono audio. No other channel count is guaranteed.

Checking the Device State

/// <summary>
/// Is the device running?
/// </summary>
bool running { get; }
Refer to the Checking the Device State section of the IMediaDevice interface for more information.

Streaming Audio Buffers

/// <summary>
/// Start running.
/// </summary>
/// <param name="handler">Delegate to receive audio buffers.</param>
void StartRunning (Action<AudioBuffer> handler);
Audio devices stream sample buffers to the specified delegate as they are reported by the hardware microphone. The audio buffers will always contain single-precision floating-point (float) linear PCM data, interleaved by channel (when channelCount is greater than 1).
The provided handler is always invoked on a dedicated audio recording thread, separate from the Unity main thread.

Stopping the Stream

/// <summary>
/// Stop running.
/// </summary>
void StopRunning ();
Refer to the Stopping the Stream section of the IMediaDevice interface for more information.

Handling Device Disconnection

/// <summary>
/// Event raised when the device is disconnected.
/// </summary>
event Action onDisconnected { add; remove; }
Refer to the Handling Device Disconnection section of the IMediaDevice interface for more information.
This event is not guaranteed to be raised on the Unity main thread.