2022-09-23 02:05:04 +00:00
|
|
|
using System;
|
2023-09-11 22:28:04 +00:00
|
|
|
using System.Collections;
|
2021-12-19 04:10:43 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
using Starpelly;
|
2023-06-10 19:13:29 +00:00
|
|
|
using Jukebox;
|
2023-09-11 22:28:04 +00:00
|
|
|
using HeavenStudio.Util;
|
2023-10-29 19:44:47 +00:00
|
|
|
using System.Data.Common;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
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
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
public struct AddedPitchChange
|
|
|
|
{
|
|
|
|
public double time;
|
|
|
|
public float pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<AddedPitchChange> addedPitchChanges = new List<AddedPitchChange>();
|
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
// 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
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
// The number of seconds for each song beat
|
2023-06-10 19:13:29 +00:00
|
|
|
public float secPerBeat => (float)secPerBeatAsDouble;
|
|
|
|
public double secPerBeatAsDouble;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2022-03-22 18:29:15 +00:00
|
|
|
// The number of seconds for each song beat, inversely scaled to song pitch (higer pitch = shorter time)
|
2023-06-10 19:13:29 +00:00
|
|
|
public float pitchedSecPerBeat => (float)pitchedSecPerBeatAsDouble;
|
|
|
|
public double pitchedSecPerBeatAsDouble => (secPerBeat / SongPitch);
|
2022-03-22 18:29:15 +00:00
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
// Current song position, in seconds
|
2022-09-23 02:05:04 +00:00
|
|
|
private double songPos; // for Conductor use only
|
2023-09-11 22:28:04 +00:00
|
|
|
public float songPosition => (float)songPos;
|
2023-01-05 04:04:31 +00:00
|
|
|
public double songPositionAsDouble => songPos;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
// Current song position, in beats
|
2023-06-04 04:30:42 +00:00
|
|
|
public double songPosBeat; // for Conductor use only
|
2023-09-11 22:28:04 +00:00
|
|
|
public float songPositionInBeats => (float)songPosBeat;
|
2023-01-05 04:04:31 +00:00
|
|
|
public double songPositionInBeatsAsDouble => songPosBeat;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
// Current time of the song
|
2022-09-23 02:05:04 +00:00
|
|
|
private double time;
|
2023-09-11 22:28:04 +00:00
|
|
|
double dspTime;
|
|
|
|
double absTime, absTimeAdjust;
|
2023-10-29 19:44:47 +00:00
|
|
|
double dspSizeSeconds;
|
2023-09-11 22:28:04 +00:00
|
|
|
double dspMargin = 128 / 44100.0;
|
2023-12-26 05:22:51 +00:00
|
|
|
bool deferTimeKeeping = false;
|
2022-09-18 20:48:14 +00:00
|
|
|
|
2023-01-05 04:04:31 +00:00
|
|
|
// the dspTime we started at
|
2023-06-10 19:13:29 +00:00
|
|
|
private double dspStart;
|
|
|
|
private float dspStartTime => (float)dspStart;
|
|
|
|
public double dspStartTimeAsDouble => dspStart;
|
|
|
|
DateTime startTime;
|
2023-01-05 04:04:31 +00:00
|
|
|
|
2023-01-12 01:42:12 +00:00
|
|
|
//the beat we started at
|
2023-09-11 22:28:04 +00:00
|
|
|
private double startPos;
|
2023-01-12 01:42:12 +00:00
|
|
|
private double startBeat;
|
|
|
|
public double startBeatAsDouble => startBeat;
|
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
// an AudioSource attached to this GameObject that will play the music.
|
|
|
|
public AudioSource musicSource;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
// The offset to the first beat of the song in seconds
|
2023-06-10 19:13:29 +00:00
|
|
|
public double firstBeatOffset;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2022-01-07 11:36:23 +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
|
2022-01-07 11:36:23 +00:00
|
|
|
public bool isPlaying;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2022-01-15 17:45:08 +00:00
|
|
|
// Conductor is currently paused, but not fully stopped
|
2022-01-07 11:36:23 +00:00
|
|
|
public bool isPaused;
|
|
|
|
|
2022-01-15 17:45:08 +00:00
|
|
|
// Metronome tick sound enabled
|
|
|
|
public bool metronome = false;
|
2023-01-05 04:04:31 +00:00
|
|
|
Util.Sound metronomeSound;
|
2023-11-23 16:19:39 +00:00
|
|
|
private int _metronomeTally = 0;
|
2022-01-15 17:45:08 +00:00
|
|
|
|
2023-02-21 16:26:08 +00:00
|
|
|
// pitch values
|
|
|
|
private float timelinePitch = 1f;
|
|
|
|
private float minigamePitch = 1f;
|
2023-05-07 20:33:15 +00:00
|
|
|
public float SongPitch { get => isPaused ? 0f : (timelinePitch * minigamePitch); }
|
2023-12-26 05:22:51 +00:00
|
|
|
public float TimelinePitch { get => timelinePitch; }
|
2023-06-10 19:13:29 +00:00
|
|
|
private float musicScheduledPitch = 1f;
|
|
|
|
private double musicScheduledTime = 0;
|
2022-01-28 02:50:57 +00:00
|
|
|
|
2023-11-24 22:59:34 +00:00
|
|
|
// volume modifier
|
|
|
|
private float timelineVolume = 1f;
|
|
|
|
private float minigameVolume = 1f;
|
|
|
|
|
2023-02-21 16:26:08 +00:00
|
|
|
public void SetTimelinePitch(float pitch)
|
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
if (isPaused) return;
|
2023-09-11 22:28:04 +00:00
|
|
|
if (pitch != 0 && pitch * minigamePitch != SongPitch)
|
|
|
|
{
|
|
|
|
Debug.Log("added pitch change " + pitch * minigamePitch + " at" + absTime);
|
|
|
|
addedPitchChanges.Add(new AddedPitchChange { time = absTime, pitch = pitch * minigamePitch });
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:26:08 +00:00
|
|
|
timelinePitch = pitch;
|
|
|
|
musicSource.pitch = SongPitch;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2023-02-21 16:26:08 +00:00
|
|
|
}
|
2022-01-19 05:40:49 +00:00
|
|
|
|
2023-02-21 16:26:08 +00:00
|
|
|
public void SetMinigamePitch(float pitch)
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (pitch != 0 && pitch * timelinePitch != SongPitch)
|
|
|
|
{
|
|
|
|
Debug.Log("added pitch change " + pitch * timelinePitch + " at" + absTime);
|
|
|
|
addedPitchChanges.Add(new AddedPitchChange { time = absTime, pitch = pitch * timelinePitch });
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:26:08 +00:00
|
|
|
minigamePitch = pitch;
|
|
|
|
musicSource.pitch = SongPitch;
|
|
|
|
}
|
2021-12-28 02:36:27 +00:00
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
public void SetMinigamePitch(float pitch, double beat)
|
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
BeatAction.New(this,
|
2023-09-11 22:28:04 +00:00
|
|
|
new List<BeatAction.Action> {
|
|
|
|
new BeatAction.Action(beat, delegate {
|
|
|
|
SetMinigamePitch(pitch);
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
void Awake()
|
|
|
|
{
|
|
|
|
instance = this;
|
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2023-06-25 02:32:08 +00:00
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
musicSource.priority = 0;
|
2023-09-11 22:28:04 +00:00
|
|
|
AudioConfiguration config = AudioSettings.GetConfiguration();
|
2023-10-29 19:44:47 +00:00
|
|
|
dspSizeSeconds = config.dspBufferSize / (double)config.sampleRate;
|
|
|
|
dspMargin = 2 * dspSizeSeconds;
|
2023-09-11 22:28:04 +00:00
|
|
|
addedPitchChanges.Clear();
|
2023-06-25 02:32:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 20:33:15 +00:00
|
|
|
public void SetBeat(double beat)
|
2022-01-07 11:36:23 +00:00
|
|
|
{
|
2023-06-25 02:32:08 +00:00
|
|
|
var chart = GameManager.instance.Beatmap;
|
|
|
|
double offset = chart.data.offset;
|
2023-09-11 22:28:04 +00:00
|
|
|
startPos = GetSongPosFromBeat(beat);
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2023-06-25 02:32:08 +00:00
|
|
|
double dspTime = AudioSettings.dspTime;
|
|
|
|
|
|
|
|
time = startPos;
|
|
|
|
firstBeatOffset = offset;
|
2022-01-07 23:51:08 +00:00
|
|
|
|
2023-10-29 19:44:47 +00:00
|
|
|
SeekMusicToTime(startPos, offset);
|
2023-06-25 02:32:08 +00:00
|
|
|
|
|
|
|
songPosBeat = GetBeatFromSongPos(time);
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2023-06-25 02:32:08 +00:00
|
|
|
GameManager.instance.SetCurrentEventToClosest(beat);
|
2022-01-07 23:51:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 20:33:15 +00:00
|
|
|
public void Play(double beat)
|
2022-01-07 23:51:08 +00:00
|
|
|
{
|
2023-06-10 19:13:29 +00:00
|
|
|
if (isPlaying) return;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
if (isPaused)
|
|
|
|
{
|
|
|
|
Util.SoundByte.UnpauseOneShots();
|
|
|
|
}
|
|
|
|
else
|
2023-09-11 22:28:04 +00:00
|
|
|
{
|
|
|
|
AudioConfiguration config = AudioSettings.GetConfiguration();
|
2023-10-29 19:44:47 +00:00
|
|
|
dspSizeSeconds = config.dspBufferSize / (double)config.sampleRate;
|
|
|
|
Debug.Log($"dsp size: {dspSizeSeconds}");
|
|
|
|
dspMargin = 2 * dspSizeSeconds;
|
2023-09-11 22:28:04 +00:00
|
|
|
addedPitchChanges.Clear();
|
|
|
|
addedPitchChanges.Add(new AddedPitchChange { time = 0, pitch = SongPitch });
|
2023-11-24 22:59:34 +00:00
|
|
|
|
|
|
|
SetMinigameVolume(1f);
|
2023-09-11 22:28:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
RiqBeatmap chart = GameManager.instance.Beatmap;
|
2023-06-10 19:13:29 +00:00
|
|
|
double offset = chart.data.offset;
|
|
|
|
double dspTime = AudioSettings.dspTime;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
|
|
|
startPos = GetSongPosFromBeat(beat);
|
2023-06-25 02:32:08 +00:00
|
|
|
firstBeatOffset = offset;
|
2023-06-10 19:13:29 +00:00
|
|
|
time = startPos;
|
2022-01-07 11:36:23 +00:00
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
if (musicSource.clip != null && startPos < musicSource.clip.length - offset)
|
2022-01-08 16:42:48 +00:00
|
|
|
{
|
2023-10-29 19:44:47 +00:00
|
|
|
SeekMusicToTime(startPos, offset);
|
2023-06-10 19:13:29 +00:00
|
|
|
double musicStartDelay = -offset - startPos;
|
|
|
|
if (musicStartDelay > 0)
|
2022-02-25 00:09:14 +00:00
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
musicScheduledTime = dspTime + (musicStartDelay / SongPitch) + 2*dspSizeSeconds;
|
|
|
|
dspStart = dspTime + 2*dspSizeSeconds;
|
2022-02-25 00:09:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
musicScheduledTime = dspTime + 2*dspSizeSeconds;
|
|
|
|
dspStart = dspTime + 2*dspSizeSeconds;
|
2022-02-25 00:09:14 +00:00
|
|
|
}
|
2023-10-29 19:44:47 +00:00
|
|
|
musicScheduledPitch = SongPitch;
|
2023-09-12 20:38:28 +00:00
|
|
|
musicSource.PlayScheduled(musicScheduledTime);
|
2023-12-26 05:22:51 +00:00
|
|
|
Debug.Log($"playback scheduled for dsptime {dspStart}");
|
2022-01-08 16:42:48 +00:00
|
|
|
}
|
2023-10-29 19:44:47 +00:00
|
|
|
if (musicSource.clip == null)
|
|
|
|
{
|
|
|
|
dspStart = dspTime;
|
|
|
|
}
|
2022-01-08 16:42:48 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
songPosBeat = beat;
|
2023-06-10 19:13:29 +00:00
|
|
|
startBeat = songPosBeat;
|
2023-11-23 16:19:39 +00:00
|
|
|
_metronomeTally = 0;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2023-09-12 20:38:28 +00:00
|
|
|
startTime = DateTime.Now;
|
2023-10-29 19:44:47 +00:00
|
|
|
absTimeAdjust = 0;
|
2023-12-26 05:22:51 +00:00
|
|
|
deferTimeKeeping = (musicSource.clip != null);
|
2023-09-12 20:38:28 +00:00
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
isPlaying = true;
|
|
|
|
isPaused = false;
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
void OnAudioFilterRead(float[] data, int channels)
|
|
|
|
{
|
|
|
|
if (!deferTimeKeeping) return;
|
|
|
|
// don't actually do anything with the data
|
|
|
|
// wait until we get a dsp update before starting to keep time
|
|
|
|
double dsp = AudioSettings.dspTime;
|
|
|
|
if (dsp >= dspStart - dspSizeSeconds)
|
|
|
|
{
|
|
|
|
deferTimeKeeping = false;
|
|
|
|
Debug.Log($"dsptime: {dsp}, deferred timekeeping for {DateTime.Now - startTime} seconds (delta dsp {dsp - dspStart})");
|
|
|
|
startTime += TimeSpan.FromSeconds(dsp - dspStart);
|
|
|
|
absTimeAdjust = 0;
|
|
|
|
dspStart = dsp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
public void Pause()
|
2021-12-28 02:36:27 +00:00
|
|
|
{
|
2023-06-10 19:13:29 +00:00
|
|
|
if (!isPlaying) return;
|
2022-01-07 11:36:23 +00:00
|
|
|
isPlaying = false;
|
|
|
|
isPaused = true;
|
2023-12-26 05:22:51 +00:00
|
|
|
deferTimeKeeping = false;
|
2022-01-07 11:36:23 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
musicSource.Stop();
|
|
|
|
Util.SoundByte.PauseOneShots();
|
2022-01-07 11:36:23 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
public void Stop(double beat)
|
2022-01-07 11:36:23 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (absTimeAdjust != 0)
|
|
|
|
{
|
|
|
|
Debug.Log($"Last playthrough had a dsp (audio) drift of {absTimeAdjust}.\nConsider increasing audio buffer size if audio distortion was present.");
|
|
|
|
}
|
2023-12-26 05:22:51 +00:00
|
|
|
songPosBeat = beat;
|
2022-02-02 01:11:42 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
time = GetSongPosFromBeat(beat);
|
2023-06-10 19:13:29 +00:00
|
|
|
songPos = time;
|
2023-12-26 05:22:51 +00:00
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
absTimeAdjust = 0;
|
2022-02-02 01:11:42 +00:00
|
|
|
|
2022-01-07 11:36:23 +00:00
|
|
|
isPlaying = false;
|
|
|
|
isPaused = false;
|
2023-12-26 05:22:51 +00:00
|
|
|
deferTimeKeeping = false;
|
2022-01-07 11:36:23 +00:00
|
|
|
|
|
|
|
musicSource.Stop();
|
2021-12-31 14:46:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
/// <summary>
|
|
|
|
/// stops playback of the audio without stopping beatkeeping
|
|
|
|
/// </summary>
|
|
|
|
public void StopOnlyAudio()
|
|
|
|
{
|
|
|
|
musicSource.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// fades out the audio over a duration
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="duration">duration of the fade</param>
|
|
|
|
public void FadeOutAudio(float duration)
|
|
|
|
{
|
|
|
|
StartCoroutine(FadeOutAudioCoroutine(duration));
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator FadeOutAudioCoroutine(float duration)
|
|
|
|
{
|
|
|
|
float startVolume = musicSource.volume;
|
|
|
|
float endVolume = 0f;
|
|
|
|
float startTime = Time.time;
|
|
|
|
float endTime = startTime + duration;
|
|
|
|
|
|
|
|
while (Time.time < endTime)
|
|
|
|
{
|
|
|
|
float t = (Time.time - startTime) / duration;
|
|
|
|
musicSource.volume = Mathf.Lerp(startVolume, endVolume, t);
|
|
|
|
yield return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
musicSource.volume = endVolume;
|
|
|
|
StopOnlyAudio();
|
|
|
|
}
|
|
|
|
|
2023-11-24 22:59:34 +00:00
|
|
|
Coroutine fadeOutAudioCoroutine;
|
|
|
|
public void FadeMinigameVolume(double startBeat, double durationBeats = 1f, float targetVolume = 0f)
|
|
|
|
{
|
|
|
|
if (fadeOutAudioCoroutine != null)
|
|
|
|
{
|
|
|
|
StopCoroutine(fadeOutAudioCoroutine);
|
|
|
|
}
|
|
|
|
fadeOutAudioCoroutine = StartCoroutine(FadeMinigameVolumeCoroutine(startBeat, durationBeats, targetVolume));
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator FadeMinigameVolumeCoroutine(double startBeat, double durationBeats, float targetVolume)
|
|
|
|
{
|
|
|
|
float startVolume = minigameVolume;
|
|
|
|
float endVolume = targetVolume;
|
|
|
|
double startTime = startBeat;
|
|
|
|
double endTime = startBeat + durationBeats;
|
|
|
|
|
|
|
|
while (songPositionInBeatsAsDouble < endTime)
|
|
|
|
{
|
|
|
|
if (!NotStopped()) yield break;
|
|
|
|
double t = (songPositionInBeatsAsDouble - startTime) / durationBeats;
|
|
|
|
SetMinigameVolume(Mathf.Lerp(startVolume, endVolume, (float)t));
|
|
|
|
yield return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetMinigameVolume(endVolume);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTimelineVolume(float volume)
|
|
|
|
{
|
|
|
|
timelineVolume = volume;
|
|
|
|
musicSource.volume = timelineVolume * minigameVolume;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetMinigameVolume(float volume)
|
|
|
|
{
|
|
|
|
minigameVolume = volume;
|
|
|
|
musicSource.volume = timelineVolume * minigameVolume;
|
|
|
|
}
|
|
|
|
|
2023-10-29 19:44:47 +00:00
|
|
|
void SeekMusicToTime(double fStartPos, double offset)
|
2023-06-25 02:32:08 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (musicSource.clip != null && fStartPos < musicSource.clip.length - offset)
|
2023-06-25 02:32:08 +00:00
|
|
|
{
|
|
|
|
// https://www.desmos.com/calculator/81ywfok6xk
|
2023-09-11 22:28:04 +00:00
|
|
|
double musicStartDelay = -offset - fStartPos;
|
2023-06-25 02:32:08 +00:00
|
|
|
if (musicStartDelay > 0)
|
|
|
|
{
|
|
|
|
musicSource.timeSamples = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int freq = musicSource.clip.frequency;
|
2023-09-11 22:28:04 +00:00
|
|
|
int samples = (int)(freq * (fStartPos + offset));
|
2023-06-25 02:32:08 +00:00
|
|
|
|
|
|
|
musicSource.timeSamples = samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2021-12-27 04:48:39 +00:00
|
|
|
public void Update()
|
2021-12-19 04:10:43 +00:00
|
|
|
{
|
2022-01-07 11:36:23 +00:00
|
|
|
if (isPlaying)
|
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
double dsp = AudioSettings.dspTime;
|
|
|
|
if (dsp < musicScheduledTime && musicScheduledPitch != SongPitch)
|
2023-06-10 19:13:29 +00:00
|
|
|
{
|
|
|
|
if (SongPitch == 0f)
|
|
|
|
{
|
|
|
|
musicSource.Pause();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (musicScheduledPitch == 0f)
|
|
|
|
musicSource.UnPause();
|
|
|
|
musicScheduledPitch = SongPitch;
|
2022-02-24 14:02:21 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
musicScheduledTime = (dsp + (-GameManager.instance.Beatmap.data.offset - songPositionAsDouble) / (double)SongPitch);
|
2023-06-10 19:13:29 +00:00
|
|
|
musicSource.SetScheduledStartTime(musicScheduledTime);
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
if (!deferTimeKeeping)
|
2023-09-11 22:28:04 +00:00
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
absTime = (DateTime.Now - startTime).TotalSeconds;
|
|
|
|
|
|
|
|
//dspTime to sync with audio thread in case of drift
|
|
|
|
dspTime = dsp - dspStart;
|
|
|
|
if (Math.Abs(absTime + absTimeAdjust - dspTime) > dspMargin)
|
2023-09-11 22:28:04 +00:00
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
int i = 0;
|
|
|
|
while (Math.Abs(absTime + absTimeAdjust - dspTime) > dspMargin)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
absTimeAdjust = (dspTime - absTime + absTimeAdjust) * 0.5;
|
|
|
|
if (i > 8) break;
|
|
|
|
}
|
2023-09-11 22:28:04 +00:00
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
time = MapTimeToPitchChanges(absTime + absTimeAdjust);
|
2023-06-10 19:13:29 +00:00
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
songPos = startPos + time;
|
|
|
|
songPosBeat = GetBeatFromSongPos(songPos);
|
|
|
|
}
|
2022-09-23 02:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-15 17:45:08 +00:00
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
double MapTimeToPitchChanges(double time)
|
|
|
|
{
|
|
|
|
double counter = 0;
|
|
|
|
double lastChangeTime = 0;
|
|
|
|
float pitch = addedPitchChanges[0].pitch;
|
|
|
|
foreach (var pch in addedPitchChanges)
|
|
|
|
{
|
|
|
|
double changeTime = pch.time;
|
|
|
|
if (changeTime > time)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter += (changeTime - lastChangeTime) * pitch;
|
|
|
|
lastChangeTime = changeTime;
|
|
|
|
pitch = pch.pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter += (time - lastChangeTime) * pitch;
|
|
|
|
return counter;
|
|
|
|
}
|
2023-06-10 19:13:29 +00:00
|
|
|
|
2022-09-23 02:05:04 +00:00
|
|
|
public void LateUpdate()
|
|
|
|
{
|
2023-12-05 22:38:52 +00:00
|
|
|
if (isPlaying)
|
2022-09-23 02:05:04 +00:00
|
|
|
{
|
2023-11-23 16:19:39 +00:00
|
|
|
if (songPositionInBeatsAsDouble >= Math.Ceiling(startBeat) + _metronomeTally)
|
2022-09-23 02:05:04 +00:00
|
|
|
{
|
2023-12-05 22:38:52 +00:00
|
|
|
if (metronome) metronomeSound = Util.SoundByte.PlayOneShot("metronome", Math.Ceiling(startBeat) + _metronomeTally);
|
2023-11-23 16:19:39 +00:00
|
|
|
_metronomeTally++;
|
2022-01-15 17:45:08 +00:00
|
|
|
}
|
2022-01-07 11:36:23 +00:00
|
|
|
}
|
2023-01-05 04:04:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (metronomeSound != null)
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
metronomeSound.Stop();
|
2023-01-05 04:04:31 +00:00
|
|
|
metronomeSound = null;
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
2021-12-19 04:10:43 +00:00
|
|
|
|
2023-11-23 16:19:39 +00:00
|
|
|
[Obsolete("Conductor.ReportBeat is deprecated. Please use the OnBeatPulse callback instead.")]
|
2023-06-10 19:13:29 +00:00
|
|
|
public bool ReportBeat(ref double lastReportedBeat, double offset = 0, bool shiftBeatToOffset = true)
|
2022-01-19 05:40:49 +00:00
|
|
|
{
|
2022-06-09 03:35:15 +00:00
|
|
|
bool result = songPositionInBeats + (shiftBeatToOffset ? offset : 0f) >= (lastReportedBeat) + 1f;
|
|
|
|
if (result)
|
2022-01-19 05:40:49 +00:00
|
|
|
{
|
2022-06-09 03:35:15 +00:00
|
|
|
lastReportedBeat += 1f;
|
2022-06-09 06:46:51 +00:00
|
|
|
if (lastReportedBeat < songPositionInBeats)
|
|
|
|
{
|
|
|
|
lastReportedBeat = Mathf.Round(songPositionInBeats);
|
|
|
|
}
|
2022-01-19 05:40:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
public float GetLoopPositionFromBeat(float beatOffset, float length, bool beatClamp = true)
|
2022-02-03 22:20:26 +00:00
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
float beat = songPositionInBeats;
|
|
|
|
if (beatClamp)
|
|
|
|
{
|
|
|
|
beat = Mathf.Max(beat, 0);
|
|
|
|
}
|
|
|
|
return Mathf.Repeat((beat / length) + beatOffset, 1);
|
2022-02-03 22:20:26 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
public float GetPositionFromBeat(double startBeat, double length, bool beatClamp = true)
|
2021-12-21 01:10:49 +00:00
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
float beat = songPositionInBeats;
|
|
|
|
if (beatClamp)
|
|
|
|
{
|
|
|
|
beat = Mathf.Max(beat, 0);
|
|
|
|
}
|
|
|
|
float a = Mathp.Normalize(beat, (float)startBeat, (float)(startBeat + length));
|
2021-12-27 04:48:39 +00:00
|
|
|
return a;
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
private List<RiqEntity> GetSortedTempoChanges()
|
2022-08-21 23:46:45 +00:00
|
|
|
{
|
2022-09-23 02:05:04 +00:00
|
|
|
GameManager.instance.SortEventsList();
|
2023-06-10 19:13:29 +00:00
|
|
|
return GameManager.instance.Beatmap.TempoChanges;
|
2022-08-21 23:46:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
public float GetBpmAtBeat(double beat, out float swingRatio)
|
2023-01-15 04:33:37 +00:00
|
|
|
{
|
2023-12-26 05:22:51 +00:00
|
|
|
swingRatio = 0.5f;
|
2023-01-15 04:33:37 +00:00
|
|
|
var chart = GameManager.instance.Beatmap;
|
2023-06-10 19:13:29 +00:00
|
|
|
if (chart.TempoChanges.Count == 0)
|
|
|
|
return 120f;
|
|
|
|
float bpm = chart.TempoChanges[0]["tempo"];
|
2023-12-26 05:22:51 +00:00
|
|
|
swingRatio = chart.TempoChanges[0]["swing"] + 0.5f;
|
2023-01-15 04:33:37 +00:00
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
foreach (RiqEntity t in chart.TempoChanges)
|
2023-01-15 04:33:37 +00:00
|
|
|
{
|
|
|
|
if (t.beat > beat)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-06-10 19:13:29 +00:00
|
|
|
bpm = t["tempo"];
|
2023-12-26 05:22:51 +00:00
|
|
|
swingRatio = t["swing"] + 0.5f;
|
2023-01-15 04:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bpm;
|
|
|
|
}
|
|
|
|
|
2023-12-26 05:22:51 +00:00
|
|
|
public float GetBpmAtBeat(double beat)
|
|
|
|
{
|
|
|
|
return GetBpmAtBeat(beat, out _);
|
|
|
|
}
|
|
|
|
|
|
|
|
public float GetSwingRatioAtBeat(double beat)
|
|
|
|
{
|
|
|
|
float swingRatio;
|
|
|
|
GetBpmAtBeat(beat, out swingRatio);
|
|
|
|
return swingRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double GetSwungBeat(double beat, float ratio)
|
|
|
|
{
|
|
|
|
return beat + GetSwingOffset(beat, ratio);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double GetSwingOffset(double beatFrac, float ratio)
|
|
|
|
{
|
|
|
|
beatFrac %= 1;
|
|
|
|
if (beatFrac <= 0.5)
|
|
|
|
{
|
|
|
|
return 0.5 / ratio * beatFrac;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0.5 + (0.5 / (1f - ratio) * (beatFrac - ratio));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:42:12 +00:00
|
|
|
public double GetSongPosFromBeat(double beat)
|
2021-12-31 14:46:11 +00:00
|
|
|
{
|
2022-08-21 23:46:45 +00:00
|
|
|
var chart = GameManager.instance.Beatmap;
|
2023-06-10 19:13:29 +00:00
|
|
|
float bpm = 120f;
|
2022-06-06 16:54:57 +00:00
|
|
|
|
2022-09-23 02:05:04 +00:00
|
|
|
double counter = 0f;
|
2022-06-06 16:54:57 +00:00
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
double lastTempoChangeBeat = 0f;
|
2022-06-06 16:54:57 +00:00
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
foreach (RiqEntity t in chart.TempoChanges)
|
2022-06-06 16:54:57 +00:00
|
|
|
{
|
|
|
|
if (t.beat > beat)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
counter += (t.beat - lastTempoChangeBeat) * 60 / bpm;
|
2023-06-10 19:13:29 +00:00
|
|
|
bpm = t["tempo"];
|
2022-06-06 16:54:57 +00:00
|
|
|
lastTempoChangeBeat = t.beat;
|
|
|
|
}
|
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
counter += (beat - lastTempoChangeBeat) * 60 / bpm;
|
2022-06-06 16:54:57 +00:00
|
|
|
|
|
|
|
return counter;
|
2021-12-31 14:46:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 16:54:57 +00:00
|
|
|
//thank you @wooningcharithri#7419 for the psuedo-code
|
2023-09-11 22:28:04 +00:00
|
|
|
public double BeatsToSecs(double beats, float bpm)
|
|
|
|
{
|
|
|
|
return beats / bpm * 60f;
|
|
|
|
}
|
|
|
|
public double SecsToBeats(double s, float bpm)
|
|
|
|
{
|
|
|
|
return s / 60f * bpm;
|
|
|
|
}
|
2022-06-06 16:54:57 +00:00
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
public double GetBeatFromSongPos(double seconds)
|
|
|
|
{
|
|
|
|
double lastTempoChangeBeat = 0f;
|
|
|
|
double counterSeconds = 0;
|
|
|
|
float lastBpm = 120f;
|
|
|
|
|
|
|
|
foreach (RiqEntity t in GameManager.instance.Beatmap.TempoChanges)
|
2022-06-06 16:54:57 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
double beatToNext = t.beat - lastTempoChangeBeat;
|
|
|
|
double secToNext = BeatsToSecs(beatToNext, lastBpm);
|
|
|
|
double nextSecs = counterSeconds + secToNext;
|
|
|
|
|
|
|
|
if (nextSecs >= seconds)
|
|
|
|
break;
|
|
|
|
|
|
|
|
lastTempoChangeBeat = t.beat;
|
|
|
|
lastBpm = t["tempo"];
|
|
|
|
counterSeconds = nextSecs;
|
2022-06-06 16:54:57 +00:00
|
|
|
}
|
2023-09-11 22:28:04 +00:00
|
|
|
return lastTempoChangeBeat + SecsToBeats(seconds - counterSeconds, lastBpm);
|
|
|
|
}
|
2022-06-06 16:54:57 +00:00
|
|
|
//
|
|
|
|
|
2022-03-20 23:46:12 +00:00
|
|
|
// convert real seconds to beats
|
2023-06-10 19:13:29 +00:00
|
|
|
public double GetRestFromRealTime(double seconds)
|
2022-03-20 23:46:12 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
return seconds / pitchedSecPerBeat;
|
2022-03-20 23:46:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 02:28:05 +00:00
|
|
|
public void SetBpm(float bpm)
|
|
|
|
{
|
|
|
|
this.songBpm = bpm;
|
2023-06-10 19:13:29 +00:00
|
|
|
secPerBeatAsDouble = 60.0 / songBpm;
|
2021-12-23 02:28:05 +00:00
|
|
|
}
|
2022-01-06 00:11:33 +00:00
|
|
|
|
2022-09-18 20:48:14 +00:00
|
|
|
public void SetVolume(float percent)
|
2022-03-19 12:46:38 +00:00
|
|
|
{
|
2023-11-24 22:59:34 +00:00
|
|
|
SetTimelineVolume(percent / 100f);
|
2022-03-19 12:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 00:11:33 +00:00
|
|
|
public float SongLengthInBeats()
|
|
|
|
{
|
2023-06-10 19:13:29 +00:00
|
|
|
return (float)SongLengthInBeatsAsDouble();
|
2022-01-06 00:11:33 +00:00
|
|
|
}
|
2022-01-08 16:42:48 +00:00
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
public double SongLengthInBeatsAsDouble()
|
2022-01-08 16:42:48 +00:00
|
|
|
{
|
2023-06-10 19:13:29 +00:00
|
|
|
if (!musicSource.clip) return 0;
|
|
|
|
return GetBeatFromSongPos(musicSource.clip.length - firstBeatOffset);
|
2022-01-08 16:42:48 +00:00
|
|
|
}
|
2022-01-11 00:17:29 +00:00
|
|
|
|
2022-09-23 02:05:04 +00:00
|
|
|
public bool SongPosLessThanClipLength(double t)
|
|
|
|
{
|
|
|
|
if (musicSource.clip != null)
|
2023-06-10 19:13:29 +00:00
|
|
|
return t < musicSource.clip.length - firstBeatOffset;
|
2022-09-23 02:05:04 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:17:29 +00:00
|
|
|
public bool NotStopped()
|
|
|
|
{
|
2022-01-17 02:31:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|