AudioInputSystem.cs 13 KB

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