SoundIoAudioOut.cs 11 KB

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