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