mirror of
https://github.com/RHeavenStudio/HeavenStudio.git
synced 2025-06-12 11:37:41 +02:00
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:
@ -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.
|
||||
|
@ -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()
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user