OpenALAudioOut.cs 9.6 KB

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