OpenALHardwareDeviceSession.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 OpenALHardwareDeviceDriver _driver;
  13. private int _sourceId;
  14. private ALFormat _targetFormat;
  15. private bool _isActive;
  16. private Queue<OpenALAudioBuffer> _queuedBuffers;
  17. private ulong _playedSampleCount;
  18. private object _lock = new object();
  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. switch (RequestedSampleFormat)
  32. {
  33. case SampleFormat.PcmInt16:
  34. switch (RequestedChannelCount)
  35. {
  36. case 1:
  37. return ALFormat.Mono16;
  38. case 2:
  39. return ALFormat.Stereo16;
  40. case 6:
  41. return ALFormat.Multi51Chn16Ext;
  42. default:
  43. throw new NotImplementedException($"Unsupported channel config {RequestedChannelCount}");
  44. }
  45. default:
  46. throw new NotImplementedException($"Unsupported sample format {RequestedSampleFormat}");
  47. }
  48. }
  49. public override void PrepareToClose() { }
  50. private void StartIfNotPlaying()
  51. {
  52. AL.GetSource(_sourceId, ALGetSourcei.SourceState, out int stateInt);
  53. ALSourceState State = (ALSourceState)stateInt;
  54. if (State != ALSourceState.Playing)
  55. {
  56. AL.SourcePlay(_sourceId);
  57. }
  58. }
  59. public override void QueueBuffer(AudioBuffer buffer)
  60. {
  61. lock (_lock)
  62. {
  63. OpenALAudioBuffer driverBuffer = new OpenALAudioBuffer
  64. {
  65. DriverIdentifier = buffer.DataPointer,
  66. BufferId = AL.GenBuffer(),
  67. SampleCount = GetSampleCount(buffer)
  68. };
  69. AL.BufferData(driverBuffer.BufferId, _targetFormat, buffer.Data, (int)RequestedSampleRate);
  70. _queuedBuffers.Enqueue(driverBuffer);
  71. AL.SourceQueueBuffer(_sourceId, driverBuffer.BufferId);
  72. if (_isActive)
  73. {
  74. StartIfNotPlaying();
  75. }
  76. }
  77. }
  78. public override void SetVolume(float volume)
  79. {
  80. lock (_lock)
  81. {
  82. AL.Source(_sourceId, ALSourcef.Gain, volume);
  83. }
  84. }
  85. public override float GetVolume()
  86. {
  87. AL.GetSource(_sourceId, ALSourcef.Gain, out float volume);
  88. return volume;
  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. }