v0.1.0
This commit is contained in:
171
Assets/DomeShift/DomeAudio.cs
Normal file
171
Assets/DomeShift/DomeAudio.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace DomeShift
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
internal sealed class DomeAudio : MonoBehaviour
|
||||
{
|
||||
public enum Cue { Start, Pickup, Repair, Denied, Won, Lost }
|
||||
|
||||
private const int SampleRate = 24000;
|
||||
private const float FullTurn = 2f * Mathf.PI;
|
||||
private readonly AudioClip[] cues = new AudioClip[6];
|
||||
private AudioSource effects;
|
||||
private AudioSource ambience;
|
||||
private bool muted;
|
||||
|
||||
public bool Muted
|
||||
{
|
||||
get => muted;
|
||||
set
|
||||
{
|
||||
muted = value;
|
||||
if (effects != null) effects.mute = muted;
|
||||
if (ambience != null) ambience.mute = muted;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
effects = CreateSource(0.65f);
|
||||
ambience = CreateSource(0.25f);
|
||||
|
||||
cues[(int)Cue.Start] = MakeCue("Shift start",
|
||||
new Tone(0f, 0.25f, 261.63f, 0.35f),
|
||||
new Tone(0.12f, 0.30f, 392f, 0.32f),
|
||||
new Tone(0.24f, 0.48f, 523.25f, 0.29f));
|
||||
cues[(int)Cue.Pickup] = MakeCue("Supply pickup",
|
||||
new Tone(0f, 0.11f, 392f, 0.35f, 0.025f),
|
||||
new Tone(0.055f, 0.18f, 587.33f, 0.30f));
|
||||
cues[(int)Cue.Repair] = MakeCue("Station repaired",
|
||||
new Tone(0f, 0.18f, 293.66f, 0.32f, 0.035f),
|
||||
new Tone(0.09f, 0.23f, 440f, 0.31f),
|
||||
new Tone(0.18f, 0.39f, 587.33f, 0.29f),
|
||||
new Tone(0.18f, 0.33f, 739.99f, 0.12f));
|
||||
cues[(int)Cue.Denied] = MakeCue("Access denied",
|
||||
new Tone(0f, 0.095f, 146.83f, 0.37f, 0.03f),
|
||||
new Tone(0.095f, 0.17f, 110f, 0.35f));
|
||||
cues[(int)Cue.Won] = MakeCue("Airlock released",
|
||||
new Tone(0f, 0.24f, 329.63f, 0.30f),
|
||||
new Tone(0.13f, 0.23f, 440f, 0.31f),
|
||||
new Tone(0.26f, 0.37f, 554.37f, 0.28f),
|
||||
new Tone(0.39f, 0.59f, 659.25f, 0.26f),
|
||||
new Tone(0.39f, 0.58f, 440f, 0.15f));
|
||||
cues[(int)Cue.Lost] = MakeCue("Shift timeout",
|
||||
new Tone(0f, 0.26f, 246.94f, 0.31f),
|
||||
new Tone(0.15f, 0.30f, 185f, 0.29f),
|
||||
new Tone(0.30f, 0.61f, 123.47f, 0.34f));
|
||||
|
||||
ambience.clip = MakeAmbience();
|
||||
ambience.loop = true;
|
||||
ambience.Play();
|
||||
}
|
||||
|
||||
public void Play(Cue cue)
|
||||
{
|
||||
if (muted || effects == null) return;
|
||||
|
||||
effects.clip = cues[(int)cue];
|
||||
effects.Play();
|
||||
}
|
||||
|
||||
private AudioSource CreateSource(float volume)
|
||||
{
|
||||
var source = gameObject.AddComponent<AudioSource>();
|
||||
source.playOnAwake = false;
|
||||
source.spatialBlend = 0f;
|
||||
source.volume = volume;
|
||||
source.mute = muted;
|
||||
return source;
|
||||
}
|
||||
|
||||
private static AudioClip MakeCue(string name, params Tone[] tones)
|
||||
{
|
||||
float length = 0f;
|
||||
foreach (var tone in tones)
|
||||
length = Mathf.Max(length, tone.Start + tone.Duration);
|
||||
|
||||
var samples = new float[Mathf.CeilToInt(length * SampleRate)];
|
||||
var random = new System.Random(47);
|
||||
foreach (var tone in tones)
|
||||
{
|
||||
int offset = Mathf.RoundToInt(tone.Start * SampleRate);
|
||||
int sampleCount = Mathf.Min(samples.Length - offset, Mathf.FloorToInt(tone.Duration * SampleRate));
|
||||
float filteredNoise = 0f;
|
||||
for (int i = 0; i < sampleCount; i++)
|
||||
{
|
||||
float seconds = i / (float)SampleRate;
|
||||
float phase = FullTurn * tone.Frequency * seconds;
|
||||
float attack = Mathf.Clamp01(seconds / 0.012f);
|
||||
float release = Mathf.Clamp01((tone.Duration - seconds) / 0.065f);
|
||||
float decay = Mathf.Exp(-3f * seconds / tone.Duration);
|
||||
float wave = 0.76f * Mathf.Sin(phase) + 0.19f * Mathf.Sin(phase * 2f)
|
||||
+ 0.05f * Mathf.Sin(phase * 3f);
|
||||
filteredNoise += ((float)(random.NextDouble() * 2.0 - 1.0) - filteredNoise) * 0.16f;
|
||||
float onset = filteredNoise * tone.Click * Mathf.Exp(-65f * seconds);
|
||||
samples[offset + i] += (wave * tone.Volume + onset) * attack * release * decay;
|
||||
}
|
||||
}
|
||||
|
||||
return CreateClip(name, samples);
|
||||
}
|
||||
|
||||
private static AudioClip MakeAmbience()
|
||||
{
|
||||
const int loopSeconds = 4;
|
||||
var samples = new float[SampleRate * loopSeconds];
|
||||
var random = new System.Random(103);
|
||||
float wind = 0f;
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
float seconds = i / (float)SampleRate;
|
||||
float loopPhase = FullTurn * seconds / loopSeconds;
|
||||
float fan = 0.08f * Mathf.Sin(loopPhase * 176f) + 0.025f * Mathf.Sin(loopPhase * 352f);
|
||||
float cycle = 0.72f + 0.28f * Mathf.Cos(loopPhase);
|
||||
wind += ((float)(random.NextDouble() * 2.0 - 1.0) - wind) * 0.04f;
|
||||
float edge = Mathf.Clamp01(Mathf.Min(seconds, loopSeconds - seconds) / 0.12f);
|
||||
samples[i] = fan * cycle + wind * 0.115f * edge;
|
||||
}
|
||||
|
||||
return CreateClip("Dome ventilation", samples);
|
||||
}
|
||||
|
||||
private static AudioClip CreateClip(string name, float[] samples)
|
||||
{
|
||||
var clip = AudioClip.Create(name, samples.Length, 1, SampleRate, false);
|
||||
clip.SetData(samples, 0);
|
||||
return clip;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (effects != null) effects.Stop();
|
||||
foreach (var clip in cues)
|
||||
{
|
||||
if (clip != null) Destroy(clip);
|
||||
}
|
||||
|
||||
if (ambience == null) return;
|
||||
ambience.Stop();
|
||||
if (ambience.clip != null) Destroy(ambience.clip);
|
||||
}
|
||||
|
||||
private readonly struct Tone
|
||||
{
|
||||
public readonly float Start;
|
||||
public readonly float Duration;
|
||||
public readonly float Frequency;
|
||||
public readonly float Volume;
|
||||
public readonly float Click;
|
||||
|
||||
public Tone(float start, float duration, float frequency, float volume, float click = 0f)
|
||||
{
|
||||
Start = start;
|
||||
Duration = duration;
|
||||
Frequency = frequency;
|
||||
Volume = volume;
|
||||
Click = click;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user