mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2024-11-27 21:02:02 +00:00
8275bc3c08
* Audio: Implement libsoundio as an alternative audio backend libsoundio will be preferred over OpenAL if it is available on the machine. If neither are available, it will fallback to a dummy audio renderer that outputs no sound. * Audio: Fix SoundIoRingBuffer documentation * Audio: Unroll and optimize the audio write callback Copying one sample at a time is slow, this unrolls the most common audio channel layouts and manually copies the bytes between source and destination. This is over 2x faster than calling CopyBlockUnaligned every sample. * Audio: Optimize the write callback further This dramatically reduces the audio buffer copy time. When the sample size is one of handled sample sizes the buffer copy operation is almost 10x faster than CopyBlockAligned. This works by copying full samples at a time, rather than the individual bytes that make up the sample. This allows for 2x or 4x faster copy operations depending on sample size. * Audio: Fix typo in Stereo write callback * Audio: Fix Surround (5.1) audio write callback * Audio: Update Documentation * Audio: Use built-in Unsafe.SizeOf<T>() Built-in `SizeOf<T>()` is 10x faster than our `TypeSize<T>` helper. This also helps reduce code surface area. * Audio: Keep fixed buffer style consistent * Audio: Address styling nits * Audio: More style nits * Audio: Add additional documentation * Audio: Move libsoundio bindings internal As per discussion, moving the libsoundio native bindings into Ryujinx.Audio * Audio: Bump Target Framework back up to .NET Core 2.1 * Audio: Remove voice mixing optimizations. Leaves Saturation optimizations in place.
204 lines
6.5 KiB
C#
204 lines
6.5 KiB
C#
using System;
|
|
|
|
namespace Ryujinx.Audio.SoundIo
|
|
{
|
|
/// <summary>
|
|
/// A thread-safe variable-size circular buffer
|
|
/// </summary>
|
|
internal class SoundIoRingBuffer
|
|
{
|
|
private byte[] m_Buffer;
|
|
private int m_Size;
|
|
private int m_HeadOffset;
|
|
private int m_TailOffset;
|
|
|
|
/// <summary>
|
|
/// Gets the available bytes in the ring buffer
|
|
/// </summary>
|
|
public int Length
|
|
{
|
|
get { return m_Size; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a new instance of a <see cref="SoundIoRingBuffer"/>
|
|
/// </summary>
|
|
public SoundIoRingBuffer()
|
|
{
|
|
m_Buffer = new byte[2048];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a new instance of a <see cref="SoundIoRingBuffer"/> with the specified capacity
|
|
/// </summary>
|
|
/// <param name="capacity">The number of entries that the <see cref="SoundIoRingBuffer"/> can initially contain</param>
|
|
public SoundIoRingBuffer(int capacity)
|
|
{
|
|
m_Buffer = new byte[capacity];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears the ring buffer
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
m_Size = 0;
|
|
m_HeadOffset = 0;
|
|
m_TailOffset = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears the specified amount of bytes from the ring buffer
|
|
/// </summary>
|
|
/// <param name="size">The amount of bytes to clear from the ring buffer</param>
|
|
public void Clear(int size)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (size > m_Size)
|
|
{
|
|
size = m_Size;
|
|
}
|
|
|
|
if (size == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_HeadOffset = (m_HeadOffset + size) % m_Buffer.Length;
|
|
m_Size -= size;
|
|
|
|
if (m_Size == 0)
|
|
{
|
|
m_HeadOffset = 0;
|
|
m_TailOffset = 0;
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extends the capacity of the ring buffer
|
|
/// </summary>
|
|
private void SetCapacity(int capacity)
|
|
{
|
|
byte[] buffer = new byte[capacity];
|
|
|
|
if (m_Size > 0)
|
|
{
|
|
if (m_HeadOffset < m_TailOffset)
|
|
{
|
|
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, 0, m_Size);
|
|
}
|
|
else
|
|
{
|
|
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, 0, m_Buffer.Length - m_HeadOffset);
|
|
Buffer.BlockCopy(m_Buffer, 0, buffer, m_Buffer.Length - m_HeadOffset, m_TailOffset);
|
|
}
|
|
}
|
|
|
|
m_Buffer = buffer;
|
|
m_HeadOffset = 0;
|
|
m_TailOffset = m_Size;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Writes a sequence of bytes to the ring buffer
|
|
/// </summary>
|
|
/// <param name="buffer">A byte array containing the data to write</param>
|
|
/// <param name="index">The zero-based byte offset in <paramref name="buffer" /> from which to begin copying bytes to the ring buffer</param>
|
|
/// <param name="count">The number of bytes to write</param>
|
|
public void Write<T>(T[] buffer, int index, int count)
|
|
{
|
|
if (count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
lock (this)
|
|
{
|
|
if ((m_Size + count) > m_Buffer.Length)
|
|
{
|
|
SetCapacity((m_Size + count + 2047) & ~2047);
|
|
}
|
|
|
|
if (m_HeadOffset < m_TailOffset)
|
|
{
|
|
int tailLength = m_Buffer.Length - m_TailOffset;
|
|
|
|
if (tailLength >= count)
|
|
{
|
|
Buffer.BlockCopy(buffer, index, m_Buffer, m_TailOffset, count);
|
|
}
|
|
else
|
|
{
|
|
Buffer.BlockCopy(buffer, index, m_Buffer, m_TailOffset, tailLength);
|
|
Buffer.BlockCopy(buffer, index + tailLength, m_Buffer, 0, count - tailLength);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Buffer.BlockCopy(buffer, index, m_Buffer, m_TailOffset, count);
|
|
}
|
|
|
|
m_Size += count;
|
|
m_TailOffset = (m_TailOffset + count) % m_Buffer.Length;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads a sequence of bytes from the ring buffer and advances the position within the ring buffer by the number of bytes read
|
|
/// </summary>
|
|
/// <param name="buffer">The buffer to write the data into</param>
|
|
/// <param name="index">The zero-based byte offset in <paramref name="buffer" /> at which the read bytes will be placed</param>
|
|
/// <param name="count">The maximum number of bytes to read</param>
|
|
/// <returns>The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the ring buffer is empty</returns>
|
|
public int Read<T>(T[] buffer, int index, int count)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (count > m_Size)
|
|
{
|
|
count = m_Size;
|
|
}
|
|
|
|
if (count == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (m_HeadOffset < m_TailOffset)
|
|
{
|
|
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, index, count);
|
|
}
|
|
else
|
|
{
|
|
int tailLength = m_Buffer.Length - m_HeadOffset;
|
|
|
|
if (tailLength >= count)
|
|
{
|
|
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, index, count);
|
|
}
|
|
else
|
|
{
|
|
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, index, tailLength);
|
|
Buffer.BlockCopy(m_Buffer, 0, buffer, index + tailLength, count - tailLength);
|
|
}
|
|
}
|
|
|
|
m_Size -= count;
|
|
m_HeadOffset = (m_HeadOffset + count) % m_Buffer.Length;
|
|
|
|
if (m_Size == 0)
|
|
{
|
|
m_HeadOffset = 0;
|
|
m_TailOffset = 0;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
}
|
|
}
|
|
}
|