using Ryujinx.Audio.SoundIo;
using SoundIOSharp;
using System;
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 volume of audio renderer
///
private float _volume = 1.0f;
///
/// True if the volume of audio renderer have changed
///
private bool _volumeChanged;
///
/// The audio context
///
private SoundIO _audioContext;
///
/// The audio device
///
private SoundIODevice _audioDevice;
///
/// An object pool containing objects
///
private SoundIoAudioTrackPool _trackPool;
///
/// True if SoundIO is supported on the device
///
public static bool IsSupported
{
get
{
return IsSupportedInternal();
}
}
///
/// Constructs a new instance of a
///
public SoundIoAudioOut()
{
_audioContext = new SoundIO();
_audioContext.Connect();
_audioContext.FlushEvents();
_audioDevice = FindNonRawDefaultAudioDevice(_audioContext, true);
_trackPool = new SoundIoAudioTrackPool(_audioContext, _audioDevice, MaximumTracks);
}
public bool SupportsChannelCount(int channels)
{
return _audioDevice.SupportsChannelCount(channels);
}
///
/// 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)
{
if (!_trackPool.TryGet(out SoundIoAudioTrack track))
{
return -1;
}
// Open the output. We currently only support 16-bit signed LE
track.Open(sampleRate, hardwareChannels, virtualChannels, 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 (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
// Close and dispose of the track
track.Close();
// Recycle the track back into the pool
_trackPool.Put(track);
}
}
///
/// 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 (_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 (_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];
}
///
/// 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 (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
if (_volumeChanged)
{
track.AudioStream.SetVolume(_volume);
_volumeChanged = false;
}
track.AppendBuffer(bufferTag, buffer);
}
}
///
/// Starts playback
///
/// The ID of the track to start playback on
public void Start(int trackId)
{
if (_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 (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
track.Stop();
}
}
///
/// 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 (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
return track.State;
}
return PlaybackState.Stopped;
}
///
/// Releases the unmanaged resources used by the
///
public void Dispose()
{
_trackPool.Dispose();
_audioContext.Disconnect();
_audioContext.Dispose();
}
///
/// Searches for a shared version of the default audio device
///
/// The audio context
/// Whether to fallback to the raw default audio device if a non-raw device cannot be found
private static SoundIODevice FindNonRawDefaultAudioDevice(SoundIO audioContext, bool fallback = false)
{
SoundIODevice defaultAudioDevice = audioContext.GetOutputDevice(audioContext.DefaultOutputDeviceIndex);
if (!defaultAudioDevice.IsRaw)
{
return defaultAudioDevice;
}
for (int i = 0; i < audioContext.BackendCount; i++)
{
SoundIODevice audioDevice = audioContext.GetOutputDevice(i);
if (audioDevice.Id == defaultAudioDevice.Id && !audioDevice.IsRaw)
{
return audioDevice;
}
}
return fallback ? defaultAudioDevice : null;
}
///
/// Determines if SoundIO can connect to a supported backend
///
///
private static bool IsSupportedInternal()
{
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;
}
if (context.OutputDeviceCount == 0)
{
return false;
}
device = FindNonRawDefaultAudioDevice(context);
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();
}
}
}
}
}