OpenALAudioTrack.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. public int HardwareChannels { get; }
  14. public int VirtualChannels { get; }
  15. private ReleaseCallback _callback;
  16. private ConcurrentDictionary<long, int> _buffers;
  17. private Queue<long> _queuedTagsQueue;
  18. private Queue<long> _releasedTagsQueue;
  19. private bool _disposed;
  20. public OpenALAudioTrack(int sampleRate, ALFormat format, int hardwareChannels, int virtualChannels, ReleaseCallback callback)
  21. {
  22. SampleRate = sampleRate;
  23. Format = format;
  24. State = PlaybackState.Stopped;
  25. SourceId = AL.GenSource();
  26. HardwareChannels = hardwareChannels;
  27. VirtualChannels = virtualChannels;
  28. _callback = callback;
  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. foreach (long queuedTag in _queuedTagsQueue)
  36. {
  37. if (queuedTag == tag)
  38. {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. public long[] GetReleasedBuffers(int count)
  45. {
  46. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int releasedCount);
  47. releasedCount += _releasedTagsQueue.Count;
  48. if (count > releasedCount)
  49. {
  50. count = releasedCount;
  51. }
  52. List<long> tags = new List<long>();
  53. while (count-- > 0 && _releasedTagsQueue.TryDequeue(out long tag))
  54. {
  55. tags.Add(tag);
  56. }
  57. while (count-- > 0 && _queuedTagsQueue.TryDequeue(out long tag))
  58. {
  59. AL.SourceUnqueueBuffers(SourceId, 1);
  60. tags.Add(tag);
  61. }
  62. return tags.ToArray();
  63. }
  64. public int AppendBuffer(long tag)
  65. {
  66. if (_disposed)
  67. {
  68. throw new ObjectDisposedException(GetType().Name);
  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 CallReleaseCallbackIfNeeded()
  80. {
  81. AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int releasedCount);
  82. if (releasedCount > 0)
  83. {
  84. // If we signal, then we also need to have released buffers available
  85. // to return when GetReleasedBuffers is called.
  86. // If playback needs to be re-started due to all buffers being processed,
  87. // then OpenAL zeros the counts (ReleasedCount), so we keep it on the queue.
  88. while (releasedCount-- > 0 && _queuedTagsQueue.TryDequeue(out long tag))
  89. {
  90. AL.SourceUnqueueBuffers(SourceId, 1);
  91. _releasedTagsQueue.Enqueue(tag);
  92. }
  93. _callback();
  94. }
  95. }
  96. public void Dispose()
  97. {
  98. Dispose(true);
  99. }
  100. protected virtual void Dispose(bool disposing)
  101. {
  102. if (disposing && !_disposed)
  103. {
  104. _disposed = true;
  105. AL.DeleteSource(SourceId);
  106. foreach (int id in _buffers.Values)
  107. {
  108. AL.DeleteBuffer(id);
  109. }
  110. }
  111. }
  112. }
  113. }