SoundIoAudioOut.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. SoundIO context = null;
  35. SoundIODevice device = null;
  36. SoundIOOutStream stream = null;
  37. bool backendDisconnected = false;
  38. try
  39. {
  40. context = new SoundIO();
  41. context.OnBackendDisconnect = (i) => {
  42. backendDisconnected = true;
  43. };
  44. context.Connect();
  45. context.FlushEvents();
  46. if(backendDisconnected)
  47. {
  48. return false;
  49. }
  50. device = context.GetOutputDevice(context.DefaultOutputDeviceIndex);
  51. if(device == null || backendDisconnected)
  52. {
  53. return false;
  54. }
  55. stream = device.CreateOutStream();
  56. if(stream == null || backendDisconnected)
  57. {
  58. return false;
  59. }
  60. return true;
  61. }
  62. catch
  63. {
  64. return false;
  65. }
  66. finally
  67. {
  68. if(stream != null)
  69. {
  70. stream.Dispose();
  71. }
  72. if(context != null)
  73. {
  74. context.Dispose();
  75. }
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// Constructs a new instance of a <see cref="SoundIoAudioOut"/>
  81. /// </summary>
  82. public SoundIoAudioOut()
  83. {
  84. m_AudioContext = new SoundIO();
  85. m_AudioContext.Connect();
  86. m_AudioContext.FlushEvents();
  87. m_AudioDevice = m_AudioContext.GetOutputDevice(m_AudioContext.DefaultOutputDeviceIndex);
  88. m_TrackPool = new SoundIoAudioTrackPool(m_AudioContext, m_AudioDevice, MaximumTracks);
  89. }
  90. /// <summary>
  91. /// Gets the current playback state of the specified track
  92. /// </summary>
  93. /// <param name="trackId">The track to retrieve the playback state for</param>
  94. public PlaybackState GetState(int trackId)
  95. {
  96. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  97. {
  98. return track.State;
  99. }
  100. return PlaybackState.Stopped;
  101. }
  102. /// <summary>
  103. /// Creates a new audio track with the specified parameters
  104. /// </summary>
  105. /// <param name="sampleRate">The requested sample rate</param>
  106. /// <param name="channels">The requested channels</param>
  107. /// <param name="callback">A <see cref="ReleaseCallback" /> that represents the delegate to invoke when a buffer has been released by the audio track</param>
  108. /// <returns>The created track's Track ID</returns>
  109. public int OpenTrack(int sampleRate, int channels, ReleaseCallback callback)
  110. {
  111. if (!m_TrackPool.TryGet(out SoundIoAudioTrack track))
  112. {
  113. return -1;
  114. }
  115. // Open the output. We currently only support 16-bit signed LE
  116. track.Open(sampleRate, channels, callback, SoundIOFormat.S16LE);
  117. return track.TrackID;
  118. }
  119. /// <summary>
  120. /// Stops playback and closes the track specified by <paramref name="trackId"/>
  121. /// </summary>
  122. /// <param name="trackId">The ID of the track to close</param>
  123. public void CloseTrack(int trackId)
  124. {
  125. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  126. {
  127. // Close and dispose of the track
  128. track.Close();
  129. // Recycle the track back into the pool
  130. m_TrackPool.Put(track);
  131. }
  132. }
  133. /// <summary>
  134. /// Starts playback
  135. /// </summary>
  136. /// <param name="trackId">The ID of the track to start playback on</param>
  137. public void Start(int trackId)
  138. {
  139. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  140. {
  141. track.Start();
  142. }
  143. }
  144. /// <summary>
  145. /// Stops playback
  146. /// </summary>
  147. /// <param name="trackId">The ID of the track to stop playback on</param>
  148. public void Stop(int trackId)
  149. {
  150. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  151. {
  152. track.Stop();
  153. }
  154. }
  155. /// <summary>
  156. /// Appends an audio buffer to the specified track
  157. /// </summary>
  158. /// <typeparam name="T">The sample type of the buffer</typeparam>
  159. /// <param name="trackId">The track to append the buffer to</param>
  160. /// <param name="bufferTag">The internal tag of the buffer</param>
  161. /// <param name="buffer">The buffer to append to the track</param>
  162. public void AppendBuffer<T>(int trackId, long bufferTag, T[] buffer)
  163. where T : struct
  164. {
  165. if(m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  166. {
  167. track.AppendBuffer(bufferTag, buffer);
  168. }
  169. }
  170. /// <summary>
  171. /// Returns a value indicating whether the specified buffer is currently reserved by the specified track
  172. /// </summary>
  173. /// <param name="trackId">The track to check</param>
  174. /// <param name="bufferTag">The buffer tag to check</param>
  175. public bool ContainsBuffer(int trackId, long bufferTag)
  176. {
  177. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  178. {
  179. return track.ContainsBuffer(bufferTag);
  180. }
  181. return false;
  182. }
  183. /// <summary>
  184. /// Gets a list of buffer tags the specified track is no longer reserving
  185. /// </summary>
  186. /// <param name="trackId">The track to retrieve buffer tags from</param>
  187. /// <param name="maxCount">The maximum amount of buffer tags to retrieve</param>
  188. /// <returns>Buffers released by the specified track</returns>
  189. public long[] GetReleasedBuffers(int trackId, int maxCount)
  190. {
  191. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  192. {
  193. List<long> bufferTags = new List<long>();
  194. while(maxCount-- > 0 && track.ReleasedBuffers.TryDequeue(out long tag))
  195. {
  196. bufferTags.Add(tag);
  197. }
  198. return bufferTags.ToArray();
  199. }
  200. return new long[0];
  201. }
  202. /// <summary>
  203. /// Releases the unmanaged resources used by the <see cref="SoundIoAudioOut" />
  204. /// </summary>
  205. public void Dispose()
  206. {
  207. m_TrackPool.Dispose();
  208. m_AudioContext.Disconnect();
  209. m_AudioContext.Dispose();
  210. }
  211. }
  212. }