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