2021-04-14 06:28:43 -04:00
|
|
|
|
using OpenTK.Audio.OpenAL;
|
2021-02-25 19:11:56 -05:00
|
|
|
|
using Ryujinx.Audio.Common;
|
|
|
|
|
using Ryujinx.Audio.Integration;
|
|
|
|
|
using Ryujinx.Memory;
|
|
|
|
|
using System;
|
2021-08-04 14:28:33 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
2021-04-14 06:28:43 -04:00
|
|
|
|
using System.Linq;
|
2021-02-25 19:11:56 -05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Audio.Backends.OpenAL
|
|
|
|
|
{
|
|
|
|
|
public class OpenALHardwareDeviceDriver : IHardwareDeviceDriver
|
|
|
|
|
{
|
2021-08-04 14:28:33 -04:00
|
|
|
|
private readonly ALDevice _device;
|
|
|
|
|
private readonly ALContext _context;
|
|
|
|
|
private readonly ManualResetEvent _updateRequiredEvent;
|
|
|
|
|
private readonly ConcurrentDictionary<OpenALHardwareDeviceSession, byte> _sessions;
|
2021-02-25 19:11:56 -05:00
|
|
|
|
private bool _stillRunning;
|
|
|
|
|
private Thread _updaterThread;
|
|
|
|
|
|
|
|
|
|
public OpenALHardwareDeviceDriver()
|
|
|
|
|
{
|
2021-04-14 06:28:43 -04:00
|
|
|
|
_device = ALC.OpenDevice("");
|
|
|
|
|
_context = ALC.CreateContext(_device, new ALContextAttributes());
|
2021-02-25 19:11:56 -05:00
|
|
|
|
_updateRequiredEvent = new ManualResetEvent(false);
|
2021-08-04 14:28:33 -04:00
|
|
|
|
_sessions = new ConcurrentDictionary<OpenALHardwareDeviceSession, byte>();
|
2021-02-25 19:11:56 -05:00
|
|
|
|
|
|
|
|
|
_stillRunning = true;
|
|
|
|
|
_updaterThread = new Thread(Update)
|
|
|
|
|
{
|
|
|
|
|
Name = "HardwareDeviceDriver.OpenAL"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_updaterThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsSupported
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-14 06:28:43 -04:00
|
|
|
|
return ALC.GetStringList(GetEnumerationStringList.DeviceSpecifier).Any();
|
2021-02-25 19:11:56 -05:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount)
|
|
|
|
|
{
|
|
|
|
|
if (channelCount == 0)
|
|
|
|
|
{
|
|
|
|
|
channelCount = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sampleRate == 0)
|
|
|
|
|
{
|
|
|
|
|
sampleRate = Constants.TargetSampleRate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (direction != Direction.Output)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"{direction}");
|
|
|
|
|
}
|
|
|
|
|
else if (!SupportsChannelCount(channelCount))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"{channelCount}");
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 14:28:33 -04:00
|
|
|
|
OpenALHardwareDeviceSession session = new OpenALHardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount);
|
2021-02-25 19:11:56 -05:00
|
|
|
|
|
2021-08-04 14:28:33 -04:00
|
|
|
|
_sessions.TryAdd(session, 0);
|
2021-02-25 19:11:56 -05:00
|
|
|
|
|
2021-08-04 14:28:33 -04:00
|
|
|
|
return session;
|
2021-02-25 19:11:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 14:28:33 -04:00
|
|
|
|
internal bool Unregister(OpenALHardwareDeviceSession session)
|
2021-02-25 19:11:56 -05:00
|
|
|
|
{
|
2021-08-04 14:28:33 -04:00
|
|
|
|
return _sessions.TryRemove(session, out _);
|
2021-02-25 19:11:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ManualResetEvent GetUpdateRequiredEvent()
|
|
|
|
|
{
|
|
|
|
|
return _updateRequiredEvent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
2021-04-14 06:28:43 -04:00
|
|
|
|
ALC.MakeContextCurrent(_context);
|
|
|
|
|
|
2021-02-25 19:11:56 -05:00
|
|
|
|
while (_stillRunning)
|
|
|
|
|
{
|
|
|
|
|
bool updateRequired = false;
|
|
|
|
|
|
2021-08-04 14:28:33 -04:00
|
|
|
|
foreach (OpenALHardwareDeviceSession session in _sessions.Keys)
|
2021-02-25 19:11:56 -05:00
|
|
|
|
{
|
2021-08-04 14:28:33 -04:00
|
|
|
|
if (session.Update())
|
2021-02-25 19:11:56 -05:00
|
|
|
|
{
|
2021-08-04 14:28:33 -04:00
|
|
|
|
updateRequired = true;
|
2021-02-25 19:11:56 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (updateRequired)
|
|
|
|
|
{
|
|
|
|
|
_updateRequiredEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it's not slept it will waste cycles.
|
|
|
|
|
Thread.Sleep(10);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
2021-06-29 13:37:13 -04:00
|
|
|
|
_stillRunning = false;
|
2021-02-25 19:11:56 -05:00
|
|
|
|
|
2021-08-04 14:28:33 -04:00
|
|
|
|
foreach (OpenALHardwareDeviceSession session in _sessions.Keys)
|
2021-06-29 13:37:13 -04:00
|
|
|
|
{
|
2021-08-04 14:28:33 -04:00
|
|
|
|
session.Dispose();
|
2021-02-25 19:11:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 06:28:43 -04:00
|
|
|
|
ALC.DestroyContext(_context);
|
|
|
|
|
ALC.CloseDevice(_device);
|
2021-02-25 19:11:56 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsSampleRate(uint sampleRate)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsSampleFormat(SampleFormat sampleFormat)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsChannelCount(uint channelCount)
|
|
|
|
|
{
|
|
|
|
|
return channelCount == 1 || channelCount == 2 || channelCount == 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsDirection(Direction direction)
|
|
|
|
|
{
|
|
|
|
|
return direction == Direction.Output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|