CompatLayerHardwareDeviceSession.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Ryujinx.Audio.Backends.Common;
  2. using Ryujinx.Audio.Common;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Audio.Backends.CompatLayer
  6. {
  7. class CompatLayerHardwareDeviceSession : HardwareDeviceSessionOutputBase
  8. {
  9. private HardwareDeviceSessionOutputBase _realSession;
  10. private uint _userChannelCount;
  11. public CompatLayerHardwareDeviceSession(HardwareDeviceSessionOutputBase realSession, uint userChannelCount) : base(realSession.MemoryManager, realSession.RequestedSampleFormat, realSession.RequestedSampleRate, userChannelCount)
  12. {
  13. _realSession = realSession;
  14. _userChannelCount = userChannelCount;
  15. }
  16. public override void Dispose()
  17. {
  18. _realSession.Dispose();
  19. }
  20. public override ulong GetPlayedSampleCount()
  21. {
  22. return _realSession.GetPlayedSampleCount();
  23. }
  24. public override float GetVolume()
  25. {
  26. return _realSession.GetVolume();
  27. }
  28. public override void PrepareToClose()
  29. {
  30. _realSession.PrepareToClose();
  31. }
  32. public override void QueueBuffer(AudioBuffer buffer)
  33. {
  34. _realSession.QueueBuffer(buffer);
  35. }
  36. public override bool RegisterBuffer(AudioBuffer buffer, byte[] samples)
  37. {
  38. if (RequestedSampleFormat != SampleFormat.PcmInt16)
  39. {
  40. throw new NotImplementedException("Downmixing formats other than PCM16 is not supported.");
  41. }
  42. if (samples == null)
  43. {
  44. return false;
  45. }
  46. short[] downmixedBufferPCM16;
  47. ReadOnlySpan<short> samplesPCM16 = MemoryMarshal.Cast<byte, short>(samples);
  48. if (_userChannelCount == 6)
  49. {
  50. downmixedBufferPCM16 = Downmixing.DownMixSurroundToStereo(samplesPCM16);
  51. if (_realSession.RequestedChannelCount == 1)
  52. {
  53. downmixedBufferPCM16 = Downmixing.DownMixStereoToMono(downmixedBufferPCM16);
  54. }
  55. }
  56. else if (_userChannelCount == 2 && _realSession.RequestedChannelCount == 1)
  57. {
  58. downmixedBufferPCM16 = Downmixing.DownMixStereoToMono(samplesPCM16);
  59. }
  60. else
  61. {
  62. throw new NotImplementedException($"Downmixing from {_userChannelCount} to {_realSession.RequestedChannelCount} not implemented.");
  63. }
  64. byte[] downmixedBuffer = MemoryMarshal.Cast<short, byte>(downmixedBufferPCM16).ToArray();
  65. AudioBuffer fakeBuffer = new AudioBuffer
  66. {
  67. BufferTag = buffer.BufferTag,
  68. DataPointer = buffer.DataPointer,
  69. DataSize = (ulong)downmixedBuffer.Length
  70. };
  71. bool result = _realSession.RegisterBuffer(fakeBuffer, downmixedBuffer);
  72. if (result)
  73. {
  74. buffer.Data = fakeBuffer.Data;
  75. buffer.DataSize = fakeBuffer.DataSize;
  76. }
  77. return result;
  78. }
  79. public override void SetVolume(float volume)
  80. {
  81. _realSession.SetVolume(volume);
  82. }
  83. public override void Start()
  84. {
  85. _realSession.Start();
  86. }
  87. public override void Stop()
  88. {
  89. _realSession.Stop();
  90. }
  91. public override void UnregisterBuffer(AudioBuffer buffer)
  92. {
  93. _realSession.UnregisterBuffer(buffer);
  94. }
  95. public override bool WasBufferFullyConsumed(AudioBuffer buffer)
  96. {
  97. return _realSession.WasBufferFullyConsumed(buffer);
  98. }
  99. }
  100. }