script based audio delay

This commit is contained in:
2023-04-30 14:09:12 +03:00
parent 353db087a6
commit 6806c89dcb
2 changed files with 87 additions and 59 deletions

View File

@@ -28,27 +28,51 @@ namespace Game
public static void PlaySound(string soundName, Actor actor, Float3 position, float volume = 1f) public static void PlaySound(string soundName, Actor actor, Float3 position, float volume = 1f)
{ {
PlaySound(soundName, actor, 0, AudioFlags.None, position, volume, Float2.One); PlaySoundInternal(soundName, actor, 0, AudioFlags.None, position, volume, Float2.One, Float2.Zero);
} }
public static void PlaySound(string soundName, Actor actor, Float3 position, float volume, Float2 pitchRange) public static void PlaySound(string soundName, Actor actor, Float3 position, float volume, Float2 pitchRange)
{ {
PlaySound(soundName, actor, 0, AudioFlags.None, position, volume, pitchRange); PlaySoundInternal(soundName, actor, 0, AudioFlags.None, position, volume, pitchRange, Float2.Zero);
} }
public static void PlaySound(string soundName, Actor actor, int channel, Float3 position, float volume = 1f) public static void PlaySound(string soundName, Actor actor, int channel, Float3 position, float volume = 1f)
{ {
PlaySound(soundName, actor, channel, AudioFlags.None, position, volume, Float2.One); PlaySoundInternal(soundName, actor, channel, AudioFlags.None, position, volume, Float2.One, Float2.Zero);
} }
public static void PlaySound(string soundName, Actor actor, int channel, AudioFlags flags, Float3 position, public static void PlaySound(string soundName, Actor actor, int channel, AudioFlags flags, Float3 position,
float volume = 1f) float volume = 1f)
{ {
PlaySound(soundName, actor, channel, flags, position, volume, Float2.One); PlaySoundInternal(soundName, actor, channel, flags, position, volume, Float2.One, Float2.Zero);
} }
public static void PlaySound(string soundName, Actor actor, int channel, AudioFlags flags, Float3 position, public static void PlaySound(string soundName, Actor actor, int channel, AudioFlags flags, Float3 position,
float volume, Float2 pitchRange) float volume, Float2 pitchRange)
{
PlaySoundInternal(soundName, actor, channel, flags, position, volume, pitchRange, Float2.Zero);
}
public static void PlaySoundDelayed(Float2 delayRange, string soundName, Actor actor, int channel,
Float3 position, float volume = 1f)
{
PlaySoundInternal(soundName, actor, channel, AudioFlags.None, position, volume, Float2.One, delayRange);
}
public static void PlaySoundDelayed(Float2 delayRange, string soundName, Actor actor, int channel,
Float3 position, float volume, Float2 pitchRange)
{
PlaySoundInternal(soundName, actor, channel, AudioFlags.None, position, volume, pitchRange, delayRange);
}
public static void PlaySoundDelayed(Float2 delayRange, string soundName, Actor actor, int channel,
AudioFlags flags, Float3 position, float volume, Float2 pitchRange)
{
PlaySoundInternal(soundName, actor, channel, flags, position, volume, pitchRange, delayRange);
}
private static void PlaySoundInternal(string soundName, Actor actor, int channel, AudioFlags flags, Float3 position,
float volume, Float2 pitchRange, Float2 delayRange)
{ {
AudioInfo audio = GetSound(soundName); AudioInfo audio = GetSound(soundName);
if (audio.AudioClips.Length == 0) if (audio.AudioClips.Length == 0)
@@ -99,82 +123,39 @@ namespace Game
audioClip = audio.AudioClips[0]; audioClip = audio.AudioClips[0];
} }
// Randomized pitch
float pitch; float pitch;
if (pitchRange[0] < pitchRange[1]) // Randomized pitch if (pitchRange[0] < pitchRange[1])
pitch = (float)(pitchRange[0] + random.NextDouble() * (pitchRange[1] - pitchRange[0])); pitch = (float)(pitchRange[0] + random.NextDouble() * (pitchRange[1] - pitchRange[0]));
else else
pitch = pitchRange[0]; pitch = pitchRange[0];
AudioSource audioSource = new AudioSource(); // Randomized start delay
float randomDelay;
if (delayRange[0] < delayRange[1])
randomDelay = (float)(delayRange[0] + random.NextDouble() * (delayRange[1] - delayRange[0]));
else
randomDelay = delayRange[0];
AudioSourceDelayed audioSource = new AudioSourceDelayed();
audioSource.Delay = randomDelay;
audioSource.Clip = audioClip; audioSource.Clip = audioClip;
audioSource.Position = position; audioSource.Position = position;
audioSource.Parent = actor.Parent; audioSource.Parent = actor.Parent;
audioSource.Pitch = pitch; audioSource.Pitch = pitch;
audioSource.Name = Path.GetFileNameWithoutExtension(audioClip.Path); audioSource.Name = Path.GetFileNameWithoutExtension(audioClip.Path);
audioSource.Volume = volume; audioSource.Volume = volume;
audioSource.PlayOnStart = randomDelay == 0;
if (volume != 1f) if (volume != 1f)
audioSource.Name += ", vol: " + volume; audioSource.Name += ", vol: " + volume;
if (pitch != 1f) if (pitch != 1f)
audioSource.Name += ", pitch: " + pitch; audioSource.Name += ", pitch: " + pitch;
audioSource.Play();
Object.Destroy(audioSource, audioClip.Length);
if (channel > 0) if (channel > 0)
actorChannels.channelSources[channel] = audioSource; actorChannels.channelSources[channel] = audioSource;
} }
public static async void PlaySoundDelayed(Float2 delayRange, string soundName, Actor actor, int channel,
Float3 position, float volume = 1f)
{
float randomDelay;
if (delayRange[0] < delayRange[1])
randomDelay = (float)(delayRange[0] + random.NextDouble() * (delayRange[1] - delayRange[0]));
else
randomDelay = delayRange[0];
//PlaySoundDelayed(delay, soundName, actor, channel, AudioFlags.None, position, volume, Float2.One);
await Task.Delay((int)(randomDelay * 1000f));
//FlaxEngine.Scripting.RunOnUpdate(() =>
// PlaySound(soundName, actor, channel, flags, position, volume, pitchRange));
if (actor) // Engine might have cleaned up the actor
PlaySound(soundName, actor, channel, AudioFlags.None, position, volume, Float2.One);
}
public static async void PlaySoundDelayed(Float2 delayRange, string soundName, Actor actor, int channel,
Float3 position, float volume, Float2 pitchRange)
{
float randomDelay;
if (delayRange[0] < delayRange[1])
randomDelay = (float)(delayRange[0] + random.NextDouble() * (delayRange[1] - delayRange[0]));
else
randomDelay = delayRange[0];
//PlaySoundDelayed(delay, soundName, actor, channel, AudioFlags.None, position, volume, Float2.One);
await Task.Delay((int)(randomDelay * 1000f));
//FlaxEngine.Scripting.RunOnUpdate(() =>
// PlaySound(soundName, actor, channel, flags, position, volume, pitchRange));
if (actor) // Engine might have cleaned up the actor
PlaySound(soundName, actor, channel, AudioFlags.None, position, volume, pitchRange);
}
public static async void PlaySoundDelayed(Float2 delayRange, string soundName, Actor actor, int channel,
AudioFlags flags, Float3 position, float volume, Float2 pitchRange)
{
float randomDelay;
if (delayRange[0] < delayRange[1])
randomDelay = (float)(delayRange[0] + random.NextDouble() * (delayRange[1] - delayRange[0]));
else
randomDelay = delayRange[0];
await Task.Delay((int)(randomDelay * 1000f));
//FlaxEngine.Scripting.RunOnUpdate(() =>
// PlaySound(soundName, actor, channel, flags, position, volume, pitchRange));
if (actor) // Engine might have cleaned up the actor
PlaySound(soundName, actor, channel, flags, position, volume, pitchRange);
}
public static void StopSound(Actor actor, int channel) public static void StopSound(Actor actor, int channel)
{ {
if (channel <= 0) if (channel <= 0)

View File

@@ -0,0 +1,47 @@
using FlaxEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
public class AudioSourceDelayed : AudioSource
{
public float Delay = 0f;
public override void OnBeginPlay()
{
if (Delay >= 0f)
{
PlayOnStart = false;
var script = AddScript<AudioSourceDelayedScript>();
script.PlayStartTime = Delay + FlaxEngine.Time.GameTime;
}
base.OnBeginPlay();
}
}
public class AudioSourceDelayedScript : Script
{
[NoSerialize]
[HideInEditor]
public float PlayStartTime = 0f;
public override void OnStart()
{
FlaxEngine.Object.Destroy(Actor, Actor.As<AudioSourceDelayed>().Clip.Length + Actor.As<AudioSourceDelayed>().Delay);
}
public override void OnFixedUpdate()
{
if (FlaxEngine.Time.GameTime >= PlayStartTime)
{
PlayStartTime = float.MaxValue;
Actor.As<AudioSourceDelayed>().Play();
}
}
}
}