HardwareDeviceSessionOutputBase.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. namespace Ryujinx.Audio.Backends.Common
  21. {
  22. public abstract class HardwareDeviceSessionOutputBase : IHardwareDeviceSession
  23. {
  24. public IVirtualMemoryManager MemoryManager { get; }
  25. public SampleFormat RequestedSampleFormat { get; }
  26. public uint RequestedSampleRate { get; }
  27. public uint RequestedChannelCount { get; }
  28. public HardwareDeviceSessionOutputBase(IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount)
  29. {
  30. MemoryManager = memoryManager;
  31. RequestedSampleFormat = requestedSampleFormat;
  32. RequestedSampleRate = requestedSampleRate;
  33. RequestedChannelCount = requestedChannelCount;
  34. }
  35. private byte[] GetBufferSamples(AudioBuffer buffer)
  36. {
  37. if (buffer.DataPointer == 0)
  38. {
  39. return null;
  40. }
  41. byte[] data = new byte[buffer.DataSize];
  42. MemoryManager.Read(buffer.DataPointer, data);
  43. return data;
  44. }
  45. protected ulong GetSampleCount(AudioBuffer buffer)
  46. {
  47. return (ulong)BackendHelper.GetSampleCount(RequestedSampleFormat, (int)RequestedChannelCount, (int)buffer.DataSize);
  48. }
  49. public abstract void Dispose();
  50. public abstract void PrepareToClose();
  51. public abstract void QueueBuffer(AudioBuffer buffer);
  52. public abstract void SetVolume(float volume);
  53. public abstract float GetVolume();
  54. public abstract void Start();
  55. public abstract void Stop();
  56. public abstract ulong GetPlayedSampleCount();
  57. public abstract bool WasBufferFullyConsumed(AudioBuffer buffer);
  58. public virtual bool RegisterBuffer(AudioBuffer buffer)
  59. {
  60. return RegisterBuffer(buffer, GetBufferSamples(buffer));
  61. }
  62. public virtual bool RegisterBuffer(AudioBuffer buffer, byte[] samples)
  63. {
  64. if (samples == null)
  65. {
  66. return false;
  67. }
  68. if (buffer.Data == null)
  69. {
  70. buffer.Data = samples;
  71. }
  72. return true;
  73. }
  74. public virtual void UnregisterBuffer(AudioBuffer buffer) { }
  75. }
  76. }