add proper way of getting clip length (#385)

add code doc to Jukebox
This commit is contained in:
minenice55 2023-04-03 12:40:41 -04:00 committed by GitHub
parent fba540f537
commit 25f469c26a
2 changed files with 84 additions and 8 deletions

View File

@ -1401,7 +1401,8 @@ namespace HeavenStudio.Games
void SuccessHoldSpin()
{
player.StartSpinBook();
SpinningLoop = Jukebox.PlayOneShotScheduledGame("cheerReaders/bookSpinLoop", Jukebox.PlayOneShotGame("cheerReaders/bookSpin").clip.length, 1, 1, true);
Jukebox.PlayOneShotGame("cheerReaders/bookSpin");
SpinningLoop = Jukebox.PlayOneShotScheduledGame("cheerReaders/bookSpinLoop", Jukebox.GetClipLengthGame("cheerReaders/bookSpin"), 1, 1, true);
}
void JustReleaseSpin(PlayerActionEvent caller, float state)

View File

@ -17,7 +17,7 @@ namespace HeavenStudio.Util
}
/// <summary>
/// This is me just idiot-proofing.
/// Ensures that the jukebox and one-shot audio source exist.
/// </summary>
public static void BasicCheck()
{
@ -45,12 +45,9 @@ namespace HeavenStudio.Util
return null;
}
public static void SetVolume(float volume)
{
BasicCheck();
FindJukebox().GetComponent<AudioSource>().volume = volume;
}
/// <summary>
/// Stops all currently playing sounds.
/// </summary>
public static void KillOneShots()
{
if (oneShotAudioSource != null)
@ -59,6 +56,63 @@ namespace HeavenStudio.Util
}
}
/// <summary>
/// Gets the length of an audio clip
/// </summary>
public static double GetClipLength(string name, float pitch = 1f, string game = null)
{
AudioClip clip = null;
if (game != null)
{
string soundName = name.Split('/')[2];
var inf = GameManager.instance.GetGameInfo(game);
//first try the game's common assetbundle
// Debug.Log("Jukebox loading sound " + soundName + " from common");
clip = inf.GetCommonAssetBundle()?.LoadAsset<AudioClip>(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetLocalizedAssetBundle()?.LoadAsset<AudioClip>(soundName);
}
}
//can't load from assetbundle, load from resources
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + name + " from resources");
clip = Resources.Load<AudioClip>($"Sfx/{name}");
}
if (clip == null)
{
Debug.LogError($"Could not load clip {name}");
return double.NaN;
}
return clip.length / pitch;
}
/// <summary>
/// Gets the length of an audio clip
/// Audio clip is fetched from minigame resources
/// </summary>
public static double GetClipLengthGame(string name, float pitch = 1f)
{
string gameName = name.Split('/')[0];
var inf = GameManager.instance.GetGameInfo(gameName);
if (inf != null)
{
return GetClipLength($"games/{name}", pitch, inf.usesAssetBundle ? gameName : null);
}
return double.NaN;
}
/// <summary>
/// Fires a one-shot sound.
/// Unpitched, non-scheduled, non-looping sounds are played using a global One-Shot audio source that doesn't create a Sound object.
/// Looped sounds return their created Sound object so they can be canceled after creation.
/// </summary>
public static Sound PlayOneShot(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, string game = null)
{
AudioClip clip = null;
@ -119,6 +173,9 @@ namespace HeavenStudio.Util
}
}
/// <summary>
/// Schedules a sound to be played at a specific time in seconds.
/// </summary>
public static Sound PlayOneShotScheduled(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, string game = null)
{
GameObject oneShot = new GameObject("oneShotScheduled");
@ -162,6 +219,11 @@ namespace HeavenStudio.Util
return snd;
}
/// <summary>
/// Fires a one-shot sound located in minigame resources.
/// Unpitched, non-scheduled, non-looping sounds are played using a global One-Shot audio source that doesn't create a Sound object.
/// Looped sounds return their created Sound object so they can be canceled after creation.
/// </summary>
public static Sound PlayOneShotGame(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false)
{
string gameName = name.Split('/')[0];
@ -174,6 +236,10 @@ namespace HeavenStudio.Util
return null;
}
/// <summary>
/// Schedules a sound to be played at a specific time in seconds.
/// Audio clip is fetched from minigame resources
/// </summary>
public static Sound PlayOneShotScheduledGame(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false)
{
string gameName = name.Split('/')[0];
@ -186,6 +252,9 @@ namespace HeavenStudio.Util
return null;
}
/// <summary>
/// Stops a looping Sound
/// </summary>
public static void KillLoop(Sound source, float fadeTime)
{
// Safeguard against previously-destroyed sounds.
@ -195,6 +264,9 @@ namespace HeavenStudio.Util
source.KillLoop(fadeTime);
}
/// <summary>
/// Gets a pitch multiplier from semitones.
/// </summary>
public static float GetPitchFromSemiTones(int semiTones, bool pitchToMusic)
{
if (pitchToMusic)
@ -207,6 +279,9 @@ namespace HeavenStudio.Util
}
}
/// <summary>
/// Gets a pitch multiplier from cents.
/// </summary>
public static float GetPitchFromCents(int cents, bool pitchToMusic)
{
if (pitchToMusic)