DummyAudioOut.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Audio
  5. {
  6. /// <summary>
  7. /// A Dummy audio renderer that does not output any audio
  8. /// </summary>
  9. public class DummyAudioOut : IAalOutput
  10. {
  11. private int _lastTrackId = 1;
  12. private float _volume = 1.0f;
  13. private ConcurrentQueue<int> _trackIds;
  14. private ConcurrentQueue<long> _buffers;
  15. private ConcurrentDictionary<int, ReleaseCallback> _releaseCallbacks;
  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. if (_releaseCallbacks.TryGetValue(trackId, out var callback))
  63. {
  64. callback?.Invoke();
  65. }
  66. }
  67. public void Start(int trackId) { }
  68. public void Stop(int trackId) { }
  69. public float GetVolume() => _volume;
  70. public void SetVolume(float volume)
  71. {
  72. _volume = volume;
  73. }
  74. public void Dispose()
  75. {
  76. _buffers.Clear();
  77. }
  78. }
  79. }