Skip to content

Instantly share code, notes, and snippets.

@yoko57822
Last active January 28, 2022 05:03
Show Gist options
  • Save yoko57822/6134787944b80aa241ee460623be8845 to your computer and use it in GitHub Desktop.
Save yoko57822/6134787944b80aa241ee460623be8845 to your computer and use it in GitHub Desktop.
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