add delayed audio tween support

pull/47/head
cosmonaut 2023-03-06 18:55:20 -08:00
parent 4d41d230af
commit 4df836a6a7
3 changed files with 114 additions and 24 deletions

View File

@ -32,6 +32,7 @@ namespace MoonWorks.Audio
private AudioTweenPool AudioTweenPool = new AudioTweenPool();
private readonly List<AudioTween> AudioTweens = new List<AudioTween>();
private readonly List<AudioTween> DelayedAudioTweens = new List<AudioTween>();
private const int Step = 200;
private TimeSpan UpdateInterval;
@ -163,6 +164,52 @@ namespace MoonWorks.Audio
}
}
for (var i = DelayedAudioTweens.Count - 1; i >= 0; i--)
{
var audioTween = DelayedAudioTweens[i];
if (audioTween.SoundInstanceReference.TryGetTarget(out var soundInstance))
{
audioTween.Time += elapsedSeconds;
if (audioTween.Time >= audioTween.DelayTime)
{
switch (audioTween.Property)
{
case AudioTweenProperty.Pan:
audioTween.StartValue = soundInstance.Pan;
break;
case AudioTweenProperty.Pitch:
audioTween.StartValue = soundInstance.Pitch;
break;
case AudioTweenProperty.Volume:
audioTween.StartValue = soundInstance.Volume;
break;
case AudioTweenProperty.FilterFrequency:
audioTween.StartValue = soundInstance.FilterFrequency;
break;
case AudioTweenProperty.Reverb:
audioTween.StartValue = soundInstance.Reverb;
break;
}
audioTween.Time = 0;
AudioTweens.Add(audioTween);
DelayedAudioTweens.RemoveAt(i);
}
}
else
{
AudioTweenPool.Free(audioTween);
DelayedAudioTweens.RemoveAt(i);
}
}
for (var i = AudioTweens.Count - 1; i >= 0; i--)
{
bool finished = true;
@ -192,7 +239,8 @@ namespace MoonWorks.Audio
EasingFunction easingFunction,
float start,
float end,
float duration
float duration,
float delayTime
) {
lock (StateLock)
{
@ -200,13 +248,21 @@ namespace MoonWorks.Audio
tween.SoundInstanceReference = new WeakReference<SoundInstance>(soundInstance);
tween.Property = property;
tween.EasingFunction = easingFunction;
tween.Start = start;
tween.End = end;
tween.StartValue = start;
tween.EndValue = end;
tween.Duration = duration;
tween.Time = 0;
tween.DelayTime = delayTime;
if (delayTime == 0)
{
AudioTweens.Add(tween);
}
else
{
DelayedAudioTweens.Add(tween);
}
}
}
private bool UpdateAudioTween(AudioTween audioTween, SoundInstance soundInstance, float delta)
@ -215,16 +271,15 @@ namespace MoonWorks.Audio
audioTween.Time += delta;
var finished = audioTween.Time >= audioTween.Duration;
if (finished)
{
value = audioTween.End;
value = audioTween.EndValue;
}
else
{
value = MoonWorks.Math.Easing.Interp(
audioTween.Start,
audioTween.End,
audioTween.StartValue,
audioTween.EndValue,
audioTween.Time,
audioTween.Duration,
audioTween.EasingFunction

View File

@ -18,8 +18,9 @@ namespace MoonWorks.Audio
public AudioTweenProperty Property;
public EasingFunction EasingFunction;
public float Time;
public float Start;
public float End;
public float StartValue;
public float EndValue;
public float DelayTime;
public float Duration;
}

View File

@ -109,10 +109,12 @@ namespace MoonWorks.Audio
}
}
private float FilterOneOverQ
public float FilterOneOverQ
{
get => filterParameters.OneOverQ;
set
internal set
{
if (filterParameters.OneOverQ != value)
{
value = System.Math.Clamp(value, 0.01f, MAX_FILTER_ONEOVERQ);
filterParameters.OneOverQ = value;
@ -124,6 +126,7 @@ namespace MoonWorks.Audio
);
}
}
}
private FilterType filterType;
public FilterType FilterType
@ -305,7 +308,12 @@ namespace MoonWorks.Audio
public void SetPan(float targetValue, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.Pan, easingFunction, Pan, targetValue, duration);
Device.CreateTween(this, AudioTweenProperty.Pan, easingFunction, Pan, targetValue, duration, 0);
}
public void SetPan(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.Pan, easingFunction, Pan, targetValue, duration, delayTime);
}
public void SetPitch(float targetValue)
@ -315,7 +323,12 @@ namespace MoonWorks.Audio
public void SetPitch(float targetValue, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.Pitch, easingFunction, Pan, targetValue, duration);
Device.CreateTween(this, AudioTweenProperty.Pitch, easingFunction, Pan, targetValue, duration, 0);
}
public void SetPitch(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.Pitch, easingFunction, Pan, targetValue, duration, delayTime);
}
public void SetVolume(float targetValue)
@ -325,7 +338,12 @@ namespace MoonWorks.Audio
public void SetVolume(float targetValue, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.Volume, easingFunction, Volume, targetValue, duration);
Device.CreateTween(this, AudioTweenProperty.Volume, easingFunction, Volume, targetValue, duration, 0);
}
public void SetVolume(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.Volume, easingFunction, Volume, targetValue, duration, delayTime);
}
public void SetFilterFrequency(float targetValue)
@ -335,7 +353,18 @@ namespace MoonWorks.Audio
public void SetFilterFrequency(float targetValue, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.FilterFrequency, easingFunction, FilterFrequency, targetValue, duration);
Device.CreateTween(this, AudioTweenProperty.FilterFrequency, easingFunction, FilterFrequency, targetValue, duration, 0);
}
public void SetFilterFrequency(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
{
Logger.LogInfo(duration.ToString());
Device.CreateTween(this, AudioTweenProperty.FilterFrequency, easingFunction, FilterFrequency, targetValue, duration, delayTime);
}
public void SetFilterOneOverQ(float targetValue)
{
FilterOneOverQ = targetValue;
}
public void SetReverb(float targetValue)
@ -345,7 +374,12 @@ namespace MoonWorks.Audio
public void SetReverb(float targetValue, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.Reverb, easingFunction, Volume, targetValue, duration);
Device.CreateTween(this, AudioTweenProperty.Reverb, easingFunction, Volume, targetValue, duration, 0);
}
public void SetReverb(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
{
Device.CreateTween(this, AudioTweenProperty.Reverb, easingFunction, Volume, targetValue, duration, delayTime);
}
public abstract void Play();