OpenALAudioOut.cs 7.7 KB

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