From f556c80d0230056335632b60c71f1567e177239e Mon Sep 17 00:00:00 2001 From: Mary Date: Fri, 26 Feb 2021 01:11:56 +0100 Subject: Haydn: Part 1 (#2007) * Haydn: Part 1 Based on my reverse of audio 11.0.0. As always, core implementation under LGPLv3 for the same reasons as for Amadeus. This place the bases of a more flexible audio system while making audout & audin accurate. This have the following improvements: - Complete reimplementation of audout and audin. - Audin currently only have a dummy backend. - Dramatically reduce CPU usage by up to 50% in common cases (SoundIO and OpenAL). - Audio Renderer now can output to 5.1 devices when supported. - Audio Renderer init its backend on demand instead of keeping two up all the time. - All backends implementation are now in their own project. - Ryujinx.Audio.Renderer was renamed Ryujinx.Audio and was refactored because of this. As a note, games having issues with OpenAL haven't improved and will not because of OpenAL design (stopping when buffers finish playing causing possible audio "pops" when buffers are very small). * Update for latest hexkyz's edits on Switchbrew * audren: Rollback channel configuration changes * Address gdkchan's comments * Fix typo in OpenAL backend driver * Address last comments * Fix a nit * Address gdkchan's comments --- .../Server/Splitter/SplitterContext.cs | 320 --------------------- .../Server/Splitter/SplitterDestination.cs | 210 -------------- .../Server/Splitter/SplitterState.cs | 237 --------------- 3 files changed, 767 deletions(-) delete mode 100644 Ryujinx.Audio.Renderer/Server/Splitter/SplitterContext.cs delete mode 100644 Ryujinx.Audio.Renderer/Server/Splitter/SplitterDestination.cs delete mode 100644 Ryujinx.Audio.Renderer/Server/Splitter/SplitterState.cs (limited to 'Ryujinx.Audio.Renderer/Server/Splitter') diff --git a/Ryujinx.Audio.Renderer/Server/Splitter/SplitterContext.cs b/Ryujinx.Audio.Renderer/Server/Splitter/SplitterContext.cs deleted file mode 100644 index 0e107a53..00000000 --- a/Ryujinx.Audio.Renderer/Server/Splitter/SplitterContext.cs +++ /dev/null @@ -1,320 +0,0 @@ -// -// Copyright (c) 2019-2021 Ryujinx -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with this program. If not, see . -// - -using Ryujinx.Audio.Renderer.Common; -using Ryujinx.Audio.Renderer.Parameter; -using Ryujinx.Audio.Renderer.Utils; -using Ryujinx.Common; -using System; -using System.Diagnostics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace Ryujinx.Audio.Renderer.Server.Splitter -{ - /// - /// Splitter context. - /// - public class SplitterContext - { - /// - /// Storage for . - /// - private Memory _splitters; - - /// - /// Storage for . - /// - private Memory _splitterDestinations; - - /// - /// If set to true, trust the user destination count in . - /// - public bool IsBugFixed { get; private set; } - - /// - /// Initialize . - /// - /// The behaviour context. - /// The audio renderer configuration. - /// The . - /// Return true if the initialization was successful. - public bool Initialize(ref BehaviourContext behaviourContext, ref AudioRendererConfiguration parameter, WorkBufferAllocator workBufferAllocator) - { - if (!behaviourContext.IsSplitterSupported() || parameter.SplitterCount <= 0 || parameter.SplitterDestinationCount <= 0) - { - Setup(Memory.Empty, Memory.Empty, false); - - return true; - } - - Memory splitters = workBufferAllocator.Allocate(parameter.SplitterCount, SplitterState.Alignment); - - if (splitters.IsEmpty) - { - return false; - } - - int splitterId = 0; - - foreach (ref SplitterState splitter in splitters.Span) - { - splitter = new SplitterState(splitterId++); - } - - Memory splitterDestinations = workBufferAllocator.Allocate(parameter.SplitterDestinationCount, - SplitterDestination.Alignment); - - if (splitterDestinations.IsEmpty) - { - return false; - } - - int splitterDestinationId = 0; - foreach (ref SplitterDestination data in splitterDestinations.Span) - { - data = new SplitterDestination(splitterDestinationId++); - } - - SplitterState.InitializeSplitters(splitters.Span); - - Setup(splitters, splitterDestinations, behaviourContext.IsSplitterBugFixed()); - - return true; - } - - /// - /// Get the work buffer size while adding the size needed for splitter to operate. - /// - /// The current size. - /// The behaviour context. - /// The renderer configuration. - /// Return the new size taking splitter into account. - public static ulong GetWorkBufferSize(ulong size, ref BehaviourContext behaviourContext, ref AudioRendererConfiguration parameter) - { - if (behaviourContext.IsSplitterSupported()) - { - size = WorkBufferAllocator.GetTargetSize(size, parameter.SplitterCount, SplitterState.Alignment); - size = WorkBufferAllocator.GetTargetSize(size, parameter.SplitterDestinationCount, SplitterDestination.Alignment); - - if (behaviourContext.IsSplitterBugFixed()) - { - size = WorkBufferAllocator.GetTargetSize(size, parameter.SplitterDestinationCount, 0x10); - } - - return size; - } - else - { - return size; - } - } - - /// - /// Setup the instance. - /// - /// The storage. - /// The storage. - /// If set to true, trust the user destination count in . - private void Setup(Memory splitters, Memory splitterDestinations, bool isBugFixed) - { - _splitters = splitters; - _splitterDestinations = splitterDestinations; - IsBugFixed = isBugFixed; - } - - /// - /// Clear the new connection flag. - /// - private void ClearAllNewConnectionFlag() - { - foreach (ref SplitterState splitter in _splitters.Span) - { - splitter.ClearNewConnectionFlag(); - } - } - - /// - /// Get the destination count using the count of splitter. - /// - /// The destination count using the count of splitter. - public int GetDestinationCountPerStateForCompatibility() - { - if (_splitters.IsEmpty) - { - return 0; - } - - return _splitterDestinations.Length / _splitters.Length; - } - - /// - /// Update one or multiple from user parameters. - /// - /// The splitter header. - /// The raw data after the splitter header. - private void UpdateState(ref SplitterInParameterHeader inputHeader, ref ReadOnlySpan input) - { - for (int i = 0; i < inputHeader.SplitterCount; i++) - { - SplitterInParameter parameter = MemoryMarshal.Read(input); - - Debug.Assert(parameter.IsMagicValid()); - - if (parameter.IsMagicValid()) - { - if (parameter.Id >= 0 && parameter.Id < _splitters.Length) - { - ref SplitterState splitter = ref GetState(parameter.Id); - - splitter.Update(this, ref parameter, input.Slice(Unsafe.SizeOf())); - } - - input = input.Slice(0x1C + (int)parameter.DestinationCount * 4); - } - } - } - - /// - /// Update one or multiple from user parameters. - /// - /// The splitter header. - /// The raw data after the splitter header. - private void UpdateData(ref SplitterInParameterHeader inputHeader, ref ReadOnlySpan input) - { - for (int i = 0; i < inputHeader.SplitterDestinationCount; i++) - { - SplitterDestinationInParameter parameter = MemoryMarshal.Read(input); - - Debug.Assert(parameter.IsMagicValid()); - - if (parameter.IsMagicValid()) - { - if (parameter.Id >= 0 && parameter.Id < _splitterDestinations.Length) - { - ref SplitterDestination destination = ref GetDestination(parameter.Id); - - destination.Update(parameter); - } - - input = input.Slice(Unsafe.SizeOf()); - } - } - } - - /// - /// Update splitter from user parameters. - /// - /// The input raw user data. - /// The total consumed size. - /// Return true if the update was successful. - public bool Update(ReadOnlySpan input, out int consumedSize) - { - if (_splitterDestinations.IsEmpty || _splitters.IsEmpty) - { - consumedSize = 0; - - return true; - } - - int originalSize = input.Length; - - SplitterInParameterHeader header = SpanIOHelper.Read(ref input); - - if (header.IsMagicValid()) - { - ClearAllNewConnectionFlag(); - - UpdateState(ref header, ref input); - UpdateData(ref header, ref input); - - consumedSize = BitUtils.AlignUp(originalSize - input.Length, 0x10); - - return true; - } - else - { - consumedSize = 0; - - return false; - } - } - - /// - /// Get a reference to a at the given . - /// - /// The index to use. - /// A reference to a at the given . - public ref SplitterState GetState(int id) - { - return ref SpanIOHelper.GetFromMemory(_splitters, id, (uint)_splitters.Length); - } - - /// - /// Get a reference to a at the given . - /// - /// The index to use. - /// A reference to a at the given . - public ref SplitterDestination GetDestination(int id) - { - return ref SpanIOHelper.GetFromMemory(_splitterDestinations, id, (uint)_splitterDestinations.Length); - } - - /// - /// Get a at the given . - /// - /// The index to use. - /// A at the given . - public Memory GetDestinationMemory(int id) - { - return SpanIOHelper.GetMemory(_splitterDestinations, id, (uint)_splitterDestinations.Length); - } - - /// - /// Get a in the at and pass to . - /// - /// The index to use to get the . - /// The index of the . - /// A . - public Span GetDestination(int id, int destinationId) - { - ref SplitterState splitter = ref GetState(id); - - return splitter.GetData(destinationId); - } - - /// - /// Return true if the audio renderer has any splitters. - /// - /// True if the audio renderer has any splitters. - public bool UsingSplitter() - { - return !_splitters.IsEmpty && !_splitterDestinations.IsEmpty; - } - - /// - /// Update the internal state of all splitters. - /// - public void UpdateInternalState() - { - foreach (ref SplitterState splitter in _splitters.Span) - { - splitter.UpdateInternalState(); - } - } - } -} diff --git a/Ryujinx.Audio.Renderer/Server/Splitter/SplitterDestination.cs b/Ryujinx.Audio.Renderer/Server/Splitter/SplitterDestination.cs deleted file mode 100644 index 6d273e45..00000000 --- a/Ryujinx.Audio.Renderer/Server/Splitter/SplitterDestination.cs +++ /dev/null @@ -1,210 +0,0 @@ -// -// Copyright (c) 2019-2021 Ryujinx -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with this program. If not, see . -// - -using Ryujinx.Audio.Renderer.Parameter; -using Ryujinx.Common.Utilities; -using System; -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace Ryujinx.Audio.Renderer.Server.Splitter -{ - /// - /// Server state for a splitter destination. - /// - [StructLayout(LayoutKind.Sequential, Size = 0xE0, Pack = Alignment)] - public struct SplitterDestination - { - public const int Alignment = 0x10; - - /// - /// The unique id of this . - /// - public int Id; - - /// - /// The mix to output the result of the splitter. - /// - public int DestinationId; - - /// - /// Mix buffer volumes storage. - /// - private MixArray _mix; - private MixArray _previousMix; - - /// - /// Pointer to the next linked element. - /// - private unsafe SplitterDestination* _next; - - /// - /// Set to true if in use. - /// - [MarshalAs(UnmanagedType.I1)] - public bool IsUsed; - - /// - /// Set to true if the internal state need to be updated. - /// - [MarshalAs(UnmanagedType.I1)] - public bool NeedToUpdateInternalState; - - [StructLayout(LayoutKind.Sequential, Size = 4 * RendererConstants.MixBufferCountMax, Pack = 1)] - private struct MixArray { } - - /// - /// Mix buffer volumes. - /// - /// Used when a splitter id is specified in the mix. - public Span MixBufferVolume => SpanHelpers.AsSpan(ref _mix); - - /// - /// Previous mix buffer volumes. - /// - /// Used when a splitter id is specified in the mix. - public Span PreviousMixBufferVolume => SpanHelpers.AsSpan(ref _previousMix); - - /// - /// Get the of the next element or if not present. - /// - public Span Next - { - get - { - unsafe - { - return _next != null ? new Span(_next, 1) : Span.Empty; - } - } - } - - /// - /// Create a new . - /// - /// The unique id of this . - public SplitterDestination(int id) : this() - { - Id = id; - DestinationId = RendererConstants.UnusedMixId; - - ClearVolumes(); - } - - /// - /// Update the from user parameter. - /// - /// The user parameter. - public void Update(SplitterDestinationInParameter parameter) - { - Debug.Assert(Id == parameter.Id); - - if (parameter.IsMagicValid() && Id == parameter.Id) - { - DestinationId = parameter.DestinationId; - - parameter.MixBufferVolume.CopyTo(MixBufferVolume); - - if (!IsUsed && parameter.IsUsed) - { - MixBufferVolume.CopyTo(PreviousMixBufferVolume); - - NeedToUpdateInternalState = false; - } - - IsUsed = parameter.IsUsed; - } - } - - /// - /// Update the internal state of the instance. - /// - public void UpdateInternalState() - { - if (IsUsed && NeedToUpdateInternalState) - { - MixBufferVolume.CopyTo(PreviousMixBufferVolume); - } - - NeedToUpdateInternalState = false; - } - - /// - /// Set the update internal state marker. - /// - public void MarkAsNeedToUpdateInternalState() - { - NeedToUpdateInternalState = true; - } - - /// - /// Return true if the is used and has a destination. - /// - /// True if the is used and has a destination. - public bool IsConfigured() - { - return IsUsed && DestinationId != RendererConstants.UnusedMixId; - } - - /// - /// Get the volume for a given destination. - /// - /// The destination index to use. - /// The volume for the given destination. - public float GetMixVolume(int destinationIndex) - { - Debug.Assert(destinationIndex >= 0 && destinationIndex < RendererConstants.MixBufferCountMax); - - return MixBufferVolume[destinationIndex]; - } - - /// - /// Clear the volumes. - /// - public void ClearVolumes() - { - MixBufferVolume.Fill(0); - PreviousMixBufferVolume.Fill(0); - } - - /// - /// Link the next element to the given . - /// - /// The given to link. - public void Link(ref SplitterDestination next) - { - unsafe - { - fixed (SplitterDestination *nextPtr = &next) - { - _next = nextPtr; - } - } - } - - /// - /// Remove the link to the next element. - /// - public void Unlink() - { - unsafe - { - _next = null; - } - } - } -} diff --git a/Ryujinx.Audio.Renderer/Server/Splitter/SplitterState.cs b/Ryujinx.Audio.Renderer/Server/Splitter/SplitterState.cs deleted file mode 100644 index c928ec53..00000000 --- a/Ryujinx.Audio.Renderer/Server/Splitter/SplitterState.cs +++ /dev/null @@ -1,237 +0,0 @@ -// -// Copyright (c) 2019-2021 Ryujinx -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with this program. If not, see . -// -using Ryujinx.Audio.Renderer.Parameter; -using System; -using System.Buffers; -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace Ryujinx.Audio.Renderer.Server.Splitter -{ - /// - /// Server state for a splitter. - /// - [StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = Alignment)] - public struct SplitterState - { - public const int Alignment = 0x10; - - /// - /// The unique id of this . - /// - public int Id; - - /// - /// Target sample rate to use on the splitter. - /// - public uint SampleRate; - - /// - /// Count of splitter destinations (). - /// - public int DestinationCount; - - /// - /// Set to true if the splitter has a new connection. - /// - [MarshalAs(UnmanagedType.I1)] - public bool HasNewConnection; - - /// - /// Linked list of . - /// - private unsafe SplitterDestination* _destinationsData; - - /// - /// Span to the first element of the linked list of . - /// - public Span Destinations - { - get - { - unsafe - { - return (IntPtr)_destinationsData != IntPtr.Zero ? new Span(_destinationsData, 1) : Span.Empty; - } - } - } - - /// - /// Create a new . - /// - /// The unique id of this . - public SplitterState(int id) : this() - { - Id = id; - } - - public Span GetData(int index) - { - int i = 0; - - Span result = Destinations; - - while (i < index) - { - if (result.IsEmpty) - { - break; - } - - result = result[0].Next; - i++; - } - - return result; - } - - /// - /// Clear the new connection flag. - /// - public void ClearNewConnectionFlag() - { - HasNewConnection = false; - } - - /// - /// Utility function to apply a given to all . - /// - /// The action to execute on each elements. - private void ForEachDestination(SpanAction action) - { - Span temp = Destinations; - - int i = 0; - - while (true) - { - if (temp.IsEmpty) - { - break; - } - - Span next = temp[0].Next; - - action.Invoke(temp, i++); - - temp = next; - } - } - - /// - /// Update the from user parameter. - /// - /// The splitter context. - /// The user parameter. - /// The raw input data after the . - public void Update(SplitterContext context, ref SplitterInParameter parameter, ReadOnlySpan input) - { - ClearLinks(); - - int destinationCount; - - if (context.IsBugFixed) - { - destinationCount = parameter.DestinationCount; - } - else - { - destinationCount = Math.Min(context.GetDestinationCountPerStateForCompatibility(), parameter.DestinationCount); - } - - if (destinationCount > 0) - { - ReadOnlySpan destinationIds = MemoryMarshal.Cast(input); - - Memory destination = context.GetDestinationMemory(destinationIds[0]); - - SetDestination(ref destination.Span[0]); - - DestinationCount = destinationCount; - - for (int i = 1; i < destinationCount; i++) - { - Memory nextDestination = context.GetDestinationMemory(destinationIds[i]); - - destination.Span[0].Link(ref nextDestination.Span[0]); - destination = nextDestination; - } - } - - Debug.Assert(parameter.Id == Id); - - if (parameter.Id == Id) - { - SampleRate = parameter.SampleRate; - HasNewConnection = true; - } - } - - /// - /// Set the head of the linked list of . - /// - /// A reference to a . - public void SetDestination(ref SplitterDestination newValue) - { - unsafe - { - fixed (SplitterDestination* newValuePtr = &newValue) - { - _destinationsData = newValuePtr; - } - } - } - - /// - /// Update the internal state of this instance. - /// - public void UpdateInternalState() - { - ForEachDestination((destination, _) => destination[0].UpdateInternalState()); - } - - /// - /// Clear all links from the . - /// - public void ClearLinks() - { - ForEachDestination((destination, _) => destination[0].Unlink()); - - unsafe - { - _destinationsData = (SplitterDestination*)IntPtr.Zero; - } - } - - /// - /// Initialize a given . - /// - /// All the to initialize. - public static void InitializeSplitters(Span splitters) - { - foreach (ref SplitterState splitter in splitters) - { - unsafe - { - splitter._destinationsData = (SplitterDestination*)IntPtr.Zero; - } - - splitter.DestinationCount = 0; - } - } - } -} -- cgit v1.2.3