2021-12-21 01:10:49 +00:00
|
|
|
using System;
|
|
|
|
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 Jukebox
|
|
|
|
{
|
2023-04-03 04:17:55 +00:00
|
|
|
static GameObject oneShotAudioSourceObject;
|
|
|
|
static AudioSource oneShotAudioSource;
|
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
public enum AudioType
|
|
|
|
{
|
|
|
|
OGG,
|
|
|
|
WAV
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2023-04-03 16:40:41 +00:00
|
|
|
/// Ensures that the jukebox and one-shot audio source exist.
|
2021-12-21 01:10:49 +00:00
|
|
|
/// </summary>
|
|
|
|
public static void BasicCheck()
|
|
|
|
{
|
|
|
|
if (FindJukebox() == null)
|
|
|
|
{
|
|
|
|
GameObject Jukebox = new GameObject("Jukebox");
|
|
|
|
Jukebox.AddComponent<AudioSource>();
|
|
|
|
Jukebox.tag = "Jukebox";
|
2023-04-03 04:17:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
if (oneShotAudioSourceObject == null)
|
|
|
|
{
|
|
|
|
oneShotAudioSourceObject = new GameObject("OneShot Audio Source");
|
|
|
|
oneShotAudioSource = oneShotAudioSourceObject.AddComponent<AudioSource>();
|
|
|
|
UnityEngine.Object.DontDestroyOnLoad(oneShotAudioSourceObject);
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static GameObject FindJukebox()
|
|
|
|
{
|
|
|
|
if (GameObject.FindGameObjectWithTag("Jukebox") != null)
|
|
|
|
return GameObject.FindGameObjectWithTag("Jukebox");
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:40:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Stops all currently playing sounds.
|
|
|
|
/// </summary>
|
2023-04-03 04:17:55 +00:00
|
|
|
public static void KillOneShots()
|
2021-12-21 01:10:49 +00:00
|
|
|
{
|
2023-04-03 04:17:55 +00:00
|
|
|
if (oneShotAudioSource != null)
|
|
|
|
{
|
|
|
|
oneShotAudioSource.Stop();
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 01:24:30 +00:00
|
|
|
|
2023-05-07 20:33:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Pauses all currently playing sounds.
|
|
|
|
/// </summary>
|
|
|
|
public static void PauseOneShots()
|
|
|
|
{
|
|
|
|
if (oneShotAudioSource != null)
|
|
|
|
{
|
|
|
|
oneShotAudioSource.Pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Unpauses all currently playing sounds.
|
|
|
|
/// </summary>
|
|
|
|
public static void UnpauseOneShots()
|
|
|
|
{
|
|
|
|
if (oneShotAudioSource != null)
|
|
|
|
{
|
|
|
|
oneShotAudioSource.UnPause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:40:41 +00:00
|
|
|
/// <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>
|
2023-04-26 12:43:35 +00:00
|
|
|
public static Sound PlayOneShot(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, string game = null, float offset = 0f)
|
2023-04-03 04:17:55 +00:00
|
|
|
{
|
2022-06-12 19:32:00 +00:00
|
|
|
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}");
|
|
|
|
}
|
|
|
|
|
2023-04-03 04:17:55 +00:00
|
|
|
if (looping || beat != -1 || pitch != 1f)
|
|
|
|
{
|
|
|
|
GameObject oneShot = new GameObject("oneShot");
|
|
|
|
|
|
|
|
AudioSource audioSource = oneShot.AddComponent<AudioSource>();
|
|
|
|
//audioSource.outputAudioMixerGroup = Settings.GetSFXMixer();
|
|
|
|
audioSource.playOnAwake = false;
|
|
|
|
|
|
|
|
Sound snd = oneShot.AddComponent<Sound>();
|
2022-01-21 01:24:30 +00:00
|
|
|
|
2023-04-03 04:17:55 +00:00
|
|
|
snd.clip = clip;
|
|
|
|
snd.beat = beat;
|
|
|
|
snd.pitch = pitch;
|
|
|
|
snd.volume = volume;
|
|
|
|
snd.looping = looping;
|
2023-04-26 12:43:35 +00:00
|
|
|
snd.offset = offset;
|
2023-04-03 04:17:55 +00:00
|
|
|
// snd.pitch = (clip.length / Conductor.instance.secPerBeat);
|
|
|
|
|
|
|
|
GameManager.instance.SoundObjects.Add(oneShot);
|
|
|
|
|
|
|
|
return snd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (oneShotAudioSourceObject == null)
|
|
|
|
{
|
|
|
|
oneShotAudioSourceObject = new GameObject("OneShot Audio Source");
|
|
|
|
oneShotAudioSource = oneShotAudioSourceObject.AddComponent<AudioSource>();
|
|
|
|
UnityEngine.Object.DontDestroyOnLoad(oneShotAudioSourceObject);
|
|
|
|
}
|
2022-02-11 07:21:43 +00:00
|
|
|
|
2023-04-03 04:17:55 +00:00
|
|
|
oneShotAudioSource.PlayOneShot(clip, volume);
|
|
|
|
return null;
|
|
|
|
}
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
2021-12-23 22:39:03 +00:00
|
|
|
|
2023-04-03 16:40:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Schedules a sound to be played at a specific time in seconds.
|
|
|
|
/// </summary>
|
2022-06-12 19:32:00 +00:00
|
|
|
public static Sound PlayOneShotScheduled(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, string game = null)
|
2022-02-05 13:08:33 +00:00
|
|
|
{
|
|
|
|
GameObject oneShot = new GameObject("oneShotScheduled");
|
|
|
|
|
|
|
|
var audioSource = oneShot.AddComponent<AudioSource>();
|
|
|
|
audioSource.playOnAwake = false;
|
|
|
|
|
|
|
|
var snd = oneShot.AddComponent<Sound>();
|
2022-06-12 19:32:00 +00:00
|
|
|
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)
|
|
|
|
clip = Resources.Load<AudioClip>($"Sfx/{name}");
|
2022-02-05 13:08:33 +00:00
|
|
|
|
|
|
|
audioSource.clip = clip;
|
|
|
|
snd.clip = clip;
|
2022-03-07 09:16:31 +00:00
|
|
|
snd.pitch = pitch;
|
2022-03-07 09:34:38 +00:00
|
|
|
snd.volume = volume;
|
2022-03-07 09:16:31 +00:00
|
|
|
snd.looping = looping;
|
2022-02-05 13:08:33 +00:00
|
|
|
|
|
|
|
snd.scheduled = true;
|
|
|
|
snd.scheduledTime = targetTime;
|
|
|
|
audioSource.PlayScheduled(targetTime);
|
|
|
|
|
|
|
|
GameManager.instance.SoundObjects.Add(oneShot);
|
2022-02-11 07:21:43 +00:00
|
|
|
|
2022-03-10 03:59:48 +00:00
|
|
|
return snd;
|
2022-02-05 13:08:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:40:41 +00:00
|
|
|
/// <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>
|
2023-04-26 12:43:35 +00:00
|
|
|
public static Sound PlayOneShotGame(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false, float offset = 0f)
|
2021-12-23 22:39:03 +00:00
|
|
|
{
|
2022-06-12 19:32:00 +00:00
|
|
|
string gameName = name.Split('/')[0];
|
|
|
|
var inf = GameManager.instance.GetGameInfo(gameName);
|
|
|
|
if (GameManager.instance.currentGame == gameName || forcePlay)
|
2022-01-21 01:24:30 +00:00
|
|
|
{
|
2023-04-26 12:43:35 +00:00
|
|
|
return PlayOneShot($"games/{name}", beat, pitch, volume, looping, inf.usesAssetBundle ? gameName : null, offset);
|
2022-01-21 01:24:30 +00:00
|
|
|
}
|
2022-02-11 07:21:43 +00:00
|
|
|
|
|
|
|
return null;
|
2021-12-23 22:39:03 +00:00
|
|
|
}
|
2022-02-05 13:08:33 +00:00
|
|
|
|
2023-04-03 16:40:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Schedules a sound to be played at a specific time in seconds.
|
|
|
|
/// Audio clip is fetched from minigame resources
|
|
|
|
/// </summary>
|
2022-03-10 03:59:48 +00:00
|
|
|
public static Sound PlayOneShotScheduledGame(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false)
|
2022-02-05 13:08:33 +00:00
|
|
|
{
|
2022-06-12 19:32:00 +00:00
|
|
|
string gameName = name.Split('/')[0];
|
|
|
|
var inf = GameManager.instance.GetGameInfo(gameName);
|
|
|
|
if (GameManager.instance.currentGame == gameName || forcePlay)
|
2022-02-05 13:08:33 +00:00
|
|
|
{
|
2022-06-12 19:32:00 +00:00
|
|
|
return PlayOneShotScheduled($"games/{name}", targetTime, pitch, volume, looping, inf.usesAssetBundle ? gameName : null);
|
2022-02-05 13:08:33 +00:00
|
|
|
}
|
2022-02-11 07:21:43 +00:00
|
|
|
|
|
|
|
return null;
|
2022-02-05 13:08:33 +00:00
|
|
|
}
|
2022-03-07 09:16:31 +00:00
|
|
|
|
2023-04-03 16:40:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Stops a looping Sound
|
|
|
|
/// </summary>
|
2022-03-10 03:59:48 +00:00
|
|
|
public static void KillLoop(Sound source, float fadeTime)
|
2022-03-07 09:16:31 +00:00
|
|
|
{
|
2022-03-07 09:41:07 +00:00
|
|
|
// Safeguard against previously-destroyed sounds.
|
|
|
|
if (source == null)
|
|
|
|
return;
|
|
|
|
|
2022-03-10 03:59:48 +00:00
|
|
|
source.KillLoop(fadeTime);
|
2022-03-07 09:16:31 +00:00
|
|
|
}
|
2023-03-27 03:09:55 +00:00
|
|
|
|
2023-04-03 16:40:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a pitch multiplier from semitones.
|
|
|
|
/// </summary>
|
2023-03-27 03:09:55 +00:00
|
|
|
public static float GetPitchFromSemiTones(int semiTones, bool pitchToMusic)
|
|
|
|
{
|
|
|
|
if (pitchToMusic)
|
|
|
|
{
|
|
|
|
return Mathf.Pow(2f, (1f / 12f) * semiTones) * Conductor.instance.musicSource.pitch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Mathf.Pow(2f, (1f / 12f) * semiTones);
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 20:09:34 +00:00
|
|
|
/// <summary>
|
2023-06-03 14:51:30 +00:00
|
|
|
/// Returns the semitones from a pitch.
|
2023-05-29 20:09:34 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="pitch">The pitch of the sound.</param>
|
2023-06-03 14:51:30 +00:00
|
|
|
public static int GetSemitonesFromPitch(float pitch, bool pitchToMusic)
|
2023-05-29 20:09:34 +00:00
|
|
|
{
|
2023-06-03 14:51:30 +00:00
|
|
|
if (pitchToMusic) return (int)((12f * Mathf.Log(pitch, 2)) / Conductor.instance.musicSource.pitch);
|
2023-05-29 20:09:34 +00:00
|
|
|
return (int)(12f * Mathf.Log(pitch, 2));
|
|
|
|
}
|
2023-03-27 03:09:55 +00:00
|
|
|
|
2023-04-03 16:40:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a pitch multiplier from cents.
|
|
|
|
/// </summary>
|
2023-03-27 03:09:55 +00:00
|
|
|
public static float GetPitchFromCents(int cents, bool pitchToMusic)
|
|
|
|
{
|
|
|
|
if (pitchToMusic)
|
|
|
|
{
|
|
|
|
return Mathf.Pow(2f, (1f / 12f) * (cents / 100)) * Conductor.instance.musicSource.pitch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Mathf.Pow(2f, (1f / 12f) * (cents / 100));
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|