AudioInputSystem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using Ryujinx.Audio.Common;
  2. using Ryujinx.Audio.Integration;
  3. using System;
  4. using System.Threading;
  5. namespace Ryujinx.Audio.Input
  6. {
  7. /// <summary>
  8. /// Audio input system.
  9. /// </summary>
  10. public class AudioInputSystem : IDisposable
  11. {
  12. /// <summary>
  13. /// The session id associated to the <see cref="AudioInputSystem"/>.
  14. /// </summary>
  15. private int _sessionId;
  16. /// <summary>
  17. /// The session the <see cref="AudioInputSystem"/>.
  18. /// </summary>
  19. private AudioDeviceSession _session;
  20. /// <summary>
  21. /// The target device name of the <see cref="AudioInputSystem"/>.
  22. /// </summary>
  23. public string DeviceName { get; private set; }
  24. /// <summary>
  25. /// The target sample rate of the <see cref="AudioInputSystem"/>.
  26. /// </summary>
  27. public uint SampleRate { get; private set; }
  28. /// <summary>
  29. /// The target channel count of the <see cref="AudioInputSystem"/>.
  30. /// </summary>
  31. public uint ChannelCount { get; private set; }
  32. /// <summary>
  33. /// The target sample format of the <see cref="AudioInputSystem"/>.
  34. /// </summary>
  35. public SampleFormat SampleFormat { get; private set; }
  36. /// <summary>
  37. /// The <see cref="AudioInputManager"/> owning this.
  38. /// </summary>
  39. private AudioInputManager _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="AudioInputSystem"/>.
  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 input</param>
  55. public AudioInputSystem(AudioInputManager 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.DefaultDeviceInputName;
  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="AudioInputSystem"/>.
  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="AudioInputSystem"/>.
  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="AudioInputSystem"/></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 input.
  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. /// Append a new audio buffer to the audio input.
  180. /// </summary>
  181. /// <remarks>This is broken by design, only added for completness.</remarks>
  182. /// <param name="bufferTag">The unique tag of this buffer.</param>
  183. /// <param name="userBuffer">The buffer informations.</param>
  184. /// <param name="handle">Some unknown handle.</param>
  185. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  186. public ResultCode AppendUacBuffer(ulong bufferTag, ref AudioUserBuffer userBuffer, uint handle)
  187. {
  188. lock (_parentLock)
  189. {
  190. AudioBuffer buffer = new AudioBuffer
  191. {
  192. BufferTag = bufferTag,
  193. DataPointer = userBuffer.Data,
  194. DataSize = userBuffer.DataSize
  195. };
  196. if (_session.AppendUacBuffer(buffer, handle))
  197. {
  198. return ResultCode.Success;
  199. }
  200. return ResultCode.BufferRingFull;
  201. }
  202. }
  203. /// <summary>
  204. /// Get the release buffers.
  205. /// </summary>
  206. /// <param name="releasedBuffers">The buffer to write the release buffers</param>
  207. /// <param name="releasedCount">The count of released buffers</param>
  208. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  209. public ResultCode GetReleasedBuffers(Span<ulong> releasedBuffers, out uint releasedCount)
  210. {
  211. releasedCount = 0;
  212. // Ensure that the first entry is set to zero if no entries are returned.
  213. if (releasedBuffers.Length > 0)
  214. {
  215. releasedBuffers[0] = 0;
  216. }
  217. lock (_parentLock)
  218. {
  219. for (int i = 0; i < releasedBuffers.Length; i++)
  220. {
  221. if (!_session.TryPopReleasedBuffer(out AudioBuffer buffer))
  222. {
  223. break;
  224. }
  225. releasedBuffers[i] = buffer.BufferTag;
  226. releasedCount++;
  227. }
  228. }
  229. return ResultCode.Success;
  230. }
  231. /// <summary>
  232. /// Get the current state of the <see cref="AudioInputSystem"/>.
  233. /// </summary>
  234. /// <returns>Return the curent sta\te of the <see cref="AudioInputSystem"/></returns>
  235. public AudioDeviceState GetState()
  236. {
  237. lock (_parentLock)
  238. {
  239. return _session.GetState();
  240. }
  241. }
  242. /// <summary>
  243. /// Start the audio session.
  244. /// </summary>
  245. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  246. public ResultCode Start()
  247. {
  248. lock (_parentLock)
  249. {
  250. return _session.Start();
  251. }
  252. }
  253. /// <summary>
  254. /// Stop the audio session.
  255. /// </summary>
  256. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  257. public ResultCode Stop()
  258. {
  259. lock (_parentLock)
  260. {
  261. return _session.Stop();
  262. }
  263. }
  264. /// <summary>
  265. /// Get the volume of the session.
  266. /// </summary>
  267. /// <returns>The volume of the session</returns>
  268. public float GetVolume()
  269. {
  270. lock (_parentLock)
  271. {
  272. return _session.GetVolume();
  273. }
  274. }
  275. /// <summary>
  276. /// Set the volume of the session.
  277. /// </summary>
  278. /// <param name="volume">The new volume to set</param>
  279. public void SetVolume(float volume)
  280. {
  281. lock (_parentLock)
  282. {
  283. _session.SetVolume(volume);
  284. }
  285. }
  286. /// <summary>
  287. /// Get the count of buffer currently in use (server + driver side).
  288. /// </summary>
  289. /// <returns>The count of buffer currently in use</returns>
  290. public uint GetBufferCount()
  291. {
  292. lock (_parentLock)
  293. {
  294. return _session.GetBufferCount();
  295. }
  296. }
  297. /// <summary>
  298. /// Check if a buffer is present.
  299. /// </summary>
  300. /// <param name="bufferTag">The unique tag of the buffer</param>
  301. /// <returns>Return true if a buffer is present</returns>
  302. public bool ContainsBuffer(ulong bufferTag)
  303. {
  304. lock (_parentLock)
  305. {
  306. return _session.ContainsBuffer(bufferTag);
  307. }
  308. }
  309. /// <summary>
  310. /// Get the count of sample played in this session.
  311. /// </summary>
  312. /// <returns>The count of sample played in this session</returns>
  313. public ulong GetPlayedSampleCount()
  314. {
  315. lock (_parentLock)
  316. {
  317. return _session.GetPlayedSampleCount();
  318. }
  319. }
  320. /// <summary>
  321. /// Flush all buffers to the initial state.
  322. /// </summary>
  323. /// <returns>True if any buffers was flushed</returns>
  324. public bool FlushBuffers()
  325. {
  326. lock (_parentLock)
  327. {
  328. return _session.FlushBuffers();
  329. }
  330. }
  331. public void Dispose()
  332. {
  333. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  334. {
  335. Dispose(true);
  336. }
  337. }
  338. protected virtual void Dispose(bool disposing)
  339. {
  340. if (disposing)
  341. {
  342. _session.Dispose();
  343. _manager.Unregister(this);
  344. }
  345. }
  346. }
  347. }