MultiChannelDemo2.cs
4.7 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiChannelDemo2 : MonoBehaviour
{
public GUISkin _guiSkin;
public Texture2D _speaker;
public Material _material;
public int _numChannels = 8;
private int _seed = 0;
private Vector2[] _speakerPositions;
private bool _normalise;
private float _falloff = 8.0f;
private int _totalUserCount;
private List<MultiChannelDemo2User> _activeUsers = new List<MultiChannelDemo2User>(16);
//private Dictionary<string, int> _loadedSounds = new Dictionary<string, int>(16);
private Texture2D _falloffTexture;
public int NumChannels
{
get { return _numChannels; }
}
void Start()
{
#if UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 && !UNITY_5_3
Random.InitState(0x1235431 + _seed);
#else
Random.seed = 0x1235431 + _seed;
#endif
_speakerPositions = new Vector2[_numChannels];
UpdateSpeakerPositions();
_falloffTexture = new Texture2D(128, 1, TextureFormat.ARGB32, false);
_falloffTexture.filterMode = FilterMode.Bilinear;
_falloffTexture.wrapMode = TextureWrapMode.Clamp;
UpdateFalloffTexture();
}
private void UpdateFalloffTexture()
{
for (int i = 0; i < _falloffTexture.width; i++)
{
float t = i / (float)(_falloffTexture.width - 1);
float l = GetLevel(t);
_falloffTexture.SetPixel(i, 1, Color.white * l);
}
_falloffTexture.Apply(false, false);
}
void Update()
{
UpdateFalloffTexture();
}
public void DrawFalloff(Vector2 position)
{
_material.mainTexture = _falloffTexture;
_material.SetPass(0);
Matrix4x4 m = Matrix4x4.TRS(position, Quaternion.identity, Vector3.one);
GL.PushMatrix();
GL.LoadPixelMatrix();
GL.MultMatrix(m);
GL.Begin(GL.TRIANGLES);
int numSegments = 36;
float angle = 0.0f;
float angleStep = (Mathf.PI * 2.0f) / numSegments;
for (int i = 0; i < numSegments; i++)
{
float x = Mathf.Sin(angle);
float y = Mathf.Cos(angle);
x *= Screen.width;
y *= Screen.height;
y *= 1.777f;
float z = 0.5f;
GL.TexCoord2(0, 0);
GL.Vertex3(0, 0, z);
GL.TexCoord2(1, 0);
GL.Vertex3(x, y, z);
angle += angleStep;
x = Mathf.Sin(angle);
y = Mathf.Cos(angle);
x *= Screen.width;
y *= Screen.height;
y *= 1.777f;
GL.TexCoord2(1, 0);
GL.Vertex3(x, y, z);
}
GL.End();
GL.PopMatrix();
}
public float GetLevel(float d)
{
return 1.0f - Mathf.Clamp01(d * _falloff);
}
public void UpdateAudioMatrix(Vector2 userPosition, ref float[] values)
{
float valTotal = 0f;
for (int i = 0; i < values.Length; i++)
{
Vector2 sp = _speakerPositions[i];
sp.x /= Screen.width;
sp.y /= Screen.height;
values[i] = 1.0f - Mathf.Clamp01(Vector2.Distance(sp, userPosition) * _falloff);
valTotal += values[i];
}
if (_normalise && valTotal > 0.0f)
{
for (int i = 0; i < values.Length; i++)
{
values[i] /= valTotal;
}
}
}
void UpdateSpeakerPositions()
{
if (_seed == 0)
{
for (int i = 0; i < _speakerPositions.Length; i++)
{
_speakerPositions[i] = new Vector2(_speaker.width/2 + (i*(Screen.width - _speaker.width) / _speakerPositions.Length), Screen.height / 2);
}
}
else
{
for (int i = 0; i < _speakerPositions.Length; i++)
{
_speakerPositions[i] = new Vector2(Random.Range(_speaker.width / 2, Screen.width - _speaker.width / 2), Random.Range(_speaker.height/2, Screen.height - _speaker.height/2));
}
}
}
private void CreateUser()
{
GameObject go = new GameObject("User" + _totalUserCount++);
MultiChannelDemo2User user = go.AddComponent<MultiChannelDemo2User>();
user.name = go.name;
user._guiSkin = _guiSkin;
user._parent = this;
_activeUsers.Add(user);
}
void OnGUI()
{
GUI.skin = _guiSkin;
for (int i = 0; i < _speakerPositions.Length; i++)
{
Rect r = new Rect(_speakerPositions[i].x - _speaker.width / 2, _speakerPositions[i].y - _speaker.height / 2, _speaker.width, _speaker.height);
GUI.DrawTexture(r, _speaker);
r.width = 16;
r.y -= _speaker.height;
r.x += (_speaker.width / 2) - (r.width / 2);
GUI.Label(r, i.ToString());
}
GUILayout.BeginVertical("box");
if (GUILayout.Button("Create Instance"))
{
CreateUser();
}
GUILayout.BeginHorizontal();
GUILayout.Label("Speaker Layout:");
if (GUILayout.Button("Linear"))
{
_seed = 0;
UpdateSpeakerPositions();
}
if (GUILayout.Button("Random"))
{
_seed++;
UpdateSpeakerPositions();
}
GUILayout.EndHorizontal();
_normalise = GUILayout.Toggle(_normalise, "Normalise");
GUILayout.BeginHorizontal();
GUILayout.Label("Falloff");
_falloff = GUILayout.HorizontalSlider(_falloff, 1.0f, 20f);
GUILayout.Label(_falloff.ToString("F1"));
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
}