SoundIoAudioOut.cs 11 KB

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