2023-06-10 19:13:29 +00:00
|
|
|
using System;
|
2021-12-21 01:10:49 +00:00
|
|
|
using System.Collections;
|
|
|
|
using UnityEngine;
|
2023-05-29 20:09:34 +00:00
|
|
|
using Starpelly;
|
2021-12-21 01:10:49 +00:00
|
|
|
|
2022-03-14 14:21:05 +00:00
|
|
|
namespace HeavenStudio.Util
|
2021-12-21 01:10:49 +00:00
|
|
|
{
|
|
|
|
public class Sound : MonoBehaviour
|
|
|
|
{
|
|
|
|
public AudioClip clip;
|
2022-01-15 07:08:23 +00:00
|
|
|
public float pitch = 1;
|
2023-05-29 20:09:34 +00:00
|
|
|
public float bendedPitch = 1; //only used with rockers
|
2022-03-07 09:34:38 +00:00
|
|
|
public float volume = 1;
|
2021-12-21 01:10:49 +00:00
|
|
|
|
2022-02-05 13:08:33 +00:00
|
|
|
// For use with PlayOneShotScheduled
|
|
|
|
public bool scheduled;
|
|
|
|
public double scheduledTime;
|
|
|
|
|
2022-03-07 09:16:31 +00:00
|
|
|
public bool looping;
|
2023-06-10 19:13:29 +00:00
|
|
|
public double loopEndBeat = -1;
|
|
|
|
public double fadeTime;
|
2022-03-10 03:50:19 +00:00
|
|
|
int loopIndex = 0;
|
2022-03-07 09:16:31 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
private AudioSource audioSource;
|
2023-09-11 22:28:04 +00:00
|
|
|
private Conductor cond;
|
2021-12-21 01:10:49 +00:00
|
|
|
|
2022-01-15 07:08:23 +00:00
|
|
|
private int pauseTimes = 0;
|
|
|
|
|
2023-01-05 04:04:31 +00:00
|
|
|
private double startTime;
|
2022-01-15 07:08:23 +00:00
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
public double beat;
|
|
|
|
public double offset;
|
2023-01-05 04:04:31 +00:00
|
|
|
public float scheduledPitch = 1f;
|
2022-01-21 01:24:30 +00:00
|
|
|
|
|
|
|
bool playInstant = false;
|
2023-01-05 04:04:31 +00:00
|
|
|
bool played = false;
|
2023-06-25 02:32:08 +00:00
|
|
|
bool queued = false;
|
2023-09-11 22:28:04 +00:00
|
|
|
public bool available = true;
|
2022-01-17 05:00:26 +00:00
|
|
|
|
2023-06-25 02:32:08 +00:00
|
|
|
const double PREBAKE_TIME = 0.5;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
private void Start()
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Play()
|
|
|
|
{
|
|
|
|
if (!available)
|
|
|
|
{
|
|
|
|
GameManager.instance.SoundObjects.Release(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
audioSource = GetComponent<AudioSource>();
|
2023-09-11 22:28:04 +00:00
|
|
|
cond = Conductor.instance;
|
|
|
|
|
|
|
|
available = false;
|
2022-01-15 07:08:23 +00:00
|
|
|
audioSource.clip = clip;
|
|
|
|
audioSource.pitch = pitch;
|
2022-03-07 09:34:38 +00:00
|
|
|
audioSource.volume = volume;
|
2022-03-07 09:16:31 +00:00
|
|
|
audioSource.loop = looping;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
|
|
|
loopEndBeat = -1;
|
|
|
|
loopIndex = 0;
|
|
|
|
audioSource.Stop();
|
2022-01-17 05:00:26 +00:00
|
|
|
|
2022-02-05 13:08:33 +00:00
|
|
|
if (beat == -1 && !scheduled)
|
2022-01-21 01:24:30 +00:00
|
|
|
{
|
|
|
|
playInstant = true;
|
2023-01-05 04:04:31 +00:00
|
|
|
played = true;
|
2023-06-10 19:13:29 +00:00
|
|
|
startTime = AudioSettings.dspTime;
|
2023-09-11 22:28:04 +00:00
|
|
|
audioSource.PlayScheduled(startTime);
|
2023-01-05 04:04:31 +00:00
|
|
|
StartCoroutine(NotRelyOnBeatSound());
|
2022-01-21 01:24:30 +00:00
|
|
|
}
|
|
|
|
else
|
2022-01-17 05:00:26 +00:00
|
|
|
{
|
2022-01-21 01:24:30 +00:00
|
|
|
playInstant = false;
|
2023-09-11 22:28:04 +00:00
|
|
|
scheduledPitch = cond.SongPitch;
|
|
|
|
startTime = (AudioSettings.dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch) - offset;
|
2023-06-25 02:32:08 +00:00
|
|
|
|
|
|
|
if (scheduledPitch != 0 && AudioSettings.dspTime >= startTime)
|
|
|
|
{
|
|
|
|
audioSource.PlayScheduled(startTime);
|
|
|
|
queued = true;
|
|
|
|
}
|
2022-01-17 05:00:26 +00:00
|
|
|
}
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 07:08:23 +00:00
|
|
|
private void Update()
|
2021-12-21 01:10:49 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
cond = Conductor.instance;
|
2023-06-25 02:32:08 +00:00
|
|
|
double dspTime = AudioSettings.dspTime;
|
2023-09-11 22:28:04 +00:00
|
|
|
if (!available)
|
2022-02-05 13:08:33 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (!Conductor.instance.isPlaying)
|
|
|
|
{
|
|
|
|
GameManager.instance.SoundObjects.Release(this);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-07 09:16:31 +00:00
|
|
|
if (scheduled)
|
2022-02-05 13:08:33 +00:00
|
|
|
{
|
2023-06-25 02:32:08 +00:00
|
|
|
if (!queued && dspTime > scheduledTime - PREBAKE_TIME)
|
|
|
|
{
|
|
|
|
audioSource.PlayScheduled(scheduledTime);
|
|
|
|
queued = true;
|
|
|
|
}
|
|
|
|
if (scheduledPitch != 0 && dspTime > scheduledTime)
|
2022-03-07 09:16:31 +00:00
|
|
|
{
|
|
|
|
StartCoroutine(NotRelyOnBeatSound());
|
2023-01-05 04:04:31 +00:00
|
|
|
played = true;
|
2022-03-07 09:16:31 +00:00
|
|
|
}
|
2022-02-05 13:08:33 +00:00
|
|
|
}
|
2022-03-07 09:16:31 +00:00
|
|
|
else if (!playInstant)
|
2022-01-20 01:48:52 +00:00
|
|
|
{
|
2023-06-25 02:32:08 +00:00
|
|
|
if (!queued && dspTime > startTime - PREBAKE_TIME)
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
startTime = (dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch) - offset;
|
2023-06-25 02:32:08 +00:00
|
|
|
audioSource.PlayScheduled(startTime);
|
|
|
|
queued = true;
|
|
|
|
}
|
|
|
|
if (scheduledPitch != 0 && dspTime > startTime)
|
2023-01-05 04:04:31 +00:00
|
|
|
{
|
|
|
|
played = true;
|
|
|
|
StartCoroutine(NotRelyOnBeatSound());
|
|
|
|
}
|
|
|
|
else
|
2022-03-07 09:16:31 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (!played && scheduledPitch != cond.SongPitch)
|
2023-01-05 04:04:31 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (cond.SongPitch == 0)
|
2023-05-07 20:33:15 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
scheduledPitch = cond.SongPitch;
|
2023-06-25 02:32:08 +00:00
|
|
|
if (queued)
|
|
|
|
audioSource.Pause();
|
2023-05-07 20:33:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (scheduledPitch == 0)
|
|
|
|
{
|
|
|
|
audioSource.UnPause();
|
|
|
|
}
|
2023-09-11 22:28:04 +00:00
|
|
|
scheduledPitch = cond.SongPitch;
|
|
|
|
startTime = (dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch);
|
2023-06-25 02:32:08 +00:00
|
|
|
if (queued)
|
|
|
|
audioSource.SetScheduledStartTime(startTime);
|
2023-05-07 20:33:15 +00:00
|
|
|
}
|
2023-01-05 04:04:31 +00:00
|
|
|
}
|
2022-03-10 03:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loopIndex < 1)
|
|
|
|
{
|
2022-03-10 04:00:57 +00:00
|
|
|
if (looping && loopEndBeat != -1) // Looping sounds play forever unless params are set.
|
2022-03-10 03:50:19 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (cond.songPositionInBeatsAsDouble > loopEndBeat)
|
2022-03-10 03:50:19 +00:00
|
|
|
{
|
|
|
|
KillLoop(fadeTime);
|
|
|
|
loopIndex++;
|
2022-03-07 09:16:31 +00:00
|
|
|
}
|
2022-01-20 01:48:52 +00:00
|
|
|
}
|
2022-01-15 07:08:23 +00:00
|
|
|
}
|
2022-01-17 05:00:26 +00:00
|
|
|
}
|
2022-01-15 07:08:23 +00:00
|
|
|
|
2022-01-17 05:00:26 +00:00
|
|
|
IEnumerator NotRelyOnBeatSound()
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (!Conductor.instance.isPlaying)
|
2022-03-07 09:16:31 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
GameManager.instance.SoundObjects.Release(this);
|
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
WaitUntil yieldIsAudioPlaying = new WaitUntil(() => !audioSource.isPlaying || !Conductor.instance.isPlaying);
|
|
|
|
if (played && !looping) // Looping sounds are destroyed manually.
|
|
|
|
{
|
|
|
|
yield return yieldIsAudioPlaying;
|
|
|
|
GameManager.instance.SoundObjects.Release(this);
|
2022-03-07 09:16:31 +00:00
|
|
|
}
|
2022-03-07 03:56:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
public void SetLoopParams(double endBeat, double fadeTime)
|
2022-03-10 03:59:48 +00:00
|
|
|
{
|
|
|
|
loopEndBeat = endBeat;
|
|
|
|
this.fadeTime = fadeTime;
|
|
|
|
}
|
|
|
|
|
2023-05-29 20:09:34 +00:00
|
|
|
public void Pause()
|
|
|
|
{
|
|
|
|
if (audioSource != null)
|
|
|
|
audioSource.Pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnPause()
|
|
|
|
{
|
|
|
|
if (audioSource != null)
|
|
|
|
audioSource.UnPause();
|
|
|
|
}
|
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
public void Stop()
|
2022-03-07 03:56:45 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
available = true;
|
|
|
|
played = false;
|
|
|
|
queued = false;
|
|
|
|
playInstant = false;
|
|
|
|
looping = false;
|
|
|
|
scheduled = false;
|
|
|
|
beat = 0;
|
|
|
|
loopEndBeat = -1;
|
|
|
|
loopIndex = 0;
|
|
|
|
startTime = 0;
|
|
|
|
|
|
|
|
audioSource.loop = false;
|
|
|
|
audioSource.Stop();
|
|
|
|
|
|
|
|
gameObject.SetActive(false);
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
2022-03-08 04:53:30 +00:00
|
|
|
|
2023-05-29 20:09:34 +00:00
|
|
|
#region Bend
|
|
|
|
// All of these should only be used with rockers.
|
2023-06-10 19:13:29 +00:00
|
|
|
// minenice: consider doing these in the audio thread so they can work per-sample?
|
2023-05-29 20:09:34 +00:00
|
|
|
public void BendUp(float bendTime, float bendedPitch)
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (!gameObject.activeSelf) return;
|
2023-05-29 20:09:34 +00:00
|
|
|
this.bendedPitch = bendedPitch;
|
|
|
|
StartCoroutine(BendUpLoop(bendTime));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void BendDown(float bendTime)
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (!gameObject.activeSelf) return;
|
2023-05-29 20:09:34 +00:00
|
|
|
StartCoroutine(BendDownLoop(bendTime));
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator BendUpLoop(float bendTime)
|
|
|
|
{
|
|
|
|
float startingPitch = audioSource.pitch;
|
|
|
|
float bendTimer = 0f;
|
|
|
|
|
|
|
|
while (bendTimer < bendTime)
|
|
|
|
{
|
|
|
|
bendTimer += Time.deltaTime;
|
|
|
|
float normalizedProgress = Mathp.Normalize(bendTimer, 0, bendTime);
|
|
|
|
float currentPitch = Mathf.Lerp(startingPitch, bendedPitch, normalizedProgress);
|
|
|
|
audioSource.pitch = Mathf.Min(currentPitch, bendedPitch);
|
|
|
|
yield return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator BendDownLoop(float bendTime)
|
|
|
|
{
|
|
|
|
float bendTimer = 0f;
|
|
|
|
float startingPitch = pitch;
|
|
|
|
|
|
|
|
while (bendTimer < bendTime)
|
|
|
|
{
|
|
|
|
bendTimer += Time.deltaTime;
|
|
|
|
float normalizedProgress = Mathp.Normalize(bendTimer, 0, bendTime);
|
|
|
|
float currentPitch = Mathf.Lerp(startingPitch, bendedPitch, 1 - normalizedProgress);
|
|
|
|
audioSource.pitch = Mathf.Max(currentPitch, pitch);
|
|
|
|
yield return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2023-06-10 19:13:29 +00:00
|
|
|
public void KillLoop(double fadeTime)
|
2022-03-07 09:16:31 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
if (!gameObject.activeSelf) return;
|
2022-03-07 09:16:31 +00:00
|
|
|
StartCoroutine(FadeLoop(fadeTime));
|
|
|
|
}
|
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
double loopFadeTimer = 0f;
|
2023-06-10 19:13:29 +00:00
|
|
|
IEnumerator FadeLoop(double fadeTime)
|
2022-03-07 09:16:31 +00:00
|
|
|
{
|
|
|
|
float startingVol = audioSource.volume;
|
|
|
|
|
|
|
|
while (loopFadeTimer < fadeTime)
|
|
|
|
{
|
|
|
|
loopFadeTimer += Time.deltaTime;
|
2023-09-11 22:28:04 +00:00
|
|
|
audioSource.volume = (float)Math.Max((1f - (loopFadeTimer / fadeTime)) * startingVol, 0.0);
|
2022-03-07 09:16:31 +00:00
|
|
|
yield return null;
|
|
|
|
}
|
2023-09-11 22:28:04 +00:00
|
|
|
yield return null;
|
|
|
|
GameManager.instance.SoundObjects.Release(this);
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|