aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Input
diff options
context:
space:
mode:
authorTSR Berry <20988865+TSRBerry@users.noreply.github.com>2023-04-08 01:22:00 +0200
committerMary <thog@protonmail.com>2023-04-27 23:51:14 +0200
commitcee712105850ac3385cd0091a923438167433f9f (patch)
tree4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Ryujinx.Audio/Input
parentcd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff)
Move solution and projects to src
Diffstat (limited to 'src/Ryujinx.Audio/Input')
-rw-r--r--src/Ryujinx.Audio/Input/AudioInputManager.cs266
-rw-r--r--src/Ryujinx.Audio/Input/AudioInputSystem.cs392
2 files changed, 658 insertions, 0 deletions
diff --git a/src/Ryujinx.Audio/Input/AudioInputManager.cs b/src/Ryujinx.Audio/Input/AudioInputManager.cs
new file mode 100644
index 00000000..ac012c4a
--- /dev/null
+++ b/src/Ryujinx.Audio/Input/AudioInputManager.cs
@@ -0,0 +1,266 @@
+using Ryujinx.Audio.Common;
+using Ryujinx.Audio.Integration;
+using Ryujinx.Common.Logging;
+using Ryujinx.Memory;
+using System;
+using System.Diagnostics;
+using System.Linq;
+using System.Threading;
+
+namespace Ryujinx.Audio.Input
+{
+ /// <summary>
+ /// The audio input manager.
+ /// </summary>
+ public class AudioInputManager : IDisposable
+ {
+ private object _lock = new object();
+
+ /// <summary>
+ /// Lock used for session allocation.
+ /// </summary>
+ private object _sessionLock = new object();
+
+ /// <summary>
+ /// The session ids allocation table.
+ /// </summary>
+ private int[] _sessionIds;
+
+ /// <summary>
+ /// The device driver.
+ /// </summary>
+ private IHardwareDeviceDriver _deviceDriver;
+
+ /// <summary>
+ /// The events linked to each session.
+ /// </summary>
+ private IWritableEvent[] _sessionsBufferEvents;
+
+ /// <summary>
+ /// The <see cref="AudioInputSystem"/> session instances.
+ /// </summary>
+ private AudioInputSystem[] _sessions;
+
+ /// <summary>
+ /// The count of active sessions.
+ /// </summary>
+ private int _activeSessionCount;
+
+ /// <summary>
+ /// The dispose state.
+ /// </summary>
+ private int _disposeState;
+
+ /// <summary>
+ /// Create a new <see cref="AudioInputManager"/>.
+ /// </summary>
+ public AudioInputManager()
+ {
+ _sessionIds = new int[Constants.AudioInSessionCountMax];
+ _sessions = new AudioInputSystem[Constants.AudioInSessionCountMax];
+ _activeSessionCount = 0;
+
+ for (int i = 0; i < _sessionIds.Length; i++)
+ {
+ _sessionIds[i] = i;
+ }
+ }
+
+ /// <summary>
+ /// Initialize the <see cref="AudioInputManager"/>.
+ /// </summary>
+ /// <param name="deviceDriver">The device driver.</param>
+ /// <param name="sessionRegisterEvents">The events associated to each session.</param>
+ public void Initialize(IHardwareDeviceDriver deviceDriver, IWritableEvent[] sessionRegisterEvents)
+ {
+ _deviceDriver = deviceDriver;
+ _sessionsBufferEvents = sessionRegisterEvents;
+ }
+
+ /// <summary>
+ /// Acquire a new session id.
+ /// </summary>
+ /// <returns>A new session id.</returns>
+ private int AcquireSessionId()
+ {
+ lock (_sessionLock)
+ {
+ int index = _activeSessionCount;
+
+ Debug.Assert(index < _sessionIds.Length);
+
+ int sessionId = _sessionIds[index];
+
+ _sessionIds[index] = -1;
+
+ _activeSessionCount++;
+
+ Logger.Info?.Print(LogClass.AudioRenderer, $"Registered new input ({sessionId})");
+
+ return sessionId;
+ }
+ }
+
+ /// <summary>
+ /// Release a given <paramref name="sessionId"/>.
+ /// </summary>
+ /// <param name="sessionId">The session id to release.</param>
+ private void ReleaseSessionId(int sessionId)
+ {
+ lock (_sessionLock)
+ {
+ Debug.Assert(_activeSessionCount > 0);
+
+ int newIndex = --_activeSessionCount;
+
+ _sessionIds[newIndex] = sessionId;
+ }
+
+ Logger.Info?.Print(LogClass.AudioRenderer, $"Unregistered input ({sessionId})");
+ }
+
+ /// <summary>
+ /// Used to update audio input system.
+ /// </summary>
+ public void Update()
+ {
+ lock (_sessionLock)
+ {
+ foreach (AudioInputSystem input in _sessions)
+ {
+ input?.Update();
+ }
+ }
+ }
+
+ /// <summary>
+ /// Register a new <see cref="AudioInputSystem"/>.
+ /// </summary>
+ /// <param name="input">The <see cref="AudioInputSystem"/> to register.</param>
+ private void Register(AudioInputSystem input)
+ {
+ lock (_sessionLock)
+ {
+ _sessions[input.GetSessionId()] = input;
+ }
+ }
+
+ /// <summary>
+ /// Unregister a new <see cref="AudioInputSystem"/>.
+ /// </summary>
+ /// <param name="input">The <see cref="AudioInputSystem"/> to unregister.</param>
+ internal void Unregister(AudioInputSystem input)
+ {
+ lock (_sessionLock)
+ {
+ int sessionId = input.GetSessionId();
+
+ _sessions[input.GetSessionId()] = null;
+
+ ReleaseSessionId(sessionId);
+ }
+ }
+
+ /// <summary>
+ /// Get the list of all audio inputs names.
+ /// </summary>
+ /// <param name="filtered">If true, filter disconnected devices</param>
+ /// <returns>The list of all audio inputs name</returns>
+ public string[] ListAudioIns(bool filtered)
+ {
+ if (filtered)
+ {
+ // TODO: Detect if the driver supports audio input
+ }
+
+ return new string[] { Constants.DefaultDeviceInputName };
+ }
+
+ /// <summary>
+ /// Open a new <see cref="AudioInputSystem"/>.
+ /// </summary>
+ /// <param name="outputDeviceName">The output device name selected by the <see cref="AudioInputSystem"/></param>
+ /// <param name="outputConfiguration">The output audio configuration selected by the <see cref="AudioInputSystem"/></param>
+ /// <param name="obj">The new <see cref="AudioInputSystem"/></param>
+ /// <param name="memoryManager">The memory manager that will be used for all guest memory operations</param>
+ /// <param name="inputDeviceName">The input device name wanted by the user</param>
+ /// <param name="sampleFormat">The sample format to use</param>
+ /// <param name="parameter">The user configuration</param>
+ /// <param name="appletResourceUserId">The applet resource user id of the application</param>
+ /// <param name="processHandle">The process handle of the application</param>
+ /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
+ public ResultCode OpenAudioIn(out string outputDeviceName,
+ out AudioOutputConfiguration outputConfiguration,
+ out AudioInputSystem obj,
+ IVirtualMemoryManager memoryManager,
+ string inputDeviceName,
+ SampleFormat sampleFormat,
+ ref AudioInputConfiguration parameter,
+ ulong appletResourceUserId,
+ uint processHandle)
+ {
+ int sessionId = AcquireSessionId();
+
+ _sessionsBufferEvents[sessionId].Clear();
+
+ IHardwareDeviceSession deviceSession = _deviceDriver.OpenDeviceSession(IHardwareDeviceDriver.Direction.Input, memoryManager, sampleFormat, parameter.SampleRate, parameter.ChannelCount);
+
+ AudioInputSystem audioIn = new AudioInputSystem(this, _lock, deviceSession, _sessionsBufferEvents[sessionId]);
+
+ ResultCode result = audioIn.Initialize(inputDeviceName, sampleFormat, ref parameter, sessionId);
+
+ if (result == ResultCode.Success)
+ {
+ outputDeviceName = audioIn.DeviceName;
+ outputConfiguration = new AudioOutputConfiguration
+ {
+ ChannelCount = audioIn.ChannelCount,
+ SampleFormat = audioIn.SampleFormat,
+ SampleRate = audioIn.SampleRate,
+ AudioOutState = audioIn.GetState(),
+ };
+
+ obj = audioIn;
+
+ Register(audioIn);
+ }
+ else
+ {
+ ReleaseSessionId(sessionId);
+
+ obj = null;
+ outputDeviceName = null;
+ outputConfiguration = default;
+ }
+
+ return result;
+ }
+
+ public void Dispose()
+ {
+ if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
+ {
+ Dispose(true);
+ }
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ // Clone the sessions array to dispose them outside the lock.
+ AudioInputSystem[] sessions;
+
+ lock (_sessionLock)
+ {
+ sessions = _sessions.ToArray();
+ }
+
+ foreach (AudioInputSystem input in sessions)
+ {
+ input?.Dispose();
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Input/AudioInputSystem.cs b/src/Ryujinx.Audio/Input/AudioInputSystem.cs
new file mode 100644
index 00000000..b3ca0fd6
--- /dev/null
+++ b/src/Ryujinx.Audio/Input/AudioInputSystem.cs
@@ -0,0 +1,392 @@
+using Ryujinx.Audio.Common;
+using Ryujinx.Audio.Integration;
+using System;
+using System.Threading;
+
+namespace Ryujinx.Audio.Input
+{
+ /// <summary>
+ /// Audio input system.
+ /// </summary>
+ public class AudioInputSystem : IDisposable
+ {
+ /// <summary>
+ /// The session id associated to the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ private int _sessionId;
+
+ /// <summary>
+ /// The session the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ private AudioDeviceSession _session;
+
+ /// <summary>
+ /// The target device name of the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ public string DeviceName { get; private set; }
+
+ /// <summary>
+ /// The target sample rate of the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ public uint SampleRate { get; private set; }
+
+ /// <summary>
+ /// The target channel count of the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ public uint ChannelCount { get; private set; }
+
+ /// <summary>
+ /// The target sample format of the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ public SampleFormat SampleFormat { get; private set; }
+
+ /// <summary>
+ /// The <see cref="AudioInputManager"/> owning this.
+ /// </summary>
+ private AudioInputManager _manager;
+
+ /// <summary>
+ /// The lock of the parent.
+ /// </summary>
+ private object _parentLock;
+
+ /// <summary>
+ /// The dispose state.
+ /// </summary>
+ private int _disposeState;
+
+ /// <summary>
+ /// Create a new <see cref="AudioInputSystem"/>.
+ /// </summary>
+ /// <param name="manager">The manager instance</param>
+ /// <param name="parentLock">The lock of the manager</param>
+ /// <param name="deviceSession">The hardware device session</param>
+ /// <param name="bufferEvent">The buffer release event of the audio input</param>
+ public AudioInputSystem(AudioInputManager manager, object parentLock, IHardwareDeviceSession deviceSession, IWritableEvent bufferEvent)
+ {
+ _manager = manager;
+ _parentLock = parentLock;
+ _session = new AudioDeviceSession(deviceSession, bufferEvent);
+ }
+
+ /// <summary>
+ /// Get the default device name on the system.
+ /// </summary>
+ /// <returns>The default device name on the system.</returns>
+ private static string GetDeviceDefaultName()
+ {
+ return Constants.DefaultDeviceInputName;
+ }
+
+ /// <summary>
+ /// Check if a given configuration and device name is valid on the system.
+ /// </summary>
+ /// <param name="configuration">The configuration to check.</param>
+ /// <param name="deviceName">The device name to check.</param>
+ /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
+ private static ResultCode IsConfigurationValid(ref AudioInputConfiguration configuration, string deviceName)
+ {
+ if (deviceName.Length != 0 && !deviceName.Equals(GetDeviceDefaultName()))
+ {
+ return ResultCode.DeviceNotFound;
+ }
+ else if (configuration.SampleRate != 0 && configuration.SampleRate != Constants.TargetSampleRate)
+ {
+ return ResultCode.UnsupportedSampleRate;
+ }
+ else if (configuration.ChannelCount != 0 && configuration.ChannelCount != 1 && configuration.ChannelCount != 2 && configuration.ChannelCount != 6)
+ {
+ return ResultCode.UnsupportedChannelConfiguration;
+ }
+
+ return ResultCode.Success;
+ }
+
+ /// <summary>
+ /// Get the released buffer event.
+ /// </summary>
+ /// <returns>The released buffer event</returns>
+ public IWritableEvent RegisterBufferEvent()
+ {
+ lock (_parentLock)
+ {
+ return _session.GetBufferEvent();
+ }
+ }
+
+ /// <summary>
+ /// Update the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ public void Update()
+ {
+ lock (_parentLock)
+ {
+ _session.Update();
+ }
+ }
+
+ /// <summary>
+ /// Get the id of this session.
+ /// </summary>
+ /// <returns>The id of this session</returns>
+ public int GetSessionId()
+ {
+ return _sessionId;
+ }
+
+ /// <summary>
+ /// Initialize the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ /// <param name="inputDeviceName">The input device name wanted by the user</param>
+ /// <param name="sampleFormat">The sample format to use</param>
+ /// <param name="parameter">The user configuration</param>
+ /// <param name="sessionId">The session id associated to this <see cref="AudioInputSystem"/></param>
+ /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
+ public ResultCode Initialize(string inputDeviceName, SampleFormat sampleFormat, ref AudioInputConfiguration parameter, int sessionId)
+ {
+ _sessionId = sessionId;
+
+ ResultCode result = IsConfigurationValid(ref parameter, inputDeviceName);
+
+ if (result == ResultCode.Success)
+ {
+ if (inputDeviceName.Length == 0)
+ {
+ DeviceName = GetDeviceDefaultName();
+ }
+ else
+ {
+ DeviceName = inputDeviceName;
+ }
+
+ if (parameter.ChannelCount == 6)
+ {
+ ChannelCount = 6;
+ }
+ else
+ {
+ ChannelCount = 2;
+ }
+
+ SampleFormat = sampleFormat;
+ SampleRate = Constants.TargetSampleRate;
+ }
+
+ return result;
+ }
+
+ /// <summary>
+ /// Append a new audio buffer to the audio input.
+ /// </summary>
+ /// <param name="bufferTag">The unique tag of this buffer.</param>
+ /// <param name="userBuffer">The buffer informations.</param>
+ /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
+ public ResultCode AppendBuffer(ulong bufferTag, ref AudioUserBuffer userBuffer)
+ {
+ lock (_parentLock)
+ {
+ AudioBuffer buffer = new AudioBuffer
+ {
+ BufferTag = bufferTag,
+ DataPointer = userBuffer.Data,
+ DataSize = userBuffer.DataSize
+ };
+
+ if (_session.AppendBuffer(buffer))
+ {
+ return ResultCode.Success;
+ }
+
+ return ResultCode.BufferRingFull;
+ }
+ }
+
+ /// <summary>
+ /// Append a new audio buffer to the audio input.
+ /// </summary>
+ /// <remarks>This is broken by design, only added for completness.</remarks>
+ /// <param name="bufferTag">The unique tag of this buffer.</param>
+ /// <param name="userBuffer">The buffer informations.</param>
+ /// <param name="handle">Some unknown handle.</param>
+ /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
+ public ResultCode AppendUacBuffer(ulong bufferTag, ref AudioUserBuffer userBuffer, uint handle)
+ {
+ lock (_parentLock)
+ {
+ AudioBuffer buffer = new AudioBuffer
+ {
+ BufferTag = bufferTag,
+ DataPointer = userBuffer.Data,
+ DataSize = userBuffer.DataSize
+ };
+
+ if (_session.AppendUacBuffer(buffer, handle))
+ {
+ return ResultCode.Success;
+ }
+
+ return ResultCode.BufferRingFull;
+ }
+ }
+
+ /// <summary>
+ /// Get the release buffers.
+ /// </summary>
+ /// <param name="releasedBuffers">The buffer to write the release buffers</param>
+ /// <param name="releasedCount">The count of released buffers</param>
+ /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
+ public ResultCode GetReleasedBuffers(Span<ulong> releasedBuffers, out uint releasedCount)
+ {
+ releasedCount = 0;
+
+ // Ensure that the first entry is set to zero if no entries are returned.
+ if (releasedBuffers.Length > 0)
+ {
+ releasedBuffers[0] = 0;
+ }
+
+ lock (_parentLock)
+ {
+ for (int i = 0; i < releasedBuffers.Length; i++)
+ {
+ if (!_session.TryPopReleasedBuffer(out AudioBuffer buffer))
+ {
+ break;
+ }
+
+ releasedBuffers[i] = buffer.BufferTag;
+ releasedCount++;
+ }
+ }
+
+ return ResultCode.Success;
+ }
+
+ /// <summary>
+ /// Get the current state of the <see cref="AudioInputSystem"/>.
+ /// </summary>
+ /// <returns>Return the curent sta\te of the <see cref="AudioInputSystem"/></returns>
+ public AudioDeviceState GetState()
+ {
+ lock (_parentLock)
+ {
+ return _session.GetState();
+ }
+ }
+
+ /// <summary>
+ /// Start the audio session.
+ /// </summary>
+ /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
+ public ResultCode Start()
+ {
+ lock (_parentLock)
+ {
+ return _session.Start();
+ }
+ }
+
+ /// <summary>
+ /// Stop the audio session.
+ /// </summary>
+ /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
+ public ResultCode Stop()
+ {
+ lock (_parentLock)
+ {
+ return _session.Stop();
+ }
+ }
+
+ /// <summary>
+ /// Get the volume of the session.
+ /// </summary>
+ /// <returns>The volume of the session</returns>
+ public float GetVolume()
+ {
+ lock (_parentLock)
+ {
+ return _session.GetVolume();
+ }
+ }
+
+ /// <summary>
+ /// Set the volume of the session.
+ /// </summary>
+ /// <param name="volume">The new volume to set</param>
+ public void SetVolume(float volume)
+ {
+ lock (_parentLock)
+ {
+ _session.SetVolume(volume);
+ }
+ }
+
+ /// <summary>
+ /// Get the count of buffer currently in use (server + driver side).
+ /// </summary>
+ /// <returns>The count of buffer currently in use</returns>
+ public uint GetBufferCount()
+ {
+ lock (_parentLock)
+ {
+ return _session.GetBufferCount();
+ }
+ }
+
+ /// <summary>
+ /// Check if a buffer is present.
+ /// </summary>
+ /// <param name="bufferTag">The unique tag of the buffer</param>
+ /// <returns>Return true if a buffer is present</returns>
+ public bool ContainsBuffer(ulong bufferTag)
+ {
+ lock (_parentLock)
+ {
+ return _session.ContainsBuffer(bufferTag);
+ }
+ }
+
+ /// <summary>
+ /// Get the count of sample played in this session.
+ /// </summary>
+ /// <returns>The count of sample played in this session</returns>
+ public ulong GetPlayedSampleCount()
+ {
+ lock (_parentLock)
+ {
+ return _session.GetPlayedSampleCount();
+ }
+ }
+
+ /// <summary>
+ /// Flush all buffers to the initial state.
+ /// </summary>
+ /// <returns>True if any buffers was flushed</returns>
+ public bool FlushBuffers()
+ {
+ lock (_parentLock)
+ {
+ return _session.FlushBuffers();
+ }
+ }
+
+ public void Dispose()
+ {
+ if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
+ {
+ Dispose(true);
+ }
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _session.Dispose();
+
+ _manager.Unregister(this);
+ }
+ }
+ }
+} \ No newline at end of file