SoundIoAudioOut.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using Ryujinx.Audio.SoundIo;
  2. using SoundIOSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Audio
  6. {
  7. /// <summary>
  8. /// An audio renderer that uses libsoundio as the audio backend
  9. /// </summary>
  10. public class SoundIoAudioOut : IAalOutput
  11. {
  12. /// <summary>
  13. /// The maximum amount of tracks we can issue simultaneously
  14. /// </summary>
  15. private const int MaximumTracks = 256;
  16. /// <summary>
  17. /// The volume of audio renderer
  18. /// </summary>
  19. private float _volume = 1.0f;
  20. /// <summary>
  21. /// True if the volume of audio renderer have changed
  22. /// </summary>
  23. private bool _volumeChanged;
  24. /// <summary>
  25. /// The <see cref="SoundIO"/> audio context
  26. /// </summary>
  27. private SoundIO _audioContext;
  28. /// <summary>
  29. /// The <see cref="SoundIODevice"/> audio device
  30. /// </summary>
  31. private SoundIODevice _audioDevice;
  32. /// <summary>
  33. /// An object pool containing <see cref="SoundIoAudioTrack"/> objects
  34. /// </summary>
  35. private SoundIoAudioTrackPool _trackPool;
  36. /// <summary>
  37. /// True if SoundIO is supported on the device
  38. /// </summary>
  39. public static bool IsSupported
  40. {
  41. get
  42. {
  43. return IsSupportedInternal();
  44. }
  45. }
  46. /// <summary>
  47. /// Constructs a new instance of a <see cref="SoundIoAudioOut"/>
  48. /// </summary>
  49. public SoundIoAudioOut()
  50. {
  51. _audioContext = new SoundIO();
  52. _audioContext.Connect();
  53. _audioContext.FlushEvents();
  54. _audioDevice = FindNonRawDefaultAudioDevice(_audioContext, true);
  55. _trackPool = new SoundIoAudioTrackPool(_audioContext, _audioDevice, MaximumTracks);
  56. }
  57. public bool SupportsChannelCount(int channels)
  58. {
  59. return _audioDevice.SupportsChannelCount(channels);
  60. }
  61. /// <summary>
  62. /// Creates a new audio track with the specified parameters
  63. /// </summary>
  64. /// <param name="sampleRate">The requested sample rate</param>
  65. /// <param name="hardwareChannels">The requested hardware channels</param>
  66. /// <param name="virtualChannels">The requested virtual channels</param>
  67. /// <param name="callback">A <see cref="ReleaseCallback" /> that represents the delegate to invoke when a buffer has been released by the audio track</param>
  68. /// <returns>The created track's Track ID</returns>
  69. public int OpenHardwareTrack(int sampleRate, int hardwareChannels, int virtualChannels, ReleaseCallback callback)
  70. {
  71. if (!_trackPool.TryGet(out SoundIoAudioTrack track))
  72. {
  73. return -1;
  74. }
  75. // Open the output. We currently only support 16-bit signed LE
  76. track.Open(sampleRate, hardwareChannels, virtualChannels, callback, SoundIOFormat.S16LE);
  77. return track.TrackID;
  78. }
  79. /// <summary>
  80. /// Stops playback and closes the track specified by <paramref name="trackId"/>
  81. /// </summary>
  82. /// <param name="trackId">The ID of the track to close</param>
  83. public void CloseTrack(int trackId)
  84. {
  85. if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
  86. {
  87. // Close and dispose of the track
  88. track.Close();
  89. // Recycle the track back into the pool
  90. _trackPool.Put(track);
  91. }
  92. }
  93. /// <summary>
  94. /// Returns a value indicating whether the specified buffer is currently reserved by the specified track
  95. /// </summary>
  96. /// <param name="trackId">The track to check</param>
  97. /// <param name="bufferTag">The buffer tag to check</param>
  98. public bool ContainsBuffer(int trackId, long bufferTag)
  99. {
  100. if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
  101. {
  102. return track.ContainsBuffer(bufferTag);
  103. }
  104. return false;
  105. }
  106. /// <summary>
  107. /// Gets a list of buffer tags the specified track is no longer reserving
  108. /// </summary>
  109. /// <param name="trackId">The track to retrieve buffer tags from</param>
  110. /// <param name="maxCount">The maximum amount of buffer tags to retrieve</param>
  111. /// <returns>Buffers released by the specified track</returns>
  112. public long[] GetReleasedBuffers(int trackId, int maxCount)
  113. {
  114. if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
  115. {
  116. List<long> bufferTags = new List<long>();
  117. while(maxCount-- > 0 && track.ReleasedBuffers.TryDequeue(out long tag))
  118. {
  119. bufferTags.Add(tag);
  120. }
  121. return bufferTags.ToArray();
  122. }
  123. return new long[0];
  124. }
  125. /// <summary>
  126. /// Appends an audio buffer to the specified track
  127. /// </summary>
  128. /// <typeparam name="T">The sample type of the buffer</typeparam>
  129. /// <param name="trackId">The track to append the buffer to</param>
  130. /// <param name="bufferTag">The internal tag of the buffer</param>
  131. /// <param name="buffer">The buffer to append to the track</param>
  132. public void AppendBuffer<T>(int trackId, long bufferTag, T[] buffer) where T : struct
  133. {
  134. if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
  135. {
  136. if (_volumeChanged)
  137. {
  138. track.AudioStream.SetVolume(_volume);
  139. _volumeChanged = false;
  140. }
  141. track.AppendBuffer(bufferTag, buffer);
  142. }
  143. }
  144. /// <summary>
  145. /// Starts playback
  146. /// </summary>
  147. /// <param name="trackId">The ID of the track to start playback on</param>
  148. public void Start(int trackId)
  149. {
  150. if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
  151. {
  152. track.Start();
  153. }
  154. }
  155. /// <summary>
  156. /// Stops playback
  157. /// </summary>
  158. /// <param name="trackId">The ID of the track to stop playback on</param>
  159. public void Stop(int trackId)
  160. {
  161. if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
  162. {
  163. track.Stop();
  164. }
  165. }
  166. /// <summary>
  167. /// Get playback volume
  168. /// </summary>
  169. public float GetVolume() => _volume;
  170. /// <summary>
  171. /// Set playback volume
  172. /// </summary>
  173. /// <param name="volume">The volume of the playback</param>
  174. public void SetVolume(float volume)
  175. {
  176. if (!_volumeChanged)
  177. {
  178. _volume = volume;
  179. _volumeChanged = true;
  180. }
  181. }
  182. /// <summary>
  183. /// Gets the current playback state of the specified track
  184. /// </summary>
  185. /// <param name="trackId">The track to retrieve the playback state for</param>
  186. public PlaybackState GetState(int trackId)
  187. {
  188. if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
  189. {
  190. return track.State;
  191. }
  192. return PlaybackState.Stopped;
  193. }
  194. /// <summary>
  195. /// Releases the unmanaged resources used by the <see cref="SoundIoAudioOut" />
  196. /// </summary>
  197. public void Dispose()
  198. {
  199. _trackPool.Dispose();
  200. _audioContext.Disconnect();
  201. _audioContext.Dispose();
  202. }
  203. /// <summary>
  204. /// Searches for a shared version of the default audio device
  205. /// </summary>
  206. /// <param name="audioContext">The <see cref="SoundIO"/> audio context</param>
  207. /// <param name="fallback">Whether to fallback to the raw default audio device if a non-raw device cannot be found</param>
  208. private static SoundIODevice FindNonRawDefaultAudioDevice(SoundIO audioContext, bool fallback = false)
  209. {
  210. SoundIODevice defaultAudioDevice = audioContext.GetOutputDevice(audioContext.DefaultOutputDeviceIndex);
  211. if (!defaultAudioDevice.IsRaw)
  212. {
  213. return defaultAudioDevice;
  214. }
  215. for (int i = 0; i < audioContext.BackendCount; i++)
  216. {
  217. SoundIODevice audioDevice = audioContext.GetOutputDevice(i);
  218. if (audioDevice.Id == defaultAudioDevice.Id && !audioDevice.IsRaw)
  219. {
  220. return audioDevice;
  221. }
  222. }
  223. return fallback ? defaultAudioDevice : null;
  224. }
  225. /// <summary>
  226. /// Determines if SoundIO can connect to a supported backend
  227. /// </summary>
  228. /// <returns></returns>
  229. private static bool IsSupportedInternal()
  230. {
  231. SoundIO context = null;
  232. SoundIODevice device = null;
  233. SoundIOOutStream stream = null;
  234. bool backendDisconnected = false;
  235. try
  236. {
  237. context = new SoundIO();
  238. context.OnBackendDisconnect = (i) => {
  239. backendDisconnected = true;
  240. };
  241. context.Connect();
  242. context.FlushEvents();
  243. if (backendDisconnected)
  244. {
  245. return false;
  246. }
  247. if (context.OutputDeviceCount == 0)
  248. {
  249. return false;
  250. }
  251. device = FindNonRawDefaultAudioDevice(context);
  252. if (device == null || backendDisconnected)
  253. {
  254. return false;
  255. }
  256. stream = device.CreateOutStream();
  257. if (stream == null || backendDisconnected)
  258. {
  259. return false;
  260. }
  261. return true;
  262. }
  263. catch
  264. {
  265. return false;
  266. }
  267. finally
  268. {
  269. if (stream != null)
  270. {
  271. stream.Dispose();
  272. }
  273. if (context != null)
  274. {
  275. context.Dispose();
  276. }
  277. }
  278. }
  279. }
  280. }