-
Notifications
You must be signed in to change notification settings - Fork 3
/
VoiceNetworker.cs
113 lines (104 loc) · 3.78 KB
/
VoiceNetworker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using Unity.Netcode;
using UnityEngine;
namespace ProximityChat
{
/// <summary>
/// Networks voice audio -- recording, encoding and sending it over the network if owner,
/// otherwise receiving, decoding and playing it back as 3D spatial audio.
/// </summary>
public class VoiceNetworker : NetworkBehaviour
{
[Header("Recorder")]
[SerializeField] private VoiceRecorder _voiceRecorder;
[Header("Emitter")]
[SerializeField] private VoiceEmitter _voiceEmitter;
[Header("Debug")]
[SerializeField] private bool _playbackOwnVoice;
// Encode/decode
private VoiceEncoder _voiceEncoder;
private VoiceDecoder _voiceDecoder;
void Start()
{
// Owner should record voice and encode it
if (IsOwner)
{
// Disable voice emitter
_voiceEmitter.enabled = _playbackOwnVoice;
// Initialize voice recorder
_voiceRecorder.Init();
// Initialize voice encoder
_voiceEncoder = new VoiceEncoder(_voiceRecorder.RecordedSamplesQueue);
}
// Non-owners should receive encoded voice,
// decode it and play it as audio
if (!IsOwner || _playbackOwnVoice)
{
// Disable voice recorder
_voiceRecorder.enabled = _playbackOwnVoice;
// Initialize voice decoder
_voiceDecoder = new VoiceDecoder();
// Initialize voice emitter
_voiceEmitter.Init(VoiceConsts.OpusSampleRate);
}
}
[ServerRpc]
public void SendEncodedVoiceServerRpc(byte[] encodedVoiceData)
{
SendEncodedVoiceClientRpc(encodedVoiceData);
}
[ClientRpc]
public void SendEncodedVoiceClientRpc(byte[] encodedVoiceData)
{
if (!IsOwner || _playbackOwnVoice)
{
Span<short> decodedVoiceSamples = _voiceDecoder.DecodeVoiceSamples(encodedVoiceData);
_voiceEmitter.EnqueueSamplesForPlayback(decodedVoiceSamples);
}
}
/// <summary>
/// Starts recording and sending voice data over the network.
/// </summary>
public void StartRecording()
{
if (!IsOwner) return;
_voiceRecorder.StartRecording();
}
/// <summary>
/// Stops recording and sending voice data over the network.
/// </summary>
public void StopRecording()
{
if (!IsOwner) return;
_voiceRecorder.StopRecording();
}
/// <summary>
/// Sets the output volume of the voice emitter.
/// </summary>
/// <param name="volume">Volume from 0 to 1</param>
public void SetOutputVolume(float volume)
{
if (IsOwner && !_playbackOwnVoice) return;
_voiceEmitter.SetVolume(volume);
}
void LateUpdate()
{
if (IsOwner)
{
// Encode as much queued voice as possible
while (_voiceEncoder.HasVoiceLeftToEncode)
{
Span<byte> encodedVoice = _voiceEncoder.GetEncodedVoice();
SendEncodedVoiceServerRpc(encodedVoice.ToArray());
}
// If we've stopped recording but there's still more left to be cleared,
// force encode it with silence
if (!_voiceRecorder.IsRecording && !_voiceEncoder.QueueIsEmpty)
{
Span<byte> encodedVoice = _voiceEncoder.GetEncodedVoice(true);
SendEncodedVoiceServerRpc(encodedVoice.ToArray());
}
}
}
}
}