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 AudioTweenPool AudioTweenPool = new AudioTweenPool();
private readonly List<AudioTween> AudioTweens = new List<AudioTween>(); private readonly List<AudioTween> AudioTweens = new List<AudioTween>();
private readonly List<AudioTween> DelayedAudioTweens = new List<AudioTween>();
private const int Step = 200; private const int Step = 200;
private TimeSpan UpdateInterval; 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--) for (var i = AudioTweens.Count - 1; i >= 0; i--)
{ {
bool finished = true; bool finished = true;
@ -192,7 +239,8 @@ namespace MoonWorks.Audio
EasingFunction easingFunction, EasingFunction easingFunction,
float start, float start,
float end, float end,
float duration float duration,
float delayTime
) { ) {
lock (StateLock) lock (StateLock)
{ {
@ -200,12 +248,20 @@ namespace MoonWorks.Audio
tween.SoundInstanceReference = new WeakReference<SoundInstance>(soundInstance); tween.SoundInstanceReference = new WeakReference<SoundInstance>(soundInstance);
tween.Property = property; tween.Property = property;
tween.EasingFunction = easingFunction; tween.EasingFunction = easingFunction;
tween.Start = start; tween.StartValue = start;
tween.End = end; tween.EndValue = end;
tween.Duration = duration; tween.Duration = duration;
tween.Time = 0; tween.Time = 0;
tween.DelayTime = delayTime;
AudioTweens.Add(tween); if (delayTime == 0)
{
AudioTweens.Add(tween);
}
else
{
DelayedAudioTweens.Add(tween);
}
} }
} }
@ -215,16 +271,15 @@ namespace MoonWorks.Audio
audioTween.Time += delta; audioTween.Time += delta;
var finished = audioTween.Time >= audioTween.Duration; var finished = audioTween.Time >= audioTween.Duration;
if (finished) if (finished)
{ {
value = audioTween.End; value = audioTween.EndValue;
} }
else else
{ {
value = MoonWorks.Math.Easing.Interp( value = MoonWorks.Math.Easing.Interp(
audioTween.Start, audioTween.StartValue,
audioTween.End, audioTween.EndValue,
audioTween.Time, audioTween.Time,
audioTween.Duration, audioTween.Duration,
audioTween.EasingFunction audioTween.EasingFunction

View File

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

View File

@ -109,19 +109,22 @@ namespace MoonWorks.Audio
} }
} }
private float FilterOneOverQ public float FilterOneOverQ
{ {
get => filterParameters.OneOverQ; get => filterParameters.OneOverQ;
set internal set
{ {
value = System.Math.Clamp(value, 0.01f, MAX_FILTER_ONEOVERQ); if (filterParameters.OneOverQ != value)
filterParameters.OneOverQ = value; {
value = System.Math.Clamp(value, 0.01f, MAX_FILTER_ONEOVERQ);
filterParameters.OneOverQ = value;
FAudio.FAudioVoice_SetFilterParameters( FAudio.FAudioVoice_SetFilterParameters(
Voice, Voice,
ref filterParameters, ref filterParameters,
0 0
); );
}
} }
} }
@ -305,7 +308,12 @@ namespace MoonWorks.Audio
public void SetPan(float targetValue, float duration, EasingFunction easingFunction) 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) public void SetPitch(float targetValue)
@ -315,7 +323,12 @@ namespace MoonWorks.Audio
public void SetPitch(float targetValue, float duration, EasingFunction easingFunction) 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) public void SetVolume(float targetValue)
@ -325,7 +338,12 @@ namespace MoonWorks.Audio
public void SetVolume(float targetValue, float duration, EasingFunction easingFunction) 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) public void SetFilterFrequency(float targetValue)
@ -335,7 +353,18 @@ namespace MoonWorks.Audio
public void SetFilterFrequency(float targetValue, float duration, EasingFunction easingFunction) 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) public void SetReverb(float targetValue)
@ -345,7 +374,12 @@ namespace MoonWorks.Audio
public void SetReverb(float targetValue, float duration, EasingFunction easingFunction) 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(); public abstract void Play();