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/AudioRendererManager.cs | 341 --------------------- 1 file changed, 341 deletions(-) delete mode 100644 Ryujinx.Audio.Renderer/Server/AudioRendererManager.cs (limited to 'Ryujinx.Audio.Renderer/Server/AudioRendererManager.cs') diff --git a/Ryujinx.Audio.Renderer/Server/AudioRendererManager.cs b/Ryujinx.Audio.Renderer/Server/AudioRendererManager.cs deleted file mode 100644 index f552935b..00000000 --- a/Ryujinx.Audio.Renderer/Server/AudioRendererManager.cs +++ /dev/null @@ -1,341 +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.Dsp; -using Ryujinx.Audio.Renderer.Integration; -using Ryujinx.Audio.Renderer.Parameter; -using Ryujinx.Common.Logging; -using Ryujinx.Memory; -using System; -using System.Diagnostics; -using System.Threading; - -namespace Ryujinx.Audio.Renderer.Server -{ - /// - /// The audio renderer manager. - /// - public class AudioRendererManager : IDisposable - { - /// - /// Lock used for session allocation. - /// - private object _sessionLock = new object(); - - /// - /// Lock used to control the running state. - /// - private object _audioProcessorLock = new object(); - - /// - /// The session ids allocation table. - /// - private int[] _sessionIds; - - /// - /// The events linked to each session. - /// - private IWritableEvent[] _sessionsSystemEvent; - - /// - /// The sessions instances. - /// - private AudioRenderSystem[] _sessions; - - /// - /// The count of active sessions. - /// - private int _activeSessionCount; - - /// - /// The worker thread used to run . - /// - private Thread _workerThread; - - /// - /// Indicate if the worker thread and are running. - /// - private bool _isRunning; - - /// - /// The output devices associated to each session. - /// - // TODO: get ride of this with the audout rewrite. - public HardwareDevice[] OutputDevices { get; private set; } - - /// - /// The instance associated to this manager. - /// - public AudioProcessor Processor { get; } - - /// - /// Create a new . - /// - public AudioRendererManager() - { - Processor = new AudioProcessor(); - _sessionIds = new int[RendererConstants.AudioRendererSessionCountMax]; - _sessions = new AudioRenderSystem[RendererConstants.AudioRendererSessionCountMax]; - _activeSessionCount = 0; - - for (int i = 0; i < _sessionIds.Length; i++) - { - _sessionIds[i] = i; - } - } - - /// - /// Initialize the . - /// - /// The events associated to each session. - /// The output devices associated to each session. - public void Initialize(IWritableEvent[] sessionSystemEvents, HardwareDevice[] devices) - { - _sessionsSystemEvent = sessionSystemEvents; - OutputDevices = devices; - } - - /// - /// Get the work buffer size required by a session. - /// - /// The user configuration - /// The work buffer size required by a session. - public static ulong GetWorkBufferSize(ref AudioRendererConfiguration parameter) - { - return AudioRenderSystem.GetWorkBufferSize(ref parameter); - } - - /// - /// Acquire a new session id. - /// - /// A new session id. - 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 renderer ({sessionId})"); - - return sessionId; - } - } - - /// - /// Release a given . - /// - /// The session id to release. - private void ReleaseSessionId(int sessionId) - { - lock (_sessionLock) - { - Debug.Assert(_activeSessionCount > 0); - - int newIndex = --_activeSessionCount; - - _sessionIds[newIndex] = sessionId; - } - - Logger.Info?.Print(LogClass.AudioRenderer, $"Unregistered renderer ({sessionId})"); - } - - /// - /// Check if there is any audio renderer active. - /// - /// Returns true if there is any audio renderer active. - private bool HasAnyActiveRendererLocked() - { - foreach (AudioRenderSystem renderer in _sessions) - { - if (renderer != null) - { - return true; - } - } - - return false; - } - - /// - /// Start the and worker thread. - /// - private void StartLocked() - { - _isRunning = true; - - // TODO: virtual device mapping (IAudioDevice) - Processor.SetOutputDevices(OutputDevices); - Processor.Start(); - - _workerThread = new Thread(SendCommands) - { - Name = "AudioRendererManager.Worker" - }; - - _workerThread.Start(); - } - - /// - /// Stop the and worker thread. - /// - private void StopLocked() - { - _isRunning = false; - - _workerThread.Join(); - Processor.Stop(); - - Logger.Info?.Print(LogClass.AudioRenderer, "Stopped audio renderer"); - } - - /// - /// Worker main function. This is used to dispatch audio renderer commands to the . - /// - private void SendCommands() - { - Logger.Info?.Print(LogClass.AudioRenderer, "Starting audio renderer"); - Processor.Wait(); - - while (_isRunning) - { - lock (_sessionLock) - { - foreach(AudioRenderSystem renderer in _sessions) - { - renderer?.SendCommands(); - } - } - - Processor.Signal(); - Processor.Wait(); - } - } - - /// - /// Register a new . - /// - /// The to register. - private void Register(AudioRenderSystem renderer) - { - lock (_sessionLock) - { - _sessions[renderer.GetSessionId()] = renderer; - } - - lock (_audioProcessorLock) - { - if (!_isRunning) - { - StartLocked(); - } - } - } - - /// - /// Unregister a new . - /// - /// The to unregister. - internal void Unregister(AudioRenderSystem renderer) - { - lock (_sessionLock) - { - int sessionId = renderer.GetSessionId(); - - _sessions[renderer.GetSessionId()] = null; - - ReleaseSessionId(sessionId); - } - - lock (_audioProcessorLock) - { - if (_isRunning && !HasAnyActiveRendererLocked()) - { - StopLocked(); - } - } - } - - /// - /// Open a new - /// - /// The new - /// The memory manager that will be used for all guest memory operations. - /// The user configuration - /// The applet resource user id of the application. - /// The guest work buffer address. - /// The guest work buffer size. - /// The process handle of the application. - /// A reporting an error or a success. - public ResultCode OpenAudioRenderer(out AudioRenderSystem renderer, IVirtualMemoryManager memoryManager, ref AudioRendererConfiguration parameter, ulong appletResourceUserId, ulong workBufferAddress, ulong workBufferSize, uint processHandle) - { - int sessionId = AcquireSessionId(); - - AudioRenderSystem audioRenderer = new AudioRenderSystem(this, _sessionsSystemEvent[sessionId]); - - ResultCode result = audioRenderer.Initialize(ref parameter, processHandle, workBufferAddress, workBufferSize, sessionId, appletResourceUserId, memoryManager); - - if (result == ResultCode.Success) - { - renderer = audioRenderer; - - Register(renderer); - } - else - { - ReleaseSessionId(sessionId); - - renderer = null; - } - - return result; - } - - public void Dispose() - { - Dispose(true); - } - - protected virtual void Dispose(bool disposing) - { - if (disposing) - { - lock (_audioProcessorLock) - { - if (_isRunning) - { - StopLocked(); - } - } - - Processor.Dispose(); - - foreach (HardwareDevice device in OutputDevices) - { - device.Dispose(); - } - } - } - } -} -- cgit v1.2.3