OpenALAudioTrack.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using OpenTK.Audio.OpenAL;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Audio
  6. {
  7. internal class OpenALAudioTrack : IDisposable
  8. {
  9. public int SourceId { get; private set; }
  10. public int SampleRate { get; private set; }
  11. public ALFormat Format { get; private set; }
  12. public PlaybackState State { get; set; }
  13. private ReleaseCallback _callback;
  14. private ConcurrentDictionary<long, int> _buffers;
  15. private Queue<long> _queuedTagsQueue;
  16. private Queue<long> _releasedTagsQueue;
  17. private bool _disposed;
  18. public OpenALAudioTrack(int sampleRate, ALFormat format, ReleaseCallback callback)
  19. {
  20. SampleRate = sampleRate;
  21. Format = format;
  22. State = PlaybackState.Stopped;
  23. SourceId = AL.GenSource();
  24. _callback = callback;
  25. _buffers = new ConcurrentDictionary<long, int>();
  26. _queuedTagsQueue = new Queue<long>();
  27. _releasedTagsQueue = new Queue<long>();
  28. }
  29. public bool ContainsBuffer(long tag)
  30. {
  31. foreach (long queuedTag in _queuedTagsQueue)
  32. {
  33. if (queuedTag == tag)
  34. {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. public long[] GetReleasedBuffers(int count)
  41. {
  42. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int releasedCount);
  43. releasedCount += _releasedTagsQueue.Count;
  44. if (count > releasedCount)
  45. {
  46. count = releasedCount;
  47. }
  48. List<long> tags = new List<long>();
  49. while (count-- > 0 && _releasedTagsQueue.TryDequeue(out long tag))
  50. {
  51. tags.Add(tag);
  52. }
  53. while (count-- > 0 && _queuedTagsQueue.TryDequeue(out long tag))
  54. {
  55. AL.SourceUnqueueBuffers(SourceId, 1);
  56. tags.Add(tag);
  57. }
  58. return tags.ToArray();
  59. }
  60. public int AppendBuffer(long tag)
  61. {
  62. if (_disposed)
  63. {
  64. throw new ObjectDisposedException(GetType().Name);
  65. }
  66. int id = AL.GenBuffer();
  67. _buffers.AddOrUpdate(tag, id, (key, oldId) =>
  68. {
  69. AL.DeleteBuffer(oldId);
  70. return id;
  71. });
  72. _queuedTagsQueue.Enqueue(tag);
  73. return id;
  74. }
  75. public void CallReleaseCallbackIfNeeded()
  76. {
  77. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int releasedCount);
  78. if (releasedCount > 0)
  79. {
  80. // If we signal, then we also need to have released buffers available
  81. // to return when GetReleasedBuffers is called.
  82. // If playback needs to be re-started due to all buffers being processed,
  83. // then OpenAL zeros the counts (ReleasedCount), so we keep it on the queue.
  84. while (releasedCount-- > 0 && _queuedTagsQueue.TryDequeue(out long tag))
  85. {
  86. AL.SourceUnqueueBuffers(SourceId, 1);
  87. _releasedTagsQueue.Enqueue(tag);
  88. }
  89. _callback();
  90. }
  91. }
  92. public void Dispose()
  93. {
  94. Dispose(true);
  95. }
  96. protected virtual void Dispose(bool disposing)
  97. {
  98. if (disposing && !_disposed)
  99. {
  100. _disposed = true;
  101. AL.DeleteSource(SourceId);
  102. foreach (int id in _buffers.Values)
  103. {
  104. AL.DeleteBuffer(id);
  105. }
  106. }
  107. }
  108. }
  109. }