using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HeavenStudio.Util
{
public class SoundByte
{
static GameObject oneShotAudioSourceObject;
static AudioSource oneShotAudioSource;
static int soundIdx = 0;
public enum AudioType
{
OGG,
WAV
}
public static Sound GetAvailableScheduledSound()
{
// soundIdx++;
// soundIdx %= GameManager.instance.SoundObjects.Count;
// GameManager.instance.SoundObjects[soundIdx].Stop();
// return GameManager.instance.SoundObjects[soundIdx];
return GameManager.instance.SoundObjects.Get();
}
///
/// Ensures that the jukebox and one-shot audio source exist.
///
public static void BasicCheck()
{
if (FindJukebox() == null)
{
GameObject Jukebox = new GameObject("Jukebox");
Jukebox.AddComponent();
Jukebox.tag = "Jukebox";
}
if (oneShotAudioSourceObject == null)
{
oneShotAudioSourceObject = new GameObject("OneShot Audio Source");
oneShotAudioSource = oneShotAudioSourceObject.AddComponent();
UnityEngine.Object.DontDestroyOnLoad(oneShotAudioSourceObject);
}
}
public static GameObject FindJukebox()
{
if (GameObject.FindGameObjectWithTag("Jukebox") != null)
return GameObject.FindGameObjectWithTag("Jukebox");
else
return null;
}
///
/// Stops all currently playing sounds.
///
public static void KillOneShots()
{
if (oneShotAudioSource != null)
{
oneShotAudioSource.Stop();
}
}
///
/// Pauses all currently playing sounds.
///
public static void PauseOneShots()
{
if (oneShotAudioSource != null)
{
oneShotAudioSource.Pause();
}
}
///
/// Unpauses all currently playing sounds.
///
public static void UnpauseOneShots()
{
if (oneShotAudioSource != null)
{
oneShotAudioSource.UnPause();
}
}
///
/// Gets the length of an audio clip
///
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(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetLocalizedAssetBundle()?.LoadAsset(soundName);
}
}
//can't load from assetbundle, load from resources
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + name + " from resources");
clip = Resources.Load($"Sfx/{name}");
}
if (clip == null)
{
Debug.LogError($"Could not load clip {name}");
return double.NaN;
}
return clip.length / pitch;
}
///
/// Gets the length of an audio clip
/// Audio clip is fetched from minigame resources
///
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;
}
///
/// 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.
///
public static Sound PlayOneShot(string name, double beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, string game = null, double offset = 0f)
{
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(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetLocalizedAssetBundle()?.LoadAsset(soundName);
}
}
//can't load from assetbundle, load from resources
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + name + " from resources");
clip = Resources.Load($"Sfx/{name}");
}
if (looping || beat != -1 || pitch != 1f)
{
Sound snd = GetAvailableScheduledSound();
snd.clip = clip;
snd.beat = beat;
snd.pitch = pitch;
snd.volume = volume;
snd.looping = looping;
snd.offset = offset;
snd.Play();
return snd;
}
else
{
if (oneShotAudioSourceObject == null)
{
oneShotAudioSourceObject = new GameObject("OneShot Audio Source");
oneShotAudioSource = oneShotAudioSourceObject.AddComponent();
UnityEngine.Object.DontDestroyOnLoad(oneShotAudioSourceObject);
}
oneShotAudioSource.PlayOneShot(clip, volume);
return null;
}
}
///
/// Schedules a sound to be played at a specific time in seconds.
///
public static Sound PlayOneShotScheduled(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, string game = null)
{
Sound snd = GetAvailableScheduledSound();
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(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetLocalizedAssetBundle()?.LoadAsset(soundName);
}
}
//can't load from assetbundle, load from resources
if (clip == null)
clip = Resources.Load($"Sfx/{name}");
// abort if no clip found
snd.clip = clip;
snd.pitch = pitch;
snd.volume = volume;
snd.looping = looping;
snd.scheduled = true;
snd.scheduledTime = targetTime;
snd.Play();
return snd;
}
///
/// 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.
///
public static Sound PlayOneShotGame(string name, double beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false, double offset = 0f)
{
string gameName = name.Split('/')[0];
var inf = GameManager.instance.GetGameInfo(gameName);
if (GameManager.instance.currentGame == gameName || forcePlay)
{
return PlayOneShot($"games/{name}", beat, pitch, volume, looping, inf.usesAssetBundle ? gameName : null, offset);
}
return null;
}
///
/// Schedules a sound to be played at a specific time in seconds.
/// Audio clip is fetched from minigame resources
///
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];
var inf = GameManager.instance.GetGameInfo(gameName);
if (GameManager.instance.currentGame == gameName || forcePlay)
{
return PlayOneShotScheduled($"games/{name}", targetTime, pitch, volume, looping, inf.usesAssetBundle ? gameName : null);
}
return null;
}
///
/// Stops a looping Sound
///
public static void KillLoop(Sound source, float fadeTime)
{
// Safeguard against previously-destroyed sounds.
if (source == null)
return;
source.KillLoop(fadeTime);
}
///
/// Gets a pitch multiplier from semitones.
///
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);
}
}
///
/// Returns the semitones from a pitch.
///
/// The pitch of the sound.
public static int GetSemitonesFromPitch(float pitch, bool pitchToMusic)
{
if (pitchToMusic) return (int)((12f * Mathf.Log(pitch, 2)) / Conductor.instance.musicSource.pitch);
return (int)(12f * Mathf.Log(pitch, 2));
}
///
/// Gets a pitch multiplier from cents.
///
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));
}
}
}
}