VoiceContext.cs 5.1 KB

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