PcmHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Numerics;
  3. using System.Runtime.CompilerServices;
  4. namespace Ryujinx.Audio.Renderer.Dsp
  5. {
  6. public static class PcmHelper
  7. {
  8. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  9. public static int GetCountToDecode(int startSampleOffset, int endSampleOffset, int offset, int count)
  10. {
  11. return Math.Min(count, endSampleOffset - startSampleOffset - offset);
  12. }
  13. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  14. public static ulong GetBufferOffset<T>(int startSampleOffset, int offset, int channelCount) where T : unmanaged
  15. {
  16. return (ulong)(Unsafe.SizeOf<T>() * channelCount * (startSampleOffset + offset));
  17. }
  18. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  19. public static int GetBufferSize<T>(int startSampleOffset, int endSampleOffset, int offset, int count) where T : unmanaged
  20. {
  21. return GetCountToDecode(startSampleOffset, endSampleOffset, offset, count) * Unsafe.SizeOf<T>();
  22. }
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public static float ConvertSampleToPcmFloat(short sample)
  25. {
  26. return (float)sample / short.MaxValue;
  27. }
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public static short ConvertSampleToPcmInt16(float sample)
  30. {
  31. return Saturate(sample * short.MaxValue);
  32. }
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public static TOutput ConvertSample<TInput, TOutput>(TInput value) where TInput: INumber<TInput>, IMinMaxValue<TInput> where TOutput : INumber<TOutput>, IMinMaxValue<TOutput>
  35. {
  36. TInput conversionRate = TInput.CreateSaturating(TOutput.MaxValue / TOutput.CreateSaturating(TInput.MaxValue));
  37. return TOutput.CreateSaturating(value * conversionRate);
  38. }
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public static void Convert<TInput, TOutput>(Span<TOutput> output, ReadOnlySpan<TInput> input) where TInput : INumber<TInput>, IMinMaxValue<TInput> where TOutput : INumber<TOutput>, IMinMaxValue<TOutput>
  41. {
  42. for (int i = 0; i < input.Length; i++)
  43. {
  44. output[i] = ConvertSample<TInput, TOutput>(input[i]);
  45. }
  46. }
  47. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  48. public static void ConvertSampleToPcmFloat(Span<float> output, ReadOnlySpan<short> input)
  49. {
  50. for (int i = 0; i < input.Length; i++)
  51. {
  52. output[i] = ConvertSampleToPcmFloat(input[i]);
  53. }
  54. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public static int Decode(Span<short> output, ReadOnlySpan<short> input, int startSampleOffset, int endSampleOffset, int channelIndex, int channelCount)
  57. {
  58. if (input.IsEmpty || endSampleOffset < startSampleOffset)
  59. {
  60. return 0;
  61. }
  62. int decodedCount = input.Length / channelCount;
  63. for (int i = 0; i < decodedCount; i++)
  64. {
  65. output[i] = input[i * channelCount + channelIndex];
  66. }
  67. return decodedCount;
  68. }
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public static int Decode(Span<short> output, ReadOnlySpan<float> input, int startSampleOffset, int endSampleOffset, int channelIndex, int channelCount)
  71. {
  72. if (input.IsEmpty || endSampleOffset < startSampleOffset)
  73. {
  74. return 0;
  75. }
  76. int decodedCount = input.Length / channelCount;
  77. for (int i = 0; i < decodedCount; i++)
  78. {
  79. output[i] = ConvertSampleToPcmInt16(input[i * channelCount + channelIndex]);
  80. }
  81. return decodedCount;
  82. }
  83. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  84. public static short Saturate(float value)
  85. {
  86. if (value > short.MaxValue)
  87. {
  88. return short.MaxValue;
  89. }
  90. if (value < short.MinValue)
  91. {
  92. return short.MinValue;
  93. }
  94. return (short)value;
  95. }
  96. }
  97. }