HardwareDeviceImpl.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Ryujinx.Audio.Common;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace Ryujinx.Audio.Integration
  5. {
  6. public class HardwareDeviceImpl : IHardwareDevice
  7. {
  8. private IHardwareDeviceSession _session;
  9. private uint _channelCount;
  10. private uint _sampleRate;
  11. private uint _currentBufferTag;
  12. private byte[] _buffer;
  13. public HardwareDeviceImpl(IHardwareDeviceDriver deviceDriver, uint channelCount, uint sampleRate, float volume)
  14. {
  15. _session = deviceDriver.OpenDeviceSession(IHardwareDeviceDriver.Direction.Output, null, SampleFormat.PcmInt16, sampleRate, channelCount, volume);
  16. _channelCount = channelCount;
  17. _sampleRate = sampleRate;
  18. _currentBufferTag = 0;
  19. _buffer = new byte[Constants.TargetSampleCount * channelCount * sizeof(ushort)];
  20. _session.Start();
  21. }
  22. public void AppendBuffer(ReadOnlySpan<short> data, uint channelCount)
  23. {
  24. data.CopyTo(MemoryMarshal.Cast<byte, short>(_buffer));
  25. _session.QueueBuffer(new AudioBuffer
  26. {
  27. DataPointer = _currentBufferTag++,
  28. Data = _buffer,
  29. DataSize = (ulong)_buffer.Length,
  30. });
  31. _currentBufferTag = _currentBufferTag % 4;
  32. }
  33. public void SetVolume(float volume)
  34. {
  35. _session.SetVolume(volume);
  36. }
  37. public float GetVolume()
  38. {
  39. return _session.GetVolume();
  40. }
  41. public uint GetChannelCount()
  42. {
  43. return _channelCount;
  44. }
  45. public uint GetSampleRate()
  46. {
  47. return _sampleRate;
  48. }
  49. public void Dispose()
  50. {
  51. Dispose(true);
  52. }
  53. protected virtual void Dispose(bool disposing)
  54. {
  55. if (disposing)
  56. {
  57. _session.Dispose();
  58. }
  59. }
  60. }
  61. }