2021-08-04 20:28:33 +02:00
|
|
|
|
using Ryujinx.Audio.Common;
|
2021-05-05 23:37:09 +02:00
|
|
|
|
using Ryujinx.Audio.Integration;
|
|
|
|
|
using Ryujinx.Memory;
|
|
|
|
|
using Ryujinx.SDL2.Common;
|
|
|
|
|
using System;
|
2021-08-04 20:28:33 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2021-05-05 23:37:09 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
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 20:28:33 +02:00
|
|
|
|
private readonly ManualResetEvent _updateRequiredEvent;
|
|
|
|
|
private readonly ConcurrentDictionary<SDL2HardwareDeviceSession, byte> _sessions;
|
2021-05-05 23:37:09 +02:00
|
|
|
|
|
|
|
|
|
public SDL2HardwareDeviceDriver()
|
|
|
|
|
{
|
|
|
|
|
_updateRequiredEvent = new ManualResetEvent(false);
|
2021-08-04 20:28:33 +02:00
|
|
|
|
_sessions = new ConcurrentDictionary<SDL2HardwareDeviceSession, byte>();
|
2021-05-05 23:37:09 +02:00
|
|
|
|
|
|
|
|
|
SDL2Driver.Instance.Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 NotImplementedException("Input direction is currently not implemented on SDL2 backend!");
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 20:28:33 +02:00
|
|
|
|
SDL2HardwareDeviceSession session = new SDL2HardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount);
|
2021-05-05 23:37:09 +02:00
|
|
|
|
|
2021-08-04 20:28:33 +02:00
|
|
|
|
_sessions.TryAdd(session, 0);
|
2021-05-05 23:37:09 +02:00
|
|
|
|
|
2021-08-04 20:28:33 +02:00
|
|
|
|
return session;
|
2021-05-05 23:37:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 20:28:33 +02:00
|
|
|
|
internal bool Unregister(SDL2HardwareDeviceSession session)
|
2021-05-05 23:37:09 +02:00
|
|
|
|
{
|
2021-08-04 20:28:33 +02:00
|
|
|
|
return _sessions.TryRemove(session, out _);
|
2021-05-05 23:37:09 +02: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,
|
|
|
|
|
samples = (ushort)sampleCount
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}"),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Fix this in SDL2-CS.
|
|
|
|
|
[DllImport("SDL2", EntryPoint = "SDL_OpenAudioDevice", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
|
private static extern uint SDL_OpenAudioDevice_Workaround(
|
|
|
|
|
IntPtr name,
|
|
|
|
|
int iscapture,
|
|
|
|
|
ref SDL_AudioSpec desired,
|
|
|
|
|
out SDL_AudioSpec obtained,
|
|
|
|
|
uint allowed_changes
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
uint device = SDL_OpenAudioDevice_Workaround(IntPtr.Zero, 0, ref desired, out SDL_AudioSpec got, 0);
|
|
|
|
|
|
|
|
|
|
if (device == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isValid = got.format == desired.format && got.freq == desired.freq && got.channels == desired.channels;
|
|
|
|
|
|
|
|
|
|
if (!isValid)
|
|
|
|
|
{
|
|
|
|
|
SDL_CloseAudioDevice(device);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
2021-08-04 20:28:33 +02:00
|
|
|
|
foreach (SDL2HardwareDeviceSession session in _sessions.Keys)
|
2021-05-05 23:37:09 +02:00
|
|
|
|
{
|
|
|
|
|
session.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL2Driver.Instance.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsSampleRate(uint sampleRate)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsSampleFormat(SampleFormat sampleFormat)
|
|
|
|
|
{
|
|
|
|
|
return sampleFormat != SampleFormat.PcmInt24;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsChannelCount(uint channelCount)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsDirection(Direction direction)
|
|
|
|
|
{
|
|
|
|
|
// TODO: add direction input when supported.
|
|
|
|
|
return direction == Direction.Output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|