OpenALAudioOut.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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, IDisposable
  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 ConcurrentDictionary<long, int> Buffers;
  23. private Queue<long> QueuedTagsQueue;
  24. private Queue<long> ReleasedTagsQueue;
  25. private bool Disposed;
  26. public Track(int SampleRate, ALFormat Format, ReleaseCallback Callback)
  27. {
  28. this.SampleRate = SampleRate;
  29. this.Format = Format;
  30. this.Callback = Callback;
  31. State = PlaybackState.Stopped;
  32. SourceId = AL.GenSource();
  33. Buffers = new ConcurrentDictionary<long, int>();
  34. QueuedTagsQueue = new Queue<long>();
  35. ReleasedTagsQueue = new Queue<long>();
  36. }
  37. public bool ContainsBuffer(long Tag)
  38. {
  39. foreach (long QueuedTag in QueuedTagsQueue)
  40. {
  41. if (QueuedTag == Tag)
  42. {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. public long[] GetReleasedBuffers(int Count)
  49. {
  50. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
  51. ReleasedCount += ReleasedTagsQueue.Count;
  52. if (Count > ReleasedCount)
  53. {
  54. Count = ReleasedCount;
  55. }
  56. List<long> Tags = new List<long>();
  57. while (Count-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
  58. {
  59. Tags.Add(Tag);
  60. }
  61. while (Count-- > 0 && QueuedTagsQueue.TryDequeue(out long Tag))
  62. {
  63. AL.SourceUnqueueBuffers(SourceId, 1);
  64. Tags.Add(Tag);
  65. }
  66. return Tags.ToArray();
  67. }
  68. public int AppendBuffer(long Tag)
  69. {
  70. if (Disposed)
  71. {
  72. throw new ObjectDisposedException(nameof(Track));
  73. }
  74. int Id = AL.GenBuffer();
  75. Buffers.AddOrUpdate(Tag, Id, (Key, OldId) =>
  76. {
  77. AL.DeleteBuffer(OldId);
  78. return Id;
  79. });
  80. QueuedTagsQueue.Enqueue(Tag);
  81. return Id;
  82. }
  83. public void CallReleaseCallbackIfNeeded()
  84. {
  85. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
  86. if (ReleasedCount > 0)
  87. {
  88. //If we signal, then we also need to have released buffers available
  89. //to return when GetReleasedBuffers is called.
  90. //If playback needs to be re-started due to all buffers being processed,
  91. //then OpenAL zeros the counts (ReleasedCount), so we keep it on the queue.
  92. while (ReleasedCount-- > 0 && QueuedTagsQueue.TryDequeue(out long Tag))
  93. {
  94. AL.SourceUnqueueBuffers(SourceId, 1);
  95. ReleasedTagsQueue.Enqueue(Tag);
  96. }
  97. Callback();
  98. }
  99. }
  100. public void Dispose()
  101. {
  102. Dispose(true);
  103. }
  104. protected virtual void Dispose(bool Disposing)
  105. {
  106. if (Disposing && !Disposed)
  107. {
  108. Disposed = true;
  109. AL.DeleteSource(SourceId);
  110. foreach (int Id in Buffers.Values)
  111. {
  112. AL.DeleteBuffer(Id);
  113. }
  114. }
  115. }
  116. }
  117. private ConcurrentDictionary<int, Track> Tracks;
  118. private Thread AudioPollerThread;
  119. private bool KeepPolling;
  120. public OpenALAudioOut()
  121. {
  122. Context = new AudioContext();
  123. Tracks = new ConcurrentDictionary<int, Track>();
  124. KeepPolling = true;
  125. AudioPollerThread = new Thread(AudioPollerWork);
  126. AudioPollerThread.Start();
  127. }
  128. private void AudioPollerWork()
  129. {
  130. do
  131. {
  132. foreach (Track Td in Tracks.Values)
  133. {
  134. lock (Td)
  135. {
  136. Td.CallReleaseCallbackIfNeeded();
  137. }
  138. }
  139. //If it's not slept it will waste cycles.
  140. Thread.Sleep(10);
  141. }
  142. while (KeepPolling);
  143. foreach (Track Td in Tracks.Values)
  144. {
  145. Td.Dispose();
  146. }
  147. Tracks.Clear();
  148. }
  149. public int OpenTrack(int SampleRate, int Channels, ReleaseCallback Callback)
  150. {
  151. Track Td = new Track(SampleRate, GetALFormat(Channels), Callback);
  152. for (int Id = 0; Id < MaxTracks; Id++)
  153. {
  154. if (Tracks.TryAdd(Id, Td))
  155. {
  156. return Id;
  157. }
  158. }
  159. return -1;
  160. }
  161. private ALFormat GetALFormat(int Channels)
  162. {
  163. switch (Channels)
  164. {
  165. case 1: return ALFormat.Mono16;
  166. case 2: return ALFormat.Stereo16;
  167. case 6: return ALFormat.Multi51Chn16Ext;
  168. }
  169. throw new ArgumentOutOfRangeException(nameof(Channels));
  170. }
  171. public void CloseTrack(int Track)
  172. {
  173. if (Tracks.TryRemove(Track, out Track Td))
  174. {
  175. lock (Td)
  176. {
  177. Td.Dispose();
  178. }
  179. }
  180. }
  181. public bool ContainsBuffer(int Track, long Tag)
  182. {
  183. if (Tracks.TryGetValue(Track, out Track Td))
  184. {
  185. lock (Td)
  186. {
  187. return Td.ContainsBuffer(Tag);
  188. }
  189. }
  190. return false;
  191. }
  192. public long[] GetReleasedBuffers(int Track, int MaxCount)
  193. {
  194. if (Tracks.TryGetValue(Track, out Track Td))
  195. {
  196. lock (Td)
  197. {
  198. return Td.GetReleasedBuffers(MaxCount);
  199. }
  200. }
  201. return null;
  202. }
  203. public void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct
  204. {
  205. if (Tracks.TryGetValue(Track, out Track Td))
  206. {
  207. lock (Td)
  208. {
  209. int BufferId = Td.AppendBuffer(Tag);
  210. int Size = Buffer.Length * Marshal.SizeOf<T>();
  211. AL.BufferData<T>(BufferId, Td.Format, Buffer, Size, Td.SampleRate);
  212. AL.SourceQueueBuffer(Td.SourceId, BufferId);
  213. StartPlaybackIfNeeded(Td);
  214. }
  215. }
  216. }
  217. public void Start(int Track)
  218. {
  219. if (Tracks.TryGetValue(Track, out Track Td))
  220. {
  221. lock (Td)
  222. {
  223. Td.State = PlaybackState.Playing;
  224. StartPlaybackIfNeeded(Td);
  225. }
  226. }
  227. }
  228. private void StartPlaybackIfNeeded(Track Td)
  229. {
  230. AL.GetSource(Td.SourceId, ALGetSourcei.SourceState, out int StateInt);
  231. ALSourceState State = (ALSourceState)StateInt;
  232. if (State != ALSourceState.Playing && Td.State == PlaybackState.Playing)
  233. {
  234. AL.SourcePlay(Td.SourceId);
  235. }
  236. }
  237. public void Stop(int Track)
  238. {
  239. if (Tracks.TryGetValue(Track, out Track Td))
  240. {
  241. lock (Td)
  242. {
  243. Td.State = PlaybackState.Stopped;
  244. AL.SourceStop(Td.SourceId);
  245. }
  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. public void Dispose()
  257. {
  258. Dispose(true);
  259. }
  260. protected virtual void Dispose(bool Disposing)
  261. {
  262. if (Disposing)
  263. {
  264. KeepPolling = false;
  265. }
  266. }
  267. }
  268. }