HeavenStudioPlus/Assets/Scripts/Conductor.cs

363 lines
12 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;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio
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
2021-12-21 01:10:49 +00:00
public float songBpm;
2021-12-19 04:10:43 +00:00
// The number of seconds for each song beat
2021-12-21 01:10:49 +00:00
public float secPerBeat;
2021-12-19 04:10:43 +00:00
// The number of seconds for each song beat, inversely scaled to song pitch (higer pitch = shorter time)
public float pitchedSecPerBeat => (secPerBeat / musicSource.pitch);
// Current song position, in seconds
private float songPos; // for Conductor use only
2021-12-21 01:10:49 +00:00
public float songPosition;
2021-12-19 04:10:43 +00:00
// Current song position, in beats
private float songPosBeat; // for Conductor use only
2021-12-21 01:10:49 +00:00
public float songPositionInBeats;
2021-12-19 04:10:43 +00:00
// Current time of the song
private float time;
2021-12-19 04:10:43 +00:00
// an AudioSource attached to this GameObject that will play the music.
public AudioSource musicSource;
2021-12-19 04:10:43 +00:00
// The offset to the first beat of the song in seconds
public float firstBeatOffset;
2021-12-19 04:10:43 +00:00
// Conductor instance
2021-12-21 01:10:49 +00:00
public static Conductor instance;
2021-12-19 04:10:43 +00:00
2022-01-15 17:45:08 +00:00
// Conductor is currently playing song
public bool isPlaying;
2022-01-15 17:45:08 +00:00
// Conductor is currently paused, but not fully stopped
public bool isPaused;
2022-01-15 17:45:08 +00:00
// Last reported beat based on song position
private float lastReportedBeat = 0f;
// Metronome tick sound enabled
public bool metronome = false;
public float timeSinceLastTempoChange = 0;
2022-01-19 05:40:49 +00:00
private bool beat;
// private AudioDspTimeKeeper timeKeeper;
2021-12-21 01:10:49 +00:00
void Awake()
{
instance = this;
}
2021-12-19 04:10:43 +00:00
2022-01-08 16:42:48 +00:00
public void SetBeat(float beat)
{
2022-01-08 16:42:48 +00:00
float secFromBeat = GetSongPosFromBeat(beat);
2021-12-19 04:10:43 +00:00
2022-01-09 23:35:55 +00:00
if (musicSource.clip != null)
{
if (secFromBeat < musicSource.clip.length)
musicSource.time = secFromBeat;
else
musicSource.time = 0;
}
2022-01-08 16:42:48 +00:00
GameManager.instance.SetCurrentEventToClosest(beat);
songPosBeat = beat;
songPositionInBeats = songPosBeat;
}
2022-01-08 16:42:48 +00:00
public void Play(float beat)
{
bool negativeOffset = firstBeatOffset < 0f;
bool negativeStartTime = false;
// Debug.Log("starting playback @ beat " + beat + ", offset is " + firstBeatOffset);
2022-02-24 14:02:21 +00:00
var startPos = GetSongPosFromBeat(beat);
if (negativeOffset)
{
time = startPos;
}
else
{
negativeStartTime = startPos - firstBeatOffset < 0f;
if (negativeStartTime)
time = startPos - firstBeatOffset;
else
time = startPos;
}
//TODO: make this take into account past tempo changes
songPosBeat = GetBeatFromSongPos(time - firstBeatOffset);
// Debug.Log("corrected starting playback @ beat " + songPosBeat);
isPlaying = true;
isPaused = false;
2022-02-24 14:02:21 +00:00
if (SongPosLessThanClipLength(startPos))
2022-01-08 16:42:48 +00:00
{
if (negativeOffset)
{
var musicStartTime = startPos + firstBeatOffset;
if (musicStartTime < 0f)
{
musicSource.time = startPos;
musicSource.PlayScheduled(AudioSettings.dspTime - firstBeatOffset / musicSource.pitch);
}
else
{
musicSource.time = musicStartTime;
musicSource.PlayScheduled(AudioSettings.dspTime);
}
}
else
{
if (negativeStartTime)
{
musicSource.time = startPos;
}
else
{
musicSource.time = startPos + firstBeatOffset;
}
musicSource.PlayScheduled(AudioSettings.dspTime);
}
2022-01-08 16:42:48 +00:00
}
// GameManager.instance.SetCurrentEventToClosest(songPositionInBeats);
2021-12-21 01:10:49 +00:00
}
2021-12-19 04:10:43 +00:00
public void Pause()
{
isPlaying = false;
isPaused = true;
musicSource.Pause();
}
2022-01-08 16:42:48 +00:00
public void Stop(float time)
{
2022-01-08 16:42:48 +00:00
this.time = time;
songPosBeat = 0;
songPositionInBeats = 0;
isPlaying = false;
isPaused = false;
musicSource.Stop();
2021-12-31 14:46:11 +00:00
}
float test;
2021-12-31 14:46:11 +00:00
public void Update()
2021-12-19 04:10:43 +00:00
{
secPerBeat = 60f / songBpm;
if (isPlaying)
{
2022-02-24 14:02:21 +00:00
var dt = Time.unscaledDeltaTime * musicSource.pitch;
time += dt;
2021-12-19 04:10:43 +00:00
songPos = time;
songPosition = songPos;
2021-12-19 04:10:43 +00:00
2022-02-24 14:02:21 +00:00
songPosBeat += (dt / secPerBeat);
songPositionInBeats = songPosBeat;
// songPositionInBeats = Time.deltaTime / secPerBeat;
2022-01-15 17:45:08 +00:00
if (metronome)
{
2022-01-19 05:40:49 +00:00
if (ReportBeat(ref lastReportedBeat))
2022-01-15 17:45:08 +00:00
{
2022-01-19 05:40:49 +00:00
Util.Jukebox.PlayOneShot("metronome");
2022-01-15 17:45:08 +00:00
}
else if (songPositionInBeats < lastReportedBeat)
2022-01-15 17:45:08 +00:00
{
lastReportedBeat = Mathf.Round(songPositionInBeats);
2022-01-15 17:45:08 +00:00
}
}
}
2021-12-21 01:10:49 +00:00
}
2021-12-19 04:10:43 +00:00
public bool ReportBeat(ref float lastReportedBeat, float offset = 0, bool shiftBeatToOffset = true)
2022-01-19 05:40:49 +00:00
{
bool result = songPositionInBeats + (shiftBeatToOffset ? offset : 0f) >= (lastReportedBeat) + 1f;
if (result)
2022-01-19 05:40:49 +00:00
{
lastReportedBeat += 1f;
if (lastReportedBeat < songPositionInBeats)
{
lastReportedBeat = Mathf.Round(songPositionInBeats);
}
2022-01-19 05:40:49 +00:00
}
return result;
}
2022-02-03 22:20:26 +00:00
public float GetLoopPositionFromBeat(float beatOffset, float length)
{
2022-02-09 03:58:25 +00:00
return Mathf.Repeat((songPositionInBeats / length) + beatOffset, 1);
2022-02-03 22:20:26 +00:00
}
public float GetPositionFromBeat(float startBeat, float length)
2021-12-21 01:10:49 +00:00
{
float a = Mathp.Normalize(songPositionInBeats, startBeat, startBeat + length);
return a;
2021-12-21 01:10:49 +00:00
}
2022-03-01 06:38:38 +00:00
public float GetBeatFromPosition(float position, float startBeat, float length)
{
return Mathp.DeNormalize(position, startBeat, startBeat + length);
}
public float GetPositionFromMargin(float targetBeat, float margin)
{
return GetPositionFromBeat(targetBeat - margin, margin);
}
2022-03-01 06:38:38 +00:00
public float GetBeatFromPositionAndMargin(float position, float targetBeat, float margin)
{
return GetBeatFromPosition(position, targetBeat - margin, margin);
}
private List<Beatmap.TempoChange> GetSortedTempoChanges(Beatmap chart)
{
//iterate over all tempo changes, adding to counter
List<Beatmap.TempoChange> tempoChanges = chart.tempoChanges;
tempoChanges.Sort((x, y) => x.beat.CompareTo(y.beat)); //sorts all tempo changes by ascending time (GameManager already does this but juste en cas...)
return tempoChanges;
}
public float GetSongPosFromBeat(float beat)
2021-12-31 14:46:11 +00:00
{
Beatmap chart = GameManager.instance.Beatmap;
SetBpm(chart.bpm);
//initial counter
float counter = 0f;
//time of last tempo change, to know how much to add to counter
float lastTempoChangeBeat = 0f;
//iterate over all tempo changes, adding to counter
List<Beatmap.TempoChange> tempoChanges = GetSortedTempoChanges(chart);
foreach (var t in tempoChanges)
{
if (t.beat > beat)
{
// this tempo change is past our requested time, abort
break;
}
// Debug.Log("tempo change at " + t.beat);
counter += (t.beat - lastTempoChangeBeat) * secPerBeat;
// Debug.Log("counter is now " + counter);
// now update to new bpm
SetBpm(t.tempo);
lastTempoChangeBeat = t.beat;
}
//passed all past tempo changes, now extrapolate from last tempo change until requested position
counter += (beat - lastTempoChangeBeat) * secPerBeat;
// Debug.Log("GetSongPosFromBeat returning " + counter);
return counter;
2021-12-31 14:46:11 +00:00
}
//thank you @wooningcharithri#7419 for the psuedo-code
private float BeatsToSecs(float beats, float bpm)
{
// Debug.Log("BeatsToSecs returning " + beats / bpm * 60);
return beats / bpm * 60f;
}
private float SecsToBeats(float s, float bpm)
{
// Debug.Log("SecsToBeats returning " + s / 60f / bpm);
return s / 60f * bpm;
}
public float GetBeatFromSongPos(float seconds)
{
// Debug.Log("Getting beat of seconds " + seconds);
Beatmap chart = GameManager.instance.Beatmap;
float lastTempoChangeBeat = 0f;
float lastBpm = chart.bpm;
float counterSeconds = -firstBeatOffset;
List<Beatmap.TempoChange> tempoChanges = GetSortedTempoChanges(chart);
foreach (var t in tempoChanges)
{
float beatToNext = t.beat - lastTempoChangeBeat;
float secToNext = BeatsToSecs(beatToNext, lastBpm);
float nextSecs = counterSeconds + secToNext;
// Debug.Log("nextSecs is " + nextSecs + ", seconds " + seconds);
if (nextSecs >= seconds)
break;
lastTempoChangeBeat = t.beat;
lastBpm = t.tempo;
counterSeconds = nextSecs;
}
// Debug.Log("lastTempoChangeBeat is " + lastTempoChangeBeat + ", counterSeconds is " + counterSeconds);
return lastTempoChangeBeat + SecsToBeats(seconds - counterSeconds, lastBpm);
}
//
// convert real seconds to beats
public float GetRestFromRealTime(float seconds)
{
return seconds/pitchedSecPerBeat;
}
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
2022-03-19 12:46:38 +00:00
public void SetVolume(int percent)
{
musicSource.volume = percent / 100f;
}
2022-01-06 00:11:33 +00:00
public float SongLengthInBeats()
{
if (!musicSource.clip) return 0;
return GetBeatFromSongPos(musicSource.clip.length);
2022-01-06 00:11:33 +00:00
}
2022-01-08 16:42:48 +00:00
public bool SongPosLessThanClipLength(float t)
{
2022-01-09 23:35:55 +00:00
if (musicSource.clip != null)
return t < musicSource.clip.length;
else
return false;
2022-01-08 16:42:48 +00:00
}
2022-01-11 00:17:29 +00:00
public bool NotStopped()
{
return Conductor.instance.isPlaying == true || Conductor.instance.isPaused == true;
2022-01-11 00:17:29 +00:00
}
2021-12-19 04:10:43 +00:00
}
}