2021-01-20 02:06:10 +00:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace MoonWorks.Audio
|
|
|
|
{
|
|
|
|
public class StaticSoundInstance : SoundInstance
|
|
|
|
{
|
2021-01-20 03:26:30 +00:00
|
|
|
public bool Loop { get; }
|
|
|
|
|
|
|
|
private SoundState _state = SoundState.Stopped;
|
|
|
|
public override SoundState State
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
FAudio.FAudioSourceVoice_GetState(
|
|
|
|
Handle,
|
|
|
|
out var state,
|
|
|
|
FAudio.FAUDIO_VOICE_NOSAMPLESPLAYED
|
|
|
|
);
|
|
|
|
if (state.BuffersQueued == 0)
|
|
|
|
{
|
|
|
|
Stop(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected set
|
|
|
|
{
|
|
|
|
_state = value;
|
|
|
|
}
|
|
|
|
}
|
2021-01-20 02:06:10 +00:00
|
|
|
|
|
|
|
public StaticSoundInstance(
|
|
|
|
AudioDevice device,
|
2021-01-20 03:26:30 +00:00
|
|
|
StaticSound parent,
|
|
|
|
bool is3D,
|
|
|
|
bool loop = false
|
|
|
|
) : base(device, parent, is3D)
|
|
|
|
{
|
|
|
|
Loop = loop;
|
|
|
|
}
|
2021-01-20 02:06:10 +00:00
|
|
|
|
2021-01-20 03:26:30 +00:00
|
|
|
public void Play()
|
2021-01-20 02:06:10 +00:00
|
|
|
{
|
2021-01-20 03:26:30 +00:00
|
|
|
var parent = (StaticSound) Parent;
|
|
|
|
|
2021-01-20 02:06:10 +00:00
|
|
|
if (State == SoundState.Playing)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-20 03:26:30 +00:00
|
|
|
if (Loop)
|
2021-01-20 02:06:10 +00:00
|
|
|
{
|
2021-01-20 03:26:30 +00:00
|
|
|
parent.Handle.LoopCount = 255;
|
|
|
|
parent.Handle.LoopBegin = parent.LoopStart;
|
|
|
|
parent.Handle.LoopLength = parent.LoopLength;
|
2021-01-20 02:06:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-20 03:26:30 +00:00
|
|
|
parent.Handle.LoopCount = 0;
|
|
|
|
parent.Handle.LoopBegin = 0;
|
|
|
|
parent.Handle.LoopLength = 0;
|
2021-01-20 02:06:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FAudio.FAudioSourceVoice_SubmitSourceBuffer(
|
|
|
|
Handle,
|
2021-01-20 03:26:30 +00:00
|
|
|
ref parent.Handle,
|
2021-01-20 02:06:10 +00:00
|
|
|
IntPtr.Zero
|
|
|
|
);
|
|
|
|
|
|
|
|
FAudio.FAudioSourceVoice_Start(Handle, 0, 0);
|
|
|
|
State = SoundState.Playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Pause()
|
|
|
|
{
|
|
|
|
if (State == SoundState.Paused)
|
|
|
|
{
|
|
|
|
FAudio.FAudioSourceVoice_Stop(Handle, 0, 0);
|
|
|
|
State = SoundState.Paused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Stop(bool immediate = true)
|
|
|
|
{
|
|
|
|
if (immediate)
|
|
|
|
{
|
|
|
|
FAudio.FAudioSourceVoice_Stop(Handle, 0, 0);
|
|
|
|
FAudio.FAudioSourceVoice_FlushSourceBuffers(Handle);
|
|
|
|
State = SoundState.Stopped;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAudio.FAudioSourceVoice_ExitLoop(Handle, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|