2021-01-06 16:10:11 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2021-01-07 11:05:19 +01:00
|
|
|
using DG.Tweening;
|
|
|
|
using FMOD;
|
|
|
|
using FMODUnity;
|
2021-01-09 18:29:48 +01:00
|
|
|
using Ktyl.Util;
|
2021-01-06 16:10:11 +01:00
|
|
|
using UnityEngine;
|
2021-01-07 03:00:17 +01:00
|
|
|
using UnityEngine.Serialization;
|
2021-01-07 11:05:19 +01:00
|
|
|
using Debug = UnityEngine.Debug;
|
2021-01-06 16:10:11 +01:00
|
|
|
|
2021-01-07 03:00:17 +01:00
|
|
|
public class AudioBeatManager : MonoBehaviour, IAudioBeatManager
|
2021-01-06 16:10:11 +01:00
|
|
|
{
|
2021-01-09 18:29:48 +01:00
|
|
|
[SerializeField] private SerialFloat _distanceToNextBeat;
|
|
|
|
// [SerializeField] private SerialFloat _distanceToOffbeat;
|
|
|
|
// [SerializeField] private SerialFloat _distanceToMeasure;
|
|
|
|
|
|
|
|
[SerializeField] private SerialFloat _distanceSinceLastBeat;
|
|
|
|
// [SerializeField] private SerialFloat _distanceSinceOffbeat;
|
|
|
|
// [SerializeField] private SerialFloat _distanceSinceMeasure;
|
|
|
|
|
2021-01-06 16:10:11 +01:00
|
|
|
[SerializeField] private float bpm;
|
2021-01-09 17:18:29 +01:00
|
|
|
|
|
|
|
private float _secPerBeat;
|
2021-01-07 11:05:19 +01:00
|
|
|
public float TimeBetweenBeats => _secPerBeat;
|
2021-01-06 16:10:11 +01:00
|
|
|
|
|
|
|
private int _currentBeat = 0;
|
|
|
|
private float _timer;
|
2021-01-07 03:00:17 +01:00
|
|
|
|
2021-01-07 11:05:19 +01:00
|
|
|
|
2021-01-07 03:00:17 +01:00
|
|
|
[SerializeField] [FormerlySerializedAs("OnBeat")]
|
|
|
|
private IntEvent _onBeat;
|
|
|
|
|
|
|
|
public IntEvent OnBeat => _onBeat;
|
2021-01-06 16:10:11 +01:00
|
|
|
public event Action<int> OnBeatEvent;
|
2021-01-07 11:05:19 +01:00
|
|
|
|
|
|
|
private ChannelGroup _channelGroup;
|
|
|
|
private ulong _dspClock;
|
|
|
|
private int _sampleRate;
|
|
|
|
private float _initialPower;
|
|
|
|
|
|
|
|
public float DspTime => _dspClock / (float)_sampleRate;
|
|
|
|
|
2021-01-06 16:10:11 +01:00
|
|
|
private void Awake()
|
|
|
|
{
|
2021-01-09 19:18:55 +01:00
|
|
|
DOTween.Init();
|
|
|
|
|
2021-01-07 11:05:19 +01:00
|
|
|
_secPerBeat = 60f / bpm;
|
|
|
|
RuntimeManager.CoreSystem.getMasterChannelGroup(out _channelGroup);
|
|
|
|
RuntimeManager.CoreSystem.getSoftwareFormat(out _sampleRate, out _, out _);
|
|
|
|
DOTween.SetTweensCapacity(2000,100);
|
2021-01-06 16:10:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 18:29:48 +01:00
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
_timer = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-06 16:10:11 +01:00
|
|
|
void Update()
|
|
|
|
{
|
2021-01-07 11:05:19 +01:00
|
|
|
_channelGroup.getDSPClock(out _dspClock, out _);
|
2021-01-09 18:29:48 +01:00
|
|
|
// _timer += Time.deltaTime;
|
|
|
|
|
|
|
|
// new beats
|
|
|
|
var beatsElapsed = (int)(DspTime / TimeBetweenBeats);
|
|
|
|
|
|
|
|
var lastBeatTime = beatsElapsed * TimeBetweenBeats;
|
|
|
|
|
|
|
|
var timeSinceLastBeat = DspTime - lastBeatTime;
|
|
|
|
_distanceSinceLastBeat.Value = timeSinceLastBeat / TimeBetweenBeats;
|
|
|
|
|
|
|
|
var timeToNextBeat = (lastBeatTime+TimeBetweenBeats) - DspTime;
|
|
|
|
_distanceToNextBeat.Value = timeToNextBeat / TimeBetweenBeats;
|
|
|
|
|
|
|
|
if (beatsElapsed > _currentBeat)
|
2021-01-06 16:10:11 +01:00
|
|
|
{
|
2021-01-09 18:29:48 +01:00
|
|
|
// a beat gone done did do happen
|
|
|
|
|
|
|
|
// account for this frame being a little bit past the beat!
|
|
|
|
_timer = timeSinceLastBeat;
|
|
|
|
|
2021-01-06 16:10:11 +01:00
|
|
|
++_currentBeat;
|
|
|
|
OnBeat?.Invoke(_currentBeat);
|
|
|
|
OnBeatEvent?.Invoke(_currentBeat);
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 18:29:48 +01:00
|
|
|
|
|
|
|
private void UpdateTimings()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2021-01-07 03:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface IAudioBeatManager
|
|
|
|
{
|
|
|
|
public IntEvent OnBeat { get; }
|
|
|
|
public event Action<int> OnBeatEvent;
|
2021-01-06 16:10:11 +01:00
|
|
|
}
|