Skip to content

dschwarz-tripp/tripp-unityspeex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity Speex

Cross-platform Unity native audio plugin with Opus codec encoding/decoding and SpeexDSP echo cancellation.

Features

  • Opus Encoder/Decoder: High-quality audio codec for VoIP and streaming
  • Echo Cancellation (AEC): Remove speaker echo from microphone input
  • Resampler: High-quality audio sample rate conversion
  • Cross-Platform: Android, iOS, macOS, Windows
  • Unity Package: Single UPM package for all platforms
  • C# API: Clean, IDisposable-based managed wrapper

Installation

Via Git URL (Unity Package Manager)

  1. Open Unity Package Manager (Window → Package Manager)
  2. Click "+" → "Add package from git URL"
  3. Enter: https://github.com/trippinc/unityspeex.git

Via OpenUPM

openupm add com.trippinc.unityspeex

Quick Start

Opus Encoding/Decoding

using TrippInc.UnitySpeex;

// Create encoder
var encoder = new OpusEncoder(48000, channels: 1, OpusApplication.VoIP);
encoder.Bitrate = 64000;

// Encode PCM to Opus
short[] pcmSamples = GetMicrophoneAudio();
byte[] outputBuffer = new byte[4000];
int encodedBytes = encoder.Encode(pcmSamples, frameSize: 960, outputBuffer);

// Create decoder
var decoder = new OpusDecoder(48000, channels: 1);

// Decode Opus to PCM
short[] decodedSamples = new short[960];
int decodedCount = decoder.Decode(opusPacket, decodedSamples, frameSize: 960);

// Don't forget to dispose!
encoder.Dispose();
decoder.Dispose();

Echo Cancellation

using TrippInc.UnitySpeex;

// Create echo canceller
// frameSize: samples per frame (160 = 10ms at 16kHz)
// filterLengthMs: echo tail length (typically 100-200ms)
var aec = new EchoCanceller(frameSize: 160, filterLengthMs: 100, sampleRate: 16000);

// Process audio frame
short[] micInput = GetMicrophoneFrame();      // Has echo from speaker
short[] speakerOutput = GetSpeakerFrame();    // What was played
short[] cleanOutput = new short[160];

aec.Process(micInput, speakerOutput, cleanOutput);
// cleanOutput now has echo removed

aec.Dispose();

Building from Source

Prerequisites

  • CMake 3.16+
  • For Android: Android NDK r26d+
  • For iOS/macOS: Xcode 14+
  • For Windows: Visual Studio 2019+ with C++ workload

Build Commands

# macOS (Universal binary)
./scripts/build-macos.sh

# Android (arm64-v8a, armeabi-v7a, x86_64)
./scripts/build-android.sh

# iOS (device + simulator)
./scripts/build-ios.sh

# Windows (x64)
./scripts/build-windows.ps1

Build All (macOS host)

./scripts/build-all.sh

API Reference

OpusEncoder

Method Description
Encode(short[], frameSize, byte[]) Encode 16-bit PCM to Opus
Encode(float[], frameSize, byte[]) Encode float PCM to Opus
Bitrate Get/set bitrate (bps)
Complexity Set encoder complexity (0-10)
VBR Enable/disable variable bitrate

OpusDecoder

Method Description
Decode(byte[], short[], frameSize) Decode Opus to 16-bit PCM
Decode(byte[], float[], frameSize) Decode Opus to float PCM
DecodePLC(short[], frameSize) Packet loss concealment

EchoCanceller

Method Description
Process(short[], short[], short[]) Cancel echo from mic input
Process(float[], float[], float[]) Float version
Reset() Reset AEC state

Frame Sizes

Opus supports specific frame sizes based on sample rate:

Sample Rate Valid Frame Sizes (samples)
48000 Hz 120, 240, 480, 960, 1920, 2880
24000 Hz 60, 120, 240, 480, 960, 1440
16000 Hz 40, 80, 160, 320, 640, 960

Common choices:

  • VoIP: 20ms frames (960 samples @ 48kHz)
  • Low latency: 10ms frames (480 samples @ 48kHz)

Platform Notes

iOS

  • Uses static linking (__Internal)
  • Requires bitcode disabled in Xcode project

Android

  • Supports arm64-v8a, armeabi-v7a, x86_64
  • Minimum API level: 24

macOS

  • Universal binary (arm64 + x86_64)
  • Minimum deployment target: 10.15

Windows

  • x64 only
  • Requires Visual C++ Redistributable

License

  • Opus: BSD-3-Clause
  • SpeexDSP: BSD-3-Clause
  • This wrapper: MIT

About

Wrapper for libopus and speexdsp for Unity.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors