2021-12-21 01:10:49 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-02-11 05:24:52 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Cysharp.Threading.Tasks;
|
2021-12-21 01:10:49 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
2022-03-14 14:21:05 +00:00
|
|
|
namespace HeavenStudio.Util
|
2021-12-21 01:10:49 +00:00
|
|
|
{
|
2023-06-10 19:13:29 +00:00
|
|
|
public class SoundByte
|
2021-12-21 01:10:49 +00:00
|
|
|
{
|
2023-04-03 04:17:55 +00:00
|
|
|
static GameObject oneShotAudioSourceObject;
|
|
|
|
static AudioSource oneShotAudioSource;
|
2023-09-11 22:28:04 +00:00
|
|
|
static int soundIdx = 0;
|
2023-04-03 04:17:55 +00:00
|
|
|
|
Advanced Blocks (#720)
* play sfx and play animation blocks
i also changed prescheduleFunction to preFunction, and removed the unused preFunction argument in GameAction
i can revert this if need be but it just seemed vestigial
* count in rework + preloading, multisound addition
multisound was using an array that was converted to a list..?
very silly when you consider it's a list first so sometimes it's list -> array -> list lol
new Count-In and Play SFX block preloads sfx now!! epic.
* prefab-ify event properties, Button EntityType
* things are very nearly working!
however i just hit an insane hurdle. how do i modify a dropdown while still being able to access the index/int value of that param directly. UGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
* okay it's WORKING now
i just need to do some better dropdown stuff
* ITS WORKING ITS WORKING ITS WORKING
arbitrary animations, now accessible to those without prefab knowledge! and it's piss easy to use!!
* about to make a struct + class, tooltip improvements
gonna make the struct define it, then the class will actually be the dropdown
this is gonna make things so so so so much easier to comprehend
* finishing up, probably one more commit after this
* split up Dropdown into Dropdown and DropdownObj, which basically fixed all of my problems lol
* fixed a count bug
* added param tooltip toggle
* grah it's ALMOST DONE
* it's 99.9% finished.
just some touch ups, i don't think i even know of any bugs
* alright, looks like that's all the bugs gone
* EVERYTHING IS FINISHED!!
2024-02-26 01:46:23 +00:00
|
|
|
public static Dictionary<string, AudioClip> audioClips { get; private set; } = new Dictionary<string, AudioClip>();
|
2024-02-11 05:24:52 +00:00
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
public enum AudioType
|
|
|
|
{
|
|
|
|
OGG,
|
|
|
|
WAV
|
|
|
|
}
|
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-12-21 01:10:49 +00:00
|
|
|
/// <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()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 05:24:52 +00:00
|
|
|
public static void PreloadGameAudioClips(Minigames.Minigame inf)
|
|
|
|
{
|
|
|
|
if (inf.usesAssetBundle)
|
|
|
|
{
|
|
|
|
var cmnAb = inf.GetCommonAssetBundle();
|
|
|
|
if (cmnAb != null)
|
|
|
|
{
|
|
|
|
cmnAb.LoadAllAssetsAsync<AudioClip>().completed += (op) =>
|
|
|
|
{
|
|
|
|
foreach (var clip in (op as AssetBundleRequest).allAssets.Cast<AudioClip>())
|
|
|
|
{
|
|
|
|
OnResourceLoaded(clip, $"games/{inf.name}/{clip.name}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
var locAb = inf.GetLocalizedAssetBundle();
|
|
|
|
if (locAb != null)
|
|
|
|
{
|
|
|
|
locAb.LoadAllAssetsAsync<AudioClip>().completed += (op) =>
|
|
|
|
{
|
|
|
|
foreach (var clip in (op as AssetBundleRequest).allAssets.Cast<AudioClip>())
|
|
|
|
{
|
|
|
|
OnResourceLoaded(clip, $"games/{inf.name}/{clip.name}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string path = $"Sfx/games/{inf.name}";
|
|
|
|
var clips = Resources.LoadAll<AudioClip>(path);
|
|
|
|
foreach (var clip in clips)
|
|
|
|
{
|
|
|
|
OnResourceLoaded(clip, $"games/{inf.name}/{clip.name}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void PreloadGameAudioClips(string game)
|
|
|
|
{
|
|
|
|
var inf = GameManager.instance.GetGameInfo(game);
|
|
|
|
PreloadGameAudioClips(inf);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void PreloadAudioClipAsync(string name, string game)
|
|
|
|
{
|
|
|
|
var inf = GameManager.instance.GetGameInfo(game);
|
|
|
|
if (inf != null)
|
|
|
|
{
|
|
|
|
name = $"games/{name}";
|
|
|
|
}
|
|
|
|
if (audioClips.ContainsKey(name)) return;
|
|
|
|
if (inf.usesAssetBundle)
|
|
|
|
{
|
|
|
|
var cmnAb = inf.GetCommonAssetBundle();
|
|
|
|
if (cmnAb != null && cmnAb.Contains(name))
|
|
|
|
{
|
|
|
|
var request = cmnAb.LoadAssetAsync<AudioClip>(name);
|
|
|
|
request.completed += (op) =>
|
|
|
|
{
|
|
|
|
OnResourceLoaded((op as ResourceRequest).asset as AudioClip, $"{game}/{name}");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var locAb = inf.GetLocalizedAssetBundle();
|
|
|
|
if (locAb != null && locAb.Contains(name))
|
|
|
|
{
|
|
|
|
var request = locAb.LoadAssetAsync<AudioClip>(name);
|
|
|
|
request.completed += (op) =>
|
|
|
|
{
|
|
|
|
OnResourceLoaded((op as ResourceRequest).asset as AudioClip, $"{game}/{name}");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PreloadAudioClipAsync($"{game}/{name}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void PreloadAudioClipAsync(string name)
|
|
|
|
{
|
|
|
|
if (audioClips.ContainsKey(name)) return;
|
|
|
|
string path = $"Sfx/{name}";
|
|
|
|
ResourceRequest request = Resources.LoadAsync<AudioClip>(path);
|
|
|
|
request.completed += (op) =>
|
|
|
|
{
|
|
|
|
OnResourceLoaded((op as ResourceRequest).asset as AudioClip, name);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnResourceLoaded(AudioClip clip, string name)
|
|
|
|
{
|
|
|
|
if (audioClips.ContainsKey(name))
|
|
|
|
{
|
|
|
|
audioClips[name] = clip;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
audioClips.Add(name, clip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void UnloadAudioClips(params string[] names)
|
|
|
|
{
|
|
|
|
foreach (string s in names)
|
|
|
|
{
|
|
|
|
if (audioClips.ContainsKey(s)) audioClips.Remove(s);
|
|
|
|
}
|
|
|
|
Resources.UnloadUnusedAssets();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void UnloadAudioClips()
|
|
|
|
{
|
|
|
|
audioClips.Clear();
|
|
|
|
Resources.UnloadUnusedAssets();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void UnloadAudioClips(string game)
|
|
|
|
{
|
|
|
|
string[] split;
|
|
|
|
foreach (string s in audioClips.Where(x =>
|
|
|
|
{
|
|
|
|
split = x.Key.Split('/');
|
|
|
|
return split.Length > 2 && split[0] == "games" && split[1] == game;
|
|
|
|
}).Select(x => x.Key).ToList())
|
|
|
|
{
|
|
|
|
audioClips.Remove(s);
|
|
|
|
}
|
|
|
|
Resources.UnloadUnusedAssets();
|
|
|
|
}
|
|
|
|
|
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;
|
2024-02-11 05:24:52 +00:00
|
|
|
string soundName = name.Split('/')[^1];
|
2023-04-03 16:40:41 +00:00
|
|
|
if (game != null)
|
|
|
|
{
|
2024-02-11 05:24:52 +00:00
|
|
|
string cachedName = $"games/{game}/{soundName}";
|
|
|
|
if (audioClips.ContainsKey(cachedName))
|
2023-04-03 16:40:41 +00:00
|
|
|
{
|
2024-02-11 05:24:52 +00:00
|
|
|
clip = audioClips[cachedName];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2023-04-03 16:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//can't load from assetbundle, load from resources
|
|
|
|
if (clip == null)
|
|
|
|
{
|
2024-02-11 05:24:52 +00:00
|
|
|
if (audioClips.ContainsKey(name))
|
|
|
|
{
|
|
|
|
clip = audioClips[name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Debug.Log("Jukebox loading sound " + name + " from resources");
|
|
|
|
clip = Resources.Load<AudioClip>($"Sfx/{name}");
|
|
|
|
}
|
2023-04-03 16:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-10 19:13:29 +00:00
|
|
|
public static Sound PlayOneShot(string name, double beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, string game = null, double offset = 0f)
|
2023-04-03 04:17:55 +00:00
|
|
|
{
|
2022-06-12 19:32:00 +00:00
|
|
|
AudioClip clip = null;
|
2024-02-11 05:24:52 +00:00
|
|
|
string soundName = name.Split('/')[^1];
|
2022-06-12 19:32:00 +00:00
|
|
|
if (game != null)
|
|
|
|
{
|
2024-02-11 05:24:52 +00:00
|
|
|
string cachedName = $"games/{game}/{soundName}";
|
|
|
|
if (audioClips.ContainsKey(cachedName))
|
2022-06-12 19:32:00 +00:00
|
|
|
{
|
2024-02-11 05:24:52 +00:00
|
|
|
clip = audioClips[cachedName];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2022-06-12 19:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//can't load from assetbundle, load from resources
|
|
|
|
if (clip == null)
|
|
|
|
{
|
2024-02-11 05:24:52 +00:00
|
|
|
if (audioClips.ContainsKey(name))
|
|
|
|
{
|
|
|
|
clip = audioClips[name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Debug.Log("Jukebox loading sound " + name + " from resources");
|
|
|
|
clip = Resources.Load<AudioClip>($"Sfx/{name}");
|
|
|
|
}
|
2022-06-12 19:32:00 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 04:17:55 +00:00
|
|
|
if (looping || beat != -1 || pitch != 1f)
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
Sound snd = GetAvailableScheduledSound();
|
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-09-11 22:28:04 +00:00
|
|
|
snd.Play();
|
2023-04-03 04:17:55 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
Sound snd = GetAvailableScheduledSound();
|
2022-06-12 19:32:00 +00:00
|
|
|
AudioClip clip = null;
|
2024-02-11 05:24:52 +00:00
|
|
|
string soundName = name.Split('/')[^1];
|
2022-06-12 19:32:00 +00:00
|
|
|
if (game != null)
|
|
|
|
{
|
2024-02-11 05:24:52 +00:00
|
|
|
string cachedName = $"games/{game}/{soundName}";
|
|
|
|
if (audioClips.ContainsKey(cachedName))
|
2022-06-12 19:32:00 +00:00
|
|
|
{
|
2024-02-11 05:24:52 +00:00
|
|
|
clip = audioClips[cachedName];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2022-06-12 19:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//can't load from assetbundle, load from resources
|
|
|
|
if (clip == null)
|
2024-02-11 05:24:52 +00:00
|
|
|
{
|
|
|
|
if (audioClips.ContainsKey(name))
|
|
|
|
{
|
|
|
|
clip = audioClips[name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Debug.Log("Jukebox loading sound " + name + " from resources");
|
|
|
|
clip = Resources.Load<AudioClip>($"Sfx/{name}");
|
|
|
|
}
|
|
|
|
}
|
2022-02-05 13:08:33 +00:00
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
// abort if no clip found
|
|
|
|
|
2022-02-05 13:08:33 +00:00
|
|
|
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;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2022-02-05 13:08:33 +00:00
|
|
|
snd.scheduled = true;
|
|
|
|
snd.scheduledTime = targetTime;
|
2023-09-11 22:28:04 +00:00
|
|
|
snd.Play();
|
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-06-10 19:13:29 +00:00
|
|
|
public static Sound PlayOneShotGame(string name, double beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false, double 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)
|
|
|
|
{
|
2024-02-26 17:46:30 +00:00
|
|
|
var newSemitones = Mathf.Pow(2f, (1f / 12f) * semiTones);
|
|
|
|
if (pitchToMusic) newSemitones *= Conductor.instance.musicSource.pitch;
|
|
|
|
return newSemitones;
|
2023-03-27 03:09:55 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|