VideoKitAudioManager

namespace VideoKit {
  /// <summary>
  /// Unity component for streaming microphone audio.
  /// </summary>
  class VideoKitAudioManager : MonoBehaviour { ... }
}

This components manages streaming audio buffers from an AudioDevice in a scene. The component can be added to any game object in the scene.


Configuring the Microphone

The audio manager's format settings dictate the audio format of streamed audio.

Specifying the Sample Rate

/// <summary>
/// Audio sample rate.
/// </summary>
SampleRate sampleRate { get; set; } = SampleRate.MatchUnity;

The specified sampleRate is requested on the AudioDevice when streaming is started. The following sample rates are currently supported:

/// <summary>
/// Audio sample rate.
/// </summary>
enum SampleRate : int {
  /// <summary>
  /// Match Unity's audio DSP sample rate.
  /// </summary>
  MatchUnity = 0,
  /// <summary>
  /// 8KHz.
  /// </summary>
  _8000 = 8000,
  /// <summary>
  /// 16KHz.
  /// </summary>
  _160000 = 16000,
  /// <summary>
  /// 22.05KHz.
  /// </summary>
  _22050 = 22050,
  /// <summary>
  /// 24KHz.
  /// </summary>
  _24000 = 24000,
  /// <summary>
  /// 44.1KHz.
  /// </summary>
  _44100 = 44100,
  /// <summary>
  /// 48KHz.
  /// </summary>
  _48000 = 48000,
}

Specifying the Channel Count

/// <summary>
/// Audio channel count.
/// </summary>
ChannelCount channelCount { get; set; } = ChannelCount.MatchUnity;

The specified channelCount is requested on the AudioDevice when streaming is started. The following channel counts are currently supported:

/// <summary>
/// Audio channel count.
/// </summary>
enum ChannelCount : int {
  /// <summary>
  /// Match Unity's audio DSP channel count.
  /// </summary>
  MatchUnity  = 0,
  /// <summary>
  /// Mono audio.
  /// </summary>
  _1          = 1,
  /// <summary>
  /// Stereo audio.
  /// </summary>
  _2          = 2,
}

Requesting Echo Cancellation

/// <summary>
/// Request echo cancellation if the device supports it.
/// </summary>
bool echoCancellation { get; set; }

On supported microphones, echoCancellation can be enabled to suppress echos in the streamed audio.


Streaming Audio

The audio manager allows for specifying an audio device to stream from, along with a handler to receive audio buffers:

Specifying the Audio Device

/// <summary>
/// Get or set the audio device used for streaming.
/// </summary>
AudioDevice? device { get; set; }

You can get or set the AudioDevice used for recording.

Checking the Streaming Status

/// <summary>
/// Whether the audio device is running.
/// </summary>
bool running { get; }

The running property reports whether the audio manager is currently streaming audio from the device.

Starting the Stream

/// <summary>
/// Start streaming audio.
/// </summary>
void StartRunning ();

Start streaming AudioBuffer instances from the audio device. The OnAudioBuffer event will be invoked with audio buffer instances as they become available from the audio device.

Stopping the Stream

/// <summary>
/// Stop streaming audio.
/// </summary>
void StopRunning ();

This method will stop streaming audio from the audio device.


Accessing the Audio

When streaming, the audio manager forwards AudioBuffer instances from the audio device to an event.

Listening for Audio Buffers

/// <summary>
/// Event raised when a new audio buffer is available.
/// </summary>
event Action<AudioBuffer> OnAudioBuffer { add; remove; }

Listen to this event to receive audio buffers provided by the streaming device.


Considerations on iOS

On iOS, applications have a shared audio sessions that must explicitly be configured for audio streaming. Specifically, the AVAudioSession.sharedInstance.category must be set to AVAudioSessionCategoryPlayAndRecord.

Configuring on Awake

/// <summary>
/// Configure the application audio session on awake.
/// </summary>
bool configureOnAwake { get; set; } = true;

When enabled, this will configure your app's global audio session for audio device discovery.


Was this page helpful?