MoonWorks/src/Audio/SoundSequence.cs

57 lines
1.1 KiB
C#
Raw Normal View History

2023-07-28 20:02:50 +00:00
namespace MoonWorks.Audio
{
2023-08-03 01:26:27 +00:00
/// <summary>
/// Plays back a series of AudioBuffers in sequence. Set the OnSoundNeeded callback to add AudioBuffers dynamically.
/// </summary>
public class SoundSequence : SourceVoice
2023-07-28 20:02:50 +00:00
{
2023-07-28 20:10:01 +00:00
public int NeedSoundThreshold = 0;
public delegate void OnSoundNeededFunc();
public OnSoundNeededFunc OnSoundNeeded;
2023-07-28 20:02:50 +00:00
public SoundSequence(AudioDevice device, Format format) : base(device, format)
2023-07-28 20:02:50 +00:00
{
2023-07-28 20:02:50 +00:00
}
public SoundSequence(AudioDevice device, AudioBuffer templateSound) : base(device, templateSound.Format)
2023-07-28 20:02:50 +00:00
{
2023-07-28 20:02:50 +00:00
}
public override void Update()
2023-07-28 20:02:50 +00:00
{
lock (StateLock)
{
if (State != SoundState.Playing) { return; }
2023-07-28 20:10:01 +00:00
if (NeedSoundThreshold > 0)
2023-07-28 20:02:50 +00:00
{
for (int i = 0; i < NeedSoundThreshold - BuffersQueued; i += 1)
2023-07-28 20:02:50 +00:00
{
2023-07-28 20:10:01 +00:00
if (OnSoundNeeded != null)
2023-07-28 20:02:50 +00:00
{
2023-07-28 20:10:01 +00:00
OnSoundNeeded();
2023-07-28 20:02:50 +00:00
}
}
}
}
}
public void EnqueueSound(AudioBuffer buffer)
2023-07-28 20:02:50 +00:00
{
#if DEBUG
if (!(buffer.Format == Format))
2023-07-28 20:02:50 +00:00
{
Logger.LogWarn("Sound sequence audio format mismatch!");
2023-07-28 20:02:50 +00:00
}
#endif
lock (StateLock)
{
Submit(buffer.ToFAudioBuffer());
2023-07-28 20:02:50 +00:00
}
}
}
}