SoundIoHardwareDeviceSession.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using Ryujinx.Audio.Backends.Common;
  2. using Ryujinx.Audio.Common;
  3. using Ryujinx.Memory;
  4. using SoundIOSharp;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Runtime.CompilerServices;
  8. using System.Threading;
  9. namespace Ryujinx.Audio.Backends.SoundIo
  10. {
  11. class SoundIoHardwareDeviceSession : HardwareDeviceSessionOutputBase
  12. {
  13. private SoundIoHardwareDeviceDriver _driver;
  14. private ConcurrentQueue<SoundIoAudioBuffer> _queuedBuffers;
  15. private SoundIOOutStream _outputStream;
  16. private DynamicRingBuffer _ringBuffer;
  17. private ulong _playedSampleCount;
  18. private ManualResetEvent _updateRequiredEvent;
  19. private int _disposeState;
  20. public SoundIoHardwareDeviceSession(SoundIoHardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount)
  21. {
  22. _driver = driver;
  23. _updateRequiredEvent = _driver.GetUpdateRequiredEvent();
  24. _queuedBuffers = new ConcurrentQueue<SoundIoAudioBuffer>();
  25. _ringBuffer = new DynamicRingBuffer();
  26. SetupOutputStream();
  27. }
  28. private void SetupOutputStream()
  29. {
  30. _outputStream = _driver.OpenStream(RequestedSampleFormat, RequestedSampleRate, RequestedChannelCount);
  31. _outputStream.WriteCallback += Update;
  32. // TODO: Setup other callbacks (errors, ect).
  33. _outputStream.Open();
  34. }
  35. public override ulong GetPlayedSampleCount()
  36. {
  37. return Interlocked.Read(ref _playedSampleCount);
  38. }
  39. public override float GetVolume()
  40. {
  41. return _outputStream.Volume;
  42. }
  43. public override void PrepareToClose() { }
  44. public override void QueueBuffer(AudioBuffer buffer)
  45. {
  46. SoundIoAudioBuffer driverBuffer = new SoundIoAudioBuffer(buffer.DataPointer, GetSampleCount(buffer));
  47. _ringBuffer.Write(buffer.Data, 0, buffer.Data.Length);
  48. _queuedBuffers.Enqueue(driverBuffer);
  49. }
  50. public override void SetVolume(float volume)
  51. {
  52. _outputStream.SetVolume(volume);
  53. }
  54. public override void Start()
  55. {
  56. _outputStream.Start();
  57. _outputStream.Pause(false);
  58. _driver.FlushContextEvents();
  59. }
  60. public override void Stop()
  61. {
  62. _outputStream.Pause(true);
  63. _driver.FlushContextEvents();
  64. }
  65. public override void UnregisterBuffer(AudioBuffer buffer) {}
  66. public override bool WasBufferFullyConsumed(AudioBuffer buffer)
  67. {
  68. if (!_queuedBuffers.TryPeek(out SoundIoAudioBuffer driverBuffer))
  69. {
  70. return true;
  71. }
  72. return driverBuffer.DriverIdentifier != buffer.DataPointer;
  73. }
  74. private unsafe void Update(int minFrameCount, int maxFrameCount)
  75. {
  76. int bytesPerFrame = _outputStream.BytesPerFrame;
  77. uint bytesPerSample = (uint)_outputStream.BytesPerSample;
  78. int bufferedFrames = _ringBuffer.Length / bytesPerFrame;
  79. int frameCount = Math.Min(bufferedFrames, maxFrameCount);
  80. if (frameCount == 0)
  81. {
  82. return;
  83. }
  84. SoundIOChannelAreas areas = _outputStream.BeginWrite(ref frameCount);
  85. int channelCount = areas.ChannelCount;
  86. byte[] samples = new byte[frameCount * bytesPerFrame];
  87. _ringBuffer.Read(samples, 0, samples.Length);
  88. // This is a huge ugly block of code, but we save
  89. // a significant amount of time over the generic
  90. // loop that handles other channel counts.
  91. // TODO: Is this still right in 2021?
  92. // Mono
  93. if (channelCount == 1)
  94. {
  95. SoundIOChannelArea area = areas.GetArea(0);
  96. fixed (byte* srcptr = samples)
  97. {
  98. if (bytesPerSample == 1)
  99. {
  100. for (int frame = 0; frame < frameCount; frame++)
  101. {
  102. ((byte*)area.Pointer)[0] = srcptr[frame * bytesPerFrame];
  103. area.Pointer += area.Step;
  104. }
  105. }
  106. else if (bytesPerSample == 2)
  107. {
  108. for (int frame = 0; frame < frameCount; frame++)
  109. {
  110. ((short*)area.Pointer)[0] = ((short*)srcptr)[frame * bytesPerFrame >> 1];
  111. area.Pointer += area.Step;
  112. }
  113. }
  114. else if (bytesPerSample == 4)
  115. {
  116. for (int frame = 0; frame < frameCount; frame++)
  117. {
  118. ((int*)area.Pointer)[0] = ((int*)srcptr)[frame * bytesPerFrame >> 2];
  119. area.Pointer += area.Step;
  120. }
  121. }
  122. else
  123. {
  124. for (int frame = 0; frame < frameCount; frame++)
  125. {
  126. Unsafe.CopyBlockUnaligned((byte*)area.Pointer, srcptr + (frame * bytesPerFrame), bytesPerSample);
  127. area.Pointer += area.Step;
  128. }
  129. }
  130. }
  131. }
  132. // Stereo
  133. else if (channelCount == 2)
  134. {
  135. SoundIOChannelArea area1 = areas.GetArea(0);
  136. SoundIOChannelArea area2 = areas.GetArea(1);
  137. fixed (byte* srcptr = samples)
  138. {
  139. if (bytesPerSample == 1)
  140. {
  141. for (int frame = 0; frame < frameCount; frame++)
  142. {
  143. // Channel 1
  144. ((byte*)area1.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 0];
  145. // Channel 2
  146. ((byte*)area2.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 1];
  147. area1.Pointer += area1.Step;
  148. area2.Pointer += area2.Step;
  149. }
  150. }
  151. else if (bytesPerSample == 2)
  152. {
  153. for (int frame = 0; frame < frameCount; frame++)
  154. {
  155. // Channel 1
  156. ((short*)area1.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 0];
  157. // Channel 2
  158. ((short*)area2.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 1];
  159. area1.Pointer += area1.Step;
  160. area2.Pointer += area2.Step;
  161. }
  162. }
  163. else if (bytesPerSample == 4)
  164. {
  165. for (int frame = 0; frame < frameCount; frame++)
  166. {
  167. // Channel 1
  168. ((int*)area1.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 0];
  169. // Channel 2
  170. ((int*)area2.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 1];
  171. area1.Pointer += area1.Step;
  172. area2.Pointer += area2.Step;
  173. }
  174. }
  175. else
  176. {
  177. for (int frame = 0; frame < frameCount; frame++)
  178. {
  179. // Channel 1
  180. Unsafe.CopyBlockUnaligned((byte*)area1.Pointer, srcptr + (frame * bytesPerFrame) + (0 * bytesPerSample), bytesPerSample);
  181. // Channel 2
  182. Unsafe.CopyBlockUnaligned((byte*)area2.Pointer, srcptr + (frame * bytesPerFrame) + (1 * bytesPerSample), bytesPerSample);
  183. area1.Pointer += area1.Step;
  184. area2.Pointer += area2.Step;
  185. }
  186. }
  187. }
  188. }
  189. // Surround
  190. else if (channelCount == 6)
  191. {
  192. SoundIOChannelArea area1 = areas.GetArea(0);
  193. SoundIOChannelArea area2 = areas.GetArea(1);
  194. SoundIOChannelArea area3 = areas.GetArea(2);
  195. SoundIOChannelArea area4 = areas.GetArea(3);
  196. SoundIOChannelArea area5 = areas.GetArea(4);
  197. SoundIOChannelArea area6 = areas.GetArea(5);
  198. fixed (byte* srcptr = samples)
  199. {
  200. if (bytesPerSample == 1)
  201. {
  202. for (int frame = 0; frame < frameCount; frame++)
  203. {
  204. // Channel 1
  205. ((byte*)area1.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 0];
  206. // Channel 2
  207. ((byte*)area2.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 1];
  208. // Channel 3
  209. ((byte*)area3.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 2];
  210. // Channel 4
  211. ((byte*)area4.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 3];
  212. // Channel 5
  213. ((byte*)area5.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 4];
  214. // Channel 6
  215. ((byte*)area6.Pointer)[0] = srcptr[(frame * bytesPerFrame) + 5];
  216. area1.Pointer += area1.Step;
  217. area2.Pointer += area2.Step;
  218. area3.Pointer += area3.Step;
  219. area4.Pointer += area4.Step;
  220. area5.Pointer += area5.Step;
  221. area6.Pointer += area6.Step;
  222. }
  223. }
  224. else if (bytesPerSample == 2)
  225. {
  226. for (int frame = 0; frame < frameCount; frame++)
  227. {
  228. // Channel 1
  229. ((short*)area1.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 0];
  230. // Channel 2
  231. ((short*)area2.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 1];
  232. // Channel 3
  233. ((short*)area3.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 2];
  234. // Channel 4
  235. ((short*)area4.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 3];
  236. // Channel 5
  237. ((short*)area5.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 4];
  238. // Channel 6
  239. ((short*)area6.Pointer)[0] = ((short*)srcptr)[(frame * bytesPerFrame >> 1) + 5];
  240. area1.Pointer += area1.Step;
  241. area2.Pointer += area2.Step;
  242. area3.Pointer += area3.Step;
  243. area4.Pointer += area4.Step;
  244. area5.Pointer += area5.Step;
  245. area6.Pointer += area6.Step;
  246. }
  247. }
  248. else if (bytesPerSample == 4)
  249. {
  250. for (int frame = 0; frame < frameCount; frame++)
  251. {
  252. // Channel 1
  253. ((int*)area1.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 0];
  254. // Channel 2
  255. ((int*)area2.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 1];
  256. // Channel 3
  257. ((int*)area3.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 2];
  258. // Channel 4
  259. ((int*)area4.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 3];
  260. // Channel 5
  261. ((int*)area5.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 4];
  262. // Channel 6
  263. ((int*)area6.Pointer)[0] = ((int*)srcptr)[(frame * bytesPerFrame >> 2) + 5];
  264. area1.Pointer += area1.Step;
  265. area2.Pointer += area2.Step;
  266. area3.Pointer += area3.Step;
  267. area4.Pointer += area4.Step;
  268. area5.Pointer += area5.Step;
  269. area6.Pointer += area6.Step;
  270. }
  271. }
  272. else
  273. {
  274. for (int frame = 0; frame < frameCount; frame++)
  275. {
  276. // Channel 1
  277. Unsafe.CopyBlockUnaligned((byte*)area1.Pointer, srcptr + (frame * bytesPerFrame) + (0 * bytesPerSample), bytesPerSample);
  278. // Channel 2
  279. Unsafe.CopyBlockUnaligned((byte*)area2.Pointer, srcptr + (frame * bytesPerFrame) + (1 * bytesPerSample), bytesPerSample);
  280. // Channel 3
  281. Unsafe.CopyBlockUnaligned((byte*)area3.Pointer, srcptr + (frame * bytesPerFrame) + (2 * bytesPerSample), bytesPerSample);
  282. // Channel 4
  283. Unsafe.CopyBlockUnaligned((byte*)area4.Pointer, srcptr + (frame * bytesPerFrame) + (3 * bytesPerSample), bytesPerSample);
  284. // Channel 5
  285. Unsafe.CopyBlockUnaligned((byte*)area5.Pointer, srcptr + (frame * bytesPerFrame) + (4 * bytesPerSample), bytesPerSample);
  286. // Channel 6
  287. Unsafe.CopyBlockUnaligned((byte*)area6.Pointer, srcptr + (frame * bytesPerFrame) + (5 * bytesPerSample), bytesPerSample);
  288. area1.Pointer += area1.Step;
  289. area2.Pointer += area2.Step;
  290. area3.Pointer += area3.Step;
  291. area4.Pointer += area4.Step;
  292. area5.Pointer += area5.Step;
  293. area6.Pointer += area6.Step;
  294. }
  295. }
  296. }
  297. }
  298. // Every other channel count
  299. else
  300. {
  301. SoundIOChannelArea[] channels = new SoundIOChannelArea[channelCount];
  302. // Obtain the channel area for each channel
  303. for (int i = 0; i < channelCount; i++)
  304. {
  305. channels[i] = areas.GetArea(i);
  306. }
  307. fixed (byte* srcptr = samples)
  308. {
  309. for (int frame = 0; frame < frameCount; frame++)
  310. for (int channel = 0; channel < areas.ChannelCount; channel++)
  311. {
  312. // Copy channel by channel, frame by frame. This is slow!
  313. Unsafe.CopyBlockUnaligned((byte*)channels[channel].Pointer, srcptr + (frame * bytesPerFrame) + (channel * bytesPerSample), bytesPerSample);
  314. channels[channel].Pointer += channels[channel].Step;
  315. }
  316. }
  317. }
  318. _outputStream.EndWrite();
  319. ulong sampleCount = (ulong)(samples.Length / bytesPerSample / channelCount);
  320. ulong availaibleSampleCount = sampleCount;
  321. bool needUpdate = false;
  322. while (availaibleSampleCount > 0 && _queuedBuffers.TryPeek(out SoundIoAudioBuffer driverBuffer))
  323. {
  324. ulong sampleStillNeeded = driverBuffer.SampleCount - Interlocked.Read(ref driverBuffer.SamplePlayed);
  325. ulong playedAudioBufferSampleCount = Math.Min(sampleStillNeeded, availaibleSampleCount);
  326. Interlocked.Add(ref driverBuffer.SamplePlayed, playedAudioBufferSampleCount);
  327. availaibleSampleCount -= playedAudioBufferSampleCount;
  328. if (Interlocked.Read(ref driverBuffer.SamplePlayed) == driverBuffer.SampleCount)
  329. {
  330. _queuedBuffers.TryDequeue(out _);
  331. needUpdate = true;
  332. }
  333. Interlocked.Add(ref _playedSampleCount, playedAudioBufferSampleCount);
  334. }
  335. // Notify the output if needed.
  336. if (needUpdate)
  337. {
  338. _updateRequiredEvent.Set();
  339. }
  340. }
  341. protected virtual void Dispose(bool disposing)
  342. {
  343. if (disposing && _driver.Unregister(this))
  344. {
  345. PrepareToClose();
  346. Stop();
  347. _outputStream.Dispose();
  348. }
  349. }
  350. public override void Dispose()
  351. {
  352. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  353. {
  354. Dispose(true);
  355. }
  356. }
  357. }
  358. }