SoundIoAudioOut.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 => true;
  31. /// <summary>
  32. /// Constructs a new instance of a <see cref="SoundIoAudioOut"/>
  33. /// </summary>
  34. public SoundIoAudioOut()
  35. {
  36. m_AudioContext = new SoundIO();
  37. m_AudioContext.Connect();
  38. m_AudioContext.FlushEvents();
  39. m_AudioDevice = m_AudioContext.GetOutputDevice(m_AudioContext.DefaultOutputDeviceIndex);
  40. m_TrackPool = new SoundIoAudioTrackPool(m_AudioContext, m_AudioDevice, MaximumTracks);
  41. }
  42. /// <summary>
  43. /// Gets the current playback state of the specified track
  44. /// </summary>
  45. /// <param name="trackId">The track to retrieve the playback state for</param>
  46. public PlaybackState GetState(int trackId)
  47. {
  48. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  49. {
  50. return track.State;
  51. }
  52. return PlaybackState.Stopped;
  53. }
  54. /// <summary>
  55. /// Creates a new audio track with the specified parameters
  56. /// </summary>
  57. /// <param name="sampleRate">The requested sample rate</param>
  58. /// <param name="channels">The requested 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 OpenTrack(int sampleRate, int channels, ReleaseCallback callback)
  62. {
  63. if (!m_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, channels, 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 (m_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. m_TrackPool.Put(track);
  83. }
  84. }
  85. /// <summary>
  86. /// Starts playback
  87. /// </summary>
  88. /// <param name="trackId">The ID of the track to start playback on</param>
  89. public void Start(int trackId)
  90. {
  91. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  92. {
  93. track.Start();
  94. }
  95. }
  96. /// <summary>
  97. /// Stops playback
  98. /// </summary>
  99. /// <param name="trackId">The ID of the track to stop playback on</param>
  100. public void Stop(int trackId)
  101. {
  102. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  103. {
  104. track.Stop();
  105. }
  106. }
  107. /// <summary>
  108. /// Appends an audio buffer to the specified track
  109. /// </summary>
  110. /// <typeparam name="T">The sample type of the buffer</typeparam>
  111. /// <param name="trackId">The track to append the buffer to</param>
  112. /// <param name="bufferTag">The internal tag of the buffer</param>
  113. /// <param name="buffer">The buffer to append to the track</param>
  114. public void AppendBuffer<T>(int trackId, long bufferTag, T[] buffer)
  115. where T : struct
  116. {
  117. if(m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  118. {
  119. track.AppendBuffer(bufferTag, buffer);
  120. }
  121. }
  122. /// <summary>
  123. /// Returns a value indicating whether the specified buffer is currently reserved by the specified track
  124. /// </summary>
  125. /// <param name="trackId">The track to check</param>
  126. /// <param name="bufferTag">The buffer tag to check</param>
  127. public bool ContainsBuffer(int trackId, long bufferTag)
  128. {
  129. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  130. {
  131. return track.ContainsBuffer(bufferTag);
  132. }
  133. return false;
  134. }
  135. /// <summary>
  136. /// Gets a list of buffer tags the specified track is no longer reserving
  137. /// </summary>
  138. /// <param name="trackId">The track to retrieve buffer tags from</param>
  139. /// <param name="maxCount">The maximum amount of buffer tags to retrieve</param>
  140. /// <returns>Buffers released by the specified track</returns>
  141. public long[] GetReleasedBuffers(int trackId, int maxCount)
  142. {
  143. if (m_TrackPool.TryGet(trackId, out SoundIoAudioTrack track))
  144. {
  145. List<long> bufferTags = new List<long>();
  146. while(maxCount-- > 0 && track.ReleasedBuffers.TryDequeue(out long tag))
  147. {
  148. bufferTags.Add(tag);
  149. }
  150. return bufferTags.ToArray();
  151. }
  152. return new long[0];
  153. }
  154. /// <summary>
  155. /// Releases the unmanaged resources used by the <see cref="SoundIoAudioOut" />
  156. /// </summary>
  157. public void Dispose()
  158. {
  159. m_TrackPool.Dispose();
  160. m_AudioContext.Disconnect();
  161. m_AudioContext.Dispose();
  162. }
  163. }
  164. }