Better Sound Sequences (#190)

* add way of creating sound sequences in inspector

- actually implement GameAction preFunction
- implement sound scheduling for Jukebox and MultiSound

* Dj School: fix turntable effect being parented to root

* Pajama Party: fix sleep action type not carrying over between transitions
This commit is contained in:
minenice55
2023-01-04 23:04:31 -05:00
committed by GitHub
parent 22133a5c54
commit 87d20b8c8f
28 changed files with 392 additions and 108 deletions

View File

@ -0,0 +1,79 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
namespace HeavenStudio.Util
{
/// <summary>
/// MultiSound that is serializable in the inspector, etc.
/// </summary>
[Serializable]
public class SoundSequence
{
[Tooltip("Should sequence use game specific-sounds?")]
[SerializeField] bool game;
[Tooltip("Should sequence force playback even if corresponding game is not loaded?")]
[SerializeField] bool force;
[Tooltip("Clips to play in the sequence")]
[SerializeField] private List<SequenceClip> clips = new List<SequenceClip>();
public SoundSequence(bool game, bool force, params SequenceClip[] clips)
{
this.game = game;
this.force = force;
this.clips = new List<SequenceClip>(clips);
}
public MultiSound Play(float startBeat)
{
List<MultiSound.Sound> sounds = new List<MultiSound.Sound>();
foreach (SequenceClip clip in clips)
{
sounds.Add(new MultiSound.Sound(clip.clip, startBeat + clip.beat, clip.pitch, clip.volume, clip.looping, clip.offset));
}
return MultiSound.Play(sounds.ToArray(), game, force);
}
[Serializable]
public struct SequenceClip
{
public SequenceClip(string clip, float beat, float pitch = 1f, float volume = 1f, bool looping = false, float offset = 0f)
{
this.clip = clip;
this.beat = beat;
this.pitch = pitch;
this.volume = volume;
this.looping = looping;
this.offset = offset;
}
[Tooltip("Filename of clip to use (will look in assetbundles before resources)")]
public string clip;
[Tooltip("Beat to play clip at relative to start of sequence")]
public float beat;
[Tooltip("Pitch to play clip at")]
[DefaultValue(1f)]
public float pitch;
[Tooltip("Volume to play clip at")]
[DefaultValue(1f)]
public float volume;
[Tooltip("Whether to loop the clip")]
public bool looping;
[Tooltip("Offset to start playing clip")]
public float offset;
}
[Serializable]
public struct SequenceKeyValue
{
[Tooltip("Name of sequence (game scripts will call sequences to play using this name")]
public string name;
[Tooltip("Sequence to play")]
public SoundSequence sequence;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 643b435927934b34599a4250da088992
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -22,16 +22,22 @@ namespace HeavenStudio
// Current song position, in seconds
private double songPos; // for Conductor use only
public float songPosition => (float) songPos;
public double songPositionAsDouble => songPos;
// Current song position, in beats
private double songPosBeat; // for Conductor use only
public float songPositionInBeats => (float) songPosBeat;
public double songPositionInBeatsAsDouble => songPosBeat;
// Current time of the song
private double time;
double lastAbsTime;
// the dspTime we started at
private double dspStartTime;
public double dspStartTimeAsDouble => dspStartTime;
// an AudioSource attached to this GameObject that will play the music.
public AudioSource musicSource;
@ -52,6 +58,7 @@ namespace HeavenStudio
// Metronome tick sound enabled
public bool metronome = false;
Util.Sound metronomeSound;
public float timeSinceLastTempoChange = Single.MinValue;
@ -142,6 +149,7 @@ namespace HeavenStudio
}
}
lastAbsTime = Time.realtimeSinceStartupAsDouble;
dspStartTime = AudioSettings.dspTime;
// GameManager.instance.SetCurrentEventToClosest(songPositionInBeats);
}
@ -189,13 +197,21 @@ namespace HeavenStudio
{
if (ReportBeat(ref lastReportedBeat))
{
Util.Jukebox.PlayOneShot("metronome");
metronomeSound = Util.Jukebox.PlayOneShot("metronome", lastReportedBeat + 1f);
}
else if (songPositionInBeats < lastReportedBeat)
{
lastReportedBeat = Mathf.Round(songPositionInBeats);
}
}
else
{
if (metronomeSound != null)
{
metronomeSound.Delete();
metronomeSound = null;
}
}
}
public bool ReportBeat(ref float lastReportedBeat, float offset = 0, bool shiftBeatToOffset = true)

View File

@ -89,6 +89,23 @@ namespace HeavenStudio
}
}
public void CallPreEvent(DynamicBeatmap.DynamicEntity entity)
{
string[] details = entity.datamodel.Split('/');
Minigames.Minigame game = minigames.Find(c => c.name == details[0]);
try
{
currentEntity = entity;
Minigames.GameAction action = game.actions.Find(c => c.actionName == details[1]);
action.preFunction.Invoke();
}
catch (Exception ex)
{
Debug.LogWarning("Event not found! May be spelled wrong or it is not implemented.\n" + ex);
}
}
public static List<DynamicBeatmap.DynamicEntity> GetAllInGameManagerList(string gameName, string[] include)
{
List<DynamicBeatmap.DynamicEntity> temp1 = GameManager.instance.Beatmap.entities.FindAll(c => c.datamodel.Split('/')[0] == gameName);

View File

@ -33,7 +33,7 @@ namespace HeavenStudio
[Header("Properties")]
public int currentEvent, currentTempoEvent, currentVolumeEvent, currentSectionEvent,
currentPreEvent, currentPreSwitch;
currentPreEvent, currentPreSwitch, currentPreSequence;
public float endBeat;
public float startOffset;
public bool playOnStart;
@ -48,7 +48,7 @@ namespace HeavenStudio
return (Conductor.instance.songPositionInBeats - currentSection.beat) / (nextSection.beat - currentSection.beat);
}}
public event Action<float> onBeatChanged;
public event Action<float> onBeatChanged;
public event Action<DynamicBeatmap.ChartSection> onSectionChange;
public int BeatmapEntities()
@ -69,6 +69,7 @@ namespace HeavenStudio
{
currentPreEvent= 0;
currentPreSwitch = 0;
currentPreSequence = 0;
this.transform.localScale = new Vector3(30000000, 30000000);
@ -219,6 +220,24 @@ namespace HeavenStudio
}
}
public void SeekAheadAndDoPreEvent(float start, float seekTime = 1f)
{
List<float> entities = Beatmap.entities.Select(c => c.beat).ToList();
if (currentPreSequence < Beatmap.entities.Count && currentPreSequence >= 0)
{
if (start + seekTime >= entities[currentPreSequence])
{
float beat = Beatmap.entities[currentPreSequence].beat;
var entitiesAtSameBeat = Beatmap.entities.FindAll(c => c.beat == Beatmap.entities[currentPreSequence].beat);
foreach (DynamicBeatmap.DynamicEntity entity in entitiesAtSameBeat)
{
eventCaller.CallPreEvent(entity);
}
currentPreSequence++;
}
}
}
// LateUpdate works a bit better(?) but causes some bugs (like issues with bop animations).
private void Update()
{
@ -272,6 +291,8 @@ namespace HeavenStudio
//seek ahead to preload games that have assetbundles
SeekAheadAndPreload(Conductor.instance.songPositionInBeats, seekTime);
SeekAheadAndDoPreEvent(Conductor.instance.songPositionInBeats, 1f);
if (currentEvent < Beatmap.entities.Count && currentEvent >= 0)
{
if (Conductor.instance.songPositionInBeats >= entities[currentEvent] /*&& SongPosLessThanClipLength(Conductor.instance.songPositionInBeats)*/)
@ -390,6 +411,7 @@ namespace HeavenStudio
currentEvent = entities.IndexOf(Mathp.GetClosestInList(entities, beat));
currentPreEvent = entities.IndexOf(Mathp.GetClosestInList(entities, beat));
currentPreSequence = entities.IndexOf(Mathp.GetClosestInList(entities, beat));
var gameSwitchs = Beatmap.entities.FindAll(c => c.datamodel.Split(1) == "switchGame");

View File

@ -160,7 +160,7 @@ namespace HeavenStudio.Games.Scripts_DJSchool
tableAnim.speed = 1;
tableAnim.Play("Student_Turntable_Swipe", 0, 0);
Instantiate(slamFX).SetActive(true);
Instantiate(slamFX, this.transform.parent).SetActive(true);
mixer.audioMixer.FindSnapshot("Main").TransitionTo(.01f);
}
else
@ -177,7 +177,7 @@ namespace HeavenStudio.Games.Scripts_DJSchool
tableAnim.speed = 1;
tableAnim.Play("Student_Turntable_Swipe", 0, 0);
Instantiate(slamFX).SetActive(true);
Instantiate(slamFX, this.transform.parent).SetActive(true);
mixer.audioMixer.FindSnapshot("Main").TransitionTo(.01f);
}

View File

@ -182,7 +182,6 @@ namespace HeavenStudio.Games
private void Awake()
{
instance = this;
Spectators = new List<GameObject>();
idolAnimator = Arisa.GetComponent<Animator>();
@ -528,12 +527,7 @@ namespace HeavenStudio.Games
public void CallHai(float beat, bool noSound = false, int type = 0)
{
if (!noSound)
MultiSound.Play(new MultiSound.Sound[] {
new MultiSound.Sound("fanClub/arisa_hai_1_jp", beat),
new MultiSound.Sound("fanClub/arisa_hai_2_jp", beat + 1f),
new MultiSound.Sound("fanClub/arisa_hai_3_jp", beat + 2f),
});
PlaySoundSequence("arisa_hai", beat);
responseToggle = false;
DisableBop(beat, 8f);
@ -556,12 +550,7 @@ namespace HeavenStudio.Games
new BeatAction.Action(beat + 7f, delegate { PlayOneClap(beat + 7f); DoIdolClaps();}),
});
MultiSound.Play(new MultiSound.Sound[] {
new MultiSound.Sound("fanClub/crowd_hai_jp", beat + 4f),
new MultiSound.Sound("fanClub/crowd_hai_jp", beat + 5f),
new MultiSound.Sound("fanClub/crowd_hai_jp", beat + 6f),
new MultiSound.Sound("fanClub/crowd_hai_jp", beat + 7f),
});
PlaySoundSequence("crowd_hai", beat + 4f);
}
public static void WarnHai(float beat, bool noSound = false, int type = 0)
@ -586,27 +575,12 @@ namespace HeavenStudio.Games
bool doJump = (responseType == (int) KamoneResponseType.Jump || responseType == (int) KamoneResponseType.JumpFast);
bool isBig = (responseType == (int) KamoneResponseType.ThroughFast || responseType == (int) KamoneResponseType.JumpFast);
DisableResponse(beat, 2f);
if (isBig)
if (!noSound)
{
if (!noSound)
{
MultiSound.Play(new MultiSound.Sound[] {
new MultiSound.Sound("fanClub/arisa_ka_fast_jp", beat),
new MultiSound.Sound("fanClub/arisa_mo_fast_jp", beat + 0.25f),
new MultiSound.Sound("fanClub/arisa_ne_fast_jp", beat + 0.5f),
});
}
}
else
{
if (!noSound)
{
MultiSound.Play(new MultiSound.Sound[] {
new MultiSound.Sound("fanClub/arisa_ka_jp", beat),
new MultiSound.Sound("fanClub/arisa_mo_jp", beat + 0.5f, offset: 0.07407407f),
new MultiSound.Sound("fanClub/arisa_ne_jp", beat + 1f, offset: 0.07407407f),
});
}
if (isBig)
PlaySoundSequence("arisa_kamone_fast", beat);
else
PlaySoundSequence("arisa_kamone", beat);
}
responseToggle = true;
@ -640,12 +614,7 @@ namespace HeavenStudio.Games
}),
});
MultiSound.Play(new MultiSound.Sound[] {
new MultiSound.Sound("fanClub/crowd_ka_jp", beat + 2f),
new MultiSound.Sound("fanClub/crowd_mo_jp", beat + 3.5f),
new MultiSound.Sound("fanClub/crowd_ne_jp", beat + 4f),
new MultiSound.Sound("fanClub/crowd_hey_jp", beat + 5f),
});
PlaySoundSequence("crowd_kamone", beat + 2f);
}
public static void WarnKamone(float beat, bool noSound = false, int type = 0, int responseType = (int) KamoneResponseType.Through)
@ -679,11 +648,11 @@ namespace HeavenStudio.Games
const float BIGCALL_LENGTH = 2.75f;
public void CallBigReady(float beat, bool noSound = false)
{
if (!noSound)
PlaySoundSequence("crowd_big_ready", beat);
Prepare(beat + 1.5f);
Prepare(beat + 2f);
if (!noSound)
Jukebox.PlayOneShotGame("fanClub/crowd_big_ready");
DisableSpecBop(beat, 3.75f);
@ -701,7 +670,7 @@ namespace HeavenStudio.Games
{
wantBigReady = beat;
if (noSound) return;
Jukebox.PlayOneShotGame("fanClub/crowd_big_ready");
Jukebox.PlayOneShotGame("fanClub/crowd_big_ready", beat);
}
public void ContinueBigReady(float beat)

View File

@ -2,11 +2,15 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HeavenStudio.Util;
namespace HeavenStudio.Games
{
public class Minigame : MonoBehaviour
{
public static float earlyTime = 0.1f, perfectTime = 0.08f, aceEarlyTime = 0.025f, aceLateTime = 0.025f, lateTime = 0.08f, endTime = 0.1f;
[SerializeField] public SoundSequence.SequenceKeyValue[] SoundSequences;
public List<Minigame.Eligible> EligibleHits = new List<Minigame.Eligible>();
[System.Serializable]
@ -211,5 +215,18 @@ namespace HeavenStudio.Games
return sameTime;
}
public MultiSound PlaySoundSequence(string name, float startBeat)
{
foreach (SoundSequence.SequenceKeyValue pair in SoundSequences)
{
if (pair.name == name)
{
return pair.sequence.Play(startBeat);
}
}
Debug.LogWarning($"Sound sequence {name} not found in game {this.name} (did you build AssetBundles?)");
return null;
}
}
}

View File

@ -37,7 +37,7 @@ namespace HeavenStudio.Games.Loaders
new Param("type", PajamaParty.SleepType.Normal, "Sleep Type", "Type of sleep action to use"),
new Param("toggle", false, "Alt. Animation", "Use an alternate animation for Mako")
},
inactiveFunction = delegate {var e = eventCaller.currentEntity; PajamaParty.WarnSleepSequence(e.beat, e["toggle"]);}
inactiveFunction = delegate {var e = eventCaller.currentEntity; PajamaParty.WarnSleepSequence(e.beat, e["toggle"], e["type"]);}
},
new GameAction("throw", "Throw Pillows")
{
@ -280,7 +280,7 @@ namespace HeavenStudio.Games
});
}
public static void WarnSleepSequence(float beat, bool alt = false)
public static void WarnSleepSequence(float beat, bool alt = false, int action = (int) PajamaParty.SleepType.Normal)
{
MultiSound.Play(new MultiSound.Sound[] {
new MultiSound.Sound("pajamaParty/siesta1", beat),
@ -291,6 +291,7 @@ namespace HeavenStudio.Games
}, forcePlay: true);
WantSleepSequence = beat;
WantSleepType = alt;
WantSleepAction = action;
}
public void DoBedImpact()

View File

@ -20,6 +20,7 @@ namespace HeavenStudio
{
public class Minigame
{
public string name;
public string displayName;
public string color;

View File

@ -151,8 +151,6 @@ namespace HeavenStudio.Util
return null;
}
//TODO: playing sounds from assetbundles
public static void KillLoop(Sound source, float fadeTime)
{
// Safeguard against previously-destroyed sounds.

View File

@ -8,10 +8,10 @@ namespace HeavenStudio.Util
public class MultiSound : MonoBehaviour
{
private float startBeat;
private int index;
private bool game;
private bool forcePlay;
public List<Sound> sounds = new List<Sound>();
public List<Util.Sound> playingSounds = new List<Util.Sound>();
public class Sound
{
@ -37,39 +37,35 @@ namespace HeavenStudio.Util
public static MultiSound Play(Sound[] snds, bool game = true, bool forcePlay = false)
{
List<Sound> sounds = snds.ToList();
GameObject gameObj = new GameObject();
MultiSound ms = gameObj.AddComponent<MultiSound>();
GameObject go = new GameObject("MultiSound");
MultiSound ms = go.AddComponent<MultiSound>();
ms.sounds = sounds;
ms.startBeat = sounds[0].beat;
ms.game = game;
ms.forcePlay = forcePlay;
gameObj.name = "MultiSound";
GameManager.instance.SoundObjects.Add(gameObj);
for (int i = 0; i < sounds.Count; i++)
{
Util.Sound s;
if (game)
s = Jukebox.PlayOneShotGame(sounds[i].name, sounds[i].beat - Conductor.instance.GetRestFromRealTime(sounds[i].offset), sounds[i].pitch, sounds[i].volume, sounds[i].looping, forcePlay);
else
s = Jukebox.PlayOneShot(sounds[i].name, sounds[i].beat - Conductor.instance.GetRestFromRealTime(sounds[i].offset), sounds[i].pitch, sounds[i].volume, sounds[i].looping);
ms.playingSounds.Add(s);
}
GameManager.instance.SoundObjects.Add(go);
return ms;
}
private void Update()
{
float songPositionInBeats = Conductor.instance.songPositionInBeats;
for (int i = 0; i < sounds.Count; i++)
foreach (Util.Sound sound in playingSounds)
{
if (songPositionInBeats >= sounds[i].beat - Conductor.instance.GetRestFromRealTime(sounds[i].offset) && index == i)
{
if (game)
Jukebox.PlayOneShotGame(sounds[i].name, sounds[i].beat - Conductor.instance.GetRestFromRealTime(sounds[i].offset), sounds[i].pitch, sounds[i].volume, sounds[i].looping, forcePlay);
else
Jukebox.PlayOneShot(sounds[i].name, sounds[i].beat - Conductor.instance.GetRestFromRealTime(sounds[i].offset), sounds[i].pitch, sounds[i].volume, sounds[i].looping);
index++;
}
}
if (songPositionInBeats >= (sounds[sounds.Count - 1].beat - Conductor.instance.GetRestFromRealTime(sounds[sounds.Count - 1].offset)))
{
Delete();
if (sound != null)
return;
}
Delete();
}
public void Delete()

View File

@ -23,12 +23,13 @@ namespace HeavenStudio.Util
private int pauseTimes = 0;
private float startTime;
private double startTime;
public float beat;
public float scheduledPitch = 1f;
bool playInstant = false;
int playIndex = 0;
bool played = false;
private void Start()
{
@ -40,39 +41,50 @@ namespace HeavenStudio.Util
if (beat == -1 && !scheduled)
{
audioSource.PlayScheduled(Time.time);
audioSource.PlayScheduled(AudioSettings.dspTime);
playInstant = true;
playIndex++;
played = true;
startTime = Conductor.instance.songPositionAsDouble;
StartCoroutine(NotRelyOnBeatSound());
}
else
{
playInstant = false;
scheduledPitch = Conductor.instance.musicSource.pitch;
startTime = (AudioSettings.dspTime + (Conductor.instance.GetSongPosFromBeat(beat) - Conductor.instance.songPositionAsDouble)/(double)scheduledPitch);
audioSource.PlayScheduled(startTime);
Debug.Log($"Scheduling future sound {clip.name} for beat {beat} (scheduled: {startTime}, current time: {AudioSettings.dspTime})");
}
startTime = Conductor.instance.songPosition;
if (!scheduled && !looping)
StartCoroutine(NotRelyOnBeatSound());
}
private void Update()
{
if (playIndex < 1)
if (!played)
{
if (scheduled)
{
if (AudioSettings.dspTime > scheduledTime)
{
StartCoroutine(NotRelyOnBeatSound());
playIndex++;
played = true;
}
}
else if (!playInstant)
{
if (Conductor.instance.songPositionInBeats > beat)
if (AudioSettings.dspTime > startTime)
{
audioSource.PlayScheduled(Time.time);
playIndex++;
played = true;
StartCoroutine(NotRelyOnBeatSound());
}
else
{
if (!played && scheduledPitch != Conductor.instance.musicSource.pitch)
{
scheduledPitch = Conductor.instance.musicSource.pitch;
startTime = (AudioSettings.dspTime + (Conductor.instance.GetSongPosFromBeat(beat) - Conductor.instance.songPositionAsDouble)/(double)scheduledPitch);
audioSource.SetScheduledStartTime(startTime);
Debug.Log($"Rescheduling future sound {clip.name} for beat {beat} (scheduled: {startTime}, current time: {AudioSettings.dspTime})");
}
}
}
}
@ -94,7 +106,7 @@ namespace HeavenStudio.Util
{
if (!looping) // Looping sounds are destroyed manually.
{
yield return new WaitForSeconds(clip.length);
yield return new WaitForSeconds(clip.length / pitch);
Delete();
}
}