MetaVoiceChat

repository·main·Indexed 41 days ago

https://github.com/metater/metavoicechat

A modular, high-performance voice chat solution for Unity games featuring low-latency, Opus-based audio communication. It supports integration with networking libraries including Mirror, FishNet, Netcode for GameObjects, Netick, PurrNet, and Riptide. Key features include zero garbage runtime memory usage, dynamic buffer latency compensation, and support for custom audio input/output filters.

Tokens
2.2K
Snippets
0
Records
12
Agent score
3%

What's inside MetaVoiceChat

  1. Core technical specifications and features

    main

    MetaVoiceChat is a high-performance, modular audio system for Unity with the following characteristics:

    Audio Specifications

    • Sampling Frequency: 48kHz
    • Bandwidth: Wideband Opus (SILK mode)
    • Channels: Single audio channel (Mono)
    • Bit Depth: 16-bit audio
    • Loop Time: 1 second input and output audio clip loop
    • Average Latency: ~200 ms with default settings

    Key Features

    • Zero Garbage: No memory garbage is created at runtime due to the use of pooled data buffers.
    • Latency Compensation: Uses a dynamic buffer latency compensation system (P-controller) that adjusts based on RMS jitter, sender/host/receiver FPS, and server FPS.
    • Microphone Management:
      • Automatic reconnection via VcMicAudioInput.SetSelectedDevice(string device).
      • VcMicAudioInput.OnActiveDeviceChanged event for monitoring device changes.
    • Reactive Properties: Provides events for Speaking, Deafening yourself, Input muting yourself, and Output muting others.
  2. Supported Networking Solutions

    main

    MetaVoiceChat is designed to work with existing networking libraries. It currently supports the following Unity networking solutions:

    • Mirror: Open-source Unity networking library.
    • Netcode for GameObjects: Unity's official high-level netcode solution.
    • FishNet: Unity Networking Evolved.
    • Netick: C# networking solution for Unity.
    • PurrNet: A Purrfect Unity networking solution.
    • Riptide: Lightweight C# networking for multiplayer games.

    Extending Support: You can implement support for other Unity networking solutions by composing an agnostic MonoBehaviour and implementing a minimal interface. The project encourages community contributions for new network provider implementations.

  3. Use OnAudioFilterReadVcOutput for improved playback (v4.3+)

    main

    As of v4.3, it is recommended to use OnAudioFilterReadVcOutput instead of VcAudioSourceOutput to fix playback bugs.

    Setup Steps:

    1. Delete the VcAudioSourceOutput component.
    2. Add OnAudioFilterReadVcOutput directly to the Audio Source used for the voice chat mouth.
    3. Assign the Meta Vc component to the OnAudioFilterReadVcOutput field.
    4. Click the nipple on Audio Filter Read Config and assign the default VC config.
    5. Crucial: Assign this new config as the Audio Output under the Meta Vc component.
  4. Set up MetaVoiceChat for networked voice chat

    main

    To implement networked voice chat in Unity, follow these steps to configure your networked player object:

    1. Create a Voice Chat Container: Create a new GameObject (e.g., named "Voice Chat") as a child of your networked player object.
    2. Add Core Components:
      • Add the MetaVc component.
      • Add an audio input component (e.g., VcMicAudioInput).
      • Add an audio output component (e.g., VcAudioSourceOutput).
      • Add a network provider component (e.g., MirrorNetProvider, NgoNetProvider, FishNetNetProvider, or NetickNetProvider).
    3. Connect Components:
      • Link the input and output components to their respective fields on the MetaVc component.
      • Link the network provider to the MetaVc component.
    4. Configure Performance:
      • Adjust the Complexity field based on your user count/device capabilities.
      • Use Max Codec Milliseconds to manage complexity. Set this higher than your desired frame period during testing to avoid spamming warnings, then increase it to the maximum to disable it.
      • Enable Allow Multiple Codec Warnings Per Frame to see total frame time used by the codec during warnings.
  5. Configure Audio Output for Voice Chat

    main

    To play the received voice chat audio, you need a dedicated Audio Source object (e.g., named "Voice Chat Output") positioned at the player's mouth.

    Standard Setup (VcAudioSourceOutput)

    1. Create an Audio Source GameObject as a child of the networked player.
    2. Configure Audio Source Settings:
      • Spacial Blend: Set to 1 for 3D proximity chat, or 0 for standard 2D voice chat.
      • 3D Sound Settings: Set Max Distance (e.g., 50 meters) and ensure the volume roll-off curve ends at zero.
      • Output: Assign a voice chat mixer group if desired.
      • Note: Play On Awake and Loop are managed internally.
    3. Connect Components:
      • Connect the Audio Source to the Audio Source field in the VcAudioSourceOutput component.
      • Link the VcAudioSourceOutput and MetaVc fields to the sibling MetaVc component.
  6. Optimize MetaVoiceChat for Latency and Performance

    main

    Audio Settings

    • DSP Buffer Size: In Unity Project Settings > Audio, change DSP Buffer Size from Best performance to Best latency.

    Frame Size Selection

    Adjust the frame size based on your target hardware and network:

    • 10 ms: Lowest latency; best for high-end devices and high network send rates.
    • 20 ms: A good balance for most use cases.
    • 40 ms: Optimized for lower-end devices and poor networks; increases ear-to-ear latency and can worsen quality during packet loss.

    Jitter Buffer

    If you experience crackling on poor networks or low-end hardware, you can modify the VC config scriptable object to switch Jitter Buffer Mode from Low Latency to Balanced.

  7. Integrate RNNoise4Unity for Noise Suppression

    main

    To use real-time noise suppression, follow these steps:

    1. Install RNNoise4Unity via its scoped registry.
    2. In RnnoiseVcInputFilter.cs, uncomment the #define ENABLE_RNNOISE_FOR_META_VOICE_CHAT symbol.
    3. Add the RnnoiseVcInputFilter component to your audio input GameObject and configure it as shown in the project documentation.
  8. Configure audio pipeline and jitter settings

    main

    Advanced audio tuning is available through the following configuration categories:

    General Settings

    • Jitter calculation time window size: Duration in seconds.
    • Jitter mean time offset calculation window size: Measured in updates.
    • Output min buffer size: Measured in fractional frames.

    Audio Pipeline (Filters)

    The pipeline is modular and allows for input and output filtering:

    • VcAudioInput: Supports a First input filter (nullable).
    • VcInputFilter: Supports a Next input filter (nullable).
    • VcAudioOutput: Supports a First output filter (nullable).
    • VcOutputFilter: Supports a Next output filter (nullable).

    VcAudioSourceOutput Settings

    Used for managing audio source playback and latency compensation:

    • Audio source: The Unity Audio Source to use.
    • Buffer frame lifetime: Duration in seconds.
    • Buffer max negative latency: Duration in seconds.
    • Pitch proportional gain: Percent per second of latency error.
    • Pitch max correction: Maximum allowed correction in percent.
  9. Configure Opus codec settings

    main

    MetaVoiceChat exposes several Opus settings to tune audio quality and performance. The default values are indicated in bold in the documentation.

    SettingOptions / Range
    ApplicationVOIP (default), Audio, or Restricted Low-Delay
    Complexity0 to 10 (default: 10)
    Frame size10 ms, 20 ms (default), or 40 ms
    SignalVoice (default), Music, or Auto

    Note: Higher complexity provides better quality at the cost of CPU usage.

  10. Compatibility notice for Unity 2021 and older

    main

    MetaVoiceChat requires the System.Memory namespace. If your Unity version does not include this namespace, you must use the legacy branch of MetaVoiceChat.

    • Unity 2021: Try the main branch first. If you encounter errors, switch to the legacy branch.
    • Unity 2020 and earlier: You must use the legacy branch.

    Legacy branch: https://github.com/Metater/MetaVoiceChat/tree/legacy

  11. Implement custom VcAudioInput, VcAudioOutput, and Filters

    main

    You can extend the functionality of MetaVoiceChat by implementing the following interfaces:

    Custom Audio Input (VcAudioInput)

    Extend VcAudioInput and call SendAndFilterFrame(int index, float[] samples) whenever audio data is ready.

    • index: Starts at zero and never resets.
    • samples: Pass the audio data array. Pass null when not sending audio data to maintain timing via empty heartbeats.
    • Reference: VcMicAudioInput.cs

    Custom Audio Output (VcAudioOutput)

    Extend VcAudioOutput and implement ReceiveFrame(int index, float[] samples, float targetLatency).

    • Reference: VcAudioSourceOutput.cs

    Custom Input Filter (VcInputFilter)

    Extend VcInputFilter and implement Filter(int index, ref float[] samples).

    • To stop the pipeline (e.g., for Push-To-Talk), set samples to null when disabled.

    Custom Output Filter (VcOutputFilter)

    Extend VcOutputFilter and implement Filter(int index, float[] samples, float targetLatency).

    • Directly modify the samples array to apply effects (e.g., for mouth movement visualization).