-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioReceive.cs
110 lines (89 loc) · 2.88 KB
/
AudioReceive.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
/*
This file is part of the OpenIMPRESS project.
OpenIMPRESS is free software: you can redistribute it and/or modify
it under the terms of the Lesser GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenIMPRESS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with OpenIMPRESS. If not, see <https://www.gnu.org/licenses/>.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using oi.core.network;
namespace oi.plugin.audio {
// Parse & play audio stream from network
[RequireComponent(typeof(UDPConnector))]
[RequireComponent(typeof(AudioSource))]
public class AudioReceive : MonoBehaviour {
AudioSource aud;
private int clipFreq = -1;
private int clipChan = -1;
private int clipLen = 441000;
private int lastSamplePos = 0;
private UDPConnector oiudp;
void Start () {
aud = GetComponent<AudioSource>();
oiudp = GetComponent<UDPConnector>();
aud.loop = true;
}
void Update () {
ParseData(oiudp);
UpdatePlayer();
}
void UpdatePlayer() {
if (!aud.isPlaying) {
return;
}
if (aud.timeSamples > lastSamplePos && aud.timeSamples - lastSamplePos < 10000) {
aud.Pause();
}
}
private void ParseData(UDPConnector udpSource) {
OIMSG msg = udpSource.GetNewData();
int packetsThisFrame = 0;
while (msg != null && msg.data != null && msg.data.Length > 0) {
// Make sure there is data in the stream.
packetsThisFrame++;
int packetID = -1;
using (MemoryStream str = new MemoryStream(msg.data)) {
using (BinaryReader reader = new BinaryReader(str)) {
packetID = reader.ReadInt32();
}
}
if (packetID == 7) { // audio packet
float[] samples;
int freq;
int chan;
AudioSerializer.Deserialize(msg.data, out samples, out freq, out chan);
if (freq != -1 && chan != -1) {
if (clipFreq != freq || clipChan != chan) {
clipFreq = freq;
clipChan = chan;
aud.clip = AudioClip.Create("RemoteAudio",
clipLen, clipChan, freq, false);
lastSamplePos = 0;
aud.timeSamples = 0;
}
aud.clip.SetData(samples, lastSamplePos);
if (lastSamplePos > aud.timeSamples + 10000 ||
(lastSamplePos < aud.timeSamples && lastSamplePos > 10000
&& aud.timeSamples < clipLen - 10000)) {
aud.timeSamples = lastSamplePos;
}
if (!aud.isPlaying) aud.Play();
lastSamplePos += samples.Length;
if (lastSamplePos >= clipLen)
lastSamplePos -= clipLen;
}
}
msg = udpSource.GetNewData();
}
}
}
}