VoiceContext.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Audio.Adpcm;
  3. using System;
  4. namespace Ryujinx.HLE.HOS.Services.Aud.AudioRenderer
  5. {
  6. class VoiceContext
  7. {
  8. private bool _acquired;
  9. private bool _bufferReload;
  10. private int _resamplerFracPart;
  11. private int _bufferIndex;
  12. private int _offset;
  13. public int SampleRate { get; set; }
  14. public int ChannelsCount { get; set; }
  15. public float Volume { get; set; }
  16. public PlayState PlayState { get; set; }
  17. public SampleFormat SampleFormat { get; set; }
  18. public AdpcmDecoderContext AdpcmCtx { get; set; }
  19. public WaveBuffer[] WaveBuffers { get; }
  20. public WaveBuffer CurrentWaveBuffer => WaveBuffers[_bufferIndex];
  21. private VoiceOut _outStatus;
  22. public VoiceOut OutStatus => _outStatus;
  23. private int[] _samples;
  24. public bool Playing => _acquired && PlayState == PlayState.Playing;
  25. public VoiceContext()
  26. {
  27. WaveBuffers = new WaveBuffer[4];
  28. }
  29. public void SetAcquireState(bool newState)
  30. {
  31. if (_acquired && !newState)
  32. {
  33. //Release.
  34. Reset();
  35. }
  36. _acquired = newState;
  37. }
  38. private void Reset()
  39. {
  40. _bufferReload = true;
  41. _bufferIndex = 0;
  42. _offset = 0;
  43. _outStatus.PlayedSamplesCount = 0;
  44. _outStatus.PlayedWaveBuffersCount = 0;
  45. _outStatus.VoiceDropsCount = 0;
  46. }
  47. public int[] GetBufferData(MemoryManager memory, int maxSamples, out int samplesCount)
  48. {
  49. if (!Playing)
  50. {
  51. samplesCount = 0;
  52. return null;
  53. }
  54. if (_bufferReload)
  55. {
  56. _bufferReload = false;
  57. UpdateBuffer(memory);
  58. }
  59. WaveBuffer wb = WaveBuffers[_bufferIndex];
  60. int maxSize = _samples.Length - _offset;
  61. int size = maxSamples * AudioConsts.HostChannelsCount;
  62. if (size > maxSize)
  63. {
  64. size = maxSize;
  65. }
  66. int[] output = new int[size];
  67. Array.Copy(_samples, _offset, output, 0, size);
  68. samplesCount = size / AudioConsts.HostChannelsCount;
  69. _outStatus.PlayedSamplesCount += samplesCount;
  70. _offset += size;
  71. if (_offset == _samples.Length)
  72. {
  73. _offset = 0;
  74. if (wb.Looping == 0)
  75. {
  76. SetBufferIndex((_bufferIndex + 1) & 3);
  77. }
  78. _outStatus.PlayedWaveBuffersCount++;
  79. if (wb.LastBuffer != 0)
  80. {
  81. PlayState = PlayState.Paused;
  82. }
  83. }
  84. return output;
  85. }
  86. private void UpdateBuffer(MemoryManager memory)
  87. {
  88. //TODO: Implement conversion for formats other
  89. //than interleaved stereo (2 channels).
  90. //As of now, it assumes that HostChannelsCount == 2.
  91. WaveBuffer wb = WaveBuffers[_bufferIndex];
  92. if (wb.Position == 0)
  93. {
  94. _samples = new int[0];
  95. return;
  96. }
  97. if (SampleFormat == SampleFormat.PcmInt16)
  98. {
  99. int samplesCount = (int)(wb.Size / (sizeof(short) * ChannelsCount));
  100. _samples = new int[samplesCount * AudioConsts.HostChannelsCount];
  101. if (ChannelsCount == 1)
  102. {
  103. for (int index = 0; index < samplesCount; index++)
  104. {
  105. short sample = memory.ReadInt16(wb.Position + index * 2);
  106. _samples[index * 2 + 0] = sample;
  107. _samples[index * 2 + 1] = sample;
  108. }
  109. }
  110. else
  111. {
  112. for (int index = 0; index < samplesCount * 2; index++)
  113. {
  114. _samples[index] = memory.ReadInt16(wb.Position + index * 2);
  115. }
  116. }
  117. }
  118. else if (SampleFormat == SampleFormat.Adpcm)
  119. {
  120. byte[] buffer = memory.ReadBytes(wb.Position, wb.Size);
  121. _samples = AdpcmDecoder.Decode(buffer, AdpcmCtx);
  122. }
  123. else
  124. {
  125. throw new InvalidOperationException();
  126. }
  127. if (SampleRate != AudioConsts.HostSampleRate)
  128. {
  129. //TODO: We should keep the frames being discarded (see the 4 below)
  130. //on a buffer and include it on the next samples buffer, to allow
  131. //the resampler to do seamless interpolation between wave buffers.
  132. int samplesCount = _samples.Length / AudioConsts.HostChannelsCount;
  133. samplesCount = Math.Max(samplesCount - 4, 0);
  134. _samples = Resampler.Resample2Ch(
  135. _samples,
  136. SampleRate,
  137. AudioConsts.HostSampleRate,
  138. samplesCount,
  139. ref _resamplerFracPart);
  140. }
  141. }
  142. public void SetBufferIndex(int index)
  143. {
  144. _bufferIndex = index & 3;
  145. _bufferReload = true;
  146. }
  147. }
  148. }