From 3df705dfe5579c7d06b8bb961e7a6cb358f82ead Mon Sep 17 00:00:00 2001 From: Dom Harris Date: Thu, 7 Jan 2021 10:05:19 +0000 Subject: [PATCH] fixed audio sync issues --- .../FMOD/Cache/Editor/FMODStudioCache.asset | 6 +-- Assets/Prefabs/Enemy.prefab | 2 +- Assets/Scenes/Chief's Sandbox.unity | 14 ++--- Assets/Scripts/AudioBeatManager.cs | 21 +++++++- Assets/Scripts/DisableOnDeath.cs | 4 +- Assets/Scripts/MoveAfterDelay.cs | 54 ++++++++++++++++--- Assets/Weapons/Enemy.asset | 2 +- ProjectSettings/ProjectSettings.asset | 4 +- 8 files changed, 83 insertions(+), 24 deletions(-) diff --git a/Assets/Plugins/FMOD/Cache/Editor/FMODStudioCache.asset b/Assets/Plugins/FMOD/Cache/Editor/FMODStudioCache.asset index a8b83c0..08a8648 100644 --- a/Assets/Plugins/FMOD/Cache/Editor/FMODStudioCache.asset +++ b/Assets/Plugins/FMOD/Cache/Editor/FMODStudioCache.asset @@ -73,7 +73,7 @@ MonoBehaviour: m_EditorClassIdentifier: Path: Assets/FMOD/Desktop/Master.bank Name: Master - lastModified: 637455820886389038 + lastModified: 637456045920000000 LoadResult: 0 FileSizes: [] Exists: 1 @@ -122,7 +122,7 @@ MonoBehaviour: - {fileID: -4840949124066270359} StringsBanks: - {fileID: 2930724805879487167} - stringsBankWriteTime: 637455820886398947 + stringsBankWriteTime: 637456045920000000 cacheVersion: 4 --- !u!114 &2930724805879487167 MonoBehaviour: @@ -138,7 +138,7 @@ MonoBehaviour: m_EditorClassIdentifier: Path: Assets/FMOD/Desktop/Master.strings.bank Name: Master.strings - lastModified: 637455820886398947 + lastModified: 637456045920000000 LoadResult: 0 FileSizes: - Name: diff --git a/Assets/Prefabs/Enemy.prefab b/Assets/Prefabs/Enemy.prefab index 708920a..e18f727 100644 --- a/Assets/Prefabs/Enemy.prefab +++ b/Assets/Prefabs/Enemy.prefab @@ -135,6 +135,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 331122d8907f44ed9b0f8f2337d176b9, type: 3} m_Name: m_EditorClassIdentifier: - beatsBeforeMove: 160 + beatsBeforeMove: 16 scaleAmount: 1.5 numBeatsAfterScale: 0 diff --git a/Assets/Scenes/Chief's Sandbox.unity b/Assets/Scenes/Chief's Sandbox.unity index 6179379..56a1461 100644 --- a/Assets/Scenes/Chief's Sandbox.unity +++ b/Assets/Scenes/Chief's Sandbox.unity @@ -243,9 +243,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 4f8399031f014bee928b07d26fe8ee4c, type: 3} m_Name: m_EditorClassIdentifier: - pauseOnBeat: 160 - pauseLengthInBeats: 72 - beatOffset: 16 + pauseOnBeat: 16 + pauseLengthInBeats: 8 + beatOffset: 2 --- !u!20 &502054319 stripped Camera: m_CorrespondingSourceObject: {fileID: 3397488660173986648, guid: 1f82f952c53fc7449a0091cf29ba3def, type: 3} @@ -527,7 +527,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 3397488660842926093, guid: 1f82f952c53fc7449a0091cf29ba3def, type: 3} propertyPath: m_LocalScale.x - value: 2085.424 + value: 1847.5209 objectReference: {fileID: 0} - target: {fileID: 3397488660842926093, guid: 1f82f952c53fc7449a0091cf29ba3def, type: 3} propertyPath: m_LocalScale.y @@ -774,9 +774,9 @@ MonoBehaviour: m_StringArgument: m_BoolArgument: 0 m_CallState: 2 - shootOnBeat: 160 - noTargetLengthInBeats: 72 - beatOffset: 16 + shootOnBeat: 16 + noTargetLengthInBeats: 8 + beatOffset: 2 --- !u!1 &1215971662 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/AudioBeatManager.cs b/Assets/Scripts/AudioBeatManager.cs index af6f711..1678bf4 100644 --- a/Assets/Scripts/AudioBeatManager.cs +++ b/Assets/Scripts/AudioBeatManager.cs @@ -1,32 +1,51 @@ using System; using System.Collections; using System.Collections.Generic; +using DG.Tweening; +using FMOD; +using FMODUnity; using UnityEngine; using UnityEngine.Serialization; +using Debug = UnityEngine.Debug; public class AudioBeatManager : MonoBehaviour, IAudioBeatManager { [SerializeField] private float bpm; - public float TimeBetweenBeats => _bps * Time.deltaTime; + public float TimeBetweenBeats => _secPerBeat; private float _bps; private int _currentBeat = 0; private float _timer; + private float _secPerBeat; + [SerializeField] [FormerlySerializedAs("OnBeat")] private IntEvent _onBeat; public IntEvent OnBeat => _onBeat; public event Action OnBeatEvent; + + private ChannelGroup _channelGroup; + private ulong _dspClock; + private int _sampleRate; + private float _initialPower; + + public float DspTime => _dspClock / (float)_sampleRate; + private void Awake() { _bps = bpm / 60f; + _secPerBeat = 60f / bpm; _timer = 0; + RuntimeManager.CoreSystem.getMasterChannelGroup(out _channelGroup); + RuntimeManager.CoreSystem.getSoftwareFormat(out _sampleRate, out _, out _); + DOTween.SetTweensCapacity(2000,100); } // Update is called once per frame void Update() { + _channelGroup.getDSPClock(out _dspClock, out _); _timer += Time.deltaTime; if (_timer >= TimeBetweenBeats) { diff --git a/Assets/Scripts/DisableOnDeath.cs b/Assets/Scripts/DisableOnDeath.cs index b8241ce..4c3201f 100644 --- a/Assets/Scripts/DisableOnDeath.cs +++ b/Assets/Scripts/DisableOnDeath.cs @@ -32,8 +32,8 @@ public class DisableOnDeath : MonoBehaviour var oldName = gameObject.name; gameObject.name = "disabled"; _renderer.material.DOColor(Color.white, 0.1f); - _renderer.material.DOFade(0, 0.2f).SetDelay(0.3f); - transform.DOScale(Vector3.one * 1.5f, 0.5f).SetEase(Ease.OutQuint).OnComplete(() => + _renderer.material.DOFade(0, 0.1f).SetDelay(0.2f); + transform.DOScale(Vector3.one * 1.5f, 0.3f).SetEase(Ease.OutQuint).OnComplete(() => { gameObject.name = oldName; gameObject.SetActive(false); diff --git a/Assets/Scripts/MoveAfterDelay.cs b/Assets/Scripts/MoveAfterDelay.cs index a03eca1..4de5075 100644 --- a/Assets/Scripts/MoveAfterDelay.cs +++ b/Assets/Scripts/MoveAfterDelay.cs @@ -1,9 +1,7 @@ -using System; using System.Collections; -using System.Collections.Generic; using DG.Tweening; using UnityEngine; -using Random = UnityEngine.Random; +using Utils; public class MoveAfterDelay : MonoBehaviour { @@ -33,10 +31,50 @@ public class MoveAfterDelay : MonoBehaviour { if (beat % beatsBeforeMove != 0) return; - DOTween.Sequence() - .Append(transform.DOScale(Vector3.one * scaleAmount, _audio.TimeBetweenBeats*16).SetEase(Ease.InQuint)) - .Append(transform.DOScale(Vector3.one, _audio.TimeBetweenBeats*16).SetEase(Ease.OutQuint)) - .Append(transform.DOMoveZ(-30, 1.0f).SetEase(Ease.InOutQuint).SetDelay(_audio.TimeBetweenBeats * numBeatsAfterScale)) - .Play(); + var pos = transform.localPosition; + pos.z = 0; + var dir = pos.normalized; + pos = transform.localPosition; + + DoAnim(pos, dir).Run(); + } + + private IEnumerator DoAnim(Vector3 pos, Vector3 dir) + { + var keepGoing = true; + var startTime = _audio.DspTime; + var targetDSPTime = _audio.TimeBetweenBeats * .25f; + + while (keepGoing) + { + var currentDSPTime = _audio.DspTime - startTime; + + var t = currentDSPTime / targetDSPTime; + transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * scaleAmount, 1 - Mathf.Pow(1 - t, 5)); + transform.localPosition = Vector3.Lerp(pos, pos + dir * 4, t); + if (currentDSPTime >= targetDSPTime) + keepGoing = false; + yield return null; + } + + startTime = _audio.DspTime; + keepGoing = true; + targetDSPTime = _audio.TimeBetweenBeats * .25f; + + while (keepGoing) + { + var currentDSPTime = _audio.DspTime - startTime; + + var t = currentDSPTime / targetDSPTime; + transform.localScale = Vector3.Lerp(Vector3.one* scaleAmount, Vector3.one, t*t*t*t*t); + transform.localPosition = Vector3.Lerp(pos + dir * 4, pos, t); + if (currentDSPTime >= targetDSPTime) + keepGoing = false; + yield return null; + } + + yield return new WaitForSeconds(_audio.TimeBetweenBeats); + + transform.DOMoveZ(-30, _audio.TimeBetweenBeats * 2).SetEase(Ease.OutQuint); } } \ No newline at end of file diff --git a/Assets/Weapons/Enemy.asset b/Assets/Weapons/Enemy.asset index db543f2..6840dc4 100644 --- a/Assets/Weapons/Enemy.asset +++ b/Assets/Weapons/Enemy.asset @@ -106,7 +106,7 @@ MonoBehaviour: m_Bits: 64 accuracy: 0 zone: - numToSpawn: 1 + numToSpawn: 10 offset: {x: 0, y: 0} spawnType: 0 spawnDir: 0 diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 1fc4008..74626d8 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -128,7 +128,9 @@ PlayerSettings: 16:9: 1 Others: 1 bundleVersion: 0.1 - preloadedAssets: [] + preloadedAssets: + - {fileID: 0} + - {fileID: 0} metroInputSource: 0 wsaTransparentSwapchain: 0 m_HolographicPauseOnTrackingLoss: 1