HeavenStudioPlus/Assets/Scripts/Util/Sound.cs

247 lines
7.7 KiB
C#
Raw Normal View History

using System;
2021-12-21 01:10:49 +00:00
using System.Collections;
using UnityEngine;
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
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;
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
public float bendedPitch = 1; //only used with rockers
public float volume = 1;
2021-12-21 01:10:49 +00:00
// For use with PlayOneShotScheduled
public bool scheduled;
public double scheduledTime;
2022-03-07 09:16:31 +00:00
public bool looping;
public double loopEndBeat = -1;
public double 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;
private double startTime;
2022-01-15 07:08:23 +00:00
public double beat;
public double offset;
public float scheduledPitch = 1f;
2022-01-21 01:24:30 +00:00
bool playInstant = false;
bool played = false;
bool queued = false;
const double PREBAKE_TIME = 0.5;
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;
audioSource.volume = volume;
2022-03-07 09:16:31 +00:00
audioSource.loop = looping;
Conductor cnd = Conductor.instance;
if (beat == -1 && !scheduled)
2022-01-21 01:24:30 +00:00
{
audioSource.Play();
2022-01-21 01:24:30 +00:00
playInstant = true;
played = true;
startTime = AudioSettings.dspTime;
StartCoroutine(NotRelyOnBeatSound());
2022-01-21 01:24:30 +00:00
}
else
{
2022-01-21 01:24:30 +00:00
playInstant = false;
scheduledPitch = cnd.SongPitch;
startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch) - offset;
if (scheduledPitch != 0 && AudioSettings.dspTime >= startTime)
{
audioSource.PlayScheduled(startTime);
queued = true;
}
}
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
{
Conductor cnd = Conductor.instance;
double dspTime = AudioSettings.dspTime;
if (!played)
{
2022-03-07 09:16:31 +00:00
if (scheduled)
{
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());
played = true;
2022-03-07 09:16:31 +00:00
}
}
2022-03-07 09:16:31 +00:00
else if (!playInstant)
2022-01-20 01:48:52 +00:00
{
if (!queued && dspTime > startTime - PREBAKE_TIME)
{
startTime = (dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch) - offset;
audioSource.PlayScheduled(startTime);
queued = true;
}
if (scheduledPitch != 0 && dspTime > startTime)
{
played = true;
StartCoroutine(NotRelyOnBeatSound());
}
else
2022-03-07 09:16:31 +00:00
{
if (!played && scheduledPitch != cnd.SongPitch)
{
if (cnd.SongPitch == 0)
{
scheduledPitch = cnd.SongPitch;
if (queued)
audioSource.Pause();
}
else
{
if (scheduledPitch == 0)
{
audioSource.UnPause();
}
scheduledPitch = cnd.SongPitch;
startTime = (dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch);
if (queued)
audioSource.SetScheduledStartTime(startTime);
}
}
}
}
}
if (loopIndex < 1)
{
2022-03-10 04:00:57 +00:00
if (looping && loopEndBeat != -1) // Looping sounds play forever unless params are set.
{
if (cnd.songPositionInBeats > loopEndBeat)
{
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-15 07:08:23 +00:00
IEnumerator NotRelyOnBeatSound()
{
2022-03-07 09:16:31 +00:00
if (!looping) // Looping sounds are destroyed manually.
{
yield return new WaitUntil(() => !audioSource.isPlaying);
Delete();
2022-03-07 09:16:31 +00:00
}
}
public void SetLoopParams(double endBeat, double fadeTime)
2022-03-10 03:59:48 +00:00
{
loopEndBeat = endBeat;
this.fadeTime = fadeTime;
}
public void Stop()
{
if (audioSource != null)
audioSource.Stop();
2022-03-10 03:59:48 +00:00
}
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
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);
2021-12-21 01:10:49 +00:00
}
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
#region Bend
// All of these should only be used with rockers.
// minenice: consider doing these in the audio thread so they can work per-sample?
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
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(double fadeTime)
2022-03-07 09:16:31 +00:00
{
StartCoroutine(FadeLoop(fadeTime));
}
float loopFadeTimer = 0f;
IEnumerator FadeLoop(double fadeTime)
2022-03-07 09:16:31 +00:00
{
float startingVol = audioSource.volume;
while (loopFadeTimer < fadeTime)
{
loopFadeTimer += Time.deltaTime;
audioSource.volume = (float) Math.Max((1f - (loopFadeTimer / fadeTime)) * startingVol, 0.0);
2022-03-07 09:16:31 +00:00
yield return null;
}
Delete();
2021-12-21 01:10:49 +00:00
}
}
}