HeavenStudioPlus/Assets/Scripts/Util/Sound.cs
ev ee5397e9dd Rockers + Rhythm Tweezers Call and Response API (#444)
* rock hers

* Rockers is a real game and also color maps have been added

* little more set up

* anims and mor sprites

* First version of CallAndResponseHandler added

* You can mute now wow

* Got some stuff working

* anim city

* Fixed Inputs

* Visual goodies

* Changed how some events work

* Rockers is now stack proof

* Fixed a bug

* Bend early stages

* bendbendbendbendbendover

* bend fully implemented

* Removed "noise"

* pain

* Many animation

* Bend anims implemented

* strum effect implemented

* Made bends work way better

* dfgdfsgsdffsd

* Implemented strumstart and countin

* hi

* Made strumstart transition into strumidle

* Implemented samples

* All of the btsds samples are in now too

* many anim2

* A buggy version of the custom together system has been implemented

* Ok now it's unbuggified

* fixed a small thing

* lightning eff

* anim fixes

* oops

* you can now mute voiceline and also put in a standalone voiceline block

* Tweaks to dropdowns

* more tiny anim thing

* more animation stuff

* Bug fixes

* implemented mute and gotomiddle sliders for custom together event

* Default cmon and last one added

* You can chain last ones and cmons now

* Applause sounds added

* Fixed some bugs

* Made it so you can disable camera movement

* fixed an inconsistency

* Rhythm tweezers is actually kinda playable now, not finished though, i need to make beat offset work with this

* Rhythm tweezers now works between game switches

* Beat offset eradication

* Made eye size work properly

* Camera quad ease rather than quint

* Inactive intervals added

* Rockers works inactively too now

* Bug fix

* No peeking! No way!

* Alt smile added for tweezers

* early and late riff

* jj miss anim

* icon and miss

* Long hair works properly now

* Miss anims implemented for rockers

---------

Co-authored-by: Rapandrasmus <78219215+Rapandrasmus@users.noreply.github.com>
Co-authored-by: minenice55 <star.elementa@gmail.com>
2023-05-29 20:09:34 +00:00

224 lines
6.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Starpelly;
namespace HeavenStudio.Util
{
public class Sound : MonoBehaviour
{
public AudioClip clip;
public float pitch = 1;
public float bendedPitch = 1; //only used with rockers
public float volume = 1;
// For use with PlayOneShotScheduled
public bool scheduled;
public double scheduledTime;
public bool looping;
public float loopEndBeat = -1;
public float fadeTime;
int loopIndex = 0;
private AudioSource audioSource;
private int pauseTimes = 0;
private double startTime;
public float beat;
public float offset;
public float scheduledPitch = 1f;
bool playInstant = false;
bool played = false;
private void Start()
{
audioSource = GetComponent<AudioSource>();
audioSource.clip = clip;
audioSource.pitch = pitch;
audioSource.volume = volume;
audioSource.loop = looping;
Conductor cnd = Conductor.instance;
if (beat == -1 && !scheduled)
{
audioSource.Play();
playInstant = true;
played = true;
startTime = cnd.songPositionAsDouble;
StartCoroutine(NotRelyOnBeatSound());
}
else
{
playInstant = false;
scheduledPitch = cnd.SongPitch;
startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch) - offset;
audioSource.PlayScheduled(startTime);
}
}
private void Update()
{
Conductor cnd = Conductor.instance;
if (!played)
{
if (scheduled)
{
if (scheduledPitch != 0 && AudioSettings.dspTime > scheduledTime)
{
StartCoroutine(NotRelyOnBeatSound());
played = true;
}
}
else if (!playInstant)
{
if (scheduledPitch != 0 && AudioSettings.dspTime > startTime)
{
played = true;
StartCoroutine(NotRelyOnBeatSound());
}
else
{
if (!played && scheduledPitch != cnd.SongPitch)
{
if (cnd.SongPitch == 0)
{
scheduledPitch = cnd.SongPitch;
audioSource.Pause();
}
else
{
if (scheduledPitch == 0)
{
audioSource.UnPause();
}
scheduledPitch = cnd.SongPitch;
startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch);
audioSource.SetScheduledStartTime(startTime);
}
}
}
}
}
if (loopIndex < 1)
{
if (looping && loopEndBeat != -1) // Looping sounds play forever unless params are set.
{
if (cnd.songPositionInBeats > loopEndBeat)
{
KillLoop(fadeTime);
loopIndex++;
}
}
}
}
IEnumerator NotRelyOnBeatSound()
{
if (!looping) // Looping sounds are destroyed manually.
{
yield return new WaitForSeconds(clip.length / pitch);
Delete();
}
}
public void SetLoopParams(float endBeat, float fadeTime)
{
loopEndBeat = endBeat;
this.fadeTime = fadeTime;
}
public void Stop()
{
if (audioSource != null)
audioSource.Stop();
}
public void Pause()
{
if (audioSource != null)
audioSource.Pause();
}
public void UnPause()
{
if (audioSource != null)
audioSource.UnPause();
}
public void Delete()
{
GameManager.instance.SoundObjects.Remove(gameObject);
Destroy(gameObject);
}
#region Bend
// All of these should only be used with rockers.
public void BendUp(float bendTime, float bendedPitch)
{
this.bendedPitch = bendedPitch;
StartCoroutine(BendUpLoop(bendTime));
}
public void BendDown(float bendTime)
{
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
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;
}
Delete();
}
}
}