VoiceUpdateState.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.Renderer.Dsp.State;
  18. using Ryujinx.Common.Memory;
  19. using Ryujinx.Common.Utilities;
  20. using System;
  21. using System.Runtime.InteropServices;
  22. namespace Ryujinx.Audio.Renderer.Common
  23. {
  24. /// <summary>
  25. /// Represent the update state of a voice.
  26. /// </summary>
  27. /// <remarks>This is shared between the server and audio processor.</remarks>
  28. [StructLayout(LayoutKind.Sequential, Pack = Align)]
  29. public struct VoiceUpdateState
  30. {
  31. public const int Align = 0x10;
  32. public const int BiquadStateOffset = 0x0;
  33. public const int BiquadStateSize = 0x10;
  34. /// <summary>
  35. /// The state of the biquad filters of this voice.
  36. /// </summary>
  37. public Array2<BiquadFilterState> BiquadFilterState;
  38. /// <summary>
  39. /// The total amount of samples that was played.
  40. /// </summary>
  41. /// <remarks>This is reset to 0 when a <see cref="WaveBuffer"/> finishes playing and <see cref="WaveBuffer.IsEndOfStream"/> is set.</remarks>
  42. /// <remarks>This is reset to 0 when looping while <see cref="Parameter.VoiceInParameter.DecodingBehaviour.PlayedSampleCountResetWhenLooping"/> is set.</remarks>
  43. public ulong PlayedSampleCount;
  44. /// <summary>
  45. /// The current sample offset in the <see cref="WaveBuffer"/> pointed by <see cref="WaveBufferIndex"/>.
  46. /// </summary>
  47. public int Offset;
  48. /// <summary>
  49. /// The current index of the <see cref="WaveBuffer"/> in use.
  50. /// </summary>
  51. public uint WaveBufferIndex;
  52. private WaveBufferValidArray _isWaveBufferValid;
  53. /// <summary>
  54. /// The total amount of <see cref="WaveBuffer"/> consumed.
  55. /// </summary>
  56. public uint WaveBufferConsumed;
  57. /// <summary>
  58. /// Pitch used for Sample Rate Conversion.
  59. /// </summary>
  60. public Array8<short> Pitch;
  61. public float Fraction;
  62. /// <summary>
  63. /// The ADPCM loop context when <see cref="SampleFormat.Adpcm"/> is in use.
  64. /// </summary>
  65. public AdpcmLoopContext LoopContext;
  66. /// <summary>
  67. /// The last samples after a mix ramp.
  68. /// </summary>
  69. /// <remarks>This is used for depop (to perform voice drop).</remarks>
  70. public Array24<float> LastSamples;
  71. /// <summary>
  72. /// The current count of loop performed.
  73. /// </summary>
  74. public int LoopCount;
  75. [StructLayout(LayoutKind.Sequential, Size = 1 * Constants.VoiceWaveBufferCount, Pack = 1)]
  76. private struct WaveBufferValidArray { }
  77. /// <summary>
  78. /// Contains information of <see cref="WaveBuffer"/> validity.
  79. /// </summary>
  80. public Span<bool> IsWaveBufferValid => SpanHelpers.AsSpan<WaveBufferValidArray, bool>(ref _isWaveBufferValid);
  81. /// <summary>
  82. /// Mark the current <see cref="WaveBuffer"/> as played and switch to the next one.
  83. /// </summary>
  84. /// <param name="waveBuffer">The current <see cref="WaveBuffer"/></param>
  85. /// <param name="waveBufferIndex">The wavebuffer index.</param>
  86. /// <param name="waveBufferConsumed">The amount of wavebuffers consumed.</param>
  87. /// <param name="playedSampleCount">The total count of sample played.</param>
  88. public void MarkEndOfBufferWaveBufferProcessing(ref WaveBuffer waveBuffer, ref int waveBufferIndex, ref uint waveBufferConsumed, ref ulong playedSampleCount)
  89. {
  90. IsWaveBufferValid[waveBufferIndex++] = false;
  91. LoopCount = 0;
  92. waveBufferConsumed++;
  93. if (waveBufferIndex >= Constants.VoiceWaveBufferCount)
  94. {
  95. waveBufferIndex = 0;
  96. }
  97. if (waveBuffer.IsEndOfStream)
  98. {
  99. playedSampleCount = 0;
  100. }
  101. }
  102. }
  103. }