using System.Collections.Generic; using UnityEngine; namespace OCES.Audio { public class PitchStepManager { readonly Dictionary m_pitchStepCounts = new(); readonly Dictionary m_pitchStepLastTime = new(); internal float ResolvePitch(AudioObject audioObject, double time) { if (audioObject.PitchStep == 0 || audioObject.PitchStepThreshold == 0) //没配置PitchStep { return 1f; } // 超时了,或者没播过 if (!this.m_pitchStepLastTime.TryGetValue(audioObject.Id, out double lastPitchStepTime) || time - lastPitchStepTime > audioObject.PitchStepThreshold) { this.m_pitchStepCounts[audioObject.Id] = 0; this.m_pitchStepLastTime[audioObject.Id] = time; return 1f; } //命中了 int pitchStepCount = this.m_pitchStepCounts[audioObject.Id]; pitchStepCount = this.m_pitchStepCounts[audioObject.Id] = Mathf.Min(pitchStepCount + 1, audioObject.PitchStepLimit); this.m_pitchStepLastTime[audioObject.Id] = time; return Mathf.Pow(2, audioObject.PitchStep * pitchStepCount / 12f); } } }