HardwareDeviceSessionOutputBase.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using Ryujinx.Audio.Common;
  18. using Ryujinx.Audio.Integration;
  19. using Ryujinx.Memory;
  20. using System.Runtime.CompilerServices;
  21. namespace Ryujinx.Audio.Backends.Common
  22. {
  23. public abstract class HardwareDeviceSessionOutputBase : IHardwareDeviceSession
  24. {
  25. public IVirtualMemoryManager MemoryManager { get; }
  26. public SampleFormat RequestedSampleFormat { get; }
  27. public uint RequestedSampleRate { get; }
  28. public uint RequestedChannelCount { get; }
  29. public HardwareDeviceSessionOutputBase(IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount)
  30. {
  31. MemoryManager = memoryManager;
  32. RequestedSampleFormat = requestedSampleFormat;
  33. RequestedSampleRate = requestedSampleRate;
  34. RequestedChannelCount = requestedChannelCount;
  35. }
  36. private byte[] GetBufferSamples(AudioBuffer buffer)
  37. {
  38. if (buffer.DataPointer == 0)
  39. {
  40. return null;
  41. }
  42. byte[] data = new byte[buffer.DataSize];
  43. MemoryManager.Read(buffer.DataPointer, data);
  44. return data;
  45. }
  46. protected ulong GetSampleCount(AudioBuffer buffer)
  47. {
  48. return GetSampleCount((int)buffer.DataSize);
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. protected ulong GetSampleCount(int dataSize)
  52. {
  53. return (ulong)BackendHelper.GetSampleCount(RequestedSampleFormat, (int)RequestedChannelCount, dataSize);
  54. }
  55. public abstract void Dispose();
  56. public abstract void PrepareToClose();
  57. public abstract void QueueBuffer(AudioBuffer buffer);
  58. public abstract void SetVolume(float volume);
  59. public abstract float GetVolume();
  60. public abstract void Start();
  61. public abstract void Stop();
  62. public abstract ulong GetPlayedSampleCount();
  63. public abstract bool WasBufferFullyConsumed(AudioBuffer buffer);
  64. public virtual bool RegisterBuffer(AudioBuffer buffer)
  65. {
  66. return RegisterBuffer(buffer, GetBufferSamples(buffer));
  67. }
  68. public virtual bool RegisterBuffer(AudioBuffer buffer, byte[] samples)
  69. {
  70. if (samples == null)
  71. {
  72. return false;
  73. }
  74. if (buffer.Data == null)
  75. {
  76. buffer.Data = samples;
  77. }
  78. return true;
  79. }
  80. public virtual void UnregisterBuffer(AudioBuffer buffer) { }
  81. }
  82. }