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:- Device Check: Verifies the microphone device is still recording
- Auto-Reconnect: If enabled and microphone is not recording, attempts to restart it
- Position Tracking: Gets the current microphone position and calculates sample difference
- Data Retrieval: Extracts audio samples from the AudioClip using
GetData() - Buffer Update: Enqueues the collected samples into the input buffer
- 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
Whenm_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’sMicrophone.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.