Unity-WebSocket/README.md
2025-06-28 11:34:19 +03:30

71 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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 bandpass filter with adjustable `lowFreq` and `highFreq`.
- ✅ 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+)
- [NuGetForUnity](https://github.com/GlitchEnzo/NuGetForUnity) plugin for package management
- A WebSocket server (see below)
---
## 📦 Dependencies (via NuGet)
### 1. Install NuGetForUnity
1. Download the latest `.unitypackage` from [NuGetForUnity Releases](https://github.com/GlitchEnzo/NuGetForUnity/releases)
2. Import it into your Unity project (`Assets → Import Package → Custom Package`)
3. A new **NuGet** menu will appear in the Unity toolbar.
### 2. Install Required Packages
Use the **NuGet → Manage NuGet Packages** menu and search for:
- `Concentus` (e.g., `Concentus.Oggfile` or `Concentus` by `lostromb`)
Once installed, Unity will generate `.csproj` references for you.
> ✅ No need to manually copy DLLs!
---
## Project Structure
### 🎙️ `OpusMicRecorder.cs`
Captures microphone audio, filters it, and encodes using Opus.
### 📤 `OpusVoiceSender.cs`
Sends encoded voice packets to a WebSocket server.
### 📥 `OpusVoiceReceiverWithBuffer.cs`
Receives Opus packets, decodes them, and streams via `AudioSource`.
---
## 🔌 WebSocket Server
You can use a simple WebSocket server for voice routing. Heres a Python example:
```bash
pip install websockets