-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayAudioSystem.cs
209 lines (197 loc) · 10.7 KB
/
PlayAudioSystem.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
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
208
209
using System;
using System.Collections.Generic;
using Arc.ECSAudio.Components;
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;
using static Unity.Mathematics.math;
namespace Arc.ECSAudio
{
internal struct PlayingAudioSource
{
public AudioSource AudioSource;
}
public class PlayAudioSystem : SystemBase
{
private Dictionary<Guid, PlayingAudioSource> PlayingSources = new Dictionary<Guid, PlayingAudioSource>();
private Queue<AudioSource> UnusedSources = new Queue<AudioSource>();
private GameplaySimulationCommandBufferSystem _gameplaySimulationCommandBufferSystem;
private Camera _camera;
protected override void OnCreate()
{
_gameplaySimulationCommandBufferSystem = World.GetOrCreateSystem<GameplaySimulationCommandBufferSystem>();
}
protected override void OnUpdate()
{
if (_camera == null)
{
_camera = Camera.main;
}
if (_camera == null) return;
var playingSources = PlayingSources;
var unusedSources = UnusedSources;
var audioListenerPosition = _camera.transform.position;
var commandBuffer = _gameplaySimulationCommandBufferSystem.CreateCommandBuffer();
var time = (float) Time.ElapsedTime;
Entities
.WithName("audio_process_proximity_to_listener_entities")
.ForEach((ref EcsAudioSource playAudioContinuously,
ref PlayEcsAudioSourceOnProximityToListener playAudioOnProximityToListener,
in LocalToWorld localToWorld) =>
{
if (playAudioOnProximityToListener.OneShot)
{
if (playAudioOnProximityToListener.Triggered) return;
if (!(distance(localToWorld.Position, audioListenerPosition) <
playAudioOnProximityToListener.MinProximity)) return;
playAudioContinuously.Playing = true;
playAudioContinuously.OneShot = true;
playAudioOnProximityToListener.Triggered = true;
}
else
{
playAudioContinuously.Playing = distance(localToWorld.Position, audioListenerPosition) <
playAudioOnProximityToListener.MinProximity;
playAudioContinuously.OneShot = false;
}
})
.Run();
Entities
.WithName("audio_process_entities_with_playing_state")
.WithoutBurst()
.ForEach((Entity entity,
ref EcsAudioSource playAudioContinuously,
in PlayingAudioState playAudioContinuouslyState,
in LocalToWorld localToWorld) =>
{
var audioSource = PlayingSources[playAudioContinuouslyState.AudioSourceId].AudioSource;
audioSource.transform.position = localToWorld.Position;
bool stopPlaying;
if (playAudioContinuously.OneShot)
{
if (playAudioContinuously.Playing)
{
var clip = EcsAudioStorage.RegisteredAudioClips[
playAudioContinuously.AudioData.RegisteredAudioClipGuid];
audioSource.PlayOneShot(clip);
playAudioContinuously.Playing = false;
playAudioContinuously.PlayStartTimestamp = time;
}
stopPlaying = time > playAudioContinuously.PlayStartTimestamp +
playAudioContinuously.PlayLength;
}
else
{
stopPlaying = !playAudioContinuously.Playing;
}
if (stopPlaying)
{
ReturnSourceToPool(playAudioContinuouslyState.AudioSourceId, playingSources, unusedSources);
commandBuffer.RemoveComponent<PlayingAudioState>(entity);
}
})
.Run();
Entities
.WithName("audio_process_entities_without_playing_state")
.WithoutBurst()
.WithNone<PlayingAudioState>()
.ForEach(
(Entity entity, ref EcsAudioSource playAudioContinuously, in LocalToWorld localToWorld) =>
{
if (playAudioContinuously.Playing)
{
var clip = EcsAudioStorage.RegisteredAudioClips[
playAudioContinuously.AudioData.RegisteredAudioClipGuid];
if (clip == null)
{
Debug.Log("Cannot play - null clip");
return;
}
var audioMixerGroup =
EcsAudioStorage.RegisteredAudioMixerGroups[
playAudioContinuously.AudioData.RegisteredAudioMixerGroupGuid];
var playingAudioSource = new PlayingAudioSource();
var pooledAudioSourceId = PoolSource(playingSources, unusedSources, ref playingAudioSource);
playAudioContinuously.PlayStartTimestamp = time;
playAudioContinuously.PlayLength = clip.length;
commandBuffer.AddComponent(entity, new PlayingAudioState
{
AudioSourceId = pooledAudioSourceId,
});
playingAudioSource.AudioSource.transform.position = localToWorld.Position;
playingAudioSource.AudioSource.clip = clip;
playingAudioSource.AudioSource.outputAudioMixerGroup = audioMixerGroup;
playingAudioSource.AudioSource.loop = !playAudioContinuously.OneShot;
playingAudioSource.AudioSource.pitch = playAudioContinuously.AudioData.Pitch;
playingAudioSource.AudioSource.volume = playAudioContinuously.AudioData.Volume;
playingAudioSource.AudioSource.priority = playAudioContinuously.AudioData.Priority;
playingAudioSource.AudioSource.spatialize = playAudioContinuously.AudioData.Spatialize;
playingAudioSource.AudioSource.bypassEffects =
playAudioContinuously.AudioData.BypassEffects;
playingAudioSource.AudioSource.bypassListenerEffects =
playAudioContinuously.AudioData.BypassListenerEffects;
playingAudioSource.AudioSource.bypassReverbZones =
playAudioContinuously.AudioData.BypassReverbZones;
playingAudioSource.AudioSource.panStereo = playAudioContinuously.AudioData.PanStereo;
playingAudioSource.AudioSource.spatialBlend = playAudioContinuously.AudioData.SpatialBlend;
playingAudioSource.AudioSource.reverbZoneMix =
playAudioContinuously.AudioData.ReverbZoneMix;
playingAudioSource.AudioSource.dopplerLevel = playAudioContinuously.AudioData.DopplerLevel;
playingAudioSource.AudioSource.spread = playAudioContinuously.AudioData.Spread;
playingAudioSource.AudioSource.rolloffMode = playAudioContinuously.AudioData.RolloffMode;
playingAudioSource.AudioSource.minDistance = playAudioContinuously.AudioData.MinDistance;
playingAudioSource.AudioSource.maxDistance = playAudioContinuously.AudioData.MaxDistance;
if (playAudioContinuously.OneShot)
{
playingAudioSource.AudioSource.PlayOneShot(clip);
playAudioContinuously.Playing = false;
}
else
{
playingAudioSource.AudioSource.Play();
}
}
})
.Run();
Entities
.WithoutBurst()
.WithName("audio_process_destroyed_entity")
.WithNone<EcsAudioSource>()
.ForEach((Entity entity, in PlayingAudioState playAudioContinuouslyState) =>
{
ReturnSourceToPool(playAudioContinuouslyState.AudioSourceId, playingSources, unusedSources);
commandBuffer.RemoveComponent<PlayingAudioState>(entity);
})
.Run();
}
private static Guid PoolSource(
Dictionary<Guid, PlayingAudioSource> playingSources,
Queue<AudioSource> unusedSources,
ref PlayingAudioSource playingAudioSource
)
{
var audioSourceId = Guid.NewGuid();
if (unusedSources.Count == 0)
{
playingAudioSource.AudioSource = new GameObject("EcsAudioSource").AddComponent<AudioSource>();
}
else
{
playingAudioSource.AudioSource = unusedSources.Dequeue();
playingAudioSource.AudioSource.gameObject.SetActive(true);
}
playingSources.Add(audioSourceId, playingAudioSource);
return audioSourceId;
}
private static void ReturnSourceToPool(Guid audioSourceId,
Dictionary<Guid, PlayingAudioSource> playingSources,
Queue<AudioSource> unusedSources)
{
var pooledAudioSource = playingSources[audioSourceId];
pooledAudioSource.AudioSource.gameObject.SetActive(false);
pooledAudioSource.AudioSource.Stop();
playingSources.Remove(audioSourceId);
unusedSources.Enqueue(pooledAudioSource.AudioSource);
}
}
}