2023-12-04 05:17:13 -08:00
|
|
|
using Ryujinx.Audio.Common;
|
2021-05-05 14:37:09 -07:00
|
|
|
using Ryujinx.Audio.Integration;
|
2022-12-01 08:53:13 -08:00
|
|
|
using Ryujinx.Common.Logging;
|
2021-05-05 14:37:09 -07:00
|
|
|
using Ryujinx.Memory;
|
|
|
|
using Ryujinx.SDL2.Common;
|
|
|
|
using System;
|
2021-08-04 11:28:33 -07:00
|
|
|
using System.Collections.Concurrent;
|
2023-05-13 00:15:16 -07:00
|
|
|
using System.Runtime.InteropServices;
|
2021-05-05 14:37:09 -07:00
|
|
|
using System.Threading;
|
|
|
|
using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
|
|
|
|
using static SDL2.SDL;
|
|
|
|
|
|
|
|
namespace Ryujinx.Audio.Backends.SDL2
|
|
|
|
{
|
|
|
|
public class SDL2HardwareDeviceDriver : IHardwareDeviceDriver
|
|
|
|
{
|
2021-08-04 11:28:33 -07:00
|
|
|
private readonly ManualResetEvent _updateRequiredEvent;
|
2021-09-11 13:08:25 -07:00
|
|
|
private readonly ManualResetEvent _pauseEvent;
|
2021-08-04 11:28:33 -07:00
|
|
|
private readonly ConcurrentDictionary<SDL2HardwareDeviceSession, byte> _sessions;
|
2021-05-05 14:37:09 -07:00
|
|
|
|
2023-06-25 13:50:59 -07:00
|
|
|
private readonly bool _supportSurroundConfiguration;
|
2023-05-13 00:15:16 -07:00
|
|
|
|
2024-02-22 11:58:33 -08:00
|
|
|
public float Volume { get; set; }
|
|
|
|
|
2023-05-13 00:15:16 -07:00
|
|
|
// TODO: Add this to SDL2-CS
|
|
|
|
// NOTE: We use a DllImport here because of marshaling issue for spec.
|
2023-06-25 13:50:59 -07:00
|
|
|
#pragma warning disable SYSLIB1054
|
2023-05-13 00:15:16 -07:00
|
|
|
[DllImport("SDL2")]
|
|
|
|
private static extern int SDL_GetDefaultAudioInfo(IntPtr name, out SDL_AudioSpec spec, int isCapture);
|
2023-06-25 13:50:59 -07:00
|
|
|
#pragma warning restore SYSLIB1054
|
2023-05-13 00:15:16 -07:00
|
|
|
|
2021-05-05 14:37:09 -07:00
|
|
|
public SDL2HardwareDeviceDriver()
|
|
|
|
{
|
|
|
|
_updateRequiredEvent = new ManualResetEvent(false);
|
2021-09-11 13:08:25 -07:00
|
|
|
_pauseEvent = new ManualResetEvent(true);
|
2021-08-04 11:28:33 -07:00
|
|
|
_sessions = new ConcurrentDictionary<SDL2HardwareDeviceSession, byte>();
|
2021-05-05 14:37:09 -07:00
|
|
|
|
|
|
|
SDL2Driver.Instance.Initialize();
|
2023-05-13 00:15:16 -07:00
|
|
|
|
|
|
|
int res = SDL_GetDefaultAudioInfo(IntPtr.Zero, out var spec, 0);
|
|
|
|
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
Logger.Error?.Print(LogClass.Application,
|
|
|
|
$"SDL_GetDefaultAudioInfo failed with error \"{SDL_GetError()}\"");
|
|
|
|
|
|
|
|
_supportSurroundConfiguration = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-28 15:07:27 -07:00
|
|
|
_supportSurroundConfiguration = spec.channels >= 6;
|
2023-05-13 00:15:16 -07:00
|
|
|
}
|
2024-02-22 11:58:33 -08:00
|
|
|
|
|
|
|
Volume = 1f;
|
2021-05-05 14:37:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static bool IsSupported => IsSupportedInternal();
|
|
|
|
|
|
|
|
private static bool IsSupportedInternal()
|
|
|
|
{
|
|
|
|
uint device = OpenStream(SampleFormat.PcmInt16, Constants.TargetSampleRate, Constants.ChannelCountMax, Constants.TargetSampleCount, null);
|
|
|
|
|
|
|
|
if (device != 0)
|
|
|
|
{
|
|
|
|
SDL_CloseAudioDevice(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
return device != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ManualResetEvent GetUpdateRequiredEvent()
|
|
|
|
{
|
|
|
|
return _updateRequiredEvent;
|
|
|
|
}
|
|
|
|
|
2021-09-11 13:08:25 -07:00
|
|
|
public ManualResetEvent GetPauseEvent()
|
|
|
|
{
|
|
|
|
return _pauseEvent;
|
|
|
|
}
|
|
|
|
|
2024-02-22 11:58:33 -08:00
|
|
|
public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount)
|
2021-05-05 14:37:09 -07:00
|
|
|
{
|
|
|
|
if (channelCount == 0)
|
|
|
|
{
|
|
|
|
channelCount = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sampleRate == 0)
|
|
|
|
{
|
|
|
|
sampleRate = Constants.TargetSampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction != Direction.Output)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException("Input direction is currently not implemented on SDL2 backend!");
|
|
|
|
}
|
|
|
|
|
2024-02-22 11:58:33 -08:00
|
|
|
SDL2HardwareDeviceSession session = new(this, memoryManager, sampleFormat, sampleRate, channelCount);
|
2021-05-05 14:37:09 -07:00
|
|
|
|
2021-08-04 11:28:33 -07:00
|
|
|
_sessions.TryAdd(session, 0);
|
2021-05-05 14:37:09 -07:00
|
|
|
|
2021-08-04 11:28:33 -07:00
|
|
|
return session;
|
2021-05-05 14:37:09 -07:00
|
|
|
}
|
|
|
|
|
2021-08-04 11:28:33 -07:00
|
|
|
internal bool Unregister(SDL2HardwareDeviceSession session)
|
2021-05-05 14:37:09 -07:00
|
|
|
{
|
2021-08-04 11:28:33 -07:00
|
|
|
return _sessions.TryRemove(session, out _);
|
2021-05-05 14:37:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static SDL_AudioSpec GetSDL2Spec(SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, uint sampleCount)
|
|
|
|
{
|
|
|
|
return new SDL_AudioSpec
|
|
|
|
{
|
|
|
|
channels = (byte)requestedChannelCount,
|
|
|
|
format = GetSDL2Format(requestedSampleFormat),
|
|
|
|
freq = (int)requestedSampleRate,
|
2023-07-24 09:35:04 -07:00
|
|
|
samples = (ushort)sampleCount,
|
2021-05-05 14:37:09 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static ushort GetSDL2Format(SampleFormat format)
|
|
|
|
{
|
|
|
|
return format switch
|
|
|
|
{
|
|
|
|
SampleFormat.PcmInt8 => AUDIO_S8,
|
|
|
|
SampleFormat.PcmInt16 => AUDIO_S16,
|
|
|
|
SampleFormat.PcmInt32 => AUDIO_S32,
|
|
|
|
SampleFormat.PcmFloat => AUDIO_F32,
|
|
|
|
_ => throw new ArgumentException($"Unsupported sample format {format}"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static uint OpenStream(SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, uint sampleCount, SDL_AudioCallback callback)
|
|
|
|
{
|
|
|
|
SDL_AudioSpec desired = GetSDL2Spec(requestedSampleFormat, requestedSampleRate, requestedChannelCount, sampleCount);
|
|
|
|
|
|
|
|
desired.callback = callback;
|
|
|
|
|
2021-08-26 13:26:28 -07:00
|
|
|
uint device = SDL_OpenAudioDevice(IntPtr.Zero, 0, ref desired, out SDL_AudioSpec got, 0);
|
2021-05-05 14:37:09 -07:00
|
|
|
|
|
|
|
if (device == 0)
|
|
|
|
{
|
2023-06-25 13:50:59 -07:00
|
|
|
Logger.Error?.Print(LogClass.Application, $"SDL2 open audio device initialization failed with error \"{SDL_GetError()}\"");
|
2022-12-01 08:53:13 -08:00
|
|
|
|
2021-05-05 14:37:09 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid = got.format == desired.format && got.freq == desired.freq && got.channels == desired.channels;
|
|
|
|
|
|
|
|
if (!isValid)
|
|
|
|
{
|
2022-12-01 08:53:13 -08:00
|
|
|
Logger.Error?.Print(LogClass.Application, "SDL2 open audio device is not valid");
|
2021-05-05 14:37:09 -07:00
|
|
|
SDL_CloseAudioDevice(device);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2023-06-25 13:50:59 -07:00
|
|
|
GC.SuppressFinalize(this);
|
2021-05-05 14:37:09 -07:00
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
2021-08-04 11:28:33 -07:00
|
|
|
foreach (SDL2HardwareDeviceSession session in _sessions.Keys)
|
2021-05-05 14:37:09 -07:00
|
|
|
{
|
|
|
|
session.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL2Driver.Instance.Dispose();
|
2021-09-11 13:08:25 -07:00
|
|
|
|
|
|
|
_pauseEvent.Dispose();
|
2021-05-05 14:37:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SupportsSampleRate(uint sampleRate)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SupportsSampleFormat(SampleFormat sampleFormat)
|
|
|
|
{
|
|
|
|
return sampleFormat != SampleFormat.PcmInt24;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SupportsChannelCount(uint channelCount)
|
|
|
|
{
|
2023-05-13 00:15:16 -07:00
|
|
|
if (channelCount == 6)
|
|
|
|
{
|
|
|
|
return _supportSurroundConfiguration;
|
|
|
|
}
|
|
|
|
|
2021-05-05 14:37:09 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SupportsDirection(Direction direction)
|
|
|
|
{
|
|
|
|
// TODO: add direction input when supported.
|
|
|
|
return direction == Direction.Output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|