OpenALAudioOut.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. //If it's not slept it will waste cycles
  163. Thread.Sleep(10);
  164. }
  165. while (KeepPolling);
  166. }
  167. public int OpenTrack(int SampleRate, int Channels, ReleaseCallback Callback)
  168. {
  169. Track Td = new Track(SampleRate, GetALFormat(Channels), Callback);
  170. for (int Id = 0; Id < MaxTracks; Id++)
  171. {
  172. if (Tracks.TryAdd(Id, Td))
  173. {
  174. return Id;
  175. }
  176. }
  177. return -1;
  178. }
  179. private ALFormat GetALFormat(int Channels)
  180. {
  181. switch (Channels)
  182. {
  183. case 1: return ALFormat.Mono16;
  184. case 2: return ALFormat.Stereo16;
  185. case 6: return ALFormat.Multi51Chn16Ext;
  186. }
  187. throw new ArgumentOutOfRangeException(nameof(Channels));
  188. }
  189. public void CloseTrack(int Track)
  190. {
  191. if (Tracks.TryRemove(Track, out Track Td))
  192. {
  193. Td.Dispose();
  194. }
  195. }
  196. public bool ContainsBuffer(int Track, long Tag)
  197. {
  198. if (Tracks.TryGetValue(Track, out Track Td))
  199. {
  200. return Td.ContainsBuffer(Tag);
  201. }
  202. return false;
  203. }
  204. public long[] GetReleasedBuffers(int Track, int MaxCount)
  205. {
  206. if (Tracks.TryGetValue(Track, out Track Td))
  207. {
  208. return Td.GetReleasedBuffers(MaxCount);
  209. }
  210. return null;
  211. }
  212. public void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct
  213. {
  214. if (Tracks.TryGetValue(Track, out Track Td))
  215. {
  216. int BufferId = Td.AppendBuffer(Tag);
  217. int Size = Buffer.Length * Marshal.SizeOf<T>();
  218. AL.BufferData<T>(BufferId, Td.Format, Buffer, Size, Td.SampleRate);
  219. AL.SourceQueueBuffer(Td.SourceId, BufferId);
  220. StartPlaybackIfNeeded(Td);
  221. }
  222. }
  223. public void Start(int Track)
  224. {
  225. if (Tracks.TryGetValue(Track, out Track Td))
  226. {
  227. Td.State = PlaybackState.Playing;
  228. StartPlaybackIfNeeded(Td);
  229. }
  230. }
  231. private void StartPlaybackIfNeeded(Track Td)
  232. {
  233. AL.GetSource(Td.SourceId, ALGetSourcei.SourceState, out int StateInt);
  234. ALSourceState State = (ALSourceState)StateInt;
  235. if (State != ALSourceState.Playing && Td.State == PlaybackState.Playing)
  236. {
  237. Td.ClearReleased();
  238. AL.SourcePlay(Td.SourceId);
  239. }
  240. }
  241. public void Stop(int Track)
  242. {
  243. if (Tracks.TryGetValue(Track, out Track Td))
  244. {
  245. Td.State = PlaybackState.Stopped;
  246. AL.SourceStop(Td.SourceId);
  247. }
  248. }
  249. public PlaybackState GetState(int Track)
  250. {
  251. if (Tracks.TryGetValue(Track, out Track Td))
  252. {
  253. return Td.State;
  254. }
  255. return PlaybackState.Stopped;
  256. }
  257. }
  258. }