Last active
January 28, 2022 05:03
-
-
Save yoko57822/6134787944b80aa241ee460623be8845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Test_Sample : MonoBehaviour | |
{ | |
[Header("Metronome Setting")] | |
[SerializeField] float MusicBPM = 60f; | |
const float StdBPM = 60f; | |
double SampleRate = 0f; | |
double NextTime = 0f; | |
double TikTime = 0f; | |
bool Ticked = false; | |
int N; | |
void Start() | |
{ | |
double StartTick = AudioSettings.dspTime; | |
SampleRate = AudioSettings.outputSampleRate; | |
TikTime = (StdBPM / MusicBPM); | |
NextTime = StartTick + TikTime; | |
} | |
private void LateUpdate() | |
{ | |
if (!Ticked && NextTime >= AudioSettings.dspTime) | |
{ | |
Ticked = true; | |
PlayTik(); | |
} | |
} | |
private void FixedUpdate() | |
{ | |
Metronome(); | |
} | |
void Metronome() | |
{ | |
TikTime = (StdBPM / MusicBPM); | |
if (AudioSettings.dspTime >= NextTime) | |
{ | |
Ticked = false; | |
NextTime += TikTime; | |
} | |
} | |
void PlayTik() | |
{ | |
//Debug.Log(NextTime); 시간 오차 체크 | |
N++; | |
if (N > 4) | |
{ | |
N = 1; | |
} | |
Debug.Log("Tick : " + N + "/" + 4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment