2021-12-19 04:10:43 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2021-12-31 06:56:51 +00:00
|
|
|
using UnityEngine.Audio;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
|
|
|
using Starpelly;
|
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
namespace RhythmHeavenMania
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
2022-01-03 22:42:43 +00:00
|
|
|
// [RequireComponent(typeof(AudioSource))]
|
2021-12-21 01:10:49 +00:00
|
|
|
public class Conductor : MonoBehaviour
|
|
|
|
{
|
|
|
|
//Song beats per minute
|
|
|
|
//This is determined by the song you're trying to sync up to
|
|
|
|
public float songBpm;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//The number of seconds for each song beat
|
|
|
|
public float secPerBeat;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//Current song position, in seconds
|
|
|
|
public float songPosition;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//Current song position, in beats
|
|
|
|
public float songPositionInBeats;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//How many seconds have passed since the song started
|
|
|
|
public float dspSongTime;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//an AudioSource attached to this GameObject that will play the music.
|
|
|
|
public AudioSource musicSource;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//The offset to the first beat of the song in seconds
|
|
|
|
public float firstBeatOffset;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//the number of beats in each loop
|
|
|
|
public float beatsPerLoop;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//the total number of loops completed since the looping clip first started
|
|
|
|
public int completedLoops = 0;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//The current position of the song within the loop in beats.
|
|
|
|
public float loopPositionInBeats;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//The current relative position of the song within the loop measured between 0 and 1.
|
|
|
|
public float loopPositionInAnalog;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//Conductor instance
|
|
|
|
public static Conductor instance;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-31 06:56:51 +00:00
|
|
|
private AudioDspTimeKeeper timeKeeper;
|
2021-12-28 02:36:27 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
void Awake()
|
|
|
|
{
|
|
|
|
instance = this;
|
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
//Load the AudioSource attached to the Conductor GameObject
|
|
|
|
musicSource = GetComponent<AudioSource>();
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-31 06:56:51 +00:00
|
|
|
timeKeeper = GetComponent<AudioDspTimeKeeper>();
|
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//Calculate the number of seconds in each beat
|
|
|
|
secPerBeat = 60f / songBpm;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//Record the time when the music starts
|
2021-12-31 06:56:51 +00:00
|
|
|
// dspSongTime = (float)musicSource.time;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//Start the music
|
|
|
|
// musicSource.Play();
|
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-28 02:36:27 +00:00
|
|
|
public void Play(float startBeat)
|
|
|
|
{
|
2021-12-31 06:56:51 +00:00
|
|
|
timeKeeper.Play();
|
2021-12-31 14:46:11 +00:00
|
|
|
SetTime(startBeat);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTime(float startBeat)
|
|
|
|
{
|
|
|
|
musicSource.time = GetSongPosFromBeat(startBeat);
|
|
|
|
songPositionInBeats = musicSource.time / secPerBeat;
|
|
|
|
GameManager.instance.SetCurrentEventToClosest(songPositionInBeats);
|
2021-12-28 02:36:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 04:48:39 +00:00
|
|
|
public void Update()
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
2021-12-31 06:56:51 +00:00
|
|
|
if (!musicSource.isPlaying) return;
|
2021-12-28 02:36:27 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//determine how many seconds since the song started
|
2021-12-31 06:56:51 +00:00
|
|
|
// songPosition = (float)(timeKeeper.dspTime - dspSongTime - firstBeatOffset);
|
|
|
|
songPosition = (float)timeKeeper.GetCurrentTimeInSong();
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//determine how many beats since the song started
|
|
|
|
songPositionInBeats = songPosition / secPerBeat;
|
2021-12-28 02:36:27 +00:00
|
|
|
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
//calculate the loop position
|
|
|
|
if (songPositionInBeats >= (completedLoops + 1) * beatsPerLoop)
|
|
|
|
completedLoops++;
|
|
|
|
loopPositionInBeats = songPositionInBeats - completedLoops * beatsPerLoop;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
loopPositionInAnalog = loopPositionInBeats / beatsPerLoop;
|
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
public float GetLoopPositionFromBeat(float startBeat, float length)
|
|
|
|
{
|
2021-12-27 04:48:39 +00:00
|
|
|
float a = Mathp.Normalize(songPositionInBeats, startBeat, startBeat + length);
|
|
|
|
return a;
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 14:46:11 +00:00
|
|
|
private float GetSongPosFromBeat(float beat)
|
|
|
|
{
|
|
|
|
return secPerBeat * beat;
|
|
|
|
}
|
|
|
|
|
2021-12-23 02:28:05 +00:00
|
|
|
public void SetBpm(float bpm)
|
|
|
|
{
|
|
|
|
this.songBpm = bpm;
|
|
|
|
secPerBeat = 60f / songBpm;
|
|
|
|
}
|
2022-01-06 00:11:33 +00:00
|
|
|
|
|
|
|
public float SongLengthInBeats()
|
|
|
|
{
|
|
|
|
return musicSource.clip.length / secPerBeat;
|
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
}
|
|
|
|
}
|