| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using DG.Tweening;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace DevilLink2.Presentation
- {
- /// <summary>Fixed source pool with calibrated voice admission and explicit linear gain budgets.</summary>
- public sealed class UnityAudioOutput : IAudioOutput, ICalibratedAudioOutput
- {
- public const int EffectVoices = AudioVoicePolicy.Slots;
- private readonly GameObject root;
- private readonly AudioSource[] music = new AudioSource[2], effects = new AudioSource[EffectVoices];
- private readonly AudioCrossfade blend = new AudioCrossfade();
- private readonly AudioVoicePolicy voices = new AudioVoicePolicy();
- private readonly Func<double> clock;
- private readonly bool audible;
- private int active, generation;
- private float musicVolume = AudioMixCalibration.DefaultMusic, sfxVolume = AudioMixCalibration.DefaultSfx;
- private float voiceScale = 1, headroom = 1;
- private double lastMixTime = double.NaN;
- private Tween fade;
- public int ActiveEffectCount => voices.Count;
- public UnityAudioOutput(Transform owner, bool enablePlayback, Func<double> time = null)
- {
- audible = enablePlayback; clock = time ?? (() => Time.realtimeSinceStartupAsDouble);
- root = new GameObject("AudioChannels"); root.transform.SetParent(owner, false);
- for (int i = 0; i < music.Length; i++) music[i] = Source("BGM" + i, true);
- for (int i = 0; i < effects.Length; i++) effects[i] = Source("SFX" + i, false);
- }
- private AudioSource Source(string name, bool loop)
- {
- var go = new GameObject(name); go.transform.SetParent(root.transform, false);
- var source = go.AddComponent<AudioSource>(); source.playOnAwake = false; source.loop = loop; source.spatialBlend = 0;
- source.volume = 0; return source;
- }
- public void SwitchMusic(AudioClip clip)
- {
- int token = ++generation; fade?.Kill(false); fade = null;
- int next = 1 - active; active = next;
- var source = music[next]; source.Stop(); source.clip = clip;
- blend.Begin(next, clip != null); ApplyMix();
- if (audible && clip) source.Play();
- if (!audible) { blend.Advance(1); ApplyMix(); return; }
- float progress = 0;
- fade = DOTween.To(() => progress, t => {
- if (token != generation) return;
- progress = t; blend.Advance(t); ApplyMix();
- }, 1, .6f).SetUpdate(true).SetEase(Ease.Linear).SetRecyclable(false).OnComplete(() => {
- if (token != generation) return;
- music[1 - next].Stop(); if (!clip) source.Stop();
- });
- }
- private void ApplyMix(bool immediate = false)
- {
- double now = clock();
- float delta = double.IsNaN(lastMixTime) ? 0 : (float)Math.Max(0, now - lastMixTime);
- lastMixTime = now; voices.Expire(now);
- float targetVoiceScale = AudioMixCalibration.VoiceScale(voices.GainSum);
- voiceScale = immediate ? targetVoiceScale : AudioMixCalibration.Recover(voiceScale, targetVoiceScale, delta);
- float musicGain = musicVolume * (blend.First + blend.Second);
- float sfxGain = sfxVolume * voices.GainSum * voiceScale;
- float targetHeadroom = AudioMixCalibration.Headroom(musicGain, sfxGain);
- headroom = immediate ? targetHeadroom : AudioMixCalibration.Recover(headroom, targetHeadroom, delta);
- if (music[0]) music[0].volume = musicVolume * blend.First * headroom;
- if (music[1]) music[1].volume = musicVolume * blend.Second * headroom;
- for (int i = 0; i < effects.Length; i++) if (effects[i])
- {
- float gain = voices.GainAt(i);
- effects[i].volume = sfxVolume * gain * voiceScale * headroom;
- if (gain == 0 && effects[i].clip) { effects[i].Stop(); effects[i].clip = null; }
- }
- }
- public void Tick() { ApplyMix(); }
- public void SetMusicVolume(float value) { musicVolume = Mathf.Clamp01(value); ApplyMix(true); }
- public void SetSfxVolume(float value) { sfxVolume = Mathf.Clamp01(value); ApplyMix(true); }
- public bool TryPlayEffect(AudioClip clip, AudioCue cue, float gain)
- {
- if (!clip || !voices.TryStart(cue, gain, clock(), clip.length, out int slot)) return false;
- var source = effects[slot]; source.Stop(); source.clip = clip;
- ApplyMix(); if (audible) source.Play(); return true;
- }
- public void PlayEffect(AudioClip clip)
- {
- if (!clip) return;
- var cue = Enum.TryParse(clip.name, out AudioCue parsed) ? parsed : AudioCue.Tile_Match;
- TryPlayEffect(clip, cue, AudioMixCalibration.Gain(cue));
- }
- public void StopEffects() { voices.Clear(); foreach (var source in effects) if (source) { source.Stop(); source.clip = null; } ApplyMix(); }
- public void StopAll()
- {
- ++generation; fade?.Kill(false); fade = null; blend.Clear();
- foreach (var source in music) if (source) source.Stop(); StopEffects();
- }
- public void Dispose()
- {
- StopAll(); if (!root) return; root.SetActive(false);
- if (Application.isPlaying) Object.Destroy(root); else Object.DestroyImmediate(root);
- }
- }
- }
|