PcmHelper.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Copyright (c) 2019-2020 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 System;
  18. using System.Runtime.CompilerServices;
  19. namespace Ryujinx.Audio.Renderer.Dsp
  20. {
  21. public static class PcmHelper
  22. {
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public static int GetCountToDecode(int startSampleOffset, int endSampleOffset, int offset, int count)
  25. {
  26. return Math.Min(count, endSampleOffset - startSampleOffset - offset);
  27. }
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public static ulong GetBufferOffset<T>(int startSampleOffset, int offset, int channelCount) where T : unmanaged
  30. {
  31. return (ulong)(Unsafe.SizeOf<T>() * channelCount * (startSampleOffset + offset));
  32. }
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public static int GetBufferSize<T>(int startSampleOffset, int endSampleOffset, int offset, int count) where T : unmanaged
  35. {
  36. return GetCountToDecode(startSampleOffset, endSampleOffset, offset, count) * Unsafe.SizeOf<T>();
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public static int Decode(Span<short> output, ReadOnlySpan<short> input, int startSampleOffset, int endSampleOffset, int channelIndex, int channelCount)
  40. {
  41. if (input.IsEmpty || endSampleOffset < startSampleOffset)
  42. {
  43. return 0;
  44. }
  45. int decodedCount = input.Length / channelCount;
  46. for (int i = 0; i < decodedCount; i++)
  47. {
  48. output[i] = input[i * channelCount + channelIndex];
  49. }
  50. return decodedCount;
  51. }
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. public static int Decode(Span<short> output, ReadOnlySpan<float> input, int startSampleOffset, int endSampleOffset, int channelIndex, int channelCount)
  54. {
  55. if (input.IsEmpty || endSampleOffset < startSampleOffset)
  56. {
  57. return 0;
  58. }
  59. int decodedCount = input.Length / channelCount;
  60. for (int i = 0; i < decodedCount; i++)
  61. {
  62. output[i] = (short)(input[i * channelCount + channelIndex] * short.MaxValue);
  63. }
  64. return decodedCount;
  65. }
  66. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  67. public static short Saturate(float value)
  68. {
  69. if (value > short.MaxValue)
  70. {
  71. return short.MaxValue;
  72. }
  73. if (value < short.MinValue)
  74. {
  75. return short.MinValue;
  76. }
  77. return (short)value;
  78. }
  79. }
  80. }