refactor: WaitForAlignment coroutine use BeatClock.cs insted of calculate independently.

This commit is contained in:
2026-04-21 15:01:55 +08:00
parent 15f220a4c7
commit e46c324ac5
4 changed files with 30 additions and 36 deletions
Binary file not shown.
@@ -46,6 +46,13 @@ namespace OCES.Audio
Bar, Bar,
} }
public enum CallbackFlags
{
MusicSyncBeat = 1,
MusicSyncBar = 2,
MusicSyncGrid = 3,
}
public enum ActiveSoundState public enum ActiveSoundState
{ {
Pending, // 已进入调度,但还没真正播放 Pending, // 已进入调度,但还没真正播放
@@ -91,11 +98,4 @@ namespace OCES.Audio
public partial class MusicPath : IPathEntry { } public partial class MusicPath : IPathEntry { }
public partial class AmbiencePath : IPathEntry { } public partial class AmbiencePath : IPathEntry { }
public enum CallbackFlags
{
MusicSyncBeat = 1,
MusicSyncBar = 2,
MusicSyncGrid = 3,
}
} }
@@ -26,7 +26,7 @@ namespace OCES.Audio
this.m_onGrid = onGrid; this.m_onGrid = onGrid;
} }
public void Restart(MusicContainer container, float inheritedBpm, double dspTime) internal void Restart(MusicContainer container, float inheritedBpm, double dspTime)
{ {
//Debug.Log($"[BeatClock] Restart called, container={container.Id}, bpm={container.Bpm}, inherited={inheritedBpm}"); //Debug.Log($"[BeatClock] Restart called, container={container.Id}, bpm={container.Bpm}, inherited={inheritedBpm}");
@@ -63,6 +63,26 @@ namespace OCES.Audio
this.m_beatCoroutine = this.m_barCoroutine = this.m_gridCoroutine = null; this.m_beatCoroutine = this.m_barCoroutine = this.m_gridCoroutine = null;
} }
internal double GetNextDspTime(AlignMode mode)
{
double now = AudioSettings.dspTime;
if (this.m_blendError || this.m_stopped)
{
return now;
}
double elapsed = now - this.m_startDspTime;
double period = mode switch
{
AlignMode.Beat => this.m_secondsPerBeat,
AlignMode.Bar => this.m_secondsPerBeat * this.m_beatsPerBar,
_ => this.m_secondsPerBeat,
};
long next = (long)(elapsed / period) + 1L;
return this.m_startDspTime + next * period;
}
IEnumerator BeatCoroutine() IEnumerator BeatCoroutine()
{ {
double elapsed = AudioSettings.dspTime - this.m_startDspTime; double elapsed = AudioSettings.dspTime - this.m_startDspTime;
@@ -197,34 +197,8 @@ namespace OCES.Audio
if (mode == AlignMode.Immediate || container.Bpm <= 0f) if (mode == AlignMode.Immediate || container.Bpm <= 0f)
yield break; yield break;
double now = AudioSettings.dspTime; double target = this.m_beatClock.GetNextDspTime(mode);
double elapsed = now - this.m_playStartTime; yield return new WaitUntil(() => AudioSettings.dspTime >= target);
double secondsPerBeat = 60.0 / container.Bpm;
switch (mode)
{
case AlignMode.Beat:
{
double beatsElapsed = elapsed / secondsPerBeat;
double nextBeat = Math.Ceiling(beatsElapsed);
double waitSeconds = (nextBeat - beatsElapsed) * secondsPerBeat;
if (waitSeconds > 0.001)
yield return new WaitForSeconds((float)waitSeconds);
break;
}
case AlignMode.Bar:
{
int beatsPerBar = MusicContainerConfig.GetBeatsPerBar(container.TimeSig);
double secondsPerBar = secondsPerBeat * beatsPerBar;
double barsElapsed = elapsed / secondsPerBar;
double nextBar = Math.Ceiling(barsElapsed);
double waitSeconds = (nextBar - barsElapsed) * secondsPerBar;
if (waitSeconds > 0.001)
yield return new WaitForSeconds((float)waitSeconds);
break;
}
}
} }
// ───────────────────────────────────────────── // ─────────────────────────────────────────────