DummyAudioOut.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections.Concurrent;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Audio
  4. {
  5. /// <summary>
  6. /// A Dummy audio renderer that does not output any audio
  7. /// </summary>
  8. public class DummyAudioOut : IAalOutput
  9. {
  10. private int _lastTrackId = 1;
  11. private float _volume = 1.0f;
  12. private ConcurrentQueue<int> _trackIds;
  13. private ConcurrentQueue<long> _buffers;
  14. private ConcurrentDictionary<int, ReleaseCallback> _releaseCallbacks;
  15. private ulong _playedSampleCount;
  16. public DummyAudioOut()
  17. {
  18. _buffers = new ConcurrentQueue<long>();
  19. _trackIds = new ConcurrentQueue<int>();
  20. _releaseCallbacks = new ConcurrentDictionary<int, ReleaseCallback>();
  21. }
  22. /// <summary>
  23. /// Dummy audio output is always available, Baka!
  24. /// </summary>
  25. public static bool IsSupported => true;
  26. public PlaybackState GetState(int trackId) => PlaybackState.Stopped;
  27. public bool SupportsChannelCount(int channels)
  28. {
  29. return true;
  30. }
  31. public int OpenHardwareTrack(int sampleRate, int hardwareChannels, int virtualChannels, ReleaseCallback callback)
  32. {
  33. if (!_trackIds.TryDequeue(out int trackId))
  34. {
  35. trackId = ++_lastTrackId;
  36. }
  37. _releaseCallbacks[trackId] = callback;
  38. return trackId;
  39. }
  40. public void CloseTrack(int trackId)
  41. {
  42. _trackIds.Enqueue(trackId);
  43. _releaseCallbacks.Remove(trackId, out _);
  44. }
  45. public bool ContainsBuffer(int trackID, long bufferTag) => false;
  46. public long[] GetReleasedBuffers(int trackId, int maxCount)
  47. {
  48. List<long> bufferTags = new List<long>();
  49. for (int i = 0; i < maxCount; i++)
  50. {
  51. if (!_buffers.TryDequeue(out long tag))
  52. {
  53. break;
  54. }
  55. bufferTags.Add(tag);
  56. }
  57. return bufferTags.ToArray();
  58. }
  59. public void AppendBuffer<T>(int trackId, long bufferTag, T[] buffer) where T : struct
  60. {
  61. _buffers.Enqueue(bufferTag);
  62. _playedSampleCount += (ulong)buffer.Length;
  63. if (_releaseCallbacks.TryGetValue(trackId, out var callback))
  64. {
  65. callback?.Invoke();
  66. }
  67. }
  68. public void Start(int trackId) { }
  69. public void Stop(int trackId) { }
  70. public uint GetBufferCount(int trackId) => (uint)_buffers.Count;
  71. public ulong GetPlayedSampleCount(int trackId) => _playedSampleCount;
  72. public bool FlushBuffers(int trackId) => false;
  73. public float GetVolume(int trackId) => _volume;
  74. public void SetVolume(int trackId, float volume)
  75. {
  76. _volume = volume;
  77. }
  78. public void Dispose()
  79. {
  80. _buffers.Clear();
  81. }
  82. }
  83. }