2022-08-30 05:45:28 +00:00
|
|
|
using System;
|
2021-01-20 02:06:10 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2023-03-07 23:28:57 +00:00
|
|
|
using EasingFunction = System.Func<float, float>;
|
2021-01-20 02:06:10 +00:00
|
|
|
|
|
|
|
namespace MoonWorks.Audio
|
|
|
|
{
|
2023-09-19 20:19:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Handles audio playback from audio buffer data. Can be configured with a variety of parameters.
|
|
|
|
/// </summary>
|
2023-08-03 19:54:02 +00:00
|
|
|
public abstract unsafe class Voice : AudioResource
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
protected IntPtr handle;
|
|
|
|
public IntPtr Handle => handle;
|
2023-03-07 23:28:57 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
public uint SourceChannelCount { get; }
|
|
|
|
public uint DestinationChannelCount { get; }
|
2022-02-23 05:14:32 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
protected SubmixVoice OutputVoice;
|
2023-03-02 01:47:09 +00:00
|
|
|
private ReverbEffect ReverbEffect;
|
2023-08-03 19:54:02 +00:00
|
|
|
|
|
|
|
protected byte* pMatrixCoefficients;
|
2023-03-02 01:47:09 +00:00
|
|
|
|
2022-04-07 21:19:43 +00:00
|
|
|
public bool Is3D { get; protected set; }
|
2022-02-23 05:14:32 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
private float dopplerFactor;
|
|
|
|
/// <summary>
|
|
|
|
/// The strength of the doppler effect on this voice.
|
|
|
|
/// </summary>
|
|
|
|
public float DopplerFactor
|
|
|
|
{
|
|
|
|
get => dopplerFactor;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (dopplerFactor != value)
|
|
|
|
{
|
|
|
|
dopplerFactor = value;
|
|
|
|
UpdatePitch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
private float volume = 1;
|
|
|
|
/// <summary>
|
|
|
|
/// The overall volume level for the voice.
|
|
|
|
/// </summary>
|
|
|
|
public float Volume
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
get => volume;
|
2023-03-07 23:28:57 +00:00
|
|
|
internal set
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
value = Math.MathHelper.Max(0, value);
|
|
|
|
if (volume != value)
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
volume = value;
|
|
|
|
FAudio.FAudioVoice_SetVolume(Handle, volume, 0);
|
2023-03-07 23:28:57 +00:00
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 05:45:28 +00:00
|
|
|
private float pitch = 0;
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The pitch of the voice.
|
|
|
|
/// </summary>
|
2022-02-23 05:14:32 +00:00
|
|
|
public float Pitch
|
|
|
|
{
|
2022-08-30 05:45:28 +00:00
|
|
|
get => pitch;
|
2023-03-07 23:28:57 +00:00
|
|
|
internal set
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-03-07 23:28:57 +00:00
|
|
|
value = Math.MathHelper.Clamp(value, -1f, 1f);
|
|
|
|
if (pitch != value)
|
|
|
|
{
|
|
|
|
pitch = value;
|
|
|
|
UpdatePitch();
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 05:45:28 +00:00
|
|
|
private const float MAX_FILTER_FREQUENCY = 1f;
|
|
|
|
private const float MAX_FILTER_ONEOVERQ = 1.5f;
|
|
|
|
|
|
|
|
private FAudio.FAudioFilterParameters filterParameters = new FAudio.FAudioFilterParameters
|
|
|
|
{
|
|
|
|
Type = FAudio.FAudioFilterType.FAudioLowPassFilter,
|
|
|
|
Frequency = 1f,
|
|
|
|
OneOverQ = 1f
|
|
|
|
};
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The frequency cutoff on the voice filter.
|
|
|
|
/// </summary>
|
2023-03-07 23:28:57 +00:00
|
|
|
public float FilterFrequency
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2022-08-30 05:45:28 +00:00
|
|
|
get => filterParameters.Frequency;
|
2023-03-07 23:28:57 +00:00
|
|
|
internal set
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2022-08-30 05:45:28 +00:00
|
|
|
value = System.Math.Clamp(value, 0.01f, MAX_FILTER_FREQUENCY);
|
2023-03-07 23:28:57 +00:00
|
|
|
if (filterParameters.Frequency != value)
|
|
|
|
{
|
|
|
|
filterParameters.Frequency = value;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
2023-03-07 23:28:57 +00:00
|
|
|
FAudio.FAudioVoice_SetFilterParameters(
|
2023-08-03 19:54:02 +00:00
|
|
|
Handle,
|
2023-03-07 23:28:57 +00:00
|
|
|
ref filterParameters,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Reciprocal of Q factor.
|
|
|
|
/// Controls how quickly frequencies beyond the filter frequency are dampened.
|
|
|
|
/// </summary>
|
2023-03-07 23:28:57 +00:00
|
|
|
public float FilterOneOverQ
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2022-08-30 05:45:28 +00:00
|
|
|
get => filterParameters.OneOverQ;
|
2023-03-07 23:28:57 +00:00
|
|
|
internal set
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2022-08-30 05:45:28 +00:00
|
|
|
value = System.Math.Clamp(value, 0.01f, MAX_FILTER_ONEOVERQ);
|
2023-03-07 23:28:57 +00:00
|
|
|
if (filterParameters.OneOverQ != value)
|
|
|
|
{
|
|
|
|
filterParameters.OneOverQ = value;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
2023-03-07 23:28:57 +00:00
|
|
|
FAudio.FAudioVoice_SetFilterParameters(
|
2023-08-03 19:54:02 +00:00
|
|
|
Handle,
|
2023-03-07 23:28:57 +00:00
|
|
|
ref filterParameters,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 05:45:28 +00:00
|
|
|
private FilterType filterType;
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The frequency filter that is applied to the voice.
|
|
|
|
/// </summary>
|
2022-08-30 05:45:28 +00:00
|
|
|
public FilterType FilterType
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2022-08-30 05:45:28 +00:00
|
|
|
get => filterType;
|
2022-02-23 05:14:32 +00:00
|
|
|
set
|
|
|
|
{
|
2023-03-07 23:28:57 +00:00
|
|
|
if (filterType != value)
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-03-07 23:28:57 +00:00
|
|
|
filterType = value;
|
2022-08-30 05:45:28 +00:00
|
|
|
|
2023-03-07 23:28:57 +00:00
|
|
|
switch (filterType)
|
|
|
|
{
|
|
|
|
case FilterType.None:
|
|
|
|
filterParameters = new FAudio.FAudioFilterParameters
|
|
|
|
{
|
|
|
|
Type = FAudio.FAudioFilterType.FAudioLowPassFilter,
|
|
|
|
Frequency = 1f,
|
|
|
|
OneOverQ = 1f
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterType.LowPass:
|
|
|
|
filterParameters.Type = FAudio.FAudioFilterType.FAudioLowPassFilter;
|
|
|
|
filterParameters.Frequency = 1f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterType.BandPass:
|
|
|
|
filterParameters.Type = FAudio.FAudioFilterType.FAudioBandPassFilter;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterType.HighPass:
|
|
|
|
filterParameters.Type = FAudio.FAudioFilterType.FAudioHighPassFilter;
|
|
|
|
filterParameters.Frequency = 0f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FAudio.FAudioVoice_SetFilterParameters(
|
2023-08-03 19:54:02 +00:00
|
|
|
Handle,
|
2023-03-07 23:28:57 +00:00
|
|
|
ref filterParameters,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
protected float pan = 0;
|
|
|
|
/// <summary>
|
|
|
|
/// Left-right panning. -1 is hard left pan, 1 is hard right pan.
|
|
|
|
/// </summary>
|
|
|
|
public float Pan
|
|
|
|
{
|
|
|
|
get => pan;
|
|
|
|
internal set
|
|
|
|
{
|
|
|
|
value = Math.MathHelper.Clamp(value, -1f, 1f);
|
|
|
|
if (pan != value)
|
|
|
|
{
|
|
|
|
pan = value;
|
|
|
|
|
|
|
|
if (pan < -1f)
|
|
|
|
{
|
|
|
|
pan = -1f;
|
|
|
|
}
|
|
|
|
if (pan > 1f)
|
|
|
|
{
|
|
|
|
pan = 1f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Is3D) { return; }
|
|
|
|
|
|
|
|
SetPanMatrixCoefficients();
|
|
|
|
FAudio.FAudioVoice_SetOutputMatrix(
|
|
|
|
Handle,
|
|
|
|
OutputVoice.Handle,
|
|
|
|
SourceChannelCount,
|
|
|
|
DestinationChannelCount,
|
|
|
|
(nint) pMatrixCoefficients,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 01:47:09 +00:00
|
|
|
private float reverb;
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The wet-dry mix of the reverb effect.
|
|
|
|
/// Has no effect if SetReverbEffectChain has not been called.
|
|
|
|
/// </summary>
|
2023-03-02 01:47:09 +00:00
|
|
|
public unsafe float Reverb
|
|
|
|
{
|
|
|
|
get => reverb;
|
2023-03-07 23:28:57 +00:00
|
|
|
internal set
|
2023-03-02 01:47:09 +00:00
|
|
|
{
|
|
|
|
if (ReverbEffect != null)
|
|
|
|
{
|
2023-03-07 23:28:57 +00:00
|
|
|
value = MathF.Max(0, value);
|
|
|
|
if (reverb != value)
|
2023-03-02 01:47:09 +00:00
|
|
|
{
|
2023-03-07 23:28:57 +00:00
|
|
|
reverb = value;
|
2023-03-02 01:47:09 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
float* outputMatrix = (float*) pMatrixCoefficients;
|
2023-03-07 23:28:57 +00:00
|
|
|
outputMatrix[0] = reverb;
|
2023-08-03 19:54:02 +00:00
|
|
|
if (SourceChannelCount == 2)
|
2023-03-07 23:28:57 +00:00
|
|
|
{
|
|
|
|
outputMatrix[1] = reverb;
|
|
|
|
}
|
|
|
|
|
|
|
|
FAudio.FAudioVoice_SetOutputMatrix(
|
2023-08-03 19:54:02 +00:00
|
|
|
Handle,
|
|
|
|
ReverbEffect.Handle,
|
|
|
|
SourceChannelCount,
|
2023-03-07 23:28:57 +00:00
|
|
|
1,
|
2023-08-03 19:54:02 +00:00
|
|
|
(nint) pMatrixCoefficients,
|
2023-03-07 23:28:57 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
2023-03-02 01:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
if (ReverbEffect == null)
|
|
|
|
{
|
|
|
|
Logger.LogWarn("Tried to set reverb value before applying a reverb effect");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
public Voice(AudioDevice device, uint sourceChannelCount, uint destinationChannelCount) : base(device)
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
SourceChannelCount = sourceChannelCount;
|
|
|
|
DestinationChannelCount = destinationChannelCount;
|
|
|
|
nuint memsize = 4 * sourceChannelCount * destinationChannelCount;
|
|
|
|
pMatrixCoefficients = (byte*) NativeMemory.AllocZeroed(memsize);
|
|
|
|
SetPanMatrixCoefficients();
|
2023-03-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the pitch of the voice. Valid input range is -1f to 1f.
|
|
|
|
/// </summary>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetPitch(float targetValue)
|
|
|
|
{
|
|
|
|
Pitch = targetValue;
|
2023-06-15 01:22:49 +00:00
|
|
|
Device.ClearTweens(this, AudioTweenProperty.Pitch);
|
2023-03-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the pitch of the voice over a time duration in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetPitch(float targetValue, float duration, EasingFunction easingFunction)
|
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
Device.CreateTween(this, AudioTweenProperty.Pitch, easingFunction, Pitch, targetValue, duration, 0);
|
2023-03-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the pitch of the voice over a time duration in seconds after a delay in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetPitch(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
|
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
Device.CreateTween(this, AudioTweenProperty.Pitch, easingFunction, Pitch, targetValue, duration, delayTime);
|
2023-03-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the volume of the voice. Minimum value is 0f.
|
|
|
|
/// </summary>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetVolume(float targetValue)
|
|
|
|
{
|
|
|
|
Volume = targetValue;
|
2023-06-15 01:22:49 +00:00
|
|
|
Device.ClearTweens(this, AudioTweenProperty.Volume);
|
2023-03-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the volume of the voice over a time duration in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetVolume(float targetValue, float duration, EasingFunction easingFunction)
|
|
|
|
{
|
|
|
|
Device.CreateTween(this, AudioTweenProperty.Volume, easingFunction, Volume, targetValue, duration, 0);
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the volume of the voice over a time duration in seconds after a delay in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetVolume(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
|
|
|
|
{
|
|
|
|
Device.CreateTween(this, AudioTweenProperty.Volume, easingFunction, Volume, targetValue, duration, delayTime);
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the frequency cutoff on the voice filter. Valid range is 0.01f to 1f.
|
|
|
|
/// </summary>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetFilterFrequency(float targetValue)
|
|
|
|
{
|
|
|
|
FilterFrequency = targetValue;
|
2023-06-15 01:22:49 +00:00
|
|
|
Device.ClearTweens(this, AudioTweenProperty.FilterFrequency);
|
2023-03-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the frequency cutoff on the voice filter over a time duration in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetFilterFrequency(float targetValue, float duration, EasingFunction easingFunction)
|
|
|
|
{
|
|
|
|
Device.CreateTween(this, AudioTweenProperty.FilterFrequency, easingFunction, FilterFrequency, targetValue, duration, 0);
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the frequency cutoff on the voice filter over a time duration in seconds after a delay in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetFilterFrequency(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
|
|
|
|
{
|
|
|
|
Device.CreateTween(this, AudioTweenProperty.FilterFrequency, easingFunction, FilterFrequency, targetValue, duration, delayTime);
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets reciprocal of Q factor on the frequency filter.
|
|
|
|
/// Controls how quickly frequencies beyond the filter frequency are dampened.
|
|
|
|
/// </summary>
|
2023-03-07 23:28:57 +00:00
|
|
|
public void SetFilterOneOverQ(float targetValue)
|
|
|
|
{
|
|
|
|
FilterOneOverQ = targetValue;
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets a left-right panning value. -1f is hard left pan, 1f is hard right pan.
|
|
|
|
/// </summary>
|
|
|
|
public virtual void SetPan(float targetValue)
|
|
|
|
{
|
|
|
|
Pan = targetValue;
|
|
|
|
Device.ClearTweens(this, AudioTweenProperty.Pan);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets a left-right panning value over a time duration in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
|
|
|
public virtual void SetPan(float targetValue, float duration, EasingFunction easingFunction)
|
|
|
|
{
|
|
|
|
Device.CreateTween(this, AudioTweenProperty.Pan, easingFunction, Pan, targetValue, duration, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets a left-right panning value over a time duration in seconds after a delay in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
|
|
|
public virtual void SetPan(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
|
|
|
|
{
|
|
|
|
Device.CreateTween(this, AudioTweenProperty.Pan, easingFunction, Pan, targetValue, duration, delayTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the wet-dry mix value of the reverb effect. Minimum value is 0f.
|
|
|
|
/// </summary>
|
|
|
|
public virtual void SetReverb(float targetValue)
|
2023-03-07 23:28:57 +00:00
|
|
|
{
|
|
|
|
Reverb = targetValue;
|
2023-06-15 01:22:49 +00:00
|
|
|
Device.ClearTweens(this, AudioTweenProperty.Reverb);
|
2023-03-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the wet-dry mix value of the reverb effect over a time duration in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
|
|
|
public virtual void SetReverb(float targetValue, float duration, EasingFunction easingFunction)
|
2023-03-07 23:28:57 +00:00
|
|
|
{
|
|
|
|
Device.CreateTween(this, AudioTweenProperty.Reverb, easingFunction, Volume, targetValue, duration, 0);
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the wet-dry mix value of the reverb effect over a time duration in seconds after a delay in seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="easingFunction">An easing function. See MoonWorks.Math.Easing.Function.Float</param>
|
|
|
|
public virtual void SetReverb(float targetValue, float delayTime, float duration, EasingFunction easingFunction)
|
2023-03-07 23:28:57 +00:00
|
|
|
{
|
|
|
|
Device.CreateTween(this, AudioTweenProperty.Reverb, easingFunction, Volume, targetValue, duration, delayTime);
|
|
|
|
}
|
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the output voice for this voice.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="send">Where the output should be sent.</param>
|
|
|
|
public unsafe void SetOutputVoice(SubmixVoice send)
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
OutputVoice = send;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
if (ReverbEffect != null)
|
|
|
|
{
|
|
|
|
SetReverbEffectChain(ReverbEffect);
|
|
|
|
}
|
|
|
|
else
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
FAudio.FAudioSendDescriptor* sendDesc = stackalloc FAudio.FAudioSendDescriptor[1];
|
|
|
|
sendDesc[0].Flags = 0;
|
|
|
|
sendDesc[0].pOutputVoice = send.Handle;
|
|
|
|
|
|
|
|
var sends = new FAudio.FAudioVoiceSends();
|
|
|
|
sends.SendCount = 1;
|
|
|
|
sends.pSends = (nint) sendDesc;
|
|
|
|
|
|
|
|
FAudio.FAudioVoice_SetOutputVoices(
|
|
|
|
Handle,
|
|
|
|
ref sends
|
|
|
|
);
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
2023-08-03 19:54:02 +00:00
|
|
|
}
|
2023-03-07 23:28:57 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Applies a reverb effect chain to this voice.
|
|
|
|
/// </summary>
|
|
|
|
public unsafe void SetReverbEffectChain(ReverbEffect reverbEffect)
|
|
|
|
{
|
|
|
|
var sendDesc = stackalloc FAudio.FAudioSendDescriptor[2];
|
|
|
|
sendDesc[0].Flags = 0;
|
|
|
|
sendDesc[0].pOutputVoice = OutputVoice.Handle;
|
|
|
|
sendDesc[1].Flags = 0;
|
|
|
|
sendDesc[1].pOutputVoice = reverbEffect.Handle;
|
|
|
|
|
|
|
|
var sends = new FAudio.FAudioVoiceSends();
|
|
|
|
sends.SendCount = 2;
|
|
|
|
sends.pSends = (nint) sendDesc;
|
|
|
|
|
|
|
|
FAudio.FAudioVoice_SetOutputVoices(
|
|
|
|
Handle,
|
|
|
|
ref sends
|
|
|
|
);
|
|
|
|
|
|
|
|
ReverbEffect = reverbEffect;
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
2021-01-20 02:06:10 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Removes the reverb effect chain from this voice.
|
|
|
|
/// </summary>
|
|
|
|
public void RemoveReverbEffectChain()
|
2021-02-27 00:17:26 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
if (ReverbEffect != null)
|
2021-02-27 00:17:26 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
ReverbEffect = null;
|
|
|
|
reverb = 0;
|
|
|
|
SetOutputVoice(OutputVoice);
|
2021-02-27 00:17:26 +00:00
|
|
|
}
|
2023-08-03 19:54:02 +00:00
|
|
|
}
|
2021-02-27 00:17:26 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Resets all voice parameters to defaults.
|
|
|
|
/// </summary>
|
|
|
|
public virtual void Reset()
|
|
|
|
{
|
|
|
|
RemoveReverbEffectChain();
|
|
|
|
Volume = 1;
|
|
|
|
Pan = 0;
|
|
|
|
Pitch = 0;
|
|
|
|
FilterType = FilterType.None;
|
|
|
|
SetOutputVoice(Device.MasteringVoice);
|
2021-02-27 00:17:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
// Taken from https://github.com/FNA-XNA/FNA/blob/master/src/Audio/SoundEffectInstance.cs
|
|
|
|
private unsafe void SetPanMatrixCoefficients()
|
|
|
|
{
|
|
|
|
/* Two major things to notice:
|
2021-01-20 02:06:10 +00:00
|
|
|
* 1. The spec assumes any speaker count >= 2 has Front Left/Right.
|
|
|
|
* 2. Stereo panning is WAY more complicated than you think.
|
|
|
|
* The main thing is that hard panning does NOT eliminate an
|
|
|
|
* entire channel; the two channels are blended on each side.
|
|
|
|
* -flibit
|
|
|
|
*/
|
2023-08-03 19:54:02 +00:00
|
|
|
float* outputMatrix = (float*) pMatrixCoefficients;
|
|
|
|
if (SourceChannelCount == 1)
|
2021-01-20 02:06:10 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
if (DestinationChannelCount == 1)
|
2021-01-20 02:06:10 +00:00
|
|
|
{
|
|
|
|
outputMatrix[0] = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-30 05:45:28 +00:00
|
|
|
outputMatrix[0] = (pan > 0.0f) ? (1.0f - pan) : 1.0f;
|
|
|
|
outputMatrix[1] = (pan < 0.0f) ? (1.0f + pan) : 1.0f;
|
2021-01-20 02:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
if (DestinationChannelCount == 1)
|
2021-01-20 02:06:10 +00:00
|
|
|
{
|
|
|
|
outputMatrix[0] = 1.0f;
|
|
|
|
outputMatrix[1] = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-30 05:45:28 +00:00
|
|
|
if (pan <= 0.0f)
|
2021-01-20 02:06:10 +00:00
|
|
|
{
|
|
|
|
// Left speaker blends left/right channels
|
2022-08-30 05:45:28 +00:00
|
|
|
outputMatrix[0] = 0.5f * pan + 1.0f;
|
|
|
|
outputMatrix[1] = 0.5f * -pan;
|
2021-01-20 02:06:10 +00:00
|
|
|
// Right speaker gets less of the right channel
|
|
|
|
outputMatrix[2] = 0.0f;
|
2022-08-30 05:45:28 +00:00
|
|
|
outputMatrix[3] = pan + 1.0f;
|
2021-01-20 02:06:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Left speaker gets less of the left channel
|
2022-08-30 05:45:28 +00:00
|
|
|
outputMatrix[0] = -pan + 1.0f;
|
2021-01-20 02:06:10 +00:00
|
|
|
outputMatrix[1] = 0.0f;
|
|
|
|
// Right speaker blends right/left channels
|
2022-08-30 05:45:28 +00:00
|
|
|
outputMatrix[2] = 0.5f * pan;
|
|
|
|
outputMatrix[3] = 0.5f * -pan + 1.0f;
|
2021-01-20 02:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
2021-01-20 02:06:10 +00:00
|
|
|
|
2023-08-03 19:54:02 +00:00
|
|
|
protected void UpdatePitch()
|
2022-02-23 05:14:32 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
float doppler;
|
|
|
|
float dopplerScale = Device.DopplerScale;
|
|
|
|
if (!Is3D || dopplerScale == 0.0f)
|
2023-03-02 01:47:09 +00:00
|
|
|
{
|
2023-08-03 19:54:02 +00:00
|
|
|
doppler = 1.0f;
|
2023-03-02 01:47:09 +00:00
|
|
|
}
|
2023-08-03 19:54:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
doppler = DopplerFactor * dopplerScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
FAudio.FAudioSourceVoice_SetFrequencyRatio(
|
|
|
|
Handle,
|
|
|
|
(float) System.Math.Pow(2.0, pitch) * doppler,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-09 00:33:52 +00:00
|
|
|
protected override unsafe void Dispose(bool disposing)
|
2023-08-03 19:54:02 +00:00
|
|
|
{
|
2023-12-09 00:33:52 +00:00
|
|
|
if (!IsDisposed)
|
|
|
|
{
|
|
|
|
NativeMemory.Free(pMatrixCoefficients);
|
|
|
|
FAudio.FAudioVoice_DestroyVoice(Handle);
|
|
|
|
}
|
|
|
|
base.Dispose(disposing);
|
2022-02-23 05:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-20 02:06:10 +00:00
|
|
|
}
|