SoundIoAudioOut.cs 9.1 KB

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