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