AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
AudioOccluder.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 {
16  public class AudioOccluder : MonoBehaviour, IAudioInfluencer
17  {
27  [Tooltip("Frequency above which sound will not be heard after applying occlusion.")]
28  [Range(10.0f, 22000.0f)]
29  [SerializeField]
30  private float cutoffFrequency = 5000.0f;
31  public float CutoffFrequency
32  {
33  get { return cutoffFrequency; }
34  set
35  {
36  // set cutoffFrequency and enforce the specified range
37  if (value < 10.0f)
38  {
39  cutoffFrequency = 10.0f;
40  }
41  else if (value > 22000.0f)
42  {
43  cutoffFrequency = 22000.0f;
44  }
45  else
46  {
47  cutoffFrequency = value;
48  }
49  }
50  }
51 
61  [Tooltip("Percentage of the audio source volume that will be heard after applying occlusion.")]
62  [Range(0.0f, 1.0f)]
63  [SerializeField]
64  private float volumePassThrough = 1.0f;
65  public float VolumePassThrough
66  {
67  get { return volumePassThrough; }
68  set
69  {
70  // set cutoffFrequency and enforce the specified range
71  if (value < 10.0f)
72  {
73  volumePassThrough = 10.0f;
74  }
75  else if (value > 22000.0f)
76  {
77  volumePassThrough = 22000.0f;
78  }
79  else
80  {
81  volumePassThrough = value;
82  }
83  }
84  }
85 
86  // Update is not used, but is kept so that this component can be enabled/disabled.
87  private void Update() { }
88 
98  public void ApplyEffect(GameObject soundEmittingObject, AudioSource audioSource)
99  {
100  // This version of ApplyEffect has been deprecated.
101  // Ignore the provided audioSource as the prefered method will retreive it from
102  // the soundEmittingObject GameObject.
103  ApplyEffect(soundEmittingObject);
104  }
105 
110  public void ApplyEffect(GameObject soundEmittingObject)
111  {
112  if (!isActiveAndEnabled)
113  { return; }
114 
115  AudioSource audioSource = soundEmittingObject.GetComponent<AudioSource>();
116  if (audioSource == null)
117  {
118  Debug.LogWarning("The specified emitter does not have an attached AudioSource component.");
119  return;
120  }
121 
122  // Audio occlusion is performed using a low pass filter.
123  AudioLowPassFilter lowPass = soundEmittingObject.GetComponent<AudioLowPassFilter>();
124  if (lowPass == null)
125  {
126  lowPass = soundEmittingObject.AddComponent<AudioLowPassFilter>();
127  }
128  lowPass.enabled = true;
129 
130  // In the real world, chaining multiple low-pass filters will result in the
131  // lowest of the cutoff frequencies being the highest pitches heard.
132  lowPass.cutoffFrequency = Mathf.Min(lowPass.cutoffFrequency, CutoffFrequency);
133 
134  // Unlike the cutoff frequency, volume pass-through is cumulative.
135  audioSource.volume *= VolumePassThrough;
136  }
137 
147  public void RemoveEffect(GameObject soundEmittingObject, AudioSource audioSource)
148  {
149  // This version of RemoveEffect has been deprecated.
150  // The provided audioSource has always been ignored as it is not used.
151  RemoveEffect(soundEmittingObject);
152  }
153 
158  public void RemoveEffect(GameObject soundEmittingObject)
159  {
160  // Audio occlusion is performed using a low pass filter.
161  AudioLowPassFilter lowPass = soundEmittingObject.GetComponent<AudioLowPassFilter>();
162  if (lowPass == null) { return; }
163 
164  float neutralFrequency = AudioEmitter.NeutralHighFrequency;
165  AudioEmitter influencerManager = soundEmittingObject.GetComponent<AudioEmitter>();
166  if (influencerManager != null)
167  {
168  neutralFrequency = influencerManager.GetNativeLowPassCutoffFrequency();
169  }
170 
171  lowPass.cutoffFrequency = neutralFrequency;
172  lowPass.enabled = false;
173 
174  // Note: Volume attenuation is reset in the AudioInfluencerManager, attached to the sound emitting object.
175  }
176  }
177 }
Interface that is implemented by any class that wishes to influence how an audio source sounds...
Class that implements IAudioInfluencer to provide an occlusion effect.
void ApplyEffect(GameObject soundEmittingObject)
Applies the audio effect.
void ApplyEffect(GameObject soundEmittingObject, AudioSource audioSource)
Applies the audio effect.
Class which supports IAudioInfluencers being used with audio sources.
Definition: AudioEmitter.cs:19
void RemoveEffect(GameObject soundEmittingObject, AudioSource audioSource)
Removes the previously applied audio effect.
void RemoveEffect(GameObject soundEmittingObject)
Removes the previously applied audio effect.
float GetNativeLowPassCutoffFrequency()
Gets the native cutoff frequency of the attached low pass filter.
static readonly float NeutralHighFrequency
Frequency above the nominal range of human hearing.
Definition: AudioEmitter.cs:37