OpenALAudioOut.cs 9.5 KB

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