OpenALHardwareDeviceSession.cs 6.0 KB

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