2022-02-23 05:14:32 +00:00
|
|
|
|
using System;
|
2021-01-20 03:26:30 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Audio
|
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// For streaming long playback.
|
|
|
|
|
/// Can be extended to support custom decoders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class StreamingSound : SoundInstance
|
|
|
|
|
{
|
|
|
|
|
private readonly List<IntPtr> queuedBuffers = new List<IntPtr>();
|
|
|
|
|
private readonly List<uint> queuedSizes = new List<uint>();
|
|
|
|
|
private const int MINIMUM_BUFFER_CHECK = 3;
|
|
|
|
|
|
|
|
|
|
public int PendingBufferCount => queuedBuffers.Count;
|
|
|
|
|
|
|
|
|
|
public StreamingSound(
|
|
|
|
|
AudioDevice device,
|
2022-04-05 06:33:36 +00:00
|
|
|
|
ushort formatTag,
|
|
|
|
|
ushort bitsPerSample,
|
|
|
|
|
ushort blockAlign,
|
2022-02-23 05:14:32 +00:00
|
|
|
|
ushort channels,
|
2022-04-07 21:19:43 +00:00
|
|
|
|
uint samplesPerSecond
|
|
|
|
|
) : base(device, formatTag, bitsPerSample, blockAlign, channels, samplesPerSecond) { }
|
2022-02-23 05:14:32 +00:00
|
|
|
|
|
2022-04-05 23:05:42 +00:00
|
|
|
|
public override void Play(bool loop = false)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (State == SoundState.Playing)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 23:05:42 +00:00
|
|
|
|
Loop = loop;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
State = SoundState.Playing;
|
2022-04-05 23:05:42 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
Update();
|
|
|
|
|
FAudio.FAudioSourceVoice_Start(Handle, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Pause()
|
|
|
|
|
{
|
|
|
|
|
if (State == SoundState.Playing)
|
|
|
|
|
{
|
|
|
|
|
FAudio.FAudioSourceVoice_Stop(Handle, 0, 0);
|
|
|
|
|
State = SoundState.Paused;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Stop(bool immediate = true)
|
|
|
|
|
{
|
|
|
|
|
if (immediate)
|
|
|
|
|
{
|
|
|
|
|
FAudio.FAudioSourceVoice_Stop(Handle, 0, 0);
|
|
|
|
|
FAudio.FAudioSourceVoice_FlushSourceBuffers(Handle);
|
|
|
|
|
ClearBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
State = SoundState.Stopped;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Update()
|
|
|
|
|
{
|
|
|
|
|
if (State != SoundState.Playing)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FAudio.FAudioSourceVoice_GetState(
|
|
|
|
|
Handle,
|
|
|
|
|
out var state,
|
|
|
|
|
FAudio.FAUDIO_VOICE_NOSAMPLESPLAYED
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
while (PendingBufferCount > state.BuffersQueued)
|
|
|
|
|
lock (queuedBuffers)
|
|
|
|
|
{
|
|
|
|
|
Marshal.FreeHGlobal(queuedBuffers[0]);
|
|
|
|
|
queuedBuffers.RemoveAt(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueueBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void QueueBuffers()
|
|
|
|
|
{
|
|
|
|
|
for (
|
|
|
|
|
int i = MINIMUM_BUFFER_CHECK - PendingBufferCount;
|
|
|
|
|
i > 0;
|
|
|
|
|
i -= 1
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
AddBuffer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void ClearBuffers()
|
|
|
|
|
{
|
|
|
|
|
lock (queuedBuffers)
|
|
|
|
|
{
|
|
|
|
|
foreach (IntPtr buf in queuedBuffers)
|
|
|
|
|
{
|
|
|
|
|
Marshal.FreeHGlobal(buf);
|
|
|
|
|
}
|
|
|
|
|
queuedBuffers.Clear();
|
|
|
|
|
queuedSizes.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void AddBuffer()
|
|
|
|
|
{
|
|
|
|
|
AddBuffer(
|
|
|
|
|
out var buffer,
|
|
|
|
|
out var bufferOffset,
|
|
|
|
|
out var bufferLength,
|
|
|
|
|
out var reachedEnd
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var lengthInBytes = bufferLength * sizeof(float);
|
|
|
|
|
|
|
|
|
|
IntPtr next = Marshal.AllocHGlobal((int) lengthInBytes);
|
|
|
|
|
Marshal.Copy(buffer, (int) bufferOffset, next, (int) bufferLength);
|
|
|
|
|
|
|
|
|
|
lock (queuedBuffers)
|
|
|
|
|
{
|
|
|
|
|
queuedBuffers.Add(next);
|
|
|
|
|
if (State != SoundState.Stopped)
|
|
|
|
|
{
|
|
|
|
|
FAudio.FAudioBuffer buf = new FAudio.FAudioBuffer
|
|
|
|
|
{
|
|
|
|
|
AudioBytes = lengthInBytes,
|
|
|
|
|
pAudioData = next,
|
|
|
|
|
PlayLength = (
|
|
|
|
|
lengthInBytes /
|
|
|
|
|
Format.nChannels /
|
|
|
|
|
(uint) (Format.wBitsPerSample / 8)
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FAudio.FAudioSourceVoice_SubmitSourceBuffer(
|
|
|
|
|
Handle,
|
|
|
|
|
ref buf,
|
|
|
|
|
IntPtr.Zero
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
queuedSizes.Add(lengthInBytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We have reached the end of the file, what do we do? */
|
|
|
|
|
if (reachedEnd)
|
|
|
|
|
{
|
|
|
|
|
if (Loop)
|
|
|
|
|
{
|
|
|
|
|
SeekStart();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Stop(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void AddBuffer(
|
|
|
|
|
out float[] buffer,
|
|
|
|
|
out uint bufferOffset, /* in floats */
|
|
|
|
|
out uint bufferLength, /* in floats */
|
|
|
|
|
out bool reachedEnd
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
protected abstract void SeekStart();
|
|
|
|
|
|
|
|
|
|
protected override void Destroy()
|
|
|
|
|
{
|
|
|
|
|
Stop(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-20 03:26:30 +00:00
|
|
|
|
}
|