2021-04-14 10:28:43 +00:00
|
|
|
|
using OpenTK.Audio.OpenAL;
|
2021-02-26 00:11:56 +00:00
|
|
|
|
using Ryujinx.Audio.Common;
|
|
|
|
|
using Ryujinx.Audio.Integration;
|
|
|
|
|
using Ryujinx.Memory;
|
|
|
|
|
using System;
|
2021-08-04 18:28:33 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2021-04-14 10:28:43 +00:00
|
|
|
|
using System.Linq;
|
2021-02-26 00:11:56 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Audio.Backends.OpenAL
|
|
|
|
|
{
|
|
|
|
|
public class OpenALHardwareDeviceDriver : IHardwareDeviceDriver
|
|
|
|
|
{
|
2021-08-04 18:28:33 +00:00
|
|
|
|
private readonly ALDevice _device;
|
|
|
|
|
private readonly ALContext _context;
|
|
|
|
|
private readonly ManualResetEvent _updateRequiredEvent;
|
2021-09-11 20:08:25 +00:00
|
|
|
|
private readonly ManualResetEvent _pauseEvent;
|
2021-08-04 18:28:33 +00:00
|
|
|
|
private readonly ConcurrentDictionary<OpenALHardwareDeviceSession, byte> _sessions;
|
2021-02-26 00:11:56 +00:00
|
|
|
|
private bool _stillRunning;
|
2023-06-24 23:29:40 +00:00
|
|
|
|
private readonly Thread _updaterThread;
|
2021-02-26 00:11:56 +00:00
|
|
|
|
|
|
|
|
|
public OpenALHardwareDeviceDriver()
|
|
|
|
|
{
|
2021-04-14 10:28:43 +00:00
|
|
|
|
_device = ALC.OpenDevice("");
|
|
|
|
|
_context = ALC.CreateContext(_device, new ALContextAttributes());
|
2021-02-26 00:11:56 +00:00
|
|
|
|
_updateRequiredEvent = new ManualResetEvent(false);
|
2021-09-11 20:08:25 +00:00
|
|
|
|
_pauseEvent = new ManualResetEvent(true);
|
2021-08-04 18:28:33 +00:00
|
|
|
|
_sessions = new ConcurrentDictionary<OpenALHardwareDeviceSession, byte>();
|
2021-02-26 00:11:56 +00:00
|
|
|
|
|
|
|
|
|
_stillRunning = true;
|
|
|
|
|
_updaterThread = new Thread(Update)
|
|
|
|
|
{
|
|
|
|
|
Name = "HardwareDeviceDriver.OpenAL"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_updaterThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsSupported
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-14 10:28:43 +00:00
|
|
|
|
return ALC.GetStringList(GetEnumerationStringList.DeviceSpecifier).Any();
|
2021-02-26 00:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 16:33:56 +00:00
|
|
|
|
public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount, float volume)
|
2021-02-26 00:11:56 +00:00
|
|
|
|
{
|
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 23:29:40 +00:00
|
|
|
|
OpenALHardwareDeviceSession session = new(this, memoryManager, sampleFormat, sampleRate, channelCount, volume);
|
2021-02-26 00:11:56 +00:00
|
|
|
|
|
2021-08-04 18:28:33 +00:00
|
|
|
|
_sessions.TryAdd(session, 0);
|
2021-02-26 00:11:56 +00:00
|
|
|
|
|
2021-08-04 18:28:33 +00:00
|
|
|
|
return session;
|
2021-02-26 00:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 18:28:33 +00:00
|
|
|
|
internal bool Unregister(OpenALHardwareDeviceSession session)
|
2021-02-26 00:11:56 +00:00
|
|
|
|
{
|
2021-08-04 18:28:33 +00:00
|
|
|
|
return _sessions.TryRemove(session, out _);
|
2021-02-26 00:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ManualResetEvent GetUpdateRequiredEvent()
|
|
|
|
|
{
|
|
|
|
|
return _updateRequiredEvent;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 20:08:25 +00:00
|
|
|
|
public ManualResetEvent GetPauseEvent()
|
|
|
|
|
{
|
|
|
|
|
return _pauseEvent;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 00:11:56 +00:00
|
|
|
|
private void Update()
|
|
|
|
|
{
|
2021-04-14 10:28:43 +00:00
|
|
|
|
ALC.MakeContextCurrent(_context);
|
|
|
|
|
|
2021-02-26 00:11:56 +00:00
|
|
|
|
while (_stillRunning)
|
|
|
|
|
{
|
|
|
|
|
bool updateRequired = false;
|
|
|
|
|
|
2021-08-04 18:28:33 +00:00
|
|
|
|
foreach (OpenALHardwareDeviceSession session in _sessions.Keys)
|
2021-02-26 00:11:56 +00:00
|
|
|
|
{
|
2021-08-04 18:28:33 +00:00
|
|
|
|
if (session.Update())
|
2021-02-26 00:11:56 +00:00
|
|
|
|
{
|
2021-08-04 18:28:33 +00:00
|
|
|
|
updateRequired = true;
|
2021-02-26 00:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (updateRequired)
|
|
|
|
|
{
|
|
|
|
|
_updateRequiredEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it's not slept it will waste cycles.
|
|
|
|
|
Thread.Sleep(10);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2023-06-24 23:29:40 +00:00
|
|
|
|
GC.SuppressFinalize(this);
|
2021-02-26 00:11:56 +00:00
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
2021-06-29 17:37:13 +00:00
|
|
|
|
_stillRunning = false;
|
2021-02-26 00:11:56 +00:00
|
|
|
|
|
2021-08-04 18:28:33 +00:00
|
|
|
|
foreach (OpenALHardwareDeviceSession session in _sessions.Keys)
|
2021-06-29 17:37:13 +00:00
|
|
|
|
{
|
2021-08-04 18:28:33 +00:00
|
|
|
|
session.Dispose();
|
2021-02-26 00:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 10:28:43 +00:00
|
|
|
|
ALC.DestroyContext(_context);
|
|
|
|
|
ALC.CloseDevice(_device);
|
2021-09-11 20:08:25 +00:00
|
|
|
|
|
|
|
|
|
_pauseEvent.Dispose();
|
2021-02-26 00:11:56 +00: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|