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;
///
/// The volume of audio renderer
///
private float _volume = 1.0f;
///
/// True if the volume of audio renderer have changed
///
private bool _volumeChanged;
///
/// 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();
}
///
/// Creates a new audio track with the specified parameters
///
/// The requested sample rate
/// The requested channels
/// A that represents the delegate to invoke when a buffer has been released by the audio track
public int OpenTrack(int sampleRate, int channels, ReleaseCallback callback)
{
OpenALAudioTrack track = new OpenALAudioTrack(sampleRate, GetALFormat(channels), 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);
int size = buffer.Length * Marshal.SizeOf();
AL.BufferData(bufferId, track.Format, buffer, size, track.SampleRate);
AL.SourceQueueBuffer(track.SourceId, bufferId);
StartPlaybackIfNeeded(track);
}
}
}
///
/// 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)
{
if (_volumeChanged)
{
AL.Source(track.SourceId, ALSourcef.Gain, _volume);
_volumeChanged = false;
}
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 playback volume
///
public float GetVolume() => _volume;
///
/// Set playback volume
///
/// The volume of the playback
public void SetVolume(float volume)
{
if (!_volumeChanged)
{
_volume = volume;
_volumeChanged = true;
}
}
///
/// 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;
}
}
}
}