AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ActiveEvent.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity
8 {
12  public class ActiveEvent : IDisposable
13  {
14  private AudioSource primarySource = null;
15  public AudioSource PrimarySource
16  {
17  get
18  {
19  return primarySource;
20  }
21  private set
22  {
23  primarySource = value;
24  if (primarySource != null)
25  {
26  primarySource.enabled = true;
27  }
28  }
29  }
30 
31  private AudioSource secondarySource = null;
32  public AudioSource SecondarySource
33  {
34  get
35  {
36  return secondarySource;
37  }
38  private set
39  {
40  secondarySource = value;
41  if (secondarySource != null)
42  {
43  secondarySource.enabled = true;
44  }
45  }
46  }
47 
48  public bool IsPlaying
49  {
50  get
51  {
52  return
53  (primarySource != null && primarySource.isPlaying) ||
54  (secondarySource != null && secondarySource.isPlaying);
55  }
56  }
57 
58  public GameObject AudioEmitter
59  {
60  get;
61  private set;
62  }
63 
64  public string MessageOnAudioEnd
65  {
66  get;
67  private set;
68  }
69 
70  public AudioEvent AudioEvent = null;
71  public bool IsStoppable = true;
72  public float VolDest = 1;
73  public float AltVolDest = 1;
74  public float CurrentFade = 0;
75  public bool PlayingAlt = false;
76  public bool IsActiveTimeComplete = false;
77  public float ActiveTime = 0;
78  public bool CancelEvent = false;
79 
80  public ActiveEvent(AudioEvent audioEvent, GameObject emitter, AudioSource primarySource, AudioSource secondarySource, string messageOnAudioEnd = null)
81  {
82  this.AudioEvent = audioEvent;
83  AudioEmitter = emitter;
84  PrimarySource = primarySource;
85  SecondarySource = secondarySource;
86  MessageOnAudioEnd = messageOnAudioEnd;
87  SetSourceProperties();
88  }
89 
90  public static AnimationCurve SpatialRolloff;
91 
95  private void SetSourceProperties()
96  {
97  Action<Action<AudioSource>> forEachSource = (action) =>
98  {
99  action(PrimarySource);
100  if (SecondarySource != null)
101  {
102  action(SecondarySource);
103  }
104  };
105 
106  AudioEvent audioEvent = this.AudioEvent;
107  switch (audioEvent.Spatialization)
108  {
109  case SpatialPositioningType.TwoD:
110  forEachSource((source) =>
111  {
112  source.spatialBlend = 0f;
113  source.spatialize = false;
114  });
115  break;
116  case SpatialPositioningType.ThreeD:
117  forEachSource((source) =>
118  {
119  source.spatialBlend = 1f;
120  source.spatialize = false;
121  });
122  break;
123  case SpatialPositioningType.SpatialSound:
124  forEachSource((source) =>
125  {
126  source.spatialBlend = 1f;
127  source.spatialize = true;
128  });
129  break;
130  default:
131  Debug.LogErrorFormat("Unexpected spatialization type: {0}", audioEvent.Spatialization.ToString());
132  break;
133  }
134 
135  if (audioEvent.Spatialization == SpatialPositioningType.SpatialSound)
136  {
137  forEachSource((source) =>
138  {
139  SpatialSoundSettings.SetRoomSize(source, audioEvent.RoomSize);
140  source.rolloffMode = AudioRolloffMode.Custom;
141  source.maxDistance = audioEvent.MaxDistanceAttenuation3D;
142  source.SetCustomCurve(AudioSourceCurveType.CustomRolloff, audioEvent.AttenuationCurve);
143  });
144  }
145  else
146  {
147  forEachSource((source) =>
148  {
149  if (audioEvent.Spatialization == SpatialPositioningType.ThreeD)
150  {
151  source.rolloffMode = AudioRolloffMode.Custom;
152  source.maxDistance = audioEvent.MaxDistanceAttenuation3D;
153  source.SetCustomCurve(AudioSourceCurveType.CustomRolloff, audioEvent.AttenuationCurve);
154  source.SetCustomCurve(AudioSourceCurveType.SpatialBlend, audioEvent.SpatialCurve);
155  source.SetCustomCurve(AudioSourceCurveType.Spread, audioEvent.SpreadCurve);
156  source.SetCustomCurve(AudioSourceCurveType.ReverbZoneMix, audioEvent.ReverbCurve);
157  }
158  else
159  {
160  source.rolloffMode = AudioRolloffMode.Logarithmic;
161  }
162  });
163  }
164 
165  if (audioEvent.AudioBus != null)
166  {
167  forEachSource((source) => source.outputAudioMixerGroup = audioEvent.AudioBus);
168  }
169 
170  float pitch = 1f;
171 
172  if (audioEvent.PitchRandomization != 0)
173  {
174  pitch = UnityEngine.Random.Range(audioEvent.PitchCenter - audioEvent.PitchRandomization, audioEvent.PitchCenter + audioEvent.PitchRandomization);
175  }
176  else
177  {
178  pitch = audioEvent.PitchCenter;
179  }
180  forEachSource((source) => source.pitch = pitch);
181 
182  float vol = 1f;
183  if (audioEvent.FadeInTime > 0)
184  {
185  forEachSource((source) => source.volume = 0f);
186  this.CurrentFade = audioEvent.FadeInTime;
187  if (audioEvent.VolumeRandomization != 0)
188  {
189  vol = UnityEngine.Random.Range(audioEvent.VolumeCenter - audioEvent.VolumeRandomization, audioEvent.VolumeCenter + audioEvent.VolumeRandomization);
190  }
191  else
192  {
193  vol = audioEvent.VolumeCenter;
194  }
195  this.VolDest = vol;
196  }
197  else
198  {
199  if (audioEvent.VolumeRandomization != 0)
200  {
201  vol = UnityEngine.Random.Range(audioEvent.VolumeCenter - audioEvent.VolumeRandomization, audioEvent.VolumeCenter + audioEvent.VolumeRandomization);
202  }
203  else
204  {
205  vol = audioEvent.VolumeCenter;
206  }
207  forEachSource((source) => source.volume = vol);
208  }
209 
210  float pan = audioEvent.PanCenter;
211  if (audioEvent.PanRandomization != 0)
212  {
213  pan = UnityEngine.Random.Range(audioEvent.PanCenter - audioEvent.PanRandomization, audioEvent.PanCenter + audioEvent.PanRandomization);
214  }
215  forEachSource((source) => source.panStereo = pan);
216  }
217 
222  public void SetPitch(float newPitch)
223  {
224  if (newPitch <= 0 || newPitch > 3)
225  {
226  Debug.LogErrorFormat("Invalid pitch {0} set for event", newPitch);
227  return;
228  }
229 
230  this.PrimarySource.pitch = newPitch;
231  }
232 
233  public void Dispose()
234  {
235  if (this.primarySource != null)
236  {
237  this.primarySource.enabled = false;
238  this.primarySource = null;
239  }
240 
241  if (this.secondarySource != null)
242  {
243  this.secondarySource.enabled = false;
244  this.secondarySource = null;
245  }
246  }
247 
251  public static void CreateFlatSpatialRolloffCurve()
252  {
253  if (SpatialRolloff != null)
254  {
255  return;
256  }
257  SpatialRolloff = new AnimationCurve();
258  SpatialRolloff.AddKey(0, 1);
259  SpatialRolloff.AddKey(1, 1);
260  }
261  }
262 }
SpatialSoundRoomSizes RoomSize
Definition: AudioEvent.cs:63
AnimationCurve AttenuationCurve
Definition: AudioEvent.cs:66
The SpatialSoundSettings class provides a set of methods that simplify making modifications to Micros...
Class which supports IAudioInfluencers being used with audio sources.
Definition: AudioEmitter.cs:19
SpatialPositioningType
The different types of spatial positioning.
Definition: AudioEvent.cs:34
static void SetRoomSize(AudioSource audioSource, SpatialSoundRoomSizes room)
Sets the Spatial Sound room size.
SpatialPositioningType Spatialization
Definition: AudioEvent.cs:60
The AudioEvent class is the main component of UAudioManager and contains settings and a container for...
Definition: AudioEvent.cs:54
static AnimationCurve SpatialRolloff
Definition: ActiveEvent.cs:90
float PanRandomization
The amount in either direction from Pan Center that panning can randomly vary upon playing the event...
Definition: AudioEvent.cs:115
ActiveEvent(AudioEvent audioEvent, GameObject emitter, AudioSource primarySource, AudioSource secondarySource, string messageOnAudioEnd=null)
Definition: ActiveEvent.cs:80
Currently active AudioEvents along with their AudioSource components for instance limiting events ...
Definition: ActiveEvent.cs:12
float VolumeRandomization
The amount in either direction from Volume Center that the volume can randomly vary upon playing the ...
Definition: AudioEvent.cs:104
static void CreateFlatSpatialRolloffCurve()
Creates a flat animation curve to negate Unity&#39;s distance attenuation when using Spatial Sound ...
Definition: ActiveEvent.cs:251
AudioMixerGroup AudioBus
Definition: AudioEvent.cs:82
void SetPitch(float newPitch)
Sets the pitch value for the primary source.
Definition: ActiveEvent.cs:222
float PitchRandomization
The amount in either direction from Pitch Center that the pitch can randomly vary upon playing the ev...
Definition: AudioEvent.cs:93