MoonWorks/src/Audio/StaticSoundInstance.cs

112 lines
2.0 KiB
C#
Raw Normal View History

2022-02-23 05:14:32 +00:00
using System;
2021-01-20 02:06:10 +00:00
namespace MoonWorks.Audio
{
2022-02-23 05:14:32 +00:00
public class StaticSoundInstance : SoundInstance
{
public StaticSound Parent { get; }
public bool Loop { get; set; }
2022-02-23 05:14:32 +00:00
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)
{
StopImmediate();
2022-02-23 05:14:32 +00:00
}
2021-01-20 03:26:30 +00:00
2022-02-23 05:14:32 +00:00
return _state;
}
2021-01-20 03:26:30 +00:00
2022-02-23 05:14:32 +00:00
protected set
{
_state = value;
}
}
2021-01-20 02:06:10 +00:00
2022-02-23 05:14:32 +00:00
internal StaticSoundInstance(
AudioDevice device,
2022-04-07 21:19:43 +00:00
StaticSound parent
) : base(device, parent.FormatTag, parent.BitsPerSample, parent.BlockAlign, parent.Channels, parent.SamplesPerSecond)
2022-02-23 05:14:32 +00:00
{
Parent = parent;
}
2021-01-20 02:06:10 +00:00
public override void Play()
2022-02-23 05:14:32 +00:00
{
if (State == SoundState.Playing)
{
return;
}
2021-01-20 02:06:10 +00:00
2022-02-23 05:14:32 +00:00
if (Loop)
{
Parent.Handle.LoopCount = 255;
Parent.Handle.LoopBegin = Parent.LoopStart;
Parent.Handle.LoopLength = Parent.LoopLength;
}
else
{
Parent.Handle.LoopCount = 0;
Parent.Handle.LoopBegin = 0;
Parent.Handle.LoopLength = 0;
}
2021-01-20 02:06:10 +00:00
2022-02-23 05:14:32 +00:00
FAudio.FAudioSourceVoice_SubmitSourceBuffer(
Handle,
ref Parent.Handle,
IntPtr.Zero
);
2021-01-20 02:06:10 +00:00
2022-02-23 05:14:32 +00:00
FAudio.FAudioSourceVoice_Start(Handle, 0, 0);
State = SoundState.Playing;
}
2021-01-20 02:06:10 +00:00
2022-02-23 05:14:32 +00:00
public override void Pause()
{
if (State == SoundState.Paused)
{
FAudio.FAudioSourceVoice_Stop(Handle, 0, 0);
State = SoundState.Paused;
}
}
2021-01-20 02:06:10 +00:00
public override void Stop()
2022-02-23 05:14:32 +00:00
{
FAudio.FAudioSourceVoice_ExitLoop(Handle, 0);
State = SoundState.Stopped;
2022-02-23 05:14:32 +00:00
}
public override void StopImmediate()
{
FAudio.FAudioSourceVoice_Stop(Handle, 0, 0);
FAudio.FAudioSourceVoice_FlushSourceBuffers(Handle);
State = SoundState.Stopped;
}
public void Seek(uint sampleFrame)
2022-04-20 21:29:46 +00:00
{
if (State == SoundState.Playing)
{
FAudio.FAudioSourceVoice_Stop(Handle, 0, 0);
FAudio.FAudioSourceVoice_FlushSourceBuffers(Handle);
}
Parent.Handle.PlayBegin = sampleFrame;
}
public void Free()
{
Parent.FreeInstance(this);
}
2022-02-23 05:14:32 +00:00
}
2021-01-20 02:06:10 +00:00
}