feat: music callback

This commit is contained in:
2026-04-16 11:52:55 +08:00
parent a085e182c4
commit 0fb0c51f65
5 changed files with 8 additions and 14 deletions
@@ -29,7 +29,6 @@ namespace OCES.Audio
// ───────────────────────────────────────────── // ─────────────────────────────────────────────
public event Action<uint> OnBeat; public event Action<uint> OnBeat;
public event Action<uint> OnBar; public event Action<uint> OnBar;
public event Action<uint> OnGrid; public event Action<uint> OnGrid;
@@ -196,10 +195,13 @@ namespace OCES.Audio
EnumIds.RegisterAllGameState(); EnumIds.RegisterAllGameState();
ActiveStates = new Dictionary<Type, Enum>(); ActiveStates = new Dictionary<Type, Enum>();
}
void Start()
{
// ── 启动默认音乐与环境音 ── // ── 启动默认音乐与环境音 ──
// 触发一次初始状态,让音乐系统从默认状态开始匹配 // 触发一次初始状态,让音乐系统从默认状态开始匹配
//SetState(GameState.Home); SetState(GameState.Home);
} }
AudioObject ResolveSwitchContainer(AudioObject switchContainer) AudioObject ResolveSwitchContainer(AudioObject switchContainer)
@@ -28,7 +28,7 @@ namespace OCES.Audio
public void Restart(MusicContainer container, float inheritedBpm, double dspTime) public 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}");
StopAll(); StopAll();
this.m_blendError = this.m_stopped = false; this.m_blendError = this.m_stopped = false;
@@ -69,21 +69,15 @@ namespace OCES.Audio
while (true) while (true)
{ {
double nextTime = this.m_startDspTime + index * this.m_secondsPerBeat; double nextTime = this.m_startDspTime + index * this.m_secondsPerBeat;
if (index == 0)
{
Debug.Log($"[BeatClock] BeatCoroutine waiting, nextTime={nextTime}, now={AudioSettings.dspTime}");
}
yield return new WaitUntil(() => AudioSettings.dspTime >= nextTime - 0.02); yield return new WaitUntil(() => AudioSettings.dspTime >= nextTime - 0.02);
while (AudioSettings.dspTime < nextTime) while (AudioSettings.dspTime < nextTime)
yield return null; yield return null;
if (this.m_blendError || this.m_stopped){ if (this.m_blendError || this.m_stopped){
Debug.Log($"[BeatClock] Coroutine exiting early, blendError={m_blendError}, stopped={m_stopped}");
yield break; yield break;
} }
this.m_onBeat?.Invoke(this.m_containerId); this.m_onBeat?.Invoke(this.m_containerId);
Debug.Log($"[Beat] index={index}, nextTime={nextTime:F4}, actualDspTime={AudioSettings.dspTime:F4}, diff={(AudioSettings.dspTime - nextTime)*1000:F1}ms");
index++; index++;
} }
} }
@@ -99,7 +93,6 @@ namespace OCES.Audio
while (AudioSettings.dspTime < nextTime) while (AudioSettings.dspTime < nextTime)
yield return null; yield return null;
if (this.m_blendError || this.m_stopped){ if (this.m_blendError || this.m_stopped){
Debug.Log($"[BeatClock] Coroutine exiting early, blendError={m_blendError}, stopped={m_stopped}");
yield break; yield break;
} }
this.m_onBar?.Invoke(this.m_containerId); this.m_onBar?.Invoke(this.m_containerId);
@@ -118,7 +111,6 @@ namespace OCES.Audio
while (AudioSettings.dspTime < nextTime) while (AudioSettings.dspTime < nextTime)
yield return null; yield return null;
if (this.m_blendError || this.m_stopped){ if (this.m_blendError || this.m_stopped){
Debug.Log($"[BeatClock] Coroutine exiting early, blendError={m_blendError}, stopped={m_stopped}");
yield break; yield break;
} }
this.m_onGrid?.Invoke(this.m_containerId); this.m_onGrid?.Invoke(this.m_containerId);
@@ -105,7 +105,7 @@ namespace OCES.Audio
Action onFinished) Action onFinished)
{ {
float effectiveBpm = container.Bpm > 0f ? container.Bpm : inheritedBpm; float effectiveBpm = container.Bpm > 0f ? container.Bpm : inheritedBpm;
Debug.Log($"[MusicContainerPlayer] OnContainerEntered firing, container={container.Id}, subscribers={OnContainerEntered != null}"); //Debug.Log($"[MusicContainerPlayer] OnContainerEntered firing, container={container.Id}, subscribers={OnContainerEntered != null}");
OnContainerEntered?.Invoke(container, effectiveBpm, AudioSettings.dspTime); OnContainerEntered?.Invoke(container, effectiveBpm, AudioSettings.dspTime);
int loopsCompleted = 0; int loopsCompleted = 0;
@@ -3,7 +3,7 @@ using UnityEngine;
namespace OCES namespace OCES
{ {
public class CallBackTest : MonoBehaviour public class Metronome : MonoBehaviour
{ {
void Start() void Start()
{ {