Last active
August 7, 2021 12:20
-
-
Save Starpelly/102c70ccae766a0d4cbcc36a25a17569 to your computer and use it in GitHub Desktop.
Simple metronome for Rhythm Games
This file contains hidden or 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; | |
[RequireComponent(typeof(AudioSource))] | |
public class Conductor : MonoBehaviour | |
{ | |
//Song beats per minute | |
//This is determined by the song you're trying to sync up to | |
public float songBpm; | |
//The number of seconds for each song beat | |
public float secPerBeat; | |
//Current song position, in seconds | |
public float songPosition; | |
//Current song position, in beats | |
public float songPositionInBeats; | |
//How many seconds have passed since the song started | |
public float dspSongTime; | |
//an AudioSource attached to this GameObject that will play the music. | |
public AudioSource musicSource; | |
//The offset to the first beat of the song in seconds | |
public float firstBeatOffset; | |
//the number of beats in each loop | |
public float beatsPerLoop; | |
//the total number of loops completed since the looping clip first started | |
public int completedLoops = 0; | |
//The current position of the song within the loop in beats. | |
public float loopPositionInBeats; | |
//The current relative position of the song within the loop measured between 0 and 1. | |
public float loopPositionInAnalog; | |
//Conductor instance | |
public static Conductor instance; | |
//Pause times | |
private int pauseTime = 0; | |
void Awake() | |
{ | |
instance = this; | |
} | |
void Start() | |
{ | |
//Load the AudioSource attached to the Conductor GameObject | |
musicSource = GetComponent<AudioSource>(); | |
//Calculate the number of seconds in each beat | |
secPerBeat = 60f / songBpm; | |
//Record the time when the music starts | |
dspSongTime = (float)musicSource.time; | |
//Start the music | |
musicSource.Play(); | |
} | |
void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
pauseTime++; | |
if (pauseTime == 1) | |
musicSource.Pause(); | |
else if (pauseTime > 1) { musicSource.UnPause(); pauseTime = 0; } | |
} | |
//determine how many seconds since the song started | |
songPosition = (float)(musicSource.time - dspSongTime - firstBeatOffset); | |
//determine how many beats since the song started | |
songPositionInBeats = songPosition / secPerBeat; | |
//calculate the loop position | |
if (songPositionInBeats >= (completedLoops + 1) * beatsPerLoop) | |
completedLoops++; | |
loopPositionInBeats = songPositionInBeats - completedLoops * beatsPerLoop; | |
loopPositionInAnalog = loopPositionInBeats / beatsPerLoop; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment