SoundIoAudioOut.cs 10 KB

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