-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGunshotSound.cs
127 lines (108 loc) · 3.86 KB
/
GunshotSound.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Audio;
[RequireComponent(typeof(AudioSource))]
public class GunshotSound : MonoBehaviour
{
private float[] noiseBuffer;
private int sampleRate;
private float phase;
private float decayTime = 0.05f; // 50 ms
private float chirpDuration = 0.05f;
private float chirpStartFreq = 10000f;
private float chirpEndFreq = 100f;
private bool triggerGunshot = false;
private int sampleIndex = 0;
// Bandpass filter tweaks
private float[] bandpassFrequencies = { 200f, 300f, 400f, 500f, 2500f };
private float[] bandpassBandwidths = { 50f, 50f, 50f, 50f, 50f };
void Start()
{
sampleRate = AudioSettings.outputSampleRate;
noiseBuffer = new float[sampleRate];
GenerateNoise();
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.spatialize = true;
audioSource.spatialBlend = 1.0f;
// Uncomment this and change the name of your Master mix and the Audio Mixer Group in the Audio Mixer
/* AudioMixer audioMixer = Resources.Load<AudioMixer>("Master");
if (audioMixer != null)
{
audioSource.outputAudioMixerGroup = audioMixer.FindMatchingGroups("Gunshot")[0];
} */
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
sampleIndex = 0;
phase = 0f;
triggerGunshot = true;
}
}
void GenerateNoise()
{
for (int i = 0; i < noiseBuffer.Length; i++)
{
noiseBuffer[i] = Random.Range(-1f, 1f);
}
}
float GetExcitationNoiseSample(int sampleIndex)
{
float time = sampleIndex / (float)sampleRate;
float envelope = Mathf.Exp(-time / decayTime);
return envelope * noiseBuffer[sampleIndex % noiseBuffer.Length];
}
float ApplyResonanceFilter(float sample)
{
float output = 0f;
foreach (float frequency in bandpassFrequencies)
{
float omega = 2 * Mathf.PI * frequency / sampleRate;
float alpha = Mathf.Sin(omega) / (2 * Mathf.Sqrt(2));
float cosOmega = Mathf.Cos(omega);
float a0 = 1 + alpha;
float a1 = -2 * cosOmega;
float a2 = 1 - alpha;
float b0 = alpha;
float b1 = 0;
float b2 = -alpha;
float filteredSample = (b0 / a0) * sample + (b1 / a0) * 0 + (b2 / a0) * 0
- (a1 / a0) * 0 - (a2 / a0) * 0;
output += filteredSample;
}
return output;
}
void OnAudioFilterRead(float[] data, int channels)
{
if (!triggerGunshot) return;
for (int i = 0; i < data.Length; i += channels)
{
float sample = 0f;
// Chirp
float time = sampleIndex / (float)sampleRate;
if (time <= chirpDuration)
{
float envelope = Mathf.Exp(-time / decayTime);
float frequency = Mathf.Lerp(chirpStartFreq, chirpEndFreq, time / chirpDuration);
sample += envelope * Mathf.Cos(2 * Mathf.PI * frequency * phase / sampleRate);
}
// Excitation Noise
sample += GetExcitationNoiseSample(sampleIndex);
// Body Resonance
sample = ApplyResonanceFilter(sample);
phase++;
sampleIndex++;
for (int j = 0; j < channels; j++)
{
data[i + j] = sample;
}
}
// Stop triggering after processing enough samples for a short burst
if (sampleIndex >= sampleRate * chirpDuration)
{
triggerGunshot = false;
}
}
}