2021-12-21 01:10:49 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
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;
|
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;
|
2022-03-10 03:50:19 +00:00
|
|
|
public float loopEndBeat = -1;
|
|
|
|
public float fadeTime;
|
|
|
|
int loopIndex = 0;
|
2022-03-07 09:16:31 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
private AudioSource audioSource;
|
|
|
|
|
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
|
|
|
|
2022-01-21 01:24:30 +00:00
|
|
|
public float beat;
|
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;
|
2022-01-17 05:00:26 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
audioSource = GetComponent<AudioSource>();
|
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-01-12 01:42:12 +00:00
|
|
|
Conductor cnd = Conductor.instance;
|
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
|
|
|
{
|
2023-01-05 04:04:31 +00:00
|
|
|
audioSource.PlayScheduled(AudioSettings.dspTime);
|
2022-01-21 01:24:30 +00:00
|
|
|
playInstant = true;
|
2023-01-05 04:04:31 +00:00
|
|
|
played = true;
|
2023-01-12 01:42:12 +00:00
|
|
|
startTime = cnd.songPositionAsDouble;
|
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-01-12 01:42:12 +00:00
|
|
|
scheduledPitch = cnd.musicSource.pitch;
|
2023-01-12 22:34:38 +00:00
|
|
|
startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch);
|
2023-01-05 04:04:31 +00:00
|
|
|
audioSource.PlayScheduled(startTime);
|
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-01-12 01:42:12 +00:00
|
|
|
Conductor cnd = Conductor.instance;
|
2023-01-05 04:04:31 +00:00
|
|
|
if (!played)
|
2022-02-05 13:08:33 +00:00
|
|
|
{
|
2022-03-07 09:16:31 +00:00
|
|
|
if (scheduled)
|
2022-02-05 13:08:33 +00:00
|
|
|
{
|
2022-03-07 09:16:31 +00:00
|
|
|
if (AudioSettings.dspTime > scheduledTime)
|
|
|
|
{
|
|
|
|
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-01-05 04:04:31 +00:00
|
|
|
if (AudioSettings.dspTime > startTime)
|
|
|
|
{
|
|
|
|
played = true;
|
|
|
|
StartCoroutine(NotRelyOnBeatSound());
|
|
|
|
}
|
|
|
|
else
|
2022-03-07 09:16:31 +00:00
|
|
|
{
|
2023-01-12 01:42:12 +00:00
|
|
|
if (!played && scheduledPitch != cnd.musicSource.pitch)
|
2023-01-05 04:04:31 +00:00
|
|
|
{
|
2023-01-12 01:42:12 +00:00
|
|
|
scheduledPitch = cnd.musicSource.pitch;
|
|
|
|
startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch);
|
2023-01-05 04:04:31 +00:00
|
|
|
audioSource.SetScheduledStartTime(startTime);
|
|
|
|
}
|
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-01-12 01:42:12 +00:00
|
|
|
if (cnd.songPositionInBeats > 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()
|
|
|
|
{
|
2022-03-07 09:16:31 +00:00
|
|
|
if (!looping) // Looping sounds are destroyed manually.
|
|
|
|
{
|
2023-01-05 04:04:31 +00:00
|
|
|
yield return new WaitForSeconds(clip.length / pitch);
|
2022-03-08 04:53:30 +00:00
|
|
|
Delete();
|
2022-03-07 09:16:31 +00:00
|
|
|
}
|
2022-03-07 03:56:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 03:59:48 +00:00
|
|
|
public void SetLoopParams(float endBeat, float fadeTime)
|
|
|
|
{
|
|
|
|
loopEndBeat = endBeat;
|
|
|
|
this.fadeTime = fadeTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
{
|
2022-03-10 04:24:06 +00:00
|
|
|
if (audioSource != null)
|
|
|
|
audioSource.Stop();
|
2022-03-10 03:59:48 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 03:56:45 +00:00
|
|
|
public void Delete()
|
|
|
|
{
|
|
|
|
GameManager.instance.SoundObjects.Remove(gameObject);
|
|
|
|
Destroy(gameObject);
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
2022-03-08 04:53:30 +00:00
|
|
|
|
2022-03-07 09:16:31 +00:00
|
|
|
public void KillLoop(float fadeTime)
|
|
|
|
{
|
|
|
|
StartCoroutine(FadeLoop(fadeTime));
|
|
|
|
}
|
|
|
|
|
|
|
|
float loopFadeTimer = 0f;
|
|
|
|
IEnumerator FadeLoop(float fadeTime)
|
|
|
|
{
|
|
|
|
float startingVol = audioSource.volume;
|
|
|
|
|
|
|
|
while (loopFadeTimer < fadeTime)
|
|
|
|
{
|
|
|
|
loopFadeTimer += Time.deltaTime;
|
|
|
|
audioSource.volume = Mathf.Max((1f - (loopFadeTimer / fadeTime)) * startingVol, 0f);
|
|
|
|
yield return null;
|
|
|
|
}
|
|
|
|
|
2022-03-08 04:53:30 +00:00
|
|
|
Delete();
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|