aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/Ryujinx.Audio/Renderer/Utils')
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/AudioProcessorMemoryManager.cs57
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/BitArray.cs103
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/FileHardwareDevice.cs99
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/Mailbox.cs55
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/Math/Matrix2x2.cs71
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/Math/Matrix6x6.cs97
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/Math/MatrixHelper.cs45
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/Math/Vector6.cs56
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/SpanIOHelper.cs171
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/SpanMemoryManager.cs43
-rw-r--r--src/Ryujinx.Audio/Renderer/Utils/SplitterHardwareDevice.cs58
11 files changed, 855 insertions, 0 deletions
diff --git a/src/Ryujinx.Audio/Renderer/Utils/AudioProcessorMemoryManager.cs b/src/Ryujinx.Audio/Renderer/Utils/AudioProcessorMemoryManager.cs
new file mode 100644
index 00000000..973f0672
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/AudioProcessorMemoryManager.cs
@@ -0,0 +1,57 @@
+using System.Runtime.CompilerServices;
+using CpuAddress = System.UInt64;
+using DspAddress = System.UInt64;
+
+namespace Ryujinx.Audio.Renderer.Utils
+{
+ /// <summary>
+ /// The <see cref="Dsp.AudioProcessor"/> memory management
+ /// </summary>
+ /// <remarks>This is stub for the most part but is kept to permit LLE if wanted.</remarks>
+ static class AudioProcessorMemoryManager
+ {
+ /// <summary>
+ /// Map the given <see cref="CpuAddress"/> to the <see cref="Dsp.AudioProcessor"/> address space.
+ /// </summary>
+ /// <param name="processHandle">The process owning the CPU memory.</param>
+ /// <param name="cpuAddress">The <see cref="CpuAddress"/> to map.</param>
+ /// <param name="size">The size of the CPU memory region to map.</param>
+ /// <returns>The address on the <see cref="Dsp.AudioProcessor"/> address space.</returns>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static DspAddress Map(uint processHandle, CpuAddress cpuAddress, ulong size)
+ {
+ return cpuAddress;
+ }
+
+ /// <summary>
+ /// Unmap the given <see cref="CpuAddress"/> from the <see cref="Dsp.AudioProcessor"/> address space.
+ /// </summary>
+ /// <param name="processHandle">The process owning the CPU memory.</param>
+ /// <param name="cpuAddress">The <see cref="CpuAddress"/> to unmap.</param>
+ /// <param name="size">The size of the CPU memory region to unmap.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void Unmap(uint processHandle, CpuAddress cpuAddress, ulong size)
+ {
+ }
+
+ /// <summary>
+ /// Invalidate the <see cref="Dsp.AudioProcessor"/> data cache at the given address.
+ /// </summary>
+ /// <param name="address">The base DSP address to invalidate</param>
+ /// <param name="size">The size of the DSP memory region to invalidate.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void InvalidateDspCache(DspAddress address, ulong size)
+ {
+ }
+
+ /// <summary>
+ /// Invalidate the CPU data cache at the given address.
+ /// </summary>
+ /// <param name="address">The base <see cref="CpuAddress"/> to invalidate</param>
+ /// <param name="size">The size of the CPU memory region to invalidate.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void InvalidateDataCache(CpuAddress address, ulong size)
+ {
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/BitArray.cs b/src/Ryujinx.Audio/Renderer/Utils/BitArray.cs
new file mode 100644
index 00000000..8b105477
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/BitArray.cs
@@ -0,0 +1,103 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Ryujinx.Audio.Renderer.Utils
+{
+ /// <summary>
+ /// A simple bit array implementation backed by a <see cref="Memory{T}"/>.
+ /// </summary>
+ public class BitArray
+ {
+ /// <summary>
+ /// The backing storage of the <see cref="BitArray"/>.
+ /// </summary>
+ private Memory<byte> _storage;
+
+ /// <summary>
+ /// Create a new <see cref="BitArray"/> from <see cref="Memory{T}"/>.
+ /// </summary>
+ /// <param name="storage">The backing storage of the <see cref="BitArray"/>.</param>
+ public BitArray(Memory<byte> storage)
+ {
+ _storage = storage;
+ }
+
+ /// <summary>
+ /// Get the byte position of a given bit index.
+ /// </summary>
+ /// <param name="index">A bit index.</param>
+ /// <returns>The byte position of a given bit index.</returns>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static int ToPosition(int index) => index / 8;
+
+ /// <summary>
+ /// Get the bit position of a given bit index inside a byte.
+ /// </summary>
+ /// <param name="index">A bit index.</param>
+ /// <returns>The bit position of a given bit index inside a byte.</returns>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static int ToBitPosition(int index) => index % 8;
+
+ /// <summary>
+ /// Test if the bit at the given index is set.
+ /// </summary>
+ /// <param name="index">A bit index.</param>
+ /// <returns>Return true if the bit at the given index is set</returns>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public bool Test(int index)
+ {
+ ulong mask = 1ul << ToBitPosition(index);
+
+ return (_storage.Span[ToPosition(index)] & mask) == mask;
+ }
+
+ /// <summary>
+ /// Set the bit at the given index.
+ /// </summary>
+ /// <param name="index">A bit index.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void Set(int index)
+ {
+ Set(index, true);
+ }
+
+ /// <summary>
+ /// Reset the bit at the given index.
+ /// </summary>
+ /// <param name="index">A bit index.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void Reset(int index)
+ {
+ Set(index, false);
+ }
+
+ /// <summary>
+ /// Set a bit value at the given index.
+ /// </summary>
+ /// <param name="index">A bit index.</param>
+ /// <param name="value">The new bit value.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private void Set(int index, bool value)
+ {
+ byte mask = (byte)(1 << ToBitPosition(index));
+
+ if (value)
+ {
+ _storage.Span[ToPosition(index)] |= mask;
+ }
+ else
+ {
+ _storage.Span[ToPosition(index)] &= (byte)~mask;
+ }
+ }
+
+ /// <summary>
+ /// Reset all bits in the storage.
+ /// </summary>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void Reset()
+ {
+ _storage.Span.Fill(0);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/FileHardwareDevice.cs b/src/Ryujinx.Audio/Renderer/Utils/FileHardwareDevice.cs
new file mode 100644
index 00000000..d49313ea
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/FileHardwareDevice.cs
@@ -0,0 +1,99 @@
+using Ryujinx.Audio.Integration;
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+using System.Text;
+
+namespace Ryujinx.Audio.Renderer.Utils
+{
+ /// <summary>
+ /// A <see cref="IHardwareDevice"/> that outputs to a wav file.
+ /// </summary>
+ public class FileHardwareDevice : IHardwareDevice
+ {
+ private FileStream _stream;
+ private uint _channelCount;
+ private uint _sampleRate;
+
+ private const int HeaderSize = 44;
+
+ public FileHardwareDevice(string path, uint channelCount, uint sampleRate)
+ {
+ _stream = File.OpenWrite(path);
+ _channelCount = channelCount;
+ _sampleRate = sampleRate;
+
+ _stream.Seek(HeaderSize, SeekOrigin.Begin);
+ }
+
+ private void UpdateHeader()
+ {
+ var writer = new BinaryWriter(_stream);
+
+ long currentPos = writer.Seek(0, SeekOrigin.Current);
+
+ writer.Seek(0, SeekOrigin.Begin);
+
+ writer.Write("RIFF"u8);
+ writer.Write((int)(writer.BaseStream.Length - 8));
+ writer.Write("WAVE"u8);
+ writer.Write("fmt "u8);
+ writer.Write(16);
+ writer.Write((short)1);
+ writer.Write((short)GetChannelCount());
+ writer.Write(GetSampleRate());
+ writer.Write(GetSampleRate() * GetChannelCount() * sizeof(short));
+ writer.Write((short)(GetChannelCount() * sizeof(short)));
+ writer.Write((short)(sizeof(short) * 8));
+ writer.Write("data"u8);
+ writer.Write((int)(writer.BaseStream.Length - HeaderSize));
+
+ writer.Seek((int)currentPos, SeekOrigin.Begin);
+ }
+
+ public void AppendBuffer(ReadOnlySpan<short> data, uint channelCount)
+ {
+ _stream.Write(MemoryMarshal.Cast<short, byte>(data));
+
+ UpdateHeader();
+ _stream.Flush();
+ }
+
+ public void SetVolume(float volume)
+ {
+ // Do nothing, volume is not used for FileHardwareDevice at the moment.
+ }
+
+ public float GetVolume()
+ {
+ // FileHardwareDevice does not incorporate volume.
+ return 0;
+ }
+
+ public uint GetChannelCount()
+ {
+ return _channelCount;
+ }
+
+ public uint GetSampleRate()
+ {
+ return _sampleRate;
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _stream?.Flush();
+ _stream?.Dispose();
+
+ _stream = null;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/Mailbox.cs b/src/Ryujinx.Audio/Renderer/Utils/Mailbox.cs
new file mode 100644
index 00000000..35c71ae3
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/Mailbox.cs
@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Concurrent;
+
+namespace Ryujinx.Audio.Renderer.Utils
+{
+ /// <summary>
+ /// A simple generic message queue for unmanaged types.
+ /// </summary>
+ /// <typeparam name="T">The target unmanaged type used</typeparam>
+ public class Mailbox<T> : IDisposable where T : unmanaged
+ {
+ private BlockingCollection<T> _messageQueue;
+ private BlockingCollection<T> _responseQueue;
+
+ public Mailbox()
+ {
+ _messageQueue = new BlockingCollection<T>(1);
+ _responseQueue = new BlockingCollection<T>(1);
+ }
+
+ public void SendMessage(T data)
+ {
+ _messageQueue.Add(data);
+ }
+
+ public void SendResponse(T data)
+ {
+ _responseQueue.Add(data);
+ }
+
+ public T ReceiveMessage()
+ {
+ return _messageQueue.Take();
+ }
+
+ public T ReceiveResponse()
+ {
+ return _responseQueue.Take();
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _messageQueue.Dispose();
+ _responseQueue.Dispose();
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/Math/Matrix2x2.cs b/src/Ryujinx.Audio/Renderer/Utils/Math/Matrix2x2.cs
new file mode 100644
index 00000000..5b513aff
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/Math/Matrix2x2.cs
@@ -0,0 +1,71 @@
+namespace Ryujinx.Audio.Renderer.Utils.Math
+{
+ record struct Matrix2x2
+ {
+ public float M11;
+ public float M12;
+ public float M21;
+ public float M22;
+
+ public Matrix2x2(float m11, float m12,
+ float m21, float m22)
+ {
+ M11 = m11;
+ M12 = m12;
+
+ M21 = m21;
+ M22 = m22;
+ }
+
+ public static Matrix2x2 operator +(Matrix2x2 value1, Matrix2x2 value2)
+ {
+ Matrix2x2 m;
+
+ m.M11 = value1.M11 + value2.M11;
+ m.M12 = value1.M12 + value2.M12;
+ m.M21 = value1.M21 + value2.M21;
+ m.M22 = value1.M22 + value2.M22;
+
+ return m;
+ }
+
+ public static Matrix2x2 operator -(Matrix2x2 value1, float value2)
+ {
+ Matrix2x2 m;
+
+ m.M11 = value1.M11 - value2;
+ m.M12 = value1.M12 - value2;
+ m.M21 = value1.M21 - value2;
+ m.M22 = value1.M22 - value2;
+
+ return m;
+ }
+
+ public static Matrix2x2 operator *(Matrix2x2 value1, float value2)
+ {
+ Matrix2x2 m;
+
+ m.M11 = value1.M11 * value2;
+ m.M12 = value1.M12 * value2;
+ m.M21 = value1.M21 * value2;
+ m.M22 = value1.M22 * value2;
+
+ return m;
+ }
+
+ public static Matrix2x2 operator *(Matrix2x2 value1, Matrix2x2 value2)
+ {
+ Matrix2x2 m;
+
+ // First row
+ m.M11 = value1.M11 * value2.M11 + value1.M12 * value2.M21;
+ m.M12 = value1.M11 * value2.M12 + value1.M12 * value2.M22;
+
+ // Second row
+ m.M21 = value1.M21 * value2.M11 + value1.M22 * value2.M21;
+ m.M22 = value1.M21 * value2.M12 + value1.M22 * value2.M22;
+
+ return m;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/Math/Matrix6x6.cs b/src/Ryujinx.Audio/Renderer/Utils/Math/Matrix6x6.cs
new file mode 100644
index 00000000..415a81fd
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/Math/Matrix6x6.cs
@@ -0,0 +1,97 @@
+namespace Ryujinx.Audio.Renderer.Utils.Math
+{
+ record struct Matrix6x6
+ {
+ public float M11;
+ public float M12;
+ public float M13;
+ public float M14;
+ public float M15;
+ public float M16;
+
+ public float M21;
+ public float M22;
+ public float M23;
+ public float M24;
+ public float M25;
+ public float M26;
+
+ public float M31;
+ public float M32;
+ public float M33;
+ public float M34;
+ public float M35;
+ public float M36;
+
+ public float M41;
+ public float M42;
+ public float M43;
+ public float M44;
+ public float M45;
+ public float M46;
+
+ public float M51;
+ public float M52;
+ public float M53;
+ public float M54;
+ public float M55;
+ public float M56;
+
+ public float M61;
+ public float M62;
+ public float M63;
+ public float M64;
+ public float M65;
+ public float M66;
+
+ public Matrix6x6(float m11, float m12, float m13, float m14, float m15, float m16,
+ float m21, float m22, float m23, float m24, float m25, float m26,
+ float m31, float m32, float m33, float m34, float m35, float m36,
+ float m41, float m42, float m43, float m44, float m45, float m46,
+ float m51, float m52, float m53, float m54, float m55, float m56,
+ float m61, float m62, float m63, float m64, float m65, float m66)
+ {
+ M11 = m11;
+ M12 = m12;
+ M13 = m13;
+ M14 = m14;
+ M15 = m15;
+ M16 = m16;
+
+ M21 = m21;
+ M22 = m22;
+ M23 = m23;
+ M24 = m24;
+ M25 = m25;
+ M26 = m26;
+
+ M31 = m31;
+ M32 = m32;
+ M33 = m33;
+ M34 = m34;
+ M35 = m35;
+ M36 = m36;
+
+ M41 = m41;
+ M42 = m42;
+ M43 = m43;
+ M44 = m44;
+ M45 = m45;
+ M46 = m46;
+
+ M51 = m51;
+ M52 = m52;
+ M53 = m53;
+ M54 = m54;
+ M55 = m55;
+ M56 = m56;
+
+ M61 = m61;
+ M62 = m62;
+ M63 = m63;
+ M64 = m64;
+ M65 = m65;
+ M66 = m66;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/Math/MatrixHelper.cs b/src/Ryujinx.Audio/Renderer/Utils/Math/MatrixHelper.cs
new file mode 100644
index 00000000..209a81c4
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/Math/MatrixHelper.cs
@@ -0,0 +1,45 @@
+using Ryujinx.Audio.Renderer.Utils.Math;
+using System.Numerics;
+using System.Runtime.CompilerServices;
+
+namespace Ryujinx.Audio.Renderer.Dsp
+{
+ static class MatrixHelper
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vector6 Transform(ref Vector6 value1, ref Matrix6x6 value2)
+ {
+ return new Vector6
+ {
+ X = value2.M11 * value1.X + value2.M12 * value1.Y + value2.M13 * value1.Z + value2.M14 * value1.W + value2.M15 * value1.V + value2.M16 * value1.U,
+ Y = value2.M21 * value1.X + value2.M22 * value1.Y + value2.M23 * value1.Z + value2.M24 * value1.W + value2.M25 * value1.V + value2.M26 * value1.U,
+ Z = value2.M31 * value1.X + value2.M32 * value1.Y + value2.M33 * value1.Z + value2.M34 * value1.W + value2.M35 * value1.V + value2.M36 * value1.U,
+ W = value2.M41 * value1.X + value2.M42 * value1.Y + value2.M43 * value1.Z + value2.M44 * value1.W + value2.M45 * value1.V + value2.M46 * value1.U,
+ V = value2.M51 * value1.X + value2.M52 * value1.Y + value2.M53 * value1.Z + value2.M54 * value1.W + value2.M55 * value1.V + value2.M56 * value1.U,
+ U = value2.M61 * value1.X + value2.M62 * value1.Y + value2.M63 * value1.Z + value2.M64 * value1.W + value2.M65 * value1.V + value2.M66 * value1.U,
+ };
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vector4 Transform(ref Vector4 value1, ref Matrix4x4 value2)
+ {
+ return new Vector4
+ {
+ X = value2.M11 * value1.X + value2.M12 * value1.Y + value2.M13 * value1.Z + value2.M14 * value1.W,
+ Y = value2.M21 * value1.X + value2.M22 * value1.Y + value2.M23 * value1.Z + value2.M24 * value1.W,
+ Z = value2.M31 * value1.X + value2.M32 * value1.Y + value2.M33 * value1.Z + value2.M34 * value1.W,
+ W = value2.M41 * value1.X + value2.M42 * value1.Y + value2.M43 * value1.Z + value2.M44 * value1.W
+ };
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vector2 Transform(ref Vector2 value1, ref Matrix2x2 value2)
+ {
+ return new Vector2
+ {
+ X = value2.M11 * value1.X + value2.M12 * value1.Y,
+ Y = value2.M21 * value1.X + value2.M22 * value1.Y,
+ };
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/Math/Vector6.cs b/src/Ryujinx.Audio/Renderer/Utils/Math/Vector6.cs
new file mode 100644
index 00000000..81bcb698
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/Math/Vector6.cs
@@ -0,0 +1,56 @@
+using System.Runtime.CompilerServices;
+
+namespace Ryujinx.Audio.Renderer.Utils.Math
+{
+ record struct Vector6
+ {
+ public float X;
+ public float Y;
+ public float Z;
+ public float W;
+ public float V;
+ public float U;
+
+ public Vector6(float value) : this(value, value, value, value, value, value)
+ {
+ }
+
+ public Vector6(float x, float y, float z, float w, float v, float u)
+ {
+ X = x;
+ Y = y;
+ Z = z;
+ W = w;
+ V = v;
+ U = u;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vector6 operator +(Vector6 left, Vector6 right)
+ {
+ return new Vector6(left.X + right.X,
+ left.Y + right.Y,
+ left.Z + right.Z,
+ left.W + right.W,
+ left.V + right.V,
+ left.U + right.U);
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vector6 operator *(Vector6 left, Vector6 right)
+ {
+ return new Vector6(left.X * right.X,
+ left.Y * right.Y,
+ left.Z * right.Z,
+ left.W * right.W,
+ left.V * right.V,
+ left.U * right.U);
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Vector6 operator *(Vector6 left, float right)
+ {
+ return left * new Vector6(right);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/SpanIOHelper.cs b/src/Ryujinx.Audio/Renderer/Utils/SpanIOHelper.cs
new file mode 100644
index 00000000..103fb6a0
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/SpanIOHelper.cs
@@ -0,0 +1,171 @@
+using System;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Audio.Renderer.Utils
+{
+ /// <summary>
+ /// Helper for IO operations on <see cref="Span{T}"/> and <see cref="Memory{T}"/>.
+ /// </summary>
+ public static class SpanIOHelper
+ {
+ /// <summary>
+ /// Write the given data to the given backing <see cref="Memory{T}"/> and move cursor after the written data.
+ /// </summary>
+ /// <typeparam name="T">The data type.</typeparam>
+ /// <param name="backingMemory">The backing <see cref="Memory{T}"/> to store the data.</param>
+ /// <param name="data">The data to write to the backing <see cref="Memory{T}"/>.</param>
+ public static void Write<T>(ref Memory<byte> backingMemory, ref T data) where T : unmanaged
+ {
+ int size = Unsafe.SizeOf<T>();
+
+ if (size > backingMemory.Length)
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ MemoryMarshal.Write<T>(backingMemory.Span.Slice(0, size), ref data);
+
+ backingMemory = backingMemory.Slice(size);
+ }
+
+ /// <summary>
+ /// Write the given data to the given backing <see cref="Span{T}"/> and move cursor after the written data.
+ /// </summary>
+ /// <typeparam name="T">The data type.</typeparam>
+ /// <param name="backingMemory">The backing <see cref="Span{T}"/> to store the data.</param>
+ /// <param name="data">The data to write to the backing <see cref="Span{T}"/>.</param>
+ public static void Write<T>(ref Span<byte> backingMemory, ref T data) where T : unmanaged
+ {
+ int size = Unsafe.SizeOf<T>();
+
+ if (size > backingMemory.Length)
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ MemoryMarshal.Write<T>(backingMemory.Slice(0, size), ref data);
+
+ backingMemory = backingMemory.Slice(size);
+ }
+
+ /// <summary>
+ /// Get a <see cref="Span{T}"/> out of a <paramref name="backingMemory"/> and move cursor after T size.
+ /// </summary>
+ /// <typeparam name="T">The data type.</typeparam>
+ /// <param name="backingMemory">The backing <see cref="Memory{T}"/> to get a <see cref="Span{T}"/> from.</param>
+ /// <returns>A <see cref="Span{T}"/> from backing <see cref="Memory{T}"/>.</returns>
+ public static Span<T> GetWriteRef<T>(ref Memory<byte> backingMemory) where T : unmanaged
+ {
+ int size = Unsafe.SizeOf<T>();
+
+ if (size > backingMemory.Length)
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ Span<T> result = MemoryMarshal.Cast<byte, T>(backingMemory.Span.Slice(0, size));
+
+ backingMemory = backingMemory.Slice(size);
+
+ return result;
+ }
+
+ /// <summary>
+ /// Get a <see cref="Span{T}"/> out of a backingMemory and move cursor after T size.
+ /// </summary>
+ /// <typeparam name="T">The data type.</typeparam>
+ /// <param name="backingMemory">The backing <see cref="Span{T}"/> to get a <see cref="Span{T}"/> from.</param>
+ /// <returns>A <see cref="Span{T}"/> from backing <see cref="Span{T}"/>.</returns>
+ public static Span<T> GetWriteRef<T>(ref Span<byte> backingMemory) where T : unmanaged
+ {
+ int size = Unsafe.SizeOf<T>();
+
+ if (size > backingMemory.Length)
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ Span<T> result = MemoryMarshal.Cast<byte, T>(backingMemory.Slice(0, size));
+
+ backingMemory = backingMemory.Slice(size);
+
+ return result;
+ }
+
+ /// <summary>
+ /// Read data from the given backing <see cref="ReadOnlyMemory{T}"/> and move cursor after the read data.
+ /// </summary>
+ /// <typeparam name="T">The data type.</typeparam>
+ /// <param name="backingMemory">The backing <see cref="ReadOnlyMemory{T}"/> to read data from.</param>
+ /// <returns>Return the read data.</returns>
+ public static T Read<T>(ref ReadOnlyMemory<byte> backingMemory) where T : unmanaged
+ {
+ int size = Unsafe.SizeOf<T>();
+
+ if (size > backingMemory.Length)
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ T result = MemoryMarshal.Read<T>(backingMemory.Span.Slice(0, size));
+
+ backingMemory = backingMemory.Slice(size);
+
+ return result;
+ }
+
+ /// <summary>
+ /// Read data from the given backing <see cref="ReadOnlySpan{T}"/> and move cursor after the read data.
+ /// </summary>
+ /// <typeparam name="T">The data type.</typeparam>
+ /// <param name="backingMemory">The backing <see cref="ReadOnlySpan{T}"/> to read data from.</param>
+ /// <returns>Return the read data.</returns>
+ public static T Read<T>(ref ReadOnlySpan<byte> backingMemory) where T : unmanaged
+ {
+ int size = Unsafe.SizeOf<T>();
+
+ if (size > backingMemory.Length)
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ T result = MemoryMarshal.Read<T>(backingMemory.Slice(0, size));
+
+ backingMemory = backingMemory.Slice(size);
+
+ return result;
+ }
+
+ /// <summary>
+ /// Extract a <see cref="Memory{T}"/> at the given index.
+ /// </summary>
+ /// <typeparam name="T">The data type.</typeparam>
+ /// <param name="memory">The <see cref="Memory{T}"/> to extract the data from.</param>
+ /// <param name="id">The id in the provided memory.</param>
+ /// <param name="count">The max allowed count. (for bound checking of the id in debug mode)</param>
+ /// <returns>a <see cref="Memory{T}"/> at the given id.</returns>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Memory<T> GetMemory<T>(Memory<T> memory, int id, uint count) where T : unmanaged
+ {
+ Debug.Assert(id >= 0 && id < count);
+
+ return memory.Slice(id, 1);
+ }
+
+ /// <summary>
+ /// Extract a ref T at the given index.
+ /// </summary>
+ /// <typeparam name="T">The data type.</typeparam>
+ /// <param name="memory">The <see cref="Memory{T}"/> to extract the data from.</param>
+ /// <param name="id">The id in the provided memory.</param>
+ /// <param name="count">The max allowed count. (for bound checking of the id in debug mode)</param>
+ /// <returns>a ref T at the given id.</returns>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ref T GetFromMemory<T>(Memory<T> memory, int id, uint count) where T : unmanaged
+ {
+ return ref GetMemory(memory, id, count).Span[0];
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/SpanMemoryManager.cs b/src/Ryujinx.Audio/Renderer/Utils/SpanMemoryManager.cs
new file mode 100644
index 00000000..2c48da6a
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/SpanMemoryManager.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Buffers;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Audio.Renderer.Utils
+{
+ public sealed unsafe class SpanMemoryManager<T> : MemoryManager<T>
+ where T : unmanaged
+ {
+ private readonly T* _pointer;
+ private readonly int _length;
+
+ public SpanMemoryManager(Span<T> span)
+ {
+ fixed (T* ptr = &MemoryMarshal.GetReference(span))
+ {
+ _pointer = ptr;
+ _length = span.Length;
+ }
+ }
+
+ public override Span<T> GetSpan() => new Span<T>(_pointer, _length);
+
+ public override MemoryHandle Pin(int elementIndex = 0)
+ {
+ if (elementIndex < 0 || elementIndex >= _length)
+ {
+ throw new ArgumentOutOfRangeException(nameof(elementIndex));
+ }
+
+ return new MemoryHandle(_pointer + elementIndex);
+ }
+
+ public override void Unpin() { }
+
+ protected override void Dispose(bool disposing) { }
+
+ public static Memory<T> Cast<TFrom>(Memory<TFrom> memory) where TFrom : unmanaged
+ {
+ return new SpanMemoryManager<T>(MemoryMarshal.Cast<TFrom, T>(memory.Span)).Memory;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Audio/Renderer/Utils/SplitterHardwareDevice.cs b/src/Ryujinx.Audio/Renderer/Utils/SplitterHardwareDevice.cs
new file mode 100644
index 00000000..18396078
--- /dev/null
+++ b/src/Ryujinx.Audio/Renderer/Utils/SplitterHardwareDevice.cs
@@ -0,0 +1,58 @@
+using Ryujinx.Audio.Integration;
+using System;
+
+namespace Ryujinx.Audio.Renderer.Utils
+{
+ public class SplitterHardwareDevice : IHardwareDevice
+ {
+ private IHardwareDevice _baseDevice;
+ private IHardwareDevice _secondaryDevice;
+
+ public SplitterHardwareDevice(IHardwareDevice baseDevice, IHardwareDevice secondaryDevice)
+ {
+ _baseDevice = baseDevice;
+ _secondaryDevice = secondaryDevice;
+ }
+
+ public void AppendBuffer(ReadOnlySpan<short> data, uint channelCount)
+ {
+ _baseDevice.AppendBuffer(data, channelCount);
+ _secondaryDevice?.AppendBuffer(data, channelCount);
+ }
+
+ public void SetVolume(float volume)
+ {
+ _baseDevice.SetVolume(volume);
+ _secondaryDevice.SetVolume(volume);
+ }
+
+ public float GetVolume()
+ {
+ return _baseDevice.GetVolume();
+ }
+
+ public uint GetChannelCount()
+ {
+ return _baseDevice.GetChannelCount();
+ }
+
+ public uint GetSampleRate()
+ {
+ return _baseDevice.GetSampleRate();
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _baseDevice.Dispose();
+ _secondaryDevice?.Dispose();
+ }
+ }
+ }
+} \ No newline at end of file