HeavenStudioPlus/Assets/Scripts/Util/SoundByte.cs

499 lines
18 KiB
C#
Raw Permalink Normal View History

2021-12-21 01:10:49 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BurstLinq;
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
{
public class SoundByte
2021-12-21 01:10:49 +00:00
{
static GameObject oneShotAudioSourceObject;
static AudioSource oneShotAudioSource;
//static int soundIdx = 0; Unused value - Marc
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>();
2021-12-21 01:10:49 +00:00
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();
}
2021-12-21 01:10:49 +00:00
/// <summary>
/// Ensures that the jukebox and one-shot audio source exist.
2021-12-21 01:10:49 +00:00
/// </summary>
public static void BasicCheck()
{
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
}
}
/// <summary>
/// Stops all currently playing sounds.
/// </summary>
public static void KillOneShots()
2021-12-21 01:10:49 +00:00
{
if (oneShotAudioSource != null)
{
oneShotAudioSource.Stop();
}
}
2022-01-21 01:24:30 +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();
}
}
public static void PreloadGameAudioClips(Minigames.Minigame inf)
{
if (inf.UsesAssetBundle)
{
var cmnAb = inf.GetResourcesAssetBundle();
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.GetAudioAssetBundle();
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.GetResourcesAssetBundle();
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.GetAudioAssetBundle();
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();
}
/// <summary>
/// Gets the length of an audio clip
/// </summary>
public static double GetClipLength(string name, float pitch = 1f, string game = null)
{
AudioClip clip = null;
string soundName = name.Split('/')[^1];
if (game != null)
{
string cachedName = $"games/{game}/{soundName}";
if (audioClips.ContainsKey(cachedName))
{
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.GetResourcesAssetBundle()?.LoadAsset<AudioClip>(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetAudioAssetBundle()?.LoadAsset<AudioClip>(soundName);
}
}
}
//can't load from assetbundle, load from resources
if (clip == null)
{
if (audioClips.ContainsKey(name))
{
clip = audioClips[name];
}
else
{
// 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, double beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, string game = null, double offset = 0f, bool ignoreConductorPause = false, bool ignoreSwing = false)
{
AudioClip clip = null;
string soundName = name.Split('/')[^1];
if (game != null)
{
string cachedName = $"games/{game}/{soundName}";
if (audioClips.ContainsKey(cachedName))
{
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.GetResourcesAssetBundle()?.LoadAsset<AudioClip>(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetAudioAssetBundle()?.LoadAsset<AudioClip>(soundName);
}
}
}
//can't load from assetbundle, load from resources
if (clip == null)
{
if (audioClips.ContainsKey(name))
{
clip = audioClips[name];
}
else
{
// Debug.Log("Jukebox loading sound " + name + " from resources");
clip = Resources.Load<AudioClip>($"Sfx/{name}");
}
}
if (looping || beat != -1 || pitch != 1f)
{
Sound snd = GetAvailableScheduledSound();
2022-01-21 01:24:30 +00:00
snd.clip = clip;
snd.pitch = pitch;
snd.volume = volume;
snd.looping = looping;
snd.offset = offset;
snd.ignoreConductorPause = ignoreConductorPause;
if (ignoreSwing) snd.beat = Conductor.instance.GetSwungBeat(beat);
else snd.beat = beat;
snd.Play();
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
oneShotAudioSource.PlayOneShot(clip, volume);
2024-05-14 03:04:40 +00:00
// Debug.Log("Played Sound");
return null;
}
2021-12-21 01:10:49 +00:00
}
2021-12-23 22:39:03 +00:00
/// <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, bool ignoreConductorPause = false, bool ignoreSwing = false)
{
Sound snd = GetAvailableScheduledSound();
AudioClip clip = null;
string soundName = name.Split('/')[^1];
if (game != null)
{
string cachedName = $"games/{game}/{soundName}";
if (audioClips.ContainsKey(cachedName))
{
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.GetResourcesAssetBundle()?.LoadAsset<AudioClip>(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetAudioAssetBundle()?.LoadAsset<AudioClip>(soundName);
}
}
}
//can't load from assetbundle, load from resources
if (clip == null)
{
if (audioClips.ContainsKey(name))
{
clip = audioClips[name];
}
else
{
// Debug.Log("Jukebox loading sound " + name + " from resources");
clip = Resources.Load<AudioClip>($"Sfx/{name}");
}
}
// abort if no clip found
snd.clip = clip;
2022-03-07 09:16:31 +00:00
snd.pitch = pitch;
snd.volume = volume;
2022-03-07 09:16:31 +00:00
snd.looping = looping;
snd.scheduled = true;
if (ignoreSwing) snd.scheduledTime = Conductor.instance.GetSwungBeat(targetTime);
else snd.scheduledTime = targetTime;
snd.ignoreConductorPause = ignoreConductorPause;
snd.Play();
2022-02-11 07:21:43 +00:00
2022-03-10 03:59:48 +00:00
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, double beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false, double offset = 0f, bool ignoreConductorPause = false, bool ignoreSwing = false)
2021-12-23 22:39:03 +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
{
return PlayOneShot($"games/{name}", beat, pitch, volume, looping, inf.UsesAssetBundle ? gameName : null, offset, ignoreConductorPause, ignoreSwing);
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
}
/// <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, bool ignoreConductorPause = false, bool ignoreSwing = 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, ignoreConductorPause, ignoreSwing);
}
2022-02-11 07:21:43 +00:00
return null;
}
2022-03-07 09:16:31 +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
}
/// <summary>
/// Gets a pitch multiplier from semitones.
/// </summary>
public static float GetPitchFromSemiTones(int semiTones, bool pitchToMusic)
{
var newSemitones = Mathf.Pow(2f, (1f / 12f) * semiTones);
if (pitchToMusic) newSemitones *= Conductor.instance.musicSource.pitch;
return newSemitones;
}
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
/// <summary>
/// Returns the semitones from a pitch.
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
/// </summary>
/// <param name="pitch">The pitch of the sound.</param>
public static int GetSemitonesFromPitch(float pitch, bool pitchToMusic)
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
{
if (pitchToMusic) return (int)((12f * Mathf.Log(pitch, 2)) / Conductor.instance.musicSource.pitch);
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
return (int)(12f * Mathf.Log(pitch, 2));
}
/// <summary>
/// Gets a pitch multiplier from cents.
/// </summary>
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
}
}