SDL2HardwareDeviceSession.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Ryujinx.Audio.Backends.Common;
  2. using Ryujinx.Audio.Common;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Memory;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Threading;
  8. using static SDL2.SDL;
  9. namespace Ryujinx.Audio.Backends.SDL2
  10. {
  11. class SDL2HardwareDeviceSession : HardwareDeviceSessionOutputBase
  12. {
  13. private SDL2HardwareDeviceDriver _driver;
  14. private ConcurrentQueue<SDL2AudioBuffer> _queuedBuffers;
  15. private DynamicRingBuffer _ringBuffer;
  16. private ulong _playedSampleCount;
  17. private ManualResetEvent _updateRequiredEvent;
  18. private uint _outputStream;
  19. private SDL_AudioCallback _callbackDelegate;
  20. private int _bytesPerFrame;
  21. private uint _sampleCount;
  22. private bool _started;
  23. private float _volume;
  24. private ushort _nativeSampleFormat;
  25. public SDL2HardwareDeviceSession(SDL2HardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, float requestedVolume) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount)
  26. {
  27. _driver = driver;
  28. _updateRequiredEvent = _driver.GetUpdateRequiredEvent();
  29. _queuedBuffers = new ConcurrentQueue<SDL2AudioBuffer>();
  30. _ringBuffer = new DynamicRingBuffer();
  31. _callbackDelegate = Update;
  32. _bytesPerFrame = BackendHelper.GetSampleSize(RequestedSampleFormat) * (int)RequestedChannelCount;
  33. _nativeSampleFormat = SDL2HardwareDeviceDriver.GetSDL2Format(RequestedSampleFormat);
  34. _sampleCount = uint.MaxValue;
  35. _started = false;
  36. _volume = requestedVolume;
  37. }
  38. private void EnsureAudioStreamSetup(AudioBuffer buffer)
  39. {
  40. uint bufferSampleCount = (uint)GetSampleCount(buffer);
  41. bool needAudioSetup = _outputStream == 0 ||
  42. (bufferSampleCount >= Constants.TargetSampleCount && bufferSampleCount < _sampleCount);
  43. if (needAudioSetup)
  44. {
  45. _sampleCount = Math.Max(Constants.TargetSampleCount, bufferSampleCount);
  46. uint newOutputStream = SDL2HardwareDeviceDriver.OpenStream(RequestedSampleFormat, RequestedSampleRate, RequestedChannelCount, _sampleCount, _callbackDelegate);
  47. if (newOutputStream == 0)
  48. {
  49. // No stream in place, this is unexpected.
  50. throw new InvalidOperationException($"OpenStream failed with error: \"{SDL_GetError()}\"");
  51. }
  52. else
  53. {
  54. if (_outputStream != 0)
  55. {
  56. SDL_CloseAudioDevice(_outputStream);
  57. }
  58. _outputStream = newOutputStream;
  59. SDL_PauseAudioDevice(_outputStream, _started ? 0 : 1);
  60. Logger.Info?.Print(LogClass.Audio, $"New audio stream setup with a target sample count of {_sampleCount}");
  61. }
  62. }
  63. }
  64. private unsafe void Update(IntPtr userdata, IntPtr stream, int streamLength)
  65. {
  66. Span<byte> streamSpan = new Span<byte>((void*)stream, streamLength);
  67. int maxFrameCount = (int)GetSampleCount(streamLength);
  68. int bufferedFrames = _ringBuffer.Length / _bytesPerFrame;
  69. int frameCount = Math.Min(bufferedFrames, maxFrameCount);
  70. if (frameCount == 0)
  71. {
  72. // SDL2 left the responsibility to the user to clear the buffer.
  73. streamSpan.Fill(0);
  74. return;
  75. }
  76. byte[] samples = new byte[frameCount * _bytesPerFrame];
  77. _ringBuffer.Read(samples, 0, samples.Length);
  78. fixed (byte* p = samples)
  79. {
  80. IntPtr pStreamSrc = (IntPtr)p;
  81. // Zero the dest buffer
  82. streamSpan.Fill(0);
  83. // Apply volume to written data
  84. SDL_MixAudioFormat(stream, pStreamSrc, _nativeSampleFormat, (uint)samples.Length, (int)(_volume * SDL_MIX_MAXVOLUME));
  85. }
  86. ulong sampleCount = GetSampleCount(samples.Length);
  87. ulong availaibleSampleCount = sampleCount;
  88. bool needUpdate = false;
  89. while (availaibleSampleCount > 0 && _queuedBuffers.TryPeek(out SDL2AudioBuffer driverBuffer))
  90. {
  91. ulong sampleStillNeeded = driverBuffer.SampleCount - Interlocked.Read(ref driverBuffer.SamplePlayed);
  92. ulong playedAudioBufferSampleCount = Math.Min(sampleStillNeeded, availaibleSampleCount);
  93. ulong currentSamplePlayed = Interlocked.Add(ref driverBuffer.SamplePlayed, playedAudioBufferSampleCount);
  94. availaibleSampleCount -= playedAudioBufferSampleCount;
  95. if (currentSamplePlayed == driverBuffer.SampleCount)
  96. {
  97. _queuedBuffers.TryDequeue(out _);
  98. needUpdate = true;
  99. }
  100. Interlocked.Add(ref _playedSampleCount, playedAudioBufferSampleCount);
  101. }
  102. // Notify the output if needed.
  103. if (needUpdate)
  104. {
  105. _updateRequiredEvent.Set();
  106. }
  107. }
  108. public override ulong GetPlayedSampleCount()
  109. {
  110. return Interlocked.Read(ref _playedSampleCount);
  111. }
  112. public override float GetVolume()
  113. {
  114. return _volume;
  115. }
  116. public override void PrepareToClose() { }
  117. public override void QueueBuffer(AudioBuffer buffer)
  118. {
  119. EnsureAudioStreamSetup(buffer);
  120. SDL2AudioBuffer driverBuffer = new SDL2AudioBuffer(buffer.DataPointer, GetSampleCount(buffer));
  121. _ringBuffer.Write(buffer.Data, 0, buffer.Data.Length);
  122. _queuedBuffers.Enqueue(driverBuffer);
  123. }
  124. public override void SetVolume(float volume)
  125. {
  126. _volume = volume;
  127. }
  128. public override void Start()
  129. {
  130. if (!_started)
  131. {
  132. if (_outputStream != 0)
  133. {
  134. SDL_PauseAudioDevice(_outputStream, 0);
  135. }
  136. _started = true;
  137. }
  138. }
  139. public override void Stop()
  140. {
  141. if (_started)
  142. {
  143. if (_outputStream != 0)
  144. {
  145. SDL_PauseAudioDevice(_outputStream, 1);
  146. }
  147. _started = false;
  148. }
  149. }
  150. public override void UnregisterBuffer(AudioBuffer buffer) { }
  151. public override bool WasBufferFullyConsumed(AudioBuffer buffer)
  152. {
  153. if (!_queuedBuffers.TryPeek(out SDL2AudioBuffer driverBuffer))
  154. {
  155. return true;
  156. }
  157. return driverBuffer.DriverIdentifier != buffer.DataPointer;
  158. }
  159. protected virtual void Dispose(bool disposing)
  160. {
  161. if (disposing && _driver.Unregister(this))
  162. {
  163. PrepareToClose();
  164. Stop();
  165. if (_outputStream != 0)
  166. {
  167. SDL_CloseAudioDevice(_outputStream);
  168. }
  169. }
  170. }
  171. public override void Dispose()
  172. {
  173. Dispose(true);
  174. }
  175. }
  176. }