2021-02-26 01:11:56 +01:00
|
|
|
|
using Ryujinx.Audio.Backends.Common;
|
|
|
|
|
using Ryujinx.Audio.Common;
|
|
|
|
|
using Ryujinx.Memory;
|
|
|
|
|
using SoundIOSharp;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
using System;
|
2021-02-28 17:20:34 +01:00
|
|
|
|
using System.Collections.Concurrent;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2021-02-26 01:11:56 +01:00
|
|
|
|
using System.Threading;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
namespace Ryujinx.Audio.Backends.SoundIo
|
2018-11-15 03:22:50 +01:00
|
|
|
|
{
|
2021-02-26 01:11:56 +01:00
|
|
|
|
class SoundIoHardwareDeviceSession : HardwareDeviceSessionOutputBase
|
2018-11-15 03:22:50 +01:00
|
|
|
|
{
|
2021-02-26 01:11:56 +01:00
|
|
|
|
private SoundIoHardwareDeviceDriver _driver;
|
2021-02-28 17:20:34 +01:00
|
|
|
|
private ConcurrentQueue<SoundIoAudioBuffer> _queuedBuffers;
|
2021-02-26 01:11:56 +01:00
|
|
|
|
private SoundIOOutStream _outputStream;
|
|
|
|
|
private DynamicRingBuffer _ringBuffer;
|
|
|
|
|
private ulong _playedSampleCount;
|
|
|
|
|
private ManualResetEvent _updateRequiredEvent;
|
2021-06-29 19:37:13 +02:00
|
|
|
|
private int _disposeState;
|
2021-02-26 01:11:56 +01:00
|
|
|
|
|
|
|
|
|
public SoundIoHardwareDeviceSession(SoundIoHardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount)
|
2018-11-15 03:22:50 +01:00
|
|
|
|
{
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_driver = driver;
|
|
|
|
|
_updateRequiredEvent = _driver.GetUpdateRequiredEvent();
|
2021-02-28 17:20:34 +01:00
|
|
|
|
_queuedBuffers = new ConcurrentQueue<SoundIoAudioBuffer>();
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_ringBuffer = new DynamicRingBuffer();
|
|
|
|
|
|
|
|
|
|
SetupOutputStream();
|
2018-11-15 03:22:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
private void SetupOutputStream()
|
2018-11-15 03:22:50 +01:00
|
|
|
|
{
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_outputStream = _driver.OpenStream(RequestedSampleFormat, RequestedSampleRate, RequestedChannelCount);
|
|
|
|
|
_outputStream.WriteCallback += Update;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
// TODO: Setup other callbacks (errors, ect).
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_outputStream.Open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override ulong GetPlayedSampleCount()
|
|
|
|
|
{
|
2021-02-28 17:20:34 +01:00
|
|
|
|
return Interlocked.Read(ref _playedSampleCount);
|
2021-02-26 01:11:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override float GetVolume()
|
|
|
|
|
{
|
|
|
|
|
return _outputStream.Volume;
|
|
|
|
|
}
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
public override void PrepareToClose() { }
|
|
|
|
|
|
|
|
|
|
public override void QueueBuffer(AudioBuffer buffer)
|
|
|
|
|
{
|
2021-02-28 17:20:34 +01:00
|
|
|
|
SoundIoAudioBuffer driverBuffer = new SoundIoAudioBuffer(buffer.DataPointer, GetSampleCount(buffer));
|
2021-02-26 01:11:56 +01:00
|
|
|
|
|
2021-02-28 17:20:34 +01:00
|
|
|
|
_ringBuffer.Write(buffer.Data, 0, buffer.Data.Length);
|
2021-02-26 01:11:56 +01:00
|
|
|
|
|
2021-02-28 17:20:34 +01:00
|
|
|
|
_queuedBuffers.Enqueue(driverBuffer);
|
2021-02-26 01:11:56 +01:00
|
|
|
|
}
|
2020-08-18 21:03:55 +02:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
public override void SetVolume(float volume)
|
|
|
|
|
{
|
|
|
|
|
_outputStream.SetVolume(volume);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Start()
|
|
|
|
|
{
|
|
|
|
|
_outputStream.Start();
|
|
|
|
|
_outputStream.Pause(false);
|
2020-08-18 21:03:55 +02:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_driver.FlushContextEvents();
|
|
|
|
|
}
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
public override void Stop()
|
|
|
|
|
{
|
|
|
|
|
_outputStream.Pause(true);
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_driver.FlushContextEvents();
|
|
|
|
|
}
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
public override void UnregisterBuffer(AudioBuffer buffer) {}
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
public override bool WasBufferFullyConsumed(AudioBuffer buffer)
|
|
|
|
|
{
|
2021-02-28 17:20:34 +01:00
|
|
|
|
if (!_queuedBuffers.TryPeek(out SoundIoAudioBuffer driverBuffer))
|
2021-02-26 01:11:56 +01:00
|
|
|
|
{
|
2021-02-28 17:20:34 +01:00
|
|
|
|
return true;
|
2021-02-26 01:11:56 +01:00
|
|
|
|
}
|
2021-02-28 17:20:34 +01:00
|
|
|
|
|
|
|
|
|
return driverBuffer.DriverIdentifier != buffer.DataPointer;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
private unsafe void Update(int minFrameCount, int maxFrameCount)
|
2018-11-15 03:22:50 +01:00
|
|
|
|
{
|
2021-02-26 01:11:56 +01:00
|
|
|
|
int bytesPerFrame = _outputStream.BytesPerFrame;
|
|
|
|
|
uint bytesPerSample = (uint)_outputStream.BytesPerSample;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
int bufferedFrames = _ringBuffer.Length / bytesPerFrame;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
|
|
|
|
int frameCount = Math.Min(bufferedFrames, maxFrameCount);
|
|
|
|
|
|
|
|
|
|
if (frameCount == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
SoundIOChannelAreas areas = _outputStream.BeginWrite(ref frameCount);
|
|
|
|
|
|
2018-11-15 03:22:50 +01:00
|
|
|
|
int channelCount = areas.ChannelCount;
|
|
|
|
|
|
|
|
|
|
byte[] samples = new byte[frameCount * bytesPerFrame];
|
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_ringBuffer.Read(samples, 0, samples.Length);
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
|
|
|
|
// This is a huge ugly block of code, but we save
|
|
|
|
|
// a significant amount of time over the generic
|
|
|
|
|
// loop that handles other channel counts.
|
2021-02-26 01:11:56 +01:00
|
|
|
|
// TODO: Is this still right in 2021?
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
|
|
|
|
// Mono
|
|
|
|
|
if (channelCount == 1)
|
|
|
|
|
{
|
|
|
|
|
SoundIOChannelArea area = areas.GetArea(0);
|
|
|
|
|
|
|
|
|
|
fixed (byte* srcptr = samples)
|
|
|
|
|
{
|
|
|
|
|
if (bytesPerSample == 1)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
((byte*)area.Pointer)[0] = srcptr[frame * bytesPerFrame];
|
|
|
|
|
|
|
|
|
|
area.Pointer += area.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (bytesPerSample == 2)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
((short*)area.Pointer)[0] = ((short*)srcptr)[frame * bytesPerFrame >> 1];
|
|
|
|
|
|
|
|
|
|
area.Pointer += area.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (bytesPerSample == 4)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
((int*)area.Pointer)[0] = ((int*)srcptr)[frame * bytesPerFrame >> 2];
|
|
|
|
|
|
|
|
|
|
area.Pointer += area.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area.Pointer, srcptr + (frame * bytesPerFrame), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
area.Pointer += area.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Stereo
|
|
|
|
|
else if (channelCount == 2)
|
|
|
|
|
{
|
|
|
|
|
SoundIOChannelArea area1 = areas.GetArea(0);
|
|
|
|
|
SoundIOChannelArea area2 = areas.GetArea(1);
|
|
|
|
|
|
|
|
|
|
fixed (byte* srcptr = samples)
|
|
|
|
|
{
|
|
|
|
|
if (bytesPerSample == 1)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
// Channel 1
|
|
|
|
|
((byte*)area1.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 0];
|
|
|
|
|
|
|
|
|
|
// Channel 2
|
|
|
|
|
((byte*)area2.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 1];
|
|
|
|
|
|
|
|
|
|
area1.Pointer += area1.Step;
|
|
|
|
|
area2.Pointer += area2.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (bytesPerSample == 2)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
// Channel 1
|
|
|
|
|
((short*)area1.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 0];
|
|
|
|
|
|
|
|
|
|
// Channel 2
|
|
|
|
|
((short*)area2.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 1];
|
|
|
|
|
|
|
|
|
|
area1.Pointer += area1.Step;
|
|
|
|
|
area2.Pointer += area2.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (bytesPerSample == 4)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
// Channel 1
|
|
|
|
|
((int*)area1.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 0];
|
|
|
|
|
|
|
|
|
|
// Channel 2
|
|
|
|
|
((int*)area2.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 1];
|
|
|
|
|
|
|
|
|
|
area1.Pointer += area1.Step;
|
|
|
|
|
area2.Pointer += area2.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
// Channel 1
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area1.Pointer, srcptr + (frame * bytesPerFrame) + (0 * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
// Channel 2
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area2.Pointer, srcptr + (frame * bytesPerFrame) + (1 * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
area1.Pointer += area1.Step;
|
|
|
|
|
area2.Pointer += area2.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Surround
|
|
|
|
|
else if (channelCount == 6)
|
|
|
|
|
{
|
|
|
|
|
SoundIOChannelArea area1 = areas.GetArea(0);
|
|
|
|
|
SoundIOChannelArea area2 = areas.GetArea(1);
|
|
|
|
|
SoundIOChannelArea area3 = areas.GetArea(2);
|
|
|
|
|
SoundIOChannelArea area4 = areas.GetArea(3);
|
|
|
|
|
SoundIOChannelArea area5 = areas.GetArea(4);
|
|
|
|
|
SoundIOChannelArea area6 = areas.GetArea(5);
|
|
|
|
|
|
|
|
|
|
fixed (byte* srcptr = samples)
|
|
|
|
|
{
|
|
|
|
|
if (bytesPerSample == 1)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
// Channel 1
|
|
|
|
|
((byte*)area1.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 0];
|
|
|
|
|
|
|
|
|
|
// Channel 2
|
|
|
|
|
((byte*)area2.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 1];
|
|
|
|
|
|
|
|
|
|
// Channel 3
|
|
|
|
|
((byte*)area3.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 2];
|
|
|
|
|
|
|
|
|
|
// Channel 4
|
|
|
|
|
((byte*)area4.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 3];
|
|
|
|
|
|
|
|
|
|
// Channel 5
|
|
|
|
|
((byte*)area5.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 4];
|
|
|
|
|
|
|
|
|
|
// Channel 6
|
|
|
|
|
((byte*)area6.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 5];
|
|
|
|
|
|
|
|
|
|
area1.Pointer += area1.Step;
|
|
|
|
|
area2.Pointer += area2.Step;
|
|
|
|
|
area3.Pointer += area3.Step;
|
|
|
|
|
area4.Pointer += area4.Step;
|
|
|
|
|
area5.Pointer += area5.Step;
|
|
|
|
|
area6.Pointer += area6.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (bytesPerSample == 2)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
// Channel 1
|
|
|
|
|
((short*)area1.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 0];
|
|
|
|
|
|
|
|
|
|
// Channel 2
|
|
|
|
|
((short*)area2.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 1];
|
|
|
|
|
|
|
|
|
|
// Channel 3
|
|
|
|
|
((short*)area3.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 2];
|
|
|
|
|
|
|
|
|
|
// Channel 4
|
|
|
|
|
((short*)area4.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 3];
|
|
|
|
|
|
|
|
|
|
// Channel 5
|
|
|
|
|
((short*)area5.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 4];
|
|
|
|
|
|
|
|
|
|
// Channel 6
|
|
|
|
|
((short*)area6.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 5];
|
|
|
|
|
|
|
|
|
|
area1.Pointer += area1.Step;
|
|
|
|
|
area2.Pointer += area2.Step;
|
|
|
|
|
area3.Pointer += area3.Step;
|
|
|
|
|
area4.Pointer += area4.Step;
|
|
|
|
|
area5.Pointer += area5.Step;
|
|
|
|
|
area6.Pointer += area6.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (bytesPerSample == 4)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
// Channel 1
|
|
|
|
|
((int*)area1.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 0];
|
|
|
|
|
|
|
|
|
|
// Channel 2
|
|
|
|
|
((int*)area2.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 1];
|
|
|
|
|
|
|
|
|
|
// Channel 3
|
|
|
|
|
((int*)area3.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 2];
|
|
|
|
|
|
|
|
|
|
// Channel 4
|
|
|
|
|
((int*)area4.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 3];
|
|
|
|
|
|
|
|
|
|
// Channel 5
|
|
|
|
|
((int*)area5.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 4];
|
|
|
|
|
|
|
|
|
|
// Channel 6
|
|
|
|
|
((int*)area6.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 5];
|
|
|
|
|
|
|
|
|
|
area1.Pointer += area1.Step;
|
|
|
|
|
area2.Pointer += area2.Step;
|
|
|
|
|
area3.Pointer += area3.Step;
|
|
|
|
|
area4.Pointer += area4.Step;
|
|
|
|
|
area5.Pointer += area5.Step;
|
|
|
|
|
area6.Pointer += area6.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
{
|
|
|
|
|
// Channel 1
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area1.Pointer, srcptr + (frame * bytesPerFrame) + (0 * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
// Channel 2
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area2.Pointer, srcptr + (frame * bytesPerFrame) + (1 * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
// Channel 3
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area3.Pointer, srcptr + (frame * bytesPerFrame) + (2 * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
// Channel 4
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area4.Pointer, srcptr + (frame * bytesPerFrame) + (3 * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
// Channel 5
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area5.Pointer, srcptr + (frame * bytesPerFrame) + (4 * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
// Channel 6
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)area6.Pointer, srcptr + (frame * bytesPerFrame) + (5 * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
area1.Pointer += area1.Step;
|
|
|
|
|
area2.Pointer += area2.Step;
|
|
|
|
|
area3.Pointer += area3.Step;
|
|
|
|
|
area4.Pointer += area4.Step;
|
|
|
|
|
area5.Pointer += area5.Step;
|
|
|
|
|
area6.Pointer += area6.Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Every other channel count
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SoundIOChannelArea[] channels = new SoundIOChannelArea[channelCount];
|
|
|
|
|
|
|
|
|
|
// Obtain the channel area for each channel
|
|
|
|
|
for (int i = 0; i < channelCount; i++)
|
|
|
|
|
{
|
|
|
|
|
channels[i] = areas.GetArea(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fixed (byte* srcptr = samples)
|
|
|
|
|
{
|
|
|
|
|
for (int frame = 0; frame < frameCount; frame++)
|
|
|
|
|
for (int channel = 0; channel < areas.ChannelCount; channel++)
|
|
|
|
|
{
|
|
|
|
|
// Copy channel by channel, frame by frame. This is slow!
|
|
|
|
|
Unsafe.CopyBlockUnaligned((byte*)channels[channel].Pointer, srcptr + (frame * bytesPerFrame) + (channel * bytesPerSample), bytesPerSample);
|
|
|
|
|
|
|
|
|
|
channels[channel].Pointer += channels[channel].Step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_outputStream.EndWrite();
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
ulong sampleCount = (ulong)(samples.Length / bytesPerSample / channelCount);
|
2020-11-20 21:59:01 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
ulong availaibleSampleCount = sampleCount;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
bool needUpdate = false;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
while (availaibleSampleCount > 0 && _queuedBuffers.TryPeek(out SoundIoAudioBuffer driverBuffer))
|
2018-11-15 03:22:50 +01:00
|
|
|
|
{
|
2021-02-28 17:20:34 +01:00
|
|
|
|
ulong sampleStillNeeded = driverBuffer.SampleCount - Interlocked.Read(ref driverBuffer.SamplePlayed);
|
2021-02-26 01:11:56 +01:00
|
|
|
|
ulong playedAudioBufferSampleCount = Math.Min(sampleStillNeeded, availaibleSampleCount);
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-28 17:20:34 +01:00
|
|
|
|
Interlocked.Add(ref driverBuffer.SamplePlayed, playedAudioBufferSampleCount);
|
2021-02-26 01:11:56 +01:00
|
|
|
|
availaibleSampleCount -= playedAudioBufferSampleCount;
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-28 17:20:34 +01:00
|
|
|
|
if (Interlocked.Read(ref driverBuffer.SamplePlayed) == driverBuffer.SampleCount)
|
2020-08-18 21:03:55 +02:00
|
|
|
|
{
|
2021-02-26 01:11:56 +01:00
|
|
|
|
_queuedBuffers.TryDequeue(out _);
|
2020-08-18 21:03:55 +02:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
needUpdate = true;
|
2020-08-18 21:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 17:20:34 +01:00
|
|
|
|
Interlocked.Add(ref _playedSampleCount, playedAudioBufferSampleCount);
|
2020-08-18 21:03:55 +02:00
|
|
|
|
}
|
2018-11-15 03:22:50 +01:00
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
// Notify the output if needed.
|
|
|
|
|
if (needUpdate)
|
|
|
|
|
{
|
|
|
|
|
_updateRequiredEvent.Set();
|
2020-08-18 21:03:55 +02:00
|
|
|
|
}
|
2018-11-15 03:22:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
protected virtual void Dispose(bool disposing)
|
2018-11-15 03:22:50 +01:00
|
|
|
|
{
|
2021-08-04 20:28:33 +02:00
|
|
|
|
if (disposing && _driver.Unregister(this))
|
2020-11-20 21:59:01 +01:00
|
|
|
|
{
|
2021-02-28 17:20:34 +01:00
|
|
|
|
PrepareToClose();
|
|
|
|
|
Stop();
|
2020-11-20 21:59:01 +01:00
|
|
|
|
|
2021-02-28 17:20:34 +01:00
|
|
|
|
_outputStream.Dispose();
|
2018-11-15 03:22:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
|
public override void Dispose()
|
2018-11-15 03:22:50 +01:00
|
|
|
|
{
|
2021-06-29 19:37:13 +02:00
|
|
|
|
if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
2018-11-15 03:22:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|