Skip to main content
Overview > Audio Collect Module Class: AudioCollectModule | Inherits from: InworldAudioModule | Implements: ICollectAudioHandler Audio sampling module responsible for collecting audio data from the microphone input buffer. This module interfaces with Unity’s microphone system to continuously capture audio frames and populate the input buffer for further processing by other audio modules.
Note: This module is not available on Unity WebGL builds due to microphone access limitations.

Properties

Methods

Reference

m_AutoReconnect

Determines whether the module should automatically attempt to reconnect the microphone if it becomes disconnected. When true, the module will automatically restart the microphone if recording stops unexpectedly.

Returns

Type: bool

OnCollectAudio

Collects audio data from the microphone for the current frame and updates the input buffer. Called by the AudioManager’s audio processing coroutine to capture approximately 0.1 seconds of audio data. Uses Unity’s microphone API to retrieve the latest audio samples.

Returns

Type: int Description: The number of audio samples collected, or -1 if collection failed.

ResetPointer

Resets the pointer of the Audio Buffer to the beginning.

Returns

Type: void

Audio Collection Process

The AudioCollectModule performs the following steps during audio collection:
  1. Device Check: Verifies the microphone device is still recording
  2. Auto-Reconnect: If enabled and microphone is not recording, attempts to restart it
  3. Position Tracking: Gets the current microphone position and calculates sample difference
  4. Data Retrieval: Extracts audio samples from the AudioClip using GetData()
  5. Buffer Update: Enqueues the collected samples into the input buffer
  6. Position Update: Updates the last position for the next collection cycle

Sample Collection Details

  • Collection Frequency: Called approximately every 0.1 seconds by the audio coroutine
  • Sample Size: Collects the difference between current and last microphone position
  • Buffer Management: Uses circular buffer to handle continuous audio streaming
  • Error Handling: Returns -1 if collection fails at any step

Important Notes

WebGL Compatibility

This module is not available on Unity WebGL builds due to microphone access limitations. The code is wrapped in #if !UNITY_WEBGL preprocessor directives.

Auto-Reconnect Feature

When m_AutoReconnect is enabled (default), the module will automatically attempt to restart the microphone if it detects that recording has stopped. This helps maintain continuous audio capture in case of temporary microphone disconnections.

Position Tracking

The module uses Unity’s Microphone.GetPosition() to track the current read position in the audio buffer. It handles buffer wraparound by checking if the current position is less than the last position, indicating the buffer has looped.

Integration with Audio Pipeline

This module is called by the InworldAudioManager.CollectAudio() method during the audio processing pipeline, specifically between pre-processing and post-processing stages.