VoiceUpdateState.cs 3.8 KB

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