AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
AudioLoFiEffect.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.Collections.Generic;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity
8 {
18  [RequireComponent(typeof(AudioSource))]
19  [RequireComponent(typeof(AudioLowPassFilter))]
20  [RequireComponent(typeof(AudioHighPassFilter))]
21  [DisallowMultipleComponent]
22  public class AudioLoFiEffect : MonoBehaviour
23  {
27  [Tooltip("The quality level of the simulated audio source.")]
28  [SerializeField]
29  private AudioLoFiSourceQuality sourceQuality;
30  public AudioLoFiSourceQuality SourceQuality
31  {
32  get { return sourceQuality; }
33  set { sourceQuality = value; }
34  }
35 
39  private AudioLoFiFilterSettings filterSettings;
40 
44  private AudioLowPassFilter lowPassFilter;
45  private AudioHighPassFilter highPassFilter;
46 
51  private Dictionary<AudioLoFiSourceQuality, AudioLoFiFilterSettings> sourceQualityFilterSettings =
52  new Dictionary<AudioLoFiSourceQuality, AudioLoFiFilterSettings>();
53 
54  private void Awake()
55  {
56  LoadQualityFilterSettings();
57 
58  filterSettings = sourceQualityFilterSettings[SourceQuality];
59 
60  lowPassFilter = gameObject.GetComponent<AudioLowPassFilter>();
61  lowPassFilter.cutoffFrequency = filterSettings.LowPassCutoff;
62 
63  highPassFilter = gameObject.GetComponent<AudioHighPassFilter>();
64  highPassFilter.cutoffFrequency = filterSettings.HighPassCutoff;
65  }
66 
67  private void Update()
68  {
69  AudioLoFiFilterSettings newSettings = sourceQualityFilterSettings[SourceQuality];
70  if (newSettings != filterSettings)
71  {
72  // If we have an attached AudioInfluencerManager, we need to let it know
73  // about our filter settings change.
74  AudioEmitter influencerManager = gameObject.GetComponent<AudioEmitter>();
75  if (influencerManager != null)
76  {
77  influencerManager.SetNativeLowPassCutoffFrequency(newSettings.LowPassCutoff);
78  influencerManager.SetNativeHighPassCutoffFrequency(newSettings.HighPassCutoff);
79  }
80 
81  filterSettings = newSettings;
82  lowPassFilter.cutoffFrequency = filterSettings.LowPassCutoff;
83  highPassFilter.cutoffFrequency = filterSettings.HighPassCutoff;
84  }
85  }
86 
90  private void LoadQualityFilterSettings()
91  {
92  if (sourceQualityFilterSettings.Keys.Count > 0) { return; }
93 
94  sourceQualityFilterSettings.Add(
95  AudioLoFiSourceQuality.FullRange,
96  new AudioLoFiFilterSettings(10, 22000));
97  sourceQualityFilterSettings.Add(
98  AudioLoFiSourceQuality.NarrowBandTelephony,
99  new AudioLoFiFilterSettings(300, 3400));
100  sourceQualityFilterSettings.Add(
101  AudioLoFiSourceQuality.WideBandTelephony,
102  new AudioLoFiFilterSettings(50, 7000));
103  sourceQualityFilterSettings.Add(
104  AudioLoFiSourceQuality.AmRadio,
105  new AudioLoFiFilterSettings(40, 5000));
106  sourceQualityFilterSettings.Add(
107  AudioLoFiSourceQuality.FmRadio,
108  new AudioLoFiFilterSettings(30, 15000));
109  }
110 
117  private struct AudioLoFiFilterSettings
118  {
122  public float LowPassCutoff
123  { get; private set; }
124 
128  public float HighPassCutoff
129  { get; private set; }
130 
136  public AudioLoFiFilterSettings(float highPassCutoff, float lowPassCutoff) : this()
137  {
138  HighPassCutoff = highPassCutoff;
139  LowPassCutoff = lowPassCutoff;
140  }
141 
146  public static bool operator ==(AudioLoFiFilterSettings a, AudioLoFiFilterSettings b)
147  {
148  return a.Equals(b);
149  }
150 
155  public static bool operator !=(AudioLoFiFilterSettings a, AudioLoFiFilterSettings b)
156  {
157  return !(a.Equals(b));
158  }
159 
164  public override bool Equals(object obj)
165  {
166  if (obj == null)
167  {
168  return false;
169  }
170 
171  if (!(obj is AudioLoFiFilterSettings))
172  {
173  return false;
174  }
175 
176  AudioLoFiFilterSettings other = (AudioLoFiFilterSettings)obj;
177  if ((other.LowPassCutoff != LowPassCutoff) ||
178  (other.HighPassCutoff != HighPassCutoff))
179  {
180  return false;
181  }
182 
183  return true;
184  }
185 
190  public override int GetHashCode()
191  {
192  string s = string.Format(
193  "[AudioLoFiFilterSettings] Low: {0}, High: {1}",
194  LowPassCutoff,
195  HighPassCutoff);
196 
197  return s.GetHashCode();
198  }
199  }
200  }
201 
207  {
212 
217 
221  AmRadio,
222 
230  FmRadio,
231 
239  FullRange
240  }
241 }
void SetNativeLowPassCutoffFrequency(float frequency)
Sets the cached native cutoff frequency of the attached low pass filter.
Class which supports IAudioInfluencers being used with audio sources.
Definition: AudioEmitter.cs:19
void SetNativeHighPassCutoffFrequency(float frequency)
Sets the cached native cutoff frequency of the attached high pass filter.
An audio effect that limits the frequency range of a sound to simulate being played over various tele...
AudioLoFiSourceQuality
Source quality options, used by the AudioLoFiEffect, that match common telephony and radio broadcast ...