OpenALAudioOut.cs 7.3 KB

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