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 --- Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs | 404 ----------------------- 1 file changed, 404 deletions(-) delete mode 100644 Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs (limited to 'Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs') diff --git a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs deleted file mode 100644 index 1ad82319..00000000 --- a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs +++ /dev/null @@ -1,404 +0,0 @@ -using OpenTK.Audio; -using OpenTK.Audio.OpenAL; -using System; -using System.Collections.Concurrent; -using System.Runtime.InteropServices; -using System.Threading; - -namespace Ryujinx.Audio -{ - /// - /// An audio renderer that uses OpenAL as the audio backend - /// - public class OpenALAudioOut : IAalOutput, IDisposable - { - /// - /// The maximum amount of tracks we can issue simultaneously - /// - private const int MaxTracks = 256; - - /// - /// The audio context - /// - private AudioContext _context; - - /// - /// An object pool containing objects - /// - private ConcurrentDictionary _tracks; - - /// - /// True if the thread need to keep polling - /// - private bool _keepPolling; - - /// - /// The poller thread audio context - /// - private Thread _audioPollerThread; - - /// - /// True if OpenAL is supported on the device - /// - public static bool IsSupported - { - get - { - try - { - return AudioContext.AvailableDevices.Count > 0; - } - catch - { - return false; - } - } - } - - public OpenALAudioOut() - { - _context = new AudioContext(); - _tracks = new ConcurrentDictionary(); - _keepPolling = true; - _audioPollerThread = new Thread(AudioPollerWork) - { - Name = "Audio.PollerThread" - }; - - _audioPollerThread.Start(); - } - - private void AudioPollerWork() - { - do - { - foreach (OpenALAudioTrack track in _tracks.Values) - { - lock (track) - { - track.CallReleaseCallbackIfNeeded(); - } - } - - // If it's not slept it will waste cycles. - Thread.Sleep(10); - } - while (_keepPolling); - - foreach (OpenALAudioTrack track in _tracks.Values) - { - track.Dispose(); - } - - _tracks.Clear(); - _context.Dispose(); - } - - public bool SupportsChannelCount(int channels) - { - // NOTE: OpenAL doesn't give us a way to know if the 5.1 setup is supported by hardware or actually emulated. - // TODO: find a way to determine hardware support. - return channels == 1 || channels == 2; - } - - /// - /// Creates a new audio track with the specified parameters - /// - /// The requested sample rate - /// The requested hardware channels - /// The requested virtual channels - /// A that represents the delegate to invoke when a buffer has been released by the audio track - /// The created track's Track ID - public int OpenHardwareTrack(int sampleRate, int hardwareChannels, int virtualChannels, ReleaseCallback callback) - { - OpenALAudioTrack track = new OpenALAudioTrack(sampleRate, GetALFormat(hardwareChannels), hardwareChannels, virtualChannels, callback); - - for (int id = 0; id < MaxTracks; id++) - { - if (_tracks.TryAdd(id, track)) - { - return id; - } - } - - return -1; - } - - private ALFormat GetALFormat(int channels) - { - switch (channels) - { - case 1: return ALFormat.Mono16; - case 2: return ALFormat.Stereo16; - case 6: return ALFormat.Multi51Chn16Ext; - } - - throw new ArgumentOutOfRangeException(nameof(channels)); - } - - /// - /// Stops playback and closes the track specified by - /// - /// The ID of the track to close - public void CloseTrack(int trackId) - { - if (_tracks.TryRemove(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - track.Dispose(); - } - } - } - - /// - /// Returns a value indicating whether the specified buffer is currently reserved by the specified track - /// - /// The track to check - /// The buffer tag to check - public bool ContainsBuffer(int trackId, long bufferTag) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - return track.ContainsBuffer(bufferTag); - } - } - - return false; - } - - /// - /// Gets a list of buffer tags the specified track is no longer reserving - /// - /// The track to retrieve buffer tags from - /// The maximum amount of buffer tags to retrieve - /// Buffers released by the specified track - public long[] GetReleasedBuffers(int trackId, int maxCount) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - return track.GetReleasedBuffers(maxCount); - } - } - - return null; - } - - /// - /// Appends an audio buffer to the specified track - /// - /// The sample type of the buffer - /// The track to append the buffer to - /// The internal tag of the buffer - /// The buffer to append to the track - public void AppendBuffer(int trackId, long bufferTag, T[] buffer) where T : struct - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - int bufferId = track.AppendBuffer(bufferTag); - - // Do we need to downmix? - if (track.HardwareChannels != track.VirtualChannels) - { - short[] downmixedBuffer; - - ReadOnlySpan bufferPCM16 = MemoryMarshal.Cast(buffer); - - if (track.VirtualChannels == 6) - { - downmixedBuffer = Downmixing.DownMixSurroundToStereo(bufferPCM16); - - if (track.HardwareChannels == 1) - { - downmixedBuffer = Downmixing.DownMixStereoToMono(downmixedBuffer); - } - } - else if (track.VirtualChannels == 2) - { - downmixedBuffer = Downmixing.DownMixStereoToMono(bufferPCM16); - } - else - { - throw new NotImplementedException($"Downmixing from {track.VirtualChannels} to {track.HardwareChannels} not implemented!"); - } - - AL.BufferData(bufferId, track.Format, downmixedBuffer, downmixedBuffer.Length * sizeof(ushort), track.SampleRate); - } - else - { - AL.BufferData(bufferId, track.Format, buffer, buffer.Length * sizeof(ushort), track.SampleRate); - } - - AL.SourceQueueBuffer(track.SourceId, bufferId); - - StartPlaybackIfNeeded(track); - - track.PlayedSampleCount += (ulong)buffer.Length; - } - } - } - - /// - /// Starts playback - /// - /// The ID of the track to start playback on - public void Start(int trackId) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - track.State = PlaybackState.Playing; - - StartPlaybackIfNeeded(track); - } - } - } - - private void StartPlaybackIfNeeded(OpenALAudioTrack track) - { - AL.GetSource(track.SourceId, ALGetSourcei.SourceState, out int stateInt); - - ALSourceState State = (ALSourceState)stateInt; - - if (State != ALSourceState.Playing && track.State == PlaybackState.Playing) - { - AL.SourcePlay(track.SourceId); - } - } - - /// - /// Stops playback - /// - /// The ID of the track to stop playback on - public void Stop(int trackId) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - track.State = PlaybackState.Stopped; - - AL.SourceStop(track.SourceId); - } - } - } - - /// - /// Get track buffer count - /// - /// The ID of the track to get buffer count - public uint GetBufferCount(int trackId) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - return track.BufferCount; - } - } - - return 0; - } - - /// - /// Get track played sample count - /// - /// The ID of the track to get played sample count - public ulong GetPlayedSampleCount(int trackId) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - return track.PlayedSampleCount; - } - } - - return 0; - } - - /// - /// Flush all track buffers - /// - /// The ID of the track to flush - public bool FlushBuffers(int trackId) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - track.FlushBuffers(); - } - } - - return false; - } - - /// - /// Set track volume - /// - /// The ID of the track to set volume - /// The volume of the track - public void SetVolume(int trackId, float volume) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - track.SetVolume(volume); - } - } - } - - /// - /// Get track volume - /// - /// The ID of the track to get volume - public float GetVolume(int trackId) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - lock (track) - { - return track.GetVolume(); - } - } - - return 1.0f; - } - - /// - /// Gets the current playback state of the specified track - /// - /// The track to retrieve the playback state for - public PlaybackState GetState(int trackId) - { - if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) - { - return track.State; - } - - return PlaybackState.Stopped; - } - - public void Dispose() - { - Dispose(true); - } - - protected virtual void Dispose(bool disposing) - { - if (disposing) - { - _keepPolling = false; - } - } - } -} \ No newline at end of file -- cgit v1.2.3