OpenALHardwareDeviceSession.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using OpenTK.Audio.OpenAL;
  2. using Ryujinx.Audio.Backends.Common;
  3. using Ryujinx.Audio.Common;
  4. using Ryujinx.Memory;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. namespace Ryujinx.Audio.Backends.OpenAL
  9. {
  10. class OpenALHardwareDeviceSession : HardwareDeviceSessionOutputBase
  11. {
  12. private readonly OpenALHardwareDeviceDriver _driver;
  13. private readonly int _sourceId;
  14. private readonly ALFormat _targetFormat;
  15. private bool _isActive;
  16. private readonly Queue<OpenALAudioBuffer> _queuedBuffers;
  17. private ulong _playedSampleCount;
  18. private readonly object _lock = new();
  19. public OpenALHardwareDeviceSession(OpenALHardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, float requestedVolume) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount)
  20. {
  21. _driver = driver;
  22. _queuedBuffers = new Queue<OpenALAudioBuffer>();
  23. _sourceId = AL.GenSource();
  24. _targetFormat = GetALFormat();
  25. _isActive = false;
  26. _playedSampleCount = 0;
  27. SetVolume(requestedVolume);
  28. }
  29. private ALFormat GetALFormat()
  30. {
  31. return RequestedSampleFormat switch
  32. {
  33. SampleFormat.PcmInt16 => RequestedChannelCount switch
  34. {
  35. 1 => ALFormat.Mono16,
  36. 2 => ALFormat.Stereo16,
  37. 6 => ALFormat.Multi51Chn16Ext,
  38. _ => throw new NotImplementedException($"Unsupported channel config {RequestedChannelCount}"),
  39. },
  40. _ => throw new NotImplementedException($"Unsupported sample format {RequestedSampleFormat}"),
  41. };
  42. }
  43. public override void PrepareToClose() { }
  44. private void StartIfNotPlaying()
  45. {
  46. AL.GetSource(_sourceId, ALGetSourcei.SourceState, out int stateInt);
  47. ALSourceState State = (ALSourceState)stateInt;
  48. if (State != ALSourceState.Playing)
  49. {
  50. AL.SourcePlay(_sourceId);
  51. }
  52. }
  53. public override void QueueBuffer(AudioBuffer buffer)
  54. {
  55. lock (_lock)
  56. {
  57. OpenALAudioBuffer driverBuffer = new()
  58. {
  59. DriverIdentifier = buffer.DataPointer,
  60. BufferId = AL.GenBuffer(),
  61. SampleCount = GetSampleCount(buffer),
  62. };
  63. AL.BufferData(driverBuffer.BufferId, _targetFormat, buffer.Data, (int)RequestedSampleRate);
  64. _queuedBuffers.Enqueue(driverBuffer);
  65. AL.SourceQueueBuffer(_sourceId, driverBuffer.BufferId);
  66. if (_isActive)
  67. {
  68. StartIfNotPlaying();
  69. }
  70. }
  71. }
  72. public override void SetVolume(float volume)
  73. {
  74. lock (_lock)
  75. {
  76. AL.Source(_sourceId, ALSourcef.Gain, volume);
  77. }
  78. }
  79. public override float GetVolume()
  80. {
  81. AL.GetSource(_sourceId, ALSourcef.Gain, out float volume);
  82. return volume;
  83. }
  84. public override void Start()
  85. {
  86. lock (_lock)
  87. {
  88. _isActive = true;
  89. StartIfNotPlaying();
  90. }
  91. }
  92. public override void Stop()
  93. {
  94. lock (_lock)
  95. {
  96. SetVolume(0.0f);
  97. AL.SourceStop(_sourceId);
  98. _isActive = false;
  99. }
  100. }
  101. public override void UnregisterBuffer(AudioBuffer buffer) { }
  102. public override bool WasBufferFullyConsumed(AudioBuffer buffer)
  103. {
  104. lock (_lock)
  105. {
  106. if (!_queuedBuffers.TryPeek(out OpenALAudioBuffer driverBuffer))
  107. {
  108. return true;
  109. }
  110. return driverBuffer.DriverIdentifier != buffer.DataPointer;
  111. }
  112. }
  113. public override ulong GetPlayedSampleCount()
  114. {
  115. lock (_lock)
  116. {
  117. return _playedSampleCount;
  118. }
  119. }
  120. public bool Update()
  121. {
  122. lock (_lock)
  123. {
  124. if (_isActive)
  125. {
  126. AL.GetSource(_sourceId, ALGetSourcei.BuffersProcessed, out int releasedCount);
  127. if (releasedCount > 0)
  128. {
  129. int[] bufferIds = new int[releasedCount];
  130. AL.SourceUnqueueBuffers(_sourceId, releasedCount, bufferIds);
  131. int i = 0;
  132. while (_queuedBuffers.TryPeek(out OpenALAudioBuffer buffer) && i < bufferIds.Length)
  133. {
  134. if (buffer.BufferId == bufferIds[i])
  135. {
  136. _playedSampleCount += buffer.SampleCount;
  137. _queuedBuffers.TryDequeue(out _);
  138. i++;
  139. }
  140. }
  141. Debug.Assert(i == bufferIds.Length, "Unknown buffer ids found!");
  142. AL.DeleteBuffers(bufferIds);
  143. }
  144. return releasedCount > 0;
  145. }
  146. return false;
  147. }
  148. }
  149. protected virtual void Dispose(bool disposing)
  150. {
  151. if (disposing && _driver.Unregister(this))
  152. {
  153. lock (_lock)
  154. {
  155. PrepareToClose();
  156. Stop();
  157. AL.DeleteSource(_sourceId);
  158. }
  159. }
  160. }
  161. public override void Dispose()
  162. {
  163. Dispose(true);
  164. GC.SuppressFinalize(this);
  165. }
  166. }
  167. }