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>
|
2023-07-31 08:16:19 +00:00
|
|
|
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
|
|
|
|
2023-08-01 18:22:49 +00:00
|
|
|
public SoundSequence(AudioDevice device, Format format) : base(device, format)
|
2023-07-28 20:02:50 +00:00
|
|
|
{
|
2023-08-02 21:10:48 +00:00
|
|
|
|
2023-07-28 20:02:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 00:47:44 +00:00
|
|
|
public SoundSequence(AudioDevice device, AudioBuffer templateSound) : base(device, templateSound.Format)
|
2023-07-28 20:02:50 +00:00
|
|
|
{
|
2023-08-02 21:10:48 +00:00
|
|
|
|
2023-07-28 20:02:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:10:48 +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
|
|
|
{
|
2023-08-02 21:10:48 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 00:47:44 +00:00
|
|
|
public void EnqueueSound(AudioBuffer buffer)
|
2023-07-28 20:02:50 +00:00
|
|
|
{
|
|
|
|
#if DEBUG
|
2023-08-03 00:47:44 +00:00
|
|
|
if (!(buffer.Format == Format))
|
2023-07-28 20:02:50 +00:00
|
|
|
{
|
2023-08-03 00:47:44 +00:00
|
|
|
Logger.LogWarn("Sound sequence audio format mismatch!");
|
2023-07-28 20:02:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
lock (StateLock)
|
|
|
|
{
|
2023-08-03 00:47:44 +00:00
|
|
|
Submit(buffer.ToFAudioBuffer());
|
2023-07-28 20:02:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|