HeavenStudioPlus/Assets/Scripts/Conductor.cs

114 lines
3.4 KiB
C#
Raw Normal View History

2021-12-19 04:10:43 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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
{
2021-12-21 01:10:49 +00:00
[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;
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
private AudioDspTimeKeeper timeKeeper;
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
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
// 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
public void Play(float startBeat)
{
timeKeeper.Play();
}
public void Update()
2021-12-19 04:10:43 +00:00
{
if (!musicSource.isPlaying) return;
2021-12-21 01:10:49 +00:00
//determine how many seconds since the song started
// songPosition = (float)(timeKeeper.dspTime - dspSongTime - firstBeatOffset);
songPosition = (float)timeKeeper.GetCurrentTimeInSong();
print(songPosition);
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 07:38:55 +00:00
// print($"{mst_f}(AudioSource.time), {Time.frameCount}(Time.fasrameCount)");
// print($"{musicSource.time}(0), {songPosition}");
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)
{
float a = Mathp.Normalize(songPositionInBeats, startBeat, startBeat + length);
return a;
2021-12-21 01:10:49 +00:00
}
2021-12-23 02:28:05 +00:00
public void SetBpm(float bpm)
{
this.songBpm = bpm;
secPerBeat = 60f / songBpm;
}
2021-12-19 04:10:43 +00:00
}
}