using Ryujinx.Audio.SoundIo;
using SoundIOSharp;
using System.Collections.Generic;
namespace Ryujinx.Audio
{
///
/// An audio renderer that uses libsoundio as the audio backend
///
public class SoundIoAudioOut : IAalOutput
{
///
/// The maximum amount of tracks we can issue simultaneously
///
private const int MaximumTracks = 256;
///
/// The audio context
///
private SoundIO m_AudioContext;
///
/// The audio device
///
private SoundIODevice m_AudioDevice;
///
/// An object pool containing objects
///
private SoundIoAudioTrackPool m_TrackPool;
///
/// True if SoundIO is supported on the device.
///
public static bool IsSupported
{
get
{
SoundIO context = null;
SoundIODevice device = null;
SoundIOOutStream stream = null;
bool backendDisconnected = false;
try
{
context = new SoundIO();
context.OnBackendDisconnect = (i) => {
backendDisconnected = true;
};
context.Connect();
context.FlushEvents();
if(backendDisconnected)
{
return false;
}
device = context.GetOutputDevice(context.DefaultOutputDeviceIndex);
if(device == null || backendDisconnected)
{
return false;
}
stream = device.CreateOutStream();
if(stream == null || backendDisconnected)
{
return false;
}
return true;
}
catch
{
return false;
}
finally
{
if(stream != null)
{
stream.Dispose();
}
if(context != null)
{
context.Dispose();
}
}
}
}
///
/// Constructs a new instance of a
///
public SoundIoAudioOut()
{
m_AudioContext = new SoundIO();
m_AudioContext.Connect();
m_AudioContext.FlushEvents();
m_AudioDevice = m_AudioContext.GetOutputDevice(m_AudioContext.DefaultOutputDeviceIndex);
m_TrackPool = new SoundIoAudioTrackPool(m_AudioContext, m_AudioDevice, MaximumTracks);
}
///
/// Gets the current playback state of the specified track
///
/// The track to retrieve the playback state for
public PlaybackState GetState(int trackId)
{
if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
return track.State;
}
return PlaybackState.Stopped;
}
///
/// 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
/// The created track's Track ID
public int OpenTrack(int sampleRate, int channels, ReleaseCallback callback)
{
if (!m_TrackPool.TryGet(out SoundIoAudioTrack track))
{
return -1;
}
// Open the output. We currently only support 16-bit signed LE
track.Open(sampleRate, channels, callback, SoundIOFormat.S16LE);
return track.TrackID;
}
///
/// Stops playback and closes the track specified by
///
/// The ID of the track to close
public void CloseTrack(int trackId)
{
if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
// Close and dispose of the track
track.Close();
// Recycle the track back into the pool
m_TrackPool.Put(track);
}
}
///
/// Starts playback
///
/// The ID of the track to start playback on
public void Start(int trackId)
{
if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
track.Start();
}
}
///
/// Stops playback
///
/// The ID of the track to stop playback on
public void Stop(int trackId)
{
if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
track.Stop();
}
}
///
/// 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(m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
track.AppendBuffer(bufferTag, buffer);
}
}
///
/// 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 (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack 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 (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
List bufferTags = new List();
while(maxCount-- > 0 && track.ReleasedBuffers.TryDequeue(out long tag))
{
bufferTags.Add(tag);
}
return bufferTags.ToArray();
}
return new long[0];
}
///
/// Releases the unmanaged resources used by the
///
public void Dispose()
{
m_TrackPool.Dispose();
m_AudioContext.Disconnect();
m_AudioContext.Dispose();
}
}
}