OpenALAudioOut.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using OpenTK.Audio;
  2. using OpenTK.Audio.OpenAL;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. namespace Ryujinx.Audio.OpenAL
  9. {
  10. public class OpenALAudioOut : IAalOutput
  11. {
  12. private const int MaxTracks = 256;
  13. private const int MaxReleased = 32;
  14. private AudioContext Context;
  15. private class Track : IDisposable
  16. {
  17. public int SourceId { get; private set; }
  18. public int SampleRate { get; private set; }
  19. public ALFormat Format { get; private set; }
  20. private ReleaseCallback Callback;
  21. public PlaybackState State { get; set; }
  22. private bool ShouldCallReleaseCallback;
  23. private ConcurrentDictionary<long, int> Buffers;
  24. private Queue<long> QueuedTagsQueue;
  25. private Queue<long> ReleasedTagsQueue;
  26. private int LastReleasedCount;
  27. private bool Disposed;
  28. public Track(int SampleRate, ALFormat Format, ReleaseCallback Callback)
  29. {
  30. this.SampleRate = SampleRate;
  31. this.Format = Format;
  32. this.Callback = Callback;
  33. State = PlaybackState.Stopped;
  34. SourceId = AL.GenSource();
  35. Buffers = new ConcurrentDictionary<long, int>();
  36. QueuedTagsQueue = new Queue<long>();
  37. ReleasedTagsQueue = new Queue<long>();
  38. }
  39. public bool ContainsBuffer(long Tag)
  40. {
  41. SyncQueuedTags();
  42. foreach (long QueuedTag in QueuedTagsQueue)
  43. {
  44. if (QueuedTag == Tag)
  45. {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. public long[] GetReleasedBuffers(int MaxCount)
  52. {
  53. ClearReleased();
  54. List<long> Tags = new List<long>();
  55. HashSet<long> Unique = new HashSet<long>();
  56. while (MaxCount-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
  57. {
  58. if (Unique.Add(Tag))
  59. {
  60. Tags.Add(Tag);
  61. }
  62. }
  63. return Tags.ToArray();
  64. }
  65. public int AppendBuffer(long Tag)
  66. {
  67. if (Disposed)
  68. {
  69. throw new ObjectDisposedException(nameof(Track));
  70. }
  71. int Id = AL.GenBuffer();
  72. Buffers.AddOrUpdate(Tag, Id, (Key, OldId) =>
  73. {
  74. AL.DeleteBuffer(OldId);
  75. return Id;
  76. });
  77. QueuedTagsQueue.Enqueue(Tag);
  78. return Id;
  79. }
  80. public void ClearReleased()
  81. {
  82. SyncQueuedTags();
  83. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
  84. CheckReleaseChanges(ReleasedCount);
  85. if (ReleasedCount > 0)
  86. {
  87. AL.SourceUnqueueBuffers(SourceId, ReleasedCount);
  88. }
  89. }
  90. public void CallReleaseCallbackIfNeeded()
  91. {
  92. CheckReleaseChanges();
  93. if (ShouldCallReleaseCallback)
  94. {
  95. ShouldCallReleaseCallback = false;
  96. Callback();
  97. }
  98. }
  99. private void CheckReleaseChanges()
  100. {
  101. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
  102. CheckReleaseChanges(ReleasedCount);
  103. }
  104. private void CheckReleaseChanges(int NewReleasedCount)
  105. {
  106. if (LastReleasedCount != NewReleasedCount)
  107. {
  108. LastReleasedCount = NewReleasedCount;
  109. ShouldCallReleaseCallback = true;
  110. }
  111. }
  112. private void SyncQueuedTags()
  113. {
  114. AL.GetSource(SourceId, ALGetSourcei.BuffersQueued, out int QueuedCount);
  115. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
  116. QueuedCount -= ReleasedCount;
  117. while (QueuedTagsQueue.Count > QueuedCount)
  118. {
  119. ReleasedTagsQueue.Enqueue(QueuedTagsQueue.Dequeue());
  120. }
  121. while (ReleasedTagsQueue.Count > MaxReleased)
  122. {
  123. ReleasedTagsQueue.Dequeue();
  124. }
  125. }
  126. public void Dispose()
  127. {
  128. Dispose(true);
  129. }
  130. protected virtual void Dispose(bool Disposing)
  131. {
  132. if (Disposing && !Disposed)
  133. {
  134. Disposed = true;
  135. AL.DeleteSource(SourceId);
  136. foreach (int Id in Buffers.Values)
  137. {
  138. AL.DeleteBuffer(Id);
  139. }
  140. }
  141. }
  142. }
  143. private ConcurrentDictionary<int, Track> Tracks;
  144. private Thread AudioPollerThread;
  145. private bool KeepPolling;
  146. public OpenALAudioOut()
  147. {
  148. Context = new AudioContext();
  149. Tracks = new ConcurrentDictionary<int, Track>();
  150. KeepPolling = true;
  151. AudioPollerThread = new Thread(AudioPollerWork);
  152. AudioPollerThread.Start();
  153. }
  154. private void AudioPollerWork()
  155. {
  156. do
  157. {
  158. foreach (Track Td in Tracks.Values)
  159. {
  160. Td.CallReleaseCallbackIfNeeded();
  161. }
  162. Thread.Yield();
  163. }
  164. while (KeepPolling);
  165. }
  166. public int OpenTrack(int SampleRate, int Channels, ReleaseCallback Callback)
  167. {
  168. Track Td = new Track(SampleRate, GetALFormat(Channels), Callback);
  169. for (int Id = 0; Id < MaxTracks; Id++)
  170. {
  171. if (Tracks.TryAdd(Id, Td))
  172. {
  173. return Id;
  174. }
  175. }
  176. return -1;
  177. }
  178. private ALFormat GetALFormat(int Channels)
  179. {
  180. switch (Channels)
  181. {
  182. case 1: return ALFormat.Mono16;
  183. case 2: return ALFormat.Stereo16;
  184. case 6: return ALFormat.Multi51Chn16Ext;
  185. }
  186. throw new ArgumentOutOfRangeException(nameof(Channels));
  187. }
  188. public void CloseTrack(int Track)
  189. {
  190. if (Tracks.TryRemove(Track, out Track Td))
  191. {
  192. Td.Dispose();
  193. }
  194. }
  195. public bool ContainsBuffer(int Track, long Tag)
  196. {
  197. if (Tracks.TryGetValue(Track, out Track Td))
  198. {
  199. return Td.ContainsBuffer(Tag);
  200. }
  201. return false;
  202. }
  203. public long[] GetReleasedBuffers(int Track, int MaxCount)
  204. {
  205. if (Tracks.TryGetValue(Track, out Track Td))
  206. {
  207. return Td.GetReleasedBuffers(MaxCount);
  208. }
  209. return null;
  210. }
  211. public void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct
  212. {
  213. if (Tracks.TryGetValue(Track, out Track Td))
  214. {
  215. int BufferId = Td.AppendBuffer(Tag);
  216. int Size = Buffer.Length * Marshal.SizeOf<T>();
  217. AL.BufferData<T>(BufferId, Td.Format, Buffer, Size, Td.SampleRate);
  218. AL.SourceQueueBuffer(Td.SourceId, BufferId);
  219. StartPlaybackIfNeeded(Td);
  220. }
  221. }
  222. public void Start(int Track)
  223. {
  224. if (Tracks.TryGetValue(Track, out Track Td))
  225. {
  226. Td.State = PlaybackState.Playing;
  227. StartPlaybackIfNeeded(Td);
  228. }
  229. }
  230. private void StartPlaybackIfNeeded(Track Td)
  231. {
  232. AL.GetSource(Td.SourceId, ALGetSourcei.SourceState, out int StateInt);
  233. ALSourceState State = (ALSourceState)StateInt;
  234. if (State != ALSourceState.Playing && Td.State == PlaybackState.Playing)
  235. {
  236. Td.ClearReleased();
  237. AL.SourcePlay(Td.SourceId);
  238. }
  239. }
  240. public void Stop(int Track)
  241. {
  242. if (Tracks.TryGetValue(Track, out Track Td))
  243. {
  244. Td.State = PlaybackState.Stopped;
  245. AL.SourceStop(Td.SourceId);
  246. }
  247. }
  248. public PlaybackState GetState(int Track)
  249. {
  250. if (Tracks.TryGetValue(Track, out Track Td))
  251. {
  252. return Td.State;
  253. }
  254. return PlaybackState.Stopped;
  255. }
  256. }
  257. }