forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioOccluder.cs
More file actions
83 lines (70 loc) · 3.33 KB
/
AudioOccluder.cs
File metadata and controls
83 lines (70 loc) · 3.33 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// Class that implements IAudioInfluencer to provide an occlusion effect.
/// </summary>
public class AudioOccluder : MonoBehaviour, IAudioInfluencer
{
/// <summary>
/// Frequency above the nominal range of human hearing. Applying this frequency to the filter will have no perceived impact on the audio source.
/// </summary>
private readonly float NeutralFrequency = 22000f;
[Tooltip("Frequency above which sound will not be heard.")]
[Range(10.0f, 22000.0f)]
public float CutoffFrequency = 5000.0f;
[Tooltip("Percentage of the audio source volume that will be heard after applying occlusion.")]
[Range(0.0f, 1.0f)]
public float VolumePassThrough = 1.0f;
// Update is not used, but is kept so that this component can be enabled/disabled.
private void Update()
{ }
/// <summary>
/// Applies the audio effect.
/// </summary>
/// <param name="emitter">The GameObject on which the effect is to be applied.</param>
/// <param name="audioSource">The AudioSource that will be impacted by the effect.</param>
public void ApplyEffect(GameObject emitter,
AudioSource audioSource)
{
if (!isActiveAndEnabled)
{ return; }
if (audioSource == null)
{
Debug.LogWarning("The specified emitter does not have an attached AudioSource component.");
return;
}
// Audio occlusion is performed using a low pass filter.
AudioLowPassFilter lowPass = emitter.GetComponent<AudioLowPassFilter>();
if (lowPass == null)
{
lowPass = emitter.AddComponent<AudioLowPassFilter>();
}
lowPass.enabled = true;
// In the real world, chaining multiple low-pass filters will result in the
// lowest of the cutoff frequencies being the highest pitches heard.
lowPass.cutoffFrequency = Mathf.Min(lowPass.cutoffFrequency, CutoffFrequency);
// Unlike the cutoff frequency, volume pass-through is cumulative.
audioSource.volume *= VolumePassThrough;
}
/// <summary>
/// Removes the previously applied audio effect.
/// </summary>
/// <param name="emitter">The GameObject from which the effect is to be removed.</param>
/// <param name="audioSource">The AudioSource that will be impacted by the effect.</param>
public void RemoveEffect(GameObject emitter,
AudioSource audioSource)
{
// Note: The audioSource parameter is unused.
// Audio occlusion is performed using a low pass filter.
AudioLowPassFilter lowPass = emitter.GetComponent<AudioLowPassFilter>();
if (lowPass == null) { return; }
lowPass.cutoffFrequency = NeutralFrequency;
lowPass.enabled = false;
// Note: Volume attenuation is reset in the emitter.
}
}
}