AudioOutputSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using Ryujinx.Audio.Common;
  2. using Ryujinx.Audio.Integration;
  3. using System;
  4. using System.Threading;
  5. namespace Ryujinx.Audio.Output
  6. {
  7. /// <summary>
  8. /// Audio output system.
  9. /// </summary>
  10. public class AudioOutputSystem : IDisposable
  11. {
  12. /// <summary>
  13. /// The session id associated to the <see cref="AudioOutputSystem"/>.
  14. /// </summary>
  15. private int _sessionId;
  16. /// <summary>
  17. /// The session the <see cref="AudioOutputSystem"/>.
  18. /// </summary>
  19. private AudioDeviceSession _session;
  20. /// <summary>
  21. /// The target device name of the <see cref="AudioOutputSystem"/>.
  22. /// </summary>
  23. public string DeviceName { get; private set; }
  24. /// <summary>
  25. /// The target sample rate of the <see cref="AudioOutputSystem"/>.
  26. /// </summary>
  27. public uint SampleRate { get; private set; }
  28. /// <summary>
  29. /// The target channel count of the <see cref="AudioOutputSystem"/>.
  30. /// </summary>
  31. public uint ChannelCount { get; private set; }
  32. /// <summary>
  33. /// The target sample format of the <see cref="AudioOutputSystem"/>.
  34. /// </summary>
  35. public SampleFormat SampleFormat { get; private set; }
  36. /// <summary>
  37. /// The <see cref="AudioOutputManager"/> owning this.
  38. /// </summary>
  39. private AudioOutputManager _manager;
  40. /// <summary>
  41. /// THe lock of the parent.
  42. /// </summary>
  43. private object _parentLock;
  44. /// <summary>
  45. /// The dispose state.
  46. /// </summary>
  47. private int _disposeState;
  48. /// <summary>
  49. /// Create a new <see cref="AudioOutputSystem"/>.
  50. /// </summary>
  51. /// <param name="manager">The manager instance</param>
  52. /// <param name="parentLock">The lock of the manager</param>
  53. /// <param name="deviceSession">The hardware device session</param>
  54. /// <param name="bufferEvent">The buffer release event of the audio output</param>
  55. public AudioOutputSystem(AudioOutputManager manager, object parentLock, IHardwareDeviceSession deviceSession, IWritableEvent bufferEvent)
  56. {
  57. _manager = manager;
  58. _parentLock = parentLock;
  59. _session = new AudioDeviceSession(deviceSession, bufferEvent);
  60. }
  61. /// <summary>
  62. /// Get the default device name on the system.
  63. /// </summary>
  64. /// <returns>The default device name on the system.</returns>
  65. private static string GetDeviceDefaultName()
  66. {
  67. return Constants.DefaultDeviceOutputName;
  68. }
  69. /// <summary>
  70. /// Check if a given configuration and device name is valid on the system.
  71. /// </summary>
  72. /// <param name="configuration">The configuration to check.</param>
  73. /// <param name="deviceName">The device name to check.</param>
  74. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  75. private static ResultCode IsConfigurationValid(ref AudioInputConfiguration configuration, string deviceName)
  76. {
  77. if (deviceName.Length != 0 && !deviceName.Equals(GetDeviceDefaultName()))
  78. {
  79. return ResultCode.DeviceNotFound;
  80. }
  81. else if (configuration.SampleRate != 0 && configuration.SampleRate != Constants.TargetSampleRate)
  82. {
  83. return ResultCode.UnsupportedSampleRate;
  84. }
  85. else if (configuration.ChannelCount != 0 && configuration.ChannelCount != 1 && configuration.ChannelCount != 2 && configuration.ChannelCount != 6)
  86. {
  87. return ResultCode.UnsupportedChannelConfiguration;
  88. }
  89. return ResultCode.Success;
  90. }
  91. /// <summary>
  92. /// Get the released buffer event.
  93. /// </summary>
  94. /// <returns>The released buffer event</returns>
  95. public IWritableEvent RegisterBufferEvent()
  96. {
  97. lock (_parentLock)
  98. {
  99. return _session.GetBufferEvent();
  100. }
  101. }
  102. /// <summary>
  103. /// Update the <see cref="AudioOutputSystem"/>.
  104. /// </summary>
  105. public void Update()
  106. {
  107. lock (_parentLock)
  108. {
  109. _session.Update();
  110. }
  111. }
  112. /// <summary>
  113. /// Get the id of this session.
  114. /// </summary>
  115. /// <returns>The id of this session</returns>
  116. public int GetSessionId()
  117. {
  118. return _sessionId;
  119. }
  120. /// <summary>
  121. /// Initialize the <see cref="AudioOutputSystem"/>.
  122. /// </summary>
  123. /// <param name="inputDeviceName">The input device name wanted by the user</param>
  124. /// <param name="sampleFormat">The sample format to use</param>
  125. /// <param name="parameter">The user configuration</param>
  126. /// <param name="sessionId">The session id associated to this <see cref="AudioOutputSystem"/></param>
  127. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  128. public ResultCode Initialize(string inputDeviceName, SampleFormat sampleFormat, ref AudioInputConfiguration parameter, int sessionId)
  129. {
  130. _sessionId = sessionId;
  131. ResultCode result = IsConfigurationValid(ref parameter, inputDeviceName);
  132. if (result == ResultCode.Success)
  133. {
  134. if (inputDeviceName.Length == 0)
  135. {
  136. DeviceName = GetDeviceDefaultName();
  137. }
  138. else
  139. {
  140. DeviceName = inputDeviceName;
  141. }
  142. if (parameter.ChannelCount == 6)
  143. {
  144. ChannelCount = 6;
  145. }
  146. else
  147. {
  148. ChannelCount = 2;
  149. }
  150. SampleFormat = sampleFormat;
  151. SampleRate = Constants.TargetSampleRate;
  152. }
  153. return result;
  154. }
  155. /// <summary>
  156. /// Append a new audio buffer to the audio output.
  157. /// </summary>
  158. /// <param name="bufferTag">The unique tag of this buffer.</param>
  159. /// <param name="userBuffer">The buffer informations.</param>
  160. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  161. public ResultCode AppendBuffer(ulong bufferTag, ref AudioUserBuffer userBuffer)
  162. {
  163. lock (_parentLock)
  164. {
  165. AudioBuffer buffer = new AudioBuffer
  166. {
  167. BufferTag = bufferTag,
  168. DataPointer = userBuffer.Data,
  169. DataSize = userBuffer.DataSize
  170. };
  171. if (_session.AppendBuffer(buffer))
  172. {
  173. return ResultCode.Success;
  174. }
  175. return ResultCode.BufferRingFull;
  176. }
  177. }
  178. /// <summary>
  179. /// Get the release buffers.
  180. /// </summary>
  181. /// <param name="releasedBuffers">The buffer to write the release buffers</param>
  182. /// <param name="releasedCount">The count of released buffers</param>
  183. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  184. public ResultCode GetReleasedBuffer(Span<ulong> releasedBuffers, out uint releasedCount)
  185. {
  186. releasedCount = 0;
  187. // Ensure that the first entry is set to zero if no entries are returned.
  188. if (releasedBuffers.Length > 0)
  189. {
  190. releasedBuffers[0] = 0;
  191. }
  192. lock (_parentLock)
  193. {
  194. for (int i = 0; i < releasedBuffers.Length; i++)
  195. {
  196. if (!_session.TryPopReleasedBuffer(out AudioBuffer buffer))
  197. {
  198. break;
  199. }
  200. releasedBuffers[i] = buffer.BufferTag;
  201. releasedCount++;
  202. }
  203. }
  204. return ResultCode.Success;
  205. }
  206. /// <summary>
  207. /// Get the current state of the <see cref="AudioOutputSystem"/>.
  208. /// </summary>
  209. /// <returns>Return the curent sta\te of the <see cref="AudioOutputSystem"/></returns>
  210. /// <returns></returns>
  211. public AudioDeviceState GetState()
  212. {
  213. lock (_parentLock)
  214. {
  215. return _session.GetState();
  216. }
  217. }
  218. /// <summary>
  219. /// Start the audio session.
  220. /// </summary>
  221. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  222. public ResultCode Start()
  223. {
  224. lock (_parentLock)
  225. {
  226. return _session.Start();
  227. }
  228. }
  229. /// <summary>
  230. /// Stop the audio session.
  231. /// </summary>
  232. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  233. public ResultCode Stop()
  234. {
  235. lock (_parentLock)
  236. {
  237. return _session.Stop();
  238. }
  239. }
  240. /// <summary>
  241. /// Get the volume of the session.
  242. /// </summary>
  243. /// <returns>The volume of the session</returns>
  244. public float GetVolume()
  245. {
  246. lock (_parentLock)
  247. {
  248. return _session.GetVolume();
  249. }
  250. }
  251. /// <summary>
  252. /// Set the volume of the session.
  253. /// </summary>
  254. /// <param name="volume">The new volume to set</param>
  255. public void SetVolume(float volume)
  256. {
  257. lock (_parentLock)
  258. {
  259. _session.SetVolume(volume);
  260. }
  261. }
  262. /// <summary>
  263. /// Get the count of buffer currently in use (server + driver side).
  264. /// </summary>
  265. /// <returns>The count of buffer currently in use</returns>
  266. public uint GetBufferCount()
  267. {
  268. lock (_parentLock)
  269. {
  270. return _session.GetBufferCount();
  271. }
  272. }
  273. /// <summary>
  274. /// Check if a buffer is present.
  275. /// </summary>
  276. /// <param name="bufferTag">The unique tag of the buffer</param>
  277. /// <returns>Return true if a buffer is present</returns>
  278. public bool ContainsBuffer(ulong bufferTag)
  279. {
  280. lock (_parentLock)
  281. {
  282. return _session.ContainsBuffer(bufferTag);
  283. }
  284. }
  285. /// <summary>
  286. /// Get the count of sample played in this session.
  287. /// </summary>
  288. /// <returns>The count of sample played in this session</returns>
  289. public ulong GetPlayedSampleCount()
  290. {
  291. lock (_parentLock)
  292. {
  293. return _session.GetPlayedSampleCount();
  294. }
  295. }
  296. /// <summary>
  297. /// Flush all buffers to the initial state.
  298. /// </summary>
  299. /// <returns>True if any buffers was flushed</returns>
  300. public bool FlushBuffers()
  301. {
  302. lock (_parentLock)
  303. {
  304. return _session.FlushBuffers();
  305. }
  306. }
  307. public void Dispose()
  308. {
  309. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  310. {
  311. Dispose(true);
  312. }
  313. }
  314. protected virtual void Dispose(bool disposing)
  315. {
  316. if (disposing)
  317. {
  318. _session.Dispose();
  319. _manager.Unregister(this);
  320. }
  321. }
  322. }
  323. }