SoundIoHardwareDeviceSession.cs 17 KB

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