AudioOutputSystem.cs 12 KB

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