HardwareDeviceImpl.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 System;
  19. using System.Runtime.InteropServices;
  20. namespace Ryujinx.Audio.Integration
  21. {
  22. public class HardwareDeviceImpl : IHardwareDevice
  23. {
  24. private IHardwareDeviceSession _session;
  25. private uint _channelCount;
  26. private uint _sampleRate;
  27. private uint _currentBufferTag;
  28. private byte[] _buffer;
  29. public HardwareDeviceImpl(IHardwareDeviceDriver deviceDriver, uint channelCount, uint sampleRate, float volume)
  30. {
  31. _session = deviceDriver.OpenDeviceSession(IHardwareDeviceDriver.Direction.Output, null, SampleFormat.PcmInt16, sampleRate, channelCount, volume);
  32. _channelCount = channelCount;
  33. _sampleRate = sampleRate;
  34. _currentBufferTag = 0;
  35. _buffer = new byte[Constants.TargetSampleCount * channelCount * sizeof(ushort)];
  36. _session.Start();
  37. }
  38. public void AppendBuffer(ReadOnlySpan<short> data, uint channelCount)
  39. {
  40. data.CopyTo(MemoryMarshal.Cast<byte, short>(_buffer));
  41. _session.QueueBuffer(new AudioBuffer
  42. {
  43. DataPointer = _currentBufferTag++,
  44. Data = _buffer,
  45. DataSize = (ulong)_buffer.Length,
  46. });
  47. _currentBufferTag = _currentBufferTag % 4;
  48. }
  49. public void SetVolume(float volume)
  50. {
  51. _session.SetVolume(volume);
  52. }
  53. public float GetVolume()
  54. {
  55. return _session.GetVolume();
  56. }
  57. public uint GetChannelCount()
  58. {
  59. return _channelCount;
  60. }
  61. public uint GetSampleRate()
  62. {
  63. return _sampleRate;
  64. }
  65. public void Dispose()
  66. {
  67. Dispose(true);
  68. }
  69. protected virtual void Dispose(bool disposing)
  70. {
  71. if (disposing)
  72. {
  73. _session.Dispose();
  74. }
  75. }
  76. }
  77. }