mirror of
https://github.com/Dadechin/Unity-WebSocket.git
synced 2025-07-03 03:44:34 +00:00
73 lines
1.9 KiB
Markdown
73 lines
1.9 KiB
Markdown
# Unity Socket Voice Chat Example 🎙️
|
|
|
|
This Unity project demonstrates a real-time voice chat system using:
|
|
|
|
- 🎧 **Microphone input with Opus codec (via Concentus)**
|
|
- 🔊 **Dynamic bandpass audio filtering**
|
|
- 📡 **WebSocket communication (via WebSocketSharp)**
|
|
- 🎮 **Unity AudioSource streaming with a circular buffer**
|
|
|
|
---
|
|
|
|
## Features
|
|
|
|
- ✅ Record microphone audio in real time.
|
|
- ✅ Apply a tunable bandpass filter to remove noise and improve voice clarity.
|
|
- ✅ Encode audio using Opus codec for low-latency transmission.
|
|
- ✅ Transmit voice data over a WebSocket connection.
|
|
- ✅ Decode and playback received voice with a continuous audio stream.
|
|
|
|
---
|
|
|
|
## Setup
|
|
|
|
### 🔧 Requirements
|
|
|
|
- Unity 2020.3 or newer (tested on Unity 2021+)
|
|
- [WebSocketSharp](https://github.com/sta/websocket-sharp) library
|
|
- [Concentus Opus codec for C#](https://github.com/lostromb/concentus)
|
|
- A WebSocket server (you can use a basic Python or Node.js echo server for testing)
|
|
|
|
---
|
|
|
|
## Project Structure
|
|
|
|
### 🎙️ `OpusMicRecorder.cs`
|
|
|
|
- Captures microphone audio.
|
|
- Applies a bandpass filter with adjustable `lowFreq` and `highFreq`.
|
|
- Encodes audio using Opus (20ms frame).
|
|
- Sends encoded data through `OnEncodedAudio` event.
|
|
|
|
### 📤 `OpusVoiceSender.cs`
|
|
|
|
- Connects to a WebSocket server.
|
|
- Listens for encoded audio from `OpusMicRecorder`.
|
|
- Sends voice packets via WebSocket.
|
|
|
|
### 📥 `OpusVoiceReceiverWithBuffer.cs`
|
|
|
|
- Connects to the same WebSocket server.
|
|
- Receives encoded Opus packets.
|
|
- Decodes them into PCM audio.
|
|
- Buffers the audio into a ring buffer for continuous playback using an `AudioSource`.
|
|
|
|
---
|
|
|
|
## Usage Instructions
|
|
|
|
### 1. Add Required Libraries
|
|
|
|
- Import `WebSocketSharp.dll` into your Unity project.
|
|
- Add the `Concentus` library (via Unity Package Manager or DLL).
|
|
|
|
### 2. Setup Scene
|
|
|
|
- Create an empty GameObject in your scene.
|
|
- Attach:
|
|
- `OpusMicRecorder`
|
|
- `OpusVoiceSender`
|
|
- `OpusVoiceReceiverWithBuffer`
|
|
|
|
|