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