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; } } } }